1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using Jypeli; |
---|
4 | using Jypeli.Assets; |
---|
5 | using Jypeli.Controls; |
---|
6 | using Jypeli.Effects; |
---|
7 | using Jypeli.Widgets; |
---|
8 | |
---|
9 | public class Tankz : PhysicsGame |
---|
10 | { |
---|
11 | Tankki tankki; |
---|
12 | Tankki tankki2; |
---|
13 | Tankki tankki3; |
---|
14 | SoundEffect tykinaani = LoadSoundEffect("Explosion28"); |
---|
15 | Image tankinkuva = LoadImage("epicTankkipohja"); |
---|
16 | Image tankintykki = LoadImage("epitankkiTykki"); |
---|
17 | Image TankinAmmus = LoadImage("Ammus"); |
---|
18 | public override void Begin() |
---|
19 | { |
---|
20 | tankki = Luotankki(-300,0,2); |
---|
21 | tankki2 = Luotankki(300,0,3); |
---|
22 | tankki3 = Luotankki(0, 0,4); |
---|
23 | Surface maasto = Surface.CreateBottom(Level, 50, 200, 20, 50); |
---|
24 | Add(maasto); |
---|
25 | maasto.Restitution = 0.1; |
---|
26 | Gravity = new Vector(0, -600); |
---|
27 | Keyboard.Listen(Key.Down, ButtonState.Down, KaannaTykkia,null, tankki, -1.0); |
---|
28 | Keyboard.Listen(Key.Up, ButtonState.Down, KaannaTykkia,null, tankki, 1.0); |
---|
29 | Keyboard.Listen(Key.RightShift, ButtonState.Released, Ammu,null, tankki); |
---|
30 | Keyboard.Listen(Key.W, ButtonState.Down, KaannaTykkia, null, tankki2, -1.0); |
---|
31 | Keyboard.Listen(Key.S, ButtonState.Down, KaannaTykkia, null, tankki2, 1.0); |
---|
32 | Keyboard.Listen(Key.Q, ButtonState.Released, Ammu, null, tankki2); |
---|
33 | Keyboard.Listen(Key.A, ButtonState.Down, KaannaTykkia, null, tankki3, -1.0); |
---|
34 | Keyboard.Listen(Key.Z, ButtonState.Down, KaannaTykkia, null, tankki3, 1.0); |
---|
35 | Keyboard.Listen(Key.LeftShift, ButtonState.Released, Ammu, null, tankki3); |
---|
36 | |
---|
37 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); |
---|
38 | Level.Background.Color = Color.MediumTurquoise; |
---|
39 | Camera.ZoomToAllObjects(); |
---|
40 | |
---|
41 | } |
---|
42 | void KaannaTykkia(Tankki tankki, Double kulma) |
---|
43 | { |
---|
44 | tankki.kulma += kulma; |
---|
45 | }void Ammu(Tankki tankki) { |
---|
46 | if (tankki.ElamaLaskuri.Value == 0) return; |
---|
47 | Vector suunta = Vector.FromLengthAndAngle(tankki.tykki.Width, tankki.tykki.AbsoluteAngle); |
---|
48 | PhysicsObject ammus = new PhysicsObject(17, 5); |
---|
49 | ammus.Position = tankki.Position + suunta; |
---|
50 | ammus.CollisionIgnoreGroup = tankki.CollisionIgnoreGroup; |
---|
51 | ammus.Angle = tankki.tykki.AbsoluteAngle ; |
---|
52 | Add(ammus); |
---|
53 | ammus.Image = TankinAmmus; |
---|
54 | ammus.Tag = "ammus"; |
---|
55 | AddCollisionHandler(ammus, CollisionHandler.DestroyObject); |
---|
56 | ammus.LifetimeLeft = TimeSpan.FromSeconds(4); |
---|
57 | ammus.Velocity = Vector.FromLengthAndAngle(875, tankki.tykki.AbsoluteAngle ) ; |
---|
58 | tykinaani.Play(); |
---|
59 | } |
---|
60 | Tankki Luotankki(Double X, Double Y,int tormausryhma) { |
---|
61 | Tankki tankki = new Tankki(60, 30,tankintykki); |
---|
62 | tankki.CollisionIgnoreGroup = tormausryhma; |
---|
63 | Add(tankki); |
---|
64 | tankki.X = X; |
---|
65 | tankki.Y = Y; |
---|
66 | tankki.Mass = 5; |
---|
67 | tankki.Image = tankinkuva; |
---|
68 | |
---|
69 | AddCollisionHandler(tankki, "ammus", tankkiinosui); |
---|
70 | return tankki; |
---|
71 | } |
---|
72 | |
---|
73 | void tankkiinosui(PhysicsObject tankki,PhysicsObject ammus) |
---|
74 | { |
---|
75 | Tankki pelaaja = (Tankki)tankki; |
---|
76 | pelaaja.ElamaLaskuri.Value--; |
---|
77 | |
---|
78 | int tankkeja = 0; |
---|
79 | tankkeja += this.tankki.ElamaLaskuri.Value > 0 ? 1 : 0; |
---|
80 | tankkeja += this.tankki2.ElamaLaskuri.Value > 0 ? 1 : 0; |
---|
81 | |
---|
82 | tankkeja += this.tankki3.ElamaLaskuri.Value > 0 ? 1 : 0; |
---|
83 | |
---|
84 | if(tankkeja <= 1) |
---|
85 | { |
---|
86 | ClearAll(); |
---|
87 | Begin(); |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | class Tankki : PhysicsObject |
---|
95 | { |
---|
96 | private IntMeter elamaLaskuri = new IntMeter(3, 0, 3); |
---|
97 | public IntMeter ElamaLaskuri { get { return elamaLaskuri; } } |
---|
98 | public GameObject tykki { get; set; } |
---|
99 | public Double kulma { get; set; } |
---|
100 | public Tankki(double leveys, double korkeus,Image tykinkuva) |
---|
101 | : base(leveys, korkeus) |
---|
102 | { |
---|
103 | elamaLaskuri.LowerLimit += delegate { this.Destroy(); }; |
---|
104 | tykki = new GameObject(this.Width / 1.5, this.Width / 5.0); |
---|
105 | tykki.Image = tykinkuva; |
---|
106 | Add(tykki); |
---|
107 | IsUpdated = true; |
---|
108 | } |
---|
109 | |
---|
110 | public override void Update(Time time) |
---|
111 | { |
---|
112 | tykki.Position = new Vector(0, 10) + Vector.FromLengthAndAngle(tykki.Width * 0.52, Angle.FromDegrees(kulma)); |
---|
113 | tykki.Angle = Angle.FromDegrees(kulma); |
---|
114 | |
---|
115 | base.Update(time); |
---|
116 | |
---|
117 | |
---|
118 | } |
---|
119 | } |
---|