1 | #undef DEBUG |
---|
2 | |
---|
3 | using System; |
---|
4 | using System.Collections.Generic; |
---|
5 | using Jypeli; |
---|
6 | using Jypeli.Assets; |
---|
7 | using Jypeli.Controls; |
---|
8 | using Jypeli.Effects; |
---|
9 | using Jypeli.Widgets; |
---|
10 | |
---|
11 | public class D2x_2d : PhysicsGame |
---|
12 | { |
---|
13 | public class Shieldable : PhysicsObject |
---|
14 | { |
---|
15 | public IntMeter shield = new IntMeter(0); |
---|
16 | |
---|
17 | public Shieldable(int w, int h) : base(w, h) |
---|
18 | { |
---|
19 | |
---|
20 | } |
---|
21 | |
---|
22 | public IntMeter getShieldMeter() |
---|
23 | { |
---|
24 | return shield; |
---|
25 | } |
---|
26 | |
---|
27 | public int getShield() |
---|
28 | { |
---|
29 | return shield.Value; |
---|
30 | } |
---|
31 | |
---|
32 | public void setShield(int t) |
---|
33 | { |
---|
34 | if (t > 0) shield.Value = t; |
---|
35 | else |
---|
36 | { |
---|
37 | shield.Value = 0; |
---|
38 | Destroy(); |
---|
39 | } |
---|
40 | } |
---|
41 | } |
---|
42 | |
---|
43 | public class Pelaaja : Shieldable |
---|
44 | { |
---|
45 | private IntMeter energy = new IntMeter(200); |
---|
46 | |
---|
47 | public Pelaaja(int w, int h) : base(w, h) |
---|
48 | { |
---|
49 | shield.Value = 100; |
---|
50 | Tag = "pelaaja"; |
---|
51 | MomentOfInertia = Double.PositiveInfinity; |
---|
52 | Restitution = 0; |
---|
53 | KineticFriction = 1; |
---|
54 | } |
---|
55 | |
---|
56 | public IntMeter getEnergyMeter() |
---|
57 | { |
---|
58 | return energy; |
---|
59 | } |
---|
60 | |
---|
61 | public Ammus Ammu() |
---|
62 | { |
---|
63 | #if DEBUG |
---|
64 | if (energy.Value > 0) |
---|
65 | { |
---|
66 | #endif |
---|
67 | Ammus a = new Ammus(1, "ToV", Angle.GetVector()); |
---|
68 | a.Position = Position; |
---|
69 | energy.Value--; |
---|
70 | return a; |
---|
71 | #if DEBUG |
---|
72 | } |
---|
73 | else return null; |
---|
74 | #endif |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | public class Vihollinen : Shieldable |
---|
79 | { |
---|
80 | private int deal; |
---|
81 | |
---|
82 | public Vihollinen(int d, int w, int h) : base(w, h) |
---|
83 | { |
---|
84 | deal = d; |
---|
85 | Tag = "vihollinen"; |
---|
86 | } |
---|
87 | |
---|
88 | public Ammus Ammu(PhysicsObject kohde) |
---|
89 | { |
---|
90 | Vector s = (kohde.Position - Position).Normalize(); |
---|
91 | Ammus a = new Ammus(deal, "ToP", s); |
---|
92 | a.Position = Position; |
---|
93 | return a; |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | public class Ammus : PhysicsObject |
---|
98 | { |
---|
99 | private int deal; |
---|
100 | |
---|
101 | public Ammus(int d, string t, Vector suunta) : base(3, 3) |
---|
102 | { |
---|
103 | deal = d; |
---|
104 | Tag = "ammus" + t; |
---|
105 | Shape = Shape.Circle; |
---|
106 | Restitution = 0; |
---|
107 | KineticFriction = 1; |
---|
108 | MomentOfInertia = Double.PositiveInfinity; |
---|
109 | Timer.SingleShot(2.0, Destroy); |
---|
110 | Hit(suunta * 100); |
---|
111 | } |
---|
112 | |
---|
113 | public int getDeal() |
---|
114 | { |
---|
115 | return deal; |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | protected Pelaaja pelaaja; |
---|
120 | |
---|
121 | private bool playeradded = false; |
---|
122 | |
---|
123 | private bool altpressed; |
---|
124 | private bool ctrlpressed; |
---|
125 | |
---|
126 | public override void Begin() |
---|
127 | { |
---|
128 | LuoKuuntelijat(); |
---|
129 | NaytaValikko(); |
---|
130 | } |
---|
131 | |
---|
132 | private void LuoKuuntelijat() |
---|
133 | { |
---|
134 | Keyboard.Listen(Key.A, ButtonState.Down, delegate { pelaaja.Velocity = pelaaja.Angle.GetVector() * 50; }, null); |
---|
135 | Keyboard.Listen(Key.A, ButtonState.Released, delegate { pelaaja.Velocity = Vector.Zero; }, null); |
---|
136 | Keyboard.Listen(Key.Z, ButtonState.Down, delegate { pelaaja.Velocity = pelaaja.Angle.GetVector() * -50; }, null); |
---|
137 | Keyboard.Listen(Key.Z, ButtonState.Released, delegate { pelaaja.Velocity = Vector.Zero; }, null); |
---|
138 | Keyboard.Listen(Key.Left, ButtonState.Down, delegate { if (altpressed) pelaaja.Velocity = (pelaaja.Angle + Angle.RightAngle).GetVector() * 50; else pelaaja.Angle = pelaaja.Angle + (Angle.RightAngle / 50); }, null); |
---|
139 | Keyboard.Listen(Key.Left, ButtonState.Released, delegate { pelaaja.Velocity = Vector.Zero; }, null); |
---|
140 | Keyboard.Listen(Key.Right, ButtonState.Down, delegate { if (altpressed) pelaaja.Velocity = (pelaaja.Angle - Angle.RightAngle).GetVector() * 50; else pelaaja.Angle = pelaaja.Angle - (Angle.RightAngle / 50); }, null); |
---|
141 | Keyboard.Listen(Key.Right, ButtonState.Released, delegate { pelaaja.Velocity = Vector.Zero; }, null); |
---|
142 | Keyboard.Listen(Key.LeftAlt, ButtonState.Pressed, delegate { altpressed = true; }, null); |
---|
143 | Keyboard.Listen(Key.LeftAlt, ButtonState.Released, delegate { altpressed = false; }, null); |
---|
144 | |
---|
145 | Keyboard.Listen(Key.LeftControl, ButtonState.Pressed, delegate { ctrlpressed = true; Timer j = new Timer(); j.Interval = 0.5; j.Timeout += delegate { if (ctrlpressed) { Ammus a = pelaaja.Ammu(); if (a != null) Add(a); } }; j.Start(); }, null); |
---|
146 | Keyboard.Listen(Key.LeftControl, ButtonState.Released, delegate { ctrlpressed = false; }, null); |
---|
147 | |
---|
148 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Exit the game"); |
---|
149 | } |
---|
150 | |
---|
151 | private void NaytaValikko() |
---|
152 | { |
---|
153 | MultiSelectWindow alkuValikko = new MultiSelectWindow("", "Start game", "Exit"); |
---|
154 | alkuValikko.AddItemHandler(0, AloitaPeli); |
---|
155 | alkuValikko.AddItemHandler(1, ConfirmExit); |
---|
156 | alkuValikko.DefaultCancel = 1; |
---|
157 | Add(alkuValikko); |
---|
158 | } |
---|
159 | |
---|
160 | private void AloitaPeli() |
---|
161 | { |
---|
162 | InputWindow kysymysIkkuna = new InputWindow("Enter the level number"); |
---|
163 | kysymysIkkuna.TextEntered += LuoKentta; |
---|
164 | Add(kysymysIkkuna); |
---|
165 | } |
---|
166 | |
---|
167 | private void LuoKentta(InputWindow ikkuna) |
---|
168 | { |
---|
169 | try |
---|
170 | { |
---|
171 | TileMap ruudut = TileMap.FromLevelAsset(ikkuna.InputBox.Text); |
---|
172 | ruudut.SetTileMethod('P', LuoPelaaja); |
---|
173 | ruudut.SetTileMethod('V', LuoVihollinen); |
---|
174 | ruudut.SetTileMethod('|', LuoPystySeina); |
---|
175 | ruudut.SetTileMethod('-', LuoVaakaSeina); |
---|
176 | ruudut.Execute(40, 40); |
---|
177 | } |
---|
178 | catch (Exception) |
---|
179 | { |
---|
180 | MessageDisplay.Add("Level number out of range"); |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | private void LuoVihollinen(Vector paikka, double leveys, double korkeus) |
---|
185 | { |
---|
186 | Vihollinen vih = new Vihollinen(1, 10, 10); |
---|
187 | vih.Position = paikka; |
---|
188 | vih.Tag = "vihollinen"; |
---|
189 | vih.setShield(10); |
---|
190 | Timer ajastin = new Timer(); |
---|
191 | ajastin.Interval = 1.5; |
---|
192 | ajastin.Timeout += delegate { if(!(pelaaja.IsDestroyed || vih.IsDestroyed) && RandomGen.NextBool()) Add(vih.Ammu(pelaaja)); }; |
---|
193 | ajastin.Start(); |
---|
194 | AddCollisionHandler<Vihollinen, Ammus>(vih, "ammusToV", Osuma); |
---|
195 | Add(vih); |
---|
196 | } |
---|
197 | |
---|
198 | private void LuoPystySeina(Vector paikka, double leveys, double korkeus) |
---|
199 | { |
---|
200 | PhysicsObject seina = PhysicsObject.CreateStaticObject(leveys, korkeus); |
---|
201 | seina.Position = paikka; |
---|
202 | seina.Tag = "seina"; |
---|
203 | AddCollisionHandler<PhysicsObject, Ammus>(seina, KasitteleAmmus); |
---|
204 | Add(seina); |
---|
205 | } |
---|
206 | |
---|
207 | private void LuoVaakaSeina(Vector paikka, double leveys, double korkeus) |
---|
208 | { |
---|
209 | PhysicsObject seina = PhysicsObject.CreateStaticObject(leveys, korkeus); |
---|
210 | seina.Position = paikka; |
---|
211 | seina.Tag = "seina"; |
---|
212 | AddCollisionHandler<PhysicsObject, Ammus>(seina, KasitteleAmmus); |
---|
213 | Add(seina); |
---|
214 | } |
---|
215 | |
---|
216 | private void KasitteleAmmus(PhysicsObject seina, Ammus kohde) |
---|
217 | { |
---|
218 | kohde.Destroy(); |
---|
219 | } |
---|
220 | |
---|
221 | private void LuoPelaaja(Vector paikka, double leveys, double korkeus) |
---|
222 | { |
---|
223 | if (playeradded) throw new Exception("Too many players"); |
---|
224 | pelaaja = new Pelaaja(10, 10); |
---|
225 | pelaaja.Position = paikka; |
---|
226 | pelaaja.setShield(100); |
---|
227 | |
---|
228 | Label shieldNaytto = new Label(); |
---|
229 | shieldNaytto.X = Screen.Left + 100; |
---|
230 | shieldNaytto.Y = Screen.Top - 100; |
---|
231 | shieldNaytto.TextColor = Color.Black; |
---|
232 | shieldNaytto.Color = Color.White; |
---|
233 | shieldNaytto.BindTo(pelaaja.shield); |
---|
234 | Add(shieldNaytto); |
---|
235 | |
---|
236 | Label eNaytto = new Label(); |
---|
237 | eNaytto.X = Screen.Left + 200; |
---|
238 | eNaytto.Y = Screen.Top - 100; |
---|
239 | eNaytto.TextColor = Color.Black; |
---|
240 | eNaytto.Color = Color.White; |
---|
241 | eNaytto.BindTo(pelaaja.getEnergyMeter()); |
---|
242 | Add(eNaytto); |
---|
243 | AddCollisionHandler<Pelaaja, Ammus>(pelaaja, "ammusToP", Osuma); |
---|
244 | Add(pelaaja); |
---|
245 | Camera.Follow(pelaaja); |
---|
246 | playeradded = true; |
---|
247 | |
---|
248 | } |
---|
249 | |
---|
250 | private void Osuma(Shieldable pelaaja, Ammus kohde) |
---|
251 | { |
---|
252 | pelaaja.setShield(pelaaja.getShield() - kohde.getDeal()); |
---|
253 | kohde.Destroy(); |
---|
254 | } |
---|
255 | } |
---|