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 |
---|
33 | #if DEBUG |
---|
34 | virtual |
---|
35 | #endif |
---|
36 | Explosion setShield(int t) |
---|
37 | { |
---|
38 | if (t > 0) |
---|
39 | { |
---|
40 | shield.Value = t; |
---|
41 | return null; |
---|
42 | } |
---|
43 | else |
---|
44 | { |
---|
45 | shield.Value = 0; |
---|
46 | Explosion r = new Explosion(50); |
---|
47 | r.Position = Position; |
---|
48 | r.Force = 500; |
---|
49 | Destroy(); |
---|
50 | return r; |
---|
51 | } |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | public class Pelaaja : Shieldable |
---|
56 | { |
---|
57 | private IntMeter energy = new IntMeter(200); |
---|
58 | |
---|
59 | public Pelaaja(int w, int h) : base(w, h) |
---|
60 | { |
---|
61 | shield.Value = 100; |
---|
62 | Tag = "pelaaja"; |
---|
63 | MomentOfInertia = Double.PositiveInfinity; |
---|
64 | Restitution = 0; |
---|
65 | KineticFriction = 1; |
---|
66 | } |
---|
67 | |
---|
68 | #if DEBUG |
---|
69 | public override Explosion setShield(int t) |
---|
70 | { |
---|
71 | return null; |
---|
72 | } |
---|
73 | #endif |
---|
74 | |
---|
75 | public IntMeter getEnergyMeter() |
---|
76 | { |
---|
77 | return energy; |
---|
78 | } |
---|
79 | |
---|
80 | public void AddEnergy(int inc) |
---|
81 | { |
---|
82 | energy.Value += inc; |
---|
83 | } |
---|
84 | |
---|
85 | public void Lataa() |
---|
86 | { |
---|
87 | if(energy.Value < 200) energy.Value = 200; |
---|
88 | } |
---|
89 | |
---|
90 | public Ammus Ammu() |
---|
91 | { |
---|
92 | #if !DEBUG |
---|
93 | if (energy.Value > 0) |
---|
94 | { |
---|
95 | #endif |
---|
96 | Ammus a = new Ammus(4, "ToV", Angle.GetVector(), 1); |
---|
97 | a.Position = Position; |
---|
98 | energy.Value--; |
---|
99 | return a; |
---|
100 | #if !DEBUG |
---|
101 | } |
---|
102 | else return null; |
---|
103 | #endif |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | public class Vihollinen : Shieldable |
---|
108 | { |
---|
109 | private int deal, koko; |
---|
110 | |
---|
111 | public Vihollinen(int d, int w, int h, int k) : base(w, h) |
---|
112 | { |
---|
113 | deal = d; |
---|
114 | koko = k; |
---|
115 | Tag = "vihollinen"; |
---|
116 | Color = Color.Green; |
---|
117 | } |
---|
118 | |
---|
119 | public Ammus Ammu(PhysicsObject kohde) |
---|
120 | { |
---|
121 | Vector s = (kohde.Position - Position).Normalize(); |
---|
122 | Ammus a = new Ammus(deal, "ToP", s, koko); |
---|
123 | a.Position = Position; |
---|
124 | return a; |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | public class Ammus : PhysicsObject |
---|
129 | { |
---|
130 | private int deal; |
---|
131 | |
---|
132 | public Ammus(int d, string t, Vector suunta, int koko) |
---|
133 | : base(3 * koko, 3 * koko) |
---|
134 | { |
---|
135 | deal = d; |
---|
136 | Tag = "ammus" + t; |
---|
137 | Shape = Shape.Circle; |
---|
138 | Restitution = 0; |
---|
139 | KineticFriction = 1; |
---|
140 | MomentOfInertia = Double.PositiveInfinity; |
---|
141 | Color = Color.YellowGreen; |
---|
142 | Timer.SingleShot(5.0, Destroy); |
---|
143 | Hit(suunta * 120); |
---|
144 | } |
---|
145 | |
---|
146 | public int getDeal() |
---|
147 | { |
---|
148 | return deal; |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | protected Pelaaja pelaaja; |
---|
153 | protected PhysicsObject exit; |
---|
154 | protected Vihollinen reaktori; |
---|
155 | protected IntMeter pisteet; |
---|
156 | |
---|
157 | private bool playeradded = false; |
---|
158 | |
---|
159 | private bool altpressed; |
---|
160 | private bool ctrlpressed; |
---|
161 | |
---|
162 | public override void Begin() |
---|
163 | { |
---|
164 | LuoKuuntelijat(); |
---|
165 | NaytaValikko(); |
---|
166 | } |
---|
167 | |
---|
168 | private void LuoKuuntelijat() |
---|
169 | { |
---|
170 | Keyboard.Listen(Key.A, ButtonState.Down, delegate { pelaaja.Velocity = pelaaja.Angle.GetVector() * 80; }, null); |
---|
171 | Keyboard.Listen(Key.A, ButtonState.Released, delegate { pelaaja.Velocity = Vector.Zero; }, null); |
---|
172 | Keyboard.Listen(Key.Z, ButtonState.Down, delegate { pelaaja.Velocity = pelaaja.Angle.GetVector() * -80; }, null); |
---|
173 | Keyboard.Listen(Key.Z, ButtonState.Released, delegate { pelaaja.Velocity = Vector.Zero; }, null); |
---|
174 | Keyboard.Listen(Key.Left, ButtonState.Down, delegate { if (altpressed) pelaaja.Velocity = (pelaaja.Angle + Angle.RightAngle).GetVector() * 80; else pelaaja.Angle = pelaaja.Angle + (Angle.RightAngle / 35); }, null); |
---|
175 | Keyboard.Listen(Key.Left, ButtonState.Released, delegate { pelaaja.Velocity = Vector.Zero; }, null); |
---|
176 | Keyboard.Listen(Key.Right, ButtonState.Down, delegate { if (altpressed) pelaaja.Velocity = (pelaaja.Angle - Angle.RightAngle).GetVector() * 80; else pelaaja.Angle = pelaaja.Angle - (Angle.RightAngle / 35); }, null); |
---|
177 | Keyboard.Listen(Key.Right, ButtonState.Released, delegate { pelaaja.Velocity = Vector.Zero; }, null); |
---|
178 | Keyboard.Listen(Key.LeftAlt, ButtonState.Pressed, delegate { altpressed = true; }, null); |
---|
179 | Keyboard.Listen(Key.LeftAlt, ButtonState.Released, delegate { altpressed = false; }, null); |
---|
180 | |
---|
181 | 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); |
---|
182 | Keyboard.Listen(Key.LeftControl, ButtonState.Released, delegate { ctrlpressed = false; }, null); |
---|
183 | |
---|
184 | Keyboard.Listen(Key.Pause, ButtonState.Pressed, Pause, null); |
---|
185 | Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, null); |
---|
186 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Exit the game"); |
---|
187 | } |
---|
188 | |
---|
189 | private void NaytaValikko() |
---|
190 | { |
---|
191 | MultiSelectWindow alkuValikko = new MultiSelectWindow("", "Start game", "Exit"); |
---|
192 | alkuValikko.AddItemHandler(0, AloitaPeli); |
---|
193 | alkuValikko.AddItemHandler(1, Exit); |
---|
194 | alkuValikko.DefaultCancel = 1; |
---|
195 | Add(alkuValikko); |
---|
196 | } |
---|
197 | |
---|
198 | private void AloitaPeli() |
---|
199 | { |
---|
200 | InputWindow kysymysIkkuna = new InputWindow("Enter the level number"); |
---|
201 | kysymysIkkuna.TextEntered += LuoKentta; |
---|
202 | Add(kysymysIkkuna); |
---|
203 | } |
---|
204 | |
---|
205 | private void LuoKentta(InputWindow ikkuna) |
---|
206 | { |
---|
207 | try |
---|
208 | { |
---|
209 | TileMap ruudut = TileMap.FromLevelAsset(ikkuna.InputBox.Text); |
---|
210 | ruudut.SetTileMethod('P', LuoPelaaja); |
---|
211 | ruudut.SetTileMethod('V', LuoVihollinen); |
---|
212 | ruudut.SetTileMethod('-', LuoSeina); |
---|
213 | ruudut.SetTileMethod('S', LuoShield); |
---|
214 | ruudut.SetTileMethod('E', LuoEnergia); |
---|
215 | ruudut.SetTileMethod('C', LuoLatauspaikka); |
---|
216 | ruudut.SetTileMethod('X', LuoExit); |
---|
217 | ruudut.SetTileMethod('R', LuoReaktori); |
---|
218 | ruudut.SetTileMethod('@', LuoExitTak); |
---|
219 | ruudut.Optimize('-'); |
---|
220 | ruudut.Execute(40, 40); |
---|
221 | |
---|
222 | Level.Background.Color = Color.LightGray; |
---|
223 | pisteet = new IntMeter(0); |
---|
224 | Label pisteNaytto = new Label(); |
---|
225 | pisteNaytto.X = Screen.Left + 100; |
---|
226 | pisteNaytto.Y = Screen.Bottom + 100; |
---|
227 | pisteNaytto.TextColor = Color.Black; |
---|
228 | pisteNaytto.Color = Color.White; |
---|
229 | pisteNaytto.BindTo(pisteet); |
---|
230 | Add(pisteNaytto); |
---|
231 | } |
---|
232 | catch (Exception) |
---|
233 | { |
---|
234 | MessageDisplay.Add("Level number out of range"); |
---|
235 | Begin(); |
---|
236 | } |
---|
237 | } |
---|
238 | |
---|
239 | private void LuoVihollinen(Vector paikka, double leveys, double korkeus) |
---|
240 | { |
---|
241 | Vihollinen vih = new Vihollinen(6, 10, 10, 1); |
---|
242 | vih.Position = paikka; |
---|
243 | vih.Tag = "vihollinen"; |
---|
244 | vih.setShield(20); |
---|
245 | Timer ajastin = new Timer(); |
---|
246 | ajastin.Interval = 0.8; |
---|
247 | ajastin.Timeout += delegate { if (!(pelaaja.IsDestroyed || vih.IsDestroyed) && RandomGen.NextBool()) Add(vih.Ammu(pelaaja)); vih.Move(RandomGen.NextVector(20, 20)); double ad = RandomGen.NextDouble(-0.5, 0.5); if (ajastin.Interval + ad < 0.1) ajastin.Interval = 0.1; else ajastin.Interval += ad; }; |
---|
248 | ajastin.Start(); |
---|
249 | AddCollisionHandler<Vihollinen, Ammus>(vih, "ammusToV", Osuma); |
---|
250 | Add(vih); |
---|
251 | } |
---|
252 | |
---|
253 | private void LuoSeina(Vector paikka, double leveys, double korkeus) |
---|
254 | { |
---|
255 | PhysicsObject seina = PhysicsObject.CreateStaticObject(leveys, korkeus); |
---|
256 | seina.Position = paikka; |
---|
257 | seina.Tag = "seina"; |
---|
258 | AddCollisionHandler<PhysicsObject, Ammus>(seina, KasitteleAmmus); |
---|
259 | Add(seina); |
---|
260 | } |
---|
261 | |
---|
262 | private void KasitteleAmmus(PhysicsObject seina, Ammus kohde) |
---|
263 | { |
---|
264 | kohde.Destroy(); |
---|
265 | } |
---|
266 | |
---|
267 | private void LuoPelaaja(Vector paikka, double leveys, double korkeus) |
---|
268 | { |
---|
269 | if (playeradded) throw new Exception("Too many players"); |
---|
270 | pelaaja = new Pelaaja(10, 10); |
---|
271 | pelaaja.Position = paikka; |
---|
272 | pelaaja.setShield(100); |
---|
273 | |
---|
274 | Label shieldNaytto = new Label(); |
---|
275 | shieldNaytto.X = Screen.Left + 100; |
---|
276 | shieldNaytto.Y = Screen.Top - 100; |
---|
277 | shieldNaytto.TextColor = Color.Blue; |
---|
278 | shieldNaytto.Color = Color.White; |
---|
279 | shieldNaytto.Title = "Shield"; |
---|
280 | shieldNaytto.BindTo(pelaaja.shield); |
---|
281 | Add(shieldNaytto); |
---|
282 | |
---|
283 | Label eNaytto = new Label(); |
---|
284 | eNaytto.X = Screen.Left + 250; |
---|
285 | eNaytto.Y = Screen.Top - 100; |
---|
286 | eNaytto.TextColor = Color.Yellow; |
---|
287 | eNaytto.Color = Color.Black; |
---|
288 | eNaytto.Title = "Energy"; |
---|
289 | eNaytto.BindTo(pelaaja.getEnergyMeter()); |
---|
290 | Add(eNaytto); |
---|
291 | |
---|
292 | AddCollisionHandler<Pelaaja, Ammus>(pelaaja, "ammusToP", Osuma); |
---|
293 | Add(pelaaja); |
---|
294 | Camera.Follow(pelaaja); |
---|
295 | Camera.Zoom(1.8); |
---|
296 | playeradded = true; |
---|
297 | |
---|
298 | } |
---|
299 | |
---|
300 | private void LuoShield(Vector paikka, double leveys, double korkeus) |
---|
301 | { |
---|
302 | PhysicsObject shield = new PhysicsObject(10, 10); |
---|
303 | shield.Position = paikka; |
---|
304 | shield.Shape = Shape.Circle; |
---|
305 | shield.Color = Color.Blue; |
---|
306 | AddCollisionHandler<PhysicsObject, Pelaaja>(shield, "pelaaja", delegate { pelaaja.setShield(pelaaja.getShield() + 20); shield.Destroy(); }); |
---|
307 | Add(shield); |
---|
308 | } |
---|
309 | |
---|
310 | private void LuoEnergia(Vector paikka, double leveys, double korkeus) |
---|
311 | { |
---|
312 | PhysicsObject energia = new PhysicsObject(10, 10); |
---|
313 | energia.Position = paikka; |
---|
314 | energia.Shape = Shape.Circle; |
---|
315 | energia.Color = Color.Yellow; |
---|
316 | AddCollisionHandler<PhysicsObject, Pelaaja>(energia, "pelaaja", LisaaEnergiaa); |
---|
317 | Add(energia); |
---|
318 | } |
---|
319 | |
---|
320 | private void LuoLatauspaikka(Vector paikka, double leveys, double korkeus) |
---|
321 | { |
---|
322 | PhysicsObject lp = PhysicsObject.CreateStaticObject(leveys, korkeus); |
---|
323 | lp.Position = paikka; |
---|
324 | lp.Shape = Shape.Rectangle; |
---|
325 | lp.Color = Color.Yellow; |
---|
326 | AddCollisionHandler<PhysicsObject, Pelaaja>(lp, "pelaaja", delegate { pelaaja.Lataa(); }); |
---|
327 | Add(lp); |
---|
328 | } |
---|
329 | |
---|
330 | private void LuoReaktori(Vector paikka, double leveys, double korkeus) |
---|
331 | { |
---|
332 | reaktori = new Vihollinen(8, 30, 30, 2); |
---|
333 | reaktori.Position = paikka; |
---|
334 | reaktori.MakeStatic(); |
---|
335 | reaktori.Color = Color.OrangeRed; |
---|
336 | reaktori.setShield(50); |
---|
337 | AddCollisionHandler<Shieldable, Ammus>(reaktori, "ammusToV", Osuma); |
---|
338 | Timer ajastin = new Timer(); |
---|
339 | ajastin.Interval = 0.6; |
---|
340 | ajastin.Timeout += delegate { if (!(pelaaja.IsDestroyed || reaktori.IsDestroyed) && RandomGen.NextBool()) { Ammus am = reaktori.Ammu(pelaaja); am.Color = Color.Red; Add(am); } }; |
---|
341 | ajastin.Start(); |
---|
342 | Add(reaktori); |
---|
343 | } |
---|
344 | |
---|
345 | private void LuoExit(Vector paikka, double leveys, double korkeus) |
---|
346 | { |
---|
347 | exit = PhysicsObject.CreateStaticObject(leveys, korkeus); |
---|
348 | exit.Position = paikka; |
---|
349 | exit.Color = Color.Brown; |
---|
350 | Add(exit); |
---|
351 | } |
---|
352 | |
---|
353 | private void LuoExitTak(Vector paikka, double leveys, double korkeus) |
---|
354 | { |
---|
355 | PhysicsObject et = PhysicsObject.CreateStaticObject(leveys, korkeus); |
---|
356 | et.Position = paikka; |
---|
357 | et.Color = Color.LightGray; |
---|
358 | AddCollisionHandler<PhysicsObject, Pelaaja>(et, "pelaaja", delegate { ClearAll(); playeradded = false; MessageDisplay.Add("You won the game"); Begin(); }); |
---|
359 | Add(et); |
---|
360 | } |
---|
361 | |
---|
362 | private void Osuma(Shieldable p, Ammus kohde) |
---|
363 | { |
---|
364 | kohde.Destroy(); |
---|
365 | Explosion a = p.setShield(p.getShield() - kohde.getDeal()); |
---|
366 | if (a != null) |
---|
367 | { |
---|
368 | Add(a); |
---|
369 | if(p == pelaaja) |
---|
370 | { |
---|
371 | ClearAll(); |
---|
372 | playeradded = false; |
---|
373 | MessageDisplay.Add("Game over"); |
---|
374 | Begin(); |
---|
375 | return; |
---|
376 | } |
---|
377 | else pisteet.Value += 20; |
---|
378 | if (p == reaktori) |
---|
379 | { |
---|
380 | pisteet.Value += 10; |
---|
381 | int time = 30; |
---|
382 | exit.Destroy(); |
---|
383 | IntMeter aika = new IntMeter(time); |
---|
384 | Timer jak = new Timer(); |
---|
385 | jak.Interval = 1; |
---|
386 | jak.Timeout += delegate { aika.Value--; if (aika.Value <= 0) { ClearAll(); MessageDisplay.Add("Game over"); playeradded = false; Begin(); } }; |
---|
387 | jak.Start(); |
---|
388 | |
---|
389 | Label aikaNaytto = new Label(); |
---|
390 | aikaNaytto.TextColor = Color.Green; |
---|
391 | aikaNaytto.X = Screen.Right - 100; |
---|
392 | aikaNaytto.Y = Screen.Top - 100; |
---|
393 | aikaNaytto.Title = "Time"; |
---|
394 | aikaNaytto.BindTo(aika); |
---|
395 | Add(aikaNaytto); |
---|
396 | } |
---|
397 | } |
---|
398 | } |
---|
399 | |
---|
400 | private void LisaaEnergiaa(PhysicsObject energ, Pelaaja kohde) |
---|
401 | { |
---|
402 | kohde.AddEnergy(30); |
---|
403 | energ.Destroy(); |
---|
404 | } |
---|
405 | } |
---|