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 Peli : PhysicsGame |
---|
10 | { |
---|
11 | |
---|
12 | const double nopeus = 200; |
---|
13 | const double hyppyNopeus = 1000; |
---|
14 | const int RUUDUN_KOKO = 40; |
---|
15 | |
---|
16 | IntMeter kenttaLaskuri; |
---|
17 | |
---|
18 | ScoreList topLista = new ScoreList(10, false, 0); |
---|
19 | |
---|
20 | PlatformCharacter porkkana; |
---|
21 | PhysicsObject hautakivi; |
---|
22 | PhysicsObject kuolluttomaatti; |
---|
23 | PhysicsObject lipas; |
---|
24 | PhysicsObject uusikranaatti; |
---|
25 | PhysicsObject raha; |
---|
26 | |
---|
27 | IntMeter panoslaskuri; |
---|
28 | IntMeter pistelaskuri; |
---|
29 | IntMeter kranaattiLaskuri; |
---|
30 | |
---|
31 | List<GameObject> rajahdyksenTomaattiUhrit; |
---|
32 | List<GameObject> rajahdyksenPorkkanaUhri; |
---|
33 | |
---|
34 | Image[] pelaajanKuva = LoadImages("porkkana"); |
---|
35 | Image[] pelaajanKuvaPeilattu; |
---|
36 | Image pelaajanKuvaKuolleena = LoadImage("rip"); |
---|
37 | Image[] vihollisenKuva = LoadImages("tomaatti"); |
---|
38 | Image[] vihollisenKuvaPeilattu; |
---|
39 | Image vihollisenKuvaKuolleena = LoadImage("tomaattikuolleena"); |
---|
40 | Image lippaanKuva = LoadImage("Lipas2"); |
---|
41 | Image kranaatinKuva = LoadImage("ydinpommi"); |
---|
42 | Image uudenKranaatinKuva = LoadImage("Laatikko"); |
---|
43 | Image maalinKuva = LoadImage("Avain"); |
---|
44 | Image RahanKuva = LoadImage("Raha"); |
---|
45 | |
---|
46 | ExplosionSystem rajahdys; |
---|
47 | int pMaxMaara = 50; |
---|
48 | |
---|
49 | public override void Begin() |
---|
50 | { |
---|
51 | if (DataStorage.Exists("pisteet.xlm")) |
---|
52 | { |
---|
53 | topLista = DataStorage.Load<ScoreList>(topLista, "pisteet.xlm"); |
---|
54 | } |
---|
55 | |
---|
56 | rajahdyksenTomaattiUhrit = new List<GameObject>(); |
---|
57 | rajahdyksenPorkkanaUhri = new List<GameObject>(); |
---|
58 | |
---|
59 | pelaajanKuvaPeilattu = Image.Mirror(pelaajanKuva); |
---|
60 | vihollisenKuvaPeilattu = Image.Mirror(vihollisenKuva); |
---|
61 | |
---|
62 | SeuraavaKentta(1); |
---|
63 | |
---|
64 | Gravity = new Vector(0, -1000); |
---|
65 | |
---|
66 | lisaaNappaimet(); |
---|
67 | |
---|
68 | Camera.Follow(porkkana); |
---|
69 | |
---|
70 | Camera.ZoomFactor = 1.6; |
---|
71 | Camera.StayInLevel = true; |
---|
72 | |
---|
73 | LuoPanosLaskuri(); |
---|
74 | LuoPisteLaskuri(); |
---|
75 | LuoKranaattiLaskuri(); |
---|
76 | |
---|
77 | rajahdys = new ExplosionSystem(LoadImage("rajahdys"), pMaxMaara); |
---|
78 | rajahdys.MaxLifetime = 1.0; |
---|
79 | rajahdys.MaxScale = 0.1; |
---|
80 | Add(rajahdys); |
---|
81 | } |
---|
82 | |
---|
83 | void SeuraavaKentta(int kentanNro) |
---|
84 | { |
---|
85 | ClearAll(); |
---|
86 | |
---|
87 | kenttaLaskuri = new IntMeter(kentanNro); |
---|
88 | |
---|
89 | if (kenttaLaskuri.Value == 1) luoKentta1(); |
---|
90 | else if (kenttaLaskuri.Value == 2) luoKentta2(); |
---|
91 | //else if (kenttaLaskuri.Value == 3) luoKentta3(); |
---|
92 | else if (kenttaLaskuri.Value > 2) LopetaPeli(); |
---|
93 | |
---|
94 | |
---|
95 | |
---|
96 | } |
---|
97 | |
---|
98 | void luoKentta1() |
---|
99 | { |
---|
100 | TileMap kentta = TileMap.FromFile("kentta1.txt"); |
---|
101 | kentta['#'] = lisaaTaso; |
---|
102 | kentta['K'] = lisaaTaso2; |
---|
103 | kentta['N'] = lisaaPelaaja; |
---|
104 | kentta['E'] = lisaaTomaatti; |
---|
105 | kentta['L'] = LuoLipas; |
---|
106 | kentta['A'] = LuoKranaatti; |
---|
107 | kentta['T'] = lisaaTomaatti2; |
---|
108 | kentta['M'] = LuoMaali; |
---|
109 | kentta['R'] = LuoRaha; |
---|
110 | kentta.Insert(RUUDUN_KOKO, RUUDUN_KOKO); |
---|
111 | Level.CreateBorders(); |
---|
112 | Level.Background.CreateGradient(Color.Blue,Color.Black); |
---|
113 | } |
---|
114 | |
---|
115 | void luoKentta2() |
---|
116 | { |
---|
117 | TileMap kentta = TileMap.FromFile("kentta2.txt"); |
---|
118 | kentta['#'] = lisaaTaso; |
---|
119 | kentta['K'] = lisaaTaso2; |
---|
120 | kentta['N'] = lisaaPelaaja; |
---|
121 | kentta['E'] = lisaaTomaatti; |
---|
122 | kentta['L'] = LuoLipas; |
---|
123 | kentta['A'] = LuoKranaatti; |
---|
124 | kentta['T'] = lisaaTomaatti2; |
---|
125 | kentta['M'] = LuoMaali; |
---|
126 | kentta['R'] = LuoRaha; |
---|
127 | kentta.Insert(RUUDUN_KOKO, RUUDUN_KOKO); |
---|
128 | Level.CreateBorders(); |
---|
129 | Level.Background.CreateGradient(Color.Black, Color.Blue); |
---|
130 | } |
---|
131 | |
---|
132 | //void luoKentta3() |
---|
133 | //{ |
---|
134 | // TileMap kentta = TileMap.FromFile(); |
---|
135 | // kentta['#'] = lisaaTaso; |
---|
136 | // kentta['K'] = lisaaTaso2; |
---|
137 | // kentta['N'] = lisaaPelaaja; |
---|
138 | // kentta['E'] = lisaaTomaatti; |
---|
139 | // kentta['L'] = LuoLipas; |
---|
140 | // kentta['A'] = LuoKranaatti; |
---|
141 | // kentta['T'] = lisaaTomaatti2; |
---|
142 | // kentta['M'] = LuoMaali; |
---|
143 | // kentta['R'] = LuoRaha; |
---|
144 | // kentta.Insert(RUUDUN_KOKO, RUUDUN_KOKO); |
---|
145 | // Level.CreateBorders(); |
---|
146 | //Level.Background.CreateGradient(Color.Green,Color.Black); |
---|
147 | //} |
---|
148 | |
---|
149 | PhysicsObject lisaaTaso() |
---|
150 | { |
---|
151 | PhysicsObject taso = PhysicsObject.CreateStaticObject(RUUDUN_KOKO, RUUDUN_KOKO); |
---|
152 | taso.Color = Color.Green; |
---|
153 | return taso; |
---|
154 | } |
---|
155 | |
---|
156 | PhysicsObject lisaaTaso2() |
---|
157 | { |
---|
158 | PhysicsObject taso2 = PhysicsObject.CreateStaticObject(RUUDUN_KOKO, RUUDUN_KOKO); |
---|
159 | taso2.Color = Color.Gray; |
---|
160 | return taso2; |
---|
161 | } |
---|
162 | |
---|
163 | PlatformCharacter lisaaPelaaja() |
---|
164 | { |
---|
165 | porkkana = new PlatformCharacter(40, 40); |
---|
166 | porkkana.Mass = 4.0; |
---|
167 | |
---|
168 | porkkana.Weapon = new AssaultRifle(60, 30); |
---|
169 | porkkana.Weapon.Ammo.Value = 10; |
---|
170 | porkkana.Weapon.Y = -3; |
---|
171 | porkkana.Weapon.X = 0; |
---|
172 | porkkana.Weapon.ProjectileCollision = AmmusOsui; |
---|
173 | |
---|
174 | AddCollisionHandler(porkkana, LipasKranaattiOsuma); |
---|
175 | |
---|
176 | porkkana.IgnoresExplosions = true; |
---|
177 | |
---|
178 | porkkana.RightWalkingAnimation = new Animation(pelaajanKuva); |
---|
179 | porkkana.LeftWalkingAnimation = new Animation(pelaajanKuvaPeilattu); |
---|
180 | |
---|
181 | rajahdyksenPorkkanaUhri.Add(porkkana); |
---|
182 | |
---|
183 | return porkkana; |
---|
184 | } |
---|
185 | |
---|
186 | PlatformCharacter lisaaTomaatti() |
---|
187 | { |
---|
188 | PlatformCharacter tomaatti = new PlatformCharacter(40, 40); |
---|
189 | tomaatti.Mass = 4.0; |
---|
190 | |
---|
191 | tomaatti.Tag = "Vihollinen"; |
---|
192 | |
---|
193 | FollowerBrain SeuraajanAivot = new FollowerBrain(); |
---|
194 | tomaatti.Brain = SeuraajanAivot; |
---|
195 | SeuraajanAivot.Target = porkkana; |
---|
196 | SeuraajanAivot.Speed = 100; |
---|
197 | SeuraajanAivot.TargetFollowDistance = 500; |
---|
198 | SeuraajanAivot.TargetCloseDistance = 50; |
---|
199 | SeuraajanAivot.StopWhenTargetClose = false; |
---|
200 | AddCollisionHandler(tomaatti, TomaattiOsui); |
---|
201 | |
---|
202 | tomaatti.RightWalkingAnimation = new Animation(vihollisenKuva); |
---|
203 | tomaatti.LeftWalkingAnimation = new Animation(vihollisenKuvaPeilattu); |
---|
204 | |
---|
205 | rajahdyksenTomaattiUhrit.Add(tomaatti); |
---|
206 | |
---|
207 | return tomaatti; |
---|
208 | |
---|
209 | } |
---|
210 | |
---|
211 | PlatformCharacter lisaaTomaatti2() |
---|
212 | { |
---|
213 | PlatformCharacter tomaatti2 = new PlatformCharacter(40, 40); |
---|
214 | tomaatti2.Mass = 4.0; |
---|
215 | |
---|
216 | tomaatti2.Tag = "Vihollinen2"; |
---|
217 | |
---|
218 | tomaatti2.Weapon = new AssaultRifle(60, 30); |
---|
219 | tomaatti2.Weapon.Ammo.Value = 10; |
---|
220 | tomaatti2.Weapon.Y = 0; |
---|
221 | tomaatti2.Weapon.X = 0; |
---|
222 | tomaatti2.Weapon.ProjectileCollision = AmmusOsui; |
---|
223 | |
---|
224 | Timer ampumaAjastin = new Timer(); |
---|
225 | ampumaAjastin.Interval = 2.0; |
---|
226 | ampumaAjastin.Trigger += vihollinenAmpuu; |
---|
227 | ampumaAjastin.Tag = tomaatti2; |
---|
228 | ampumaAjastin.Start(); |
---|
229 | |
---|
230 | tomaatti2.RightWalkingAnimation = new Animation(vihollisenKuva); |
---|
231 | tomaatti2.LeftWalkingAnimation = new Animation(vihollisenKuvaPeilattu); |
---|
232 | |
---|
233 | rajahdyksenTomaattiUhrit.Add(tomaatti2); |
---|
234 | |
---|
235 | return tomaatti2; |
---|
236 | } |
---|
237 | |
---|
238 | void lisaaNappaimet() |
---|
239 | { |
---|
240 | Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); |
---|
241 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu pelistä"); |
---|
242 | |
---|
243 | Keyboard.Listen(Key.Left, ButtonState.Down, liikuta, "Pelaaja liikkuu vasemmalle", porkkana, -nopeus); |
---|
244 | Keyboard.Listen(Key.Right, ButtonState.Down, liikuta, "Pelaaja liikkuu vasemmalle", porkkana, nopeus); |
---|
245 | Keyboard.Listen(Key.Up, ButtonState.Pressed, hyppaa, "Pelaaja hyppää", porkkana, hyppyNopeus); |
---|
246 | |
---|
247 | Keyboard.Listen(Key.Space, ButtonState.Down, AmmuAseella, "Ammu rynnäkkökiväärillä"); |
---|
248 | Keyboard.Listen(Key.W, ButtonState.Down, TahtaaYlos, "Tähtää ylös rynnäkkökiväärillä"); |
---|
249 | Keyboard.Listen(Key.S, ButtonState.Down, TahtaaAlas, "Tähtää alas rynnäkkökiväärillä"); |
---|
250 | Keyboard.Listen(Key.Q, ButtonState.Pressed, HeitaKranaatti, "Heitä kranaatti"); |
---|
251 | |
---|
252 | ControllerOne.Listen(Button.Start, ButtonState.Pressed, Exit, "Poistu pelistä"); |
---|
253 | ControllerOne.Listen(Button.Back, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); |
---|
254 | |
---|
255 | ControllerOne.ListenAnalog(AnalogControl.LeftStick, 0.01, liikutaTatilla, "Pelaaja liikkuu", porkkana); |
---|
256 | ControllerOne.Listen(Button.A, ButtonState.Pressed, hyppaa, "Pelaaja hyppää", porkkana, hyppyNopeus); |
---|
257 | |
---|
258 | ControllerOne.Listen(Button.RightTrigger, ButtonState.Down, AmmuAseella, "Ammu rynnäkkökiväärillä"); |
---|
259 | ControllerOne.ListenAnalog(AnalogControl.RightStick, 0.01, tahtaaTatilla, "Tähtää rynnäkkökiväärillä"); |
---|
260 | ControllerOne.Listen(Button.LeftTrigger, ButtonState.Pressed, HeitaKranaatti, "Heitä kranaatti"); |
---|
261 | } |
---|
262 | |
---|
263 | void liikuta(PlatformCharacter hahmo, double nopeus) |
---|
264 | { |
---|
265 | hahmo.Walk(nopeus); |
---|
266 | } |
---|
267 | |
---|
268 | void hyppaa(PlatformCharacter hahmo, double nopeus) |
---|
269 | { |
---|
270 | hahmo.Jump(nopeus); |
---|
271 | } |
---|
272 | |
---|
273 | void liikutaTatilla(AnalogState tatintila, PlatformCharacter hahmo) |
---|
274 | { |
---|
275 | Vector tatinAsento = tatintila.StateVector; |
---|
276 | hahmo.Walk(tatinAsento.X*nopeus); |
---|
277 | |
---|
278 | } |
---|
279 | |
---|
280 | void AmmuAseella() |
---|
281 | { |
---|
282 | PhysicsObject ammus = porkkana.Weapon.Shoot(); |
---|
283 | |
---|
284 | |
---|
285 | if (ammus != null) |
---|
286 | { |
---|
287 | ammus.Size *= 5; |
---|
288 | ammus.MaximumLifetime = TimeSpan.FromSeconds(1.5); |
---|
289 | } |
---|
290 | } |
---|
291 | |
---|
292 | void TahtaaYlos() |
---|
293 | { |
---|
294 | if (porkkana.FacingDirection == Direction.Right) |
---|
295 | { |
---|
296 | porkkana.Weapon.Angle += Angle.FromDegrees(5); |
---|
297 | } |
---|
298 | if (porkkana.FacingDirection == Direction.Left) |
---|
299 | { |
---|
300 | porkkana.Weapon.Angle += Angle.FromDegrees(-5); |
---|
301 | } |
---|
302 | |
---|
303 | |
---|
304 | } |
---|
305 | |
---|
306 | void TahtaaAlas() |
---|
307 | { |
---|
308 | if (porkkana.FacingDirection == Direction.Right) |
---|
309 | { |
---|
310 | porkkana.Weapon.Angle += Angle.FromDegrees(-5); |
---|
311 | } |
---|
312 | if (porkkana.FacingDirection == Direction.Left) |
---|
313 | { |
---|
314 | porkkana.Weapon.Angle += Angle.FromDegrees(5); |
---|
315 | } |
---|
316 | } |
---|
317 | |
---|
318 | void tahtaaTatilla(AnalogState toisentatintila) |
---|
319 | { |
---|
320 | Vector toisentatinAsento = toisentatintila.StateVector; |
---|
321 | porkkana.Weapon.Angle = toisentatinAsento.Angle; |
---|
322 | } |
---|
323 | |
---|
324 | void AmmusOsui(PhysicsObject ammus, PhysicsObject kohde) |
---|
325 | { |
---|
326 | |
---|
327 | if (kohde.Tag.ToString() == "Vihollinen" || kohde.Tag.ToString() == "Vihollinen2") |
---|
328 | { |
---|
329 | ammus.Destroy(); |
---|
330 | kohde.Brain = null; |
---|
331 | TomaattiKuoli(kohde); |
---|
332 | kohde.IgnoresCollisionResponse = true; |
---|
333 | rajahdys.AddEffect(kohde.Position, 50); |
---|
334 | |
---|
335 | } |
---|
336 | |
---|
337 | if (kohde == porkkana) |
---|
338 | { |
---|
339 | porkkana.Stop(); |
---|
340 | |
---|
341 | MediaPlayer.Play("pling"); |
---|
342 | |
---|
343 | porkkana.Destroy(); |
---|
344 | |
---|
345 | LuoHautakivi(); |
---|
346 | |
---|
347 | LopetaPeli(); |
---|
348 | |
---|
349 | } |
---|
350 | |
---|
351 | if (kohde.Tag.ToString() == "Lipas") |
---|
352 | { |
---|
353 | |
---|
354 | } |
---|
355 | } |
---|
356 | |
---|
357 | void LopetaPeli() |
---|
358 | { |
---|
359 | HighScoreWindow topIkkuna = new HighScoreWindow("Call of garden: Modern carrotfare", |
---|
360 | "Parhaat pisteet", |
---|
361 | "Onneksi olkoon, pääsit listalle pisteillä %p! Syötä nimesi:", |
---|
362 | topLista, pistelaskuri.Value); |
---|
363 | topIkkuna.Closed += TallennaPisteet; |
---|
364 | Add(topIkkuna); |
---|
365 | } |
---|
366 | |
---|
367 | void LuoHautakivi() |
---|
368 | { |
---|
369 | hautakivi = new PhysicsObject(40, 40); |
---|
370 | hautakivi.Mass = 100000000000.0; |
---|
371 | |
---|
372 | hautakivi.IgnoresExplosions = true; |
---|
373 | |
---|
374 | hautakivi.Image = pelaajanKuvaKuolleena; |
---|
375 | |
---|
376 | hautakivi.Position = porkkana.Position; |
---|
377 | Add(hautakivi); |
---|
378 | |
---|
379 | } |
---|
380 | |
---|
381 | void TomaattiKuoli(PhysicsObject tomaatti) |
---|
382 | { |
---|
383 | tomaatti.Destroy(); |
---|
384 | |
---|
385 | MediaPlayer.Play("ketshup"); |
---|
386 | |
---|
387 | pistelaskuri.Value += 100; |
---|
388 | |
---|
389 | kuolluttomaatti = new PhysicsObject(50, 50); |
---|
390 | kuolluttomaatti.Mass = 4.0; |
---|
391 | |
---|
392 | kuolluttomaatti.IgnoresCollisionResponse = true; |
---|
393 | |
---|
394 | kuolluttomaatti.IgnoresGravity = true; |
---|
395 | |
---|
396 | kuolluttomaatti.IgnoresExplosions = true; |
---|
397 | |
---|
398 | kuolluttomaatti.Image = vihollisenKuvaKuolleena; |
---|
399 | |
---|
400 | kuolluttomaatti.Position = tomaatti.Position; |
---|
401 | Add(kuolluttomaatti); |
---|
402 | |
---|
403 | LuoLipas(kuolluttomaatti.X - 30, kuolluttomaatti.Y); |
---|
404 | LuoKranaatti(kuolluttomaatti.X + 30, kuolluttomaatti.Y); |
---|
405 | } |
---|
406 | |
---|
407 | void TomaattiOsui(PhysicsObject tomaatti, PhysicsObject kohde) |
---|
408 | { |
---|
409 | if (kohde == porkkana) |
---|
410 | { |
---|
411 | tomaatti.Stop(); |
---|
412 | tomaatti.Brain.Active = false; |
---|
413 | |
---|
414 | porkkana.IgnoresCollisionResponse = true; |
---|
415 | |
---|
416 | porkkana.Stop(); |
---|
417 | |
---|
418 | MediaPlayer.Play("pling"); |
---|
419 | |
---|
420 | porkkana.Destroy(); |
---|
421 | |
---|
422 | LuoHautakivi(); |
---|
423 | |
---|
424 | LopetaPeli(); |
---|
425 | |
---|
426 | } |
---|
427 | |
---|
428 | } |
---|
429 | |
---|
430 | void LuoPanosLaskuri() |
---|
431 | { |
---|
432 | panoslaskuri = new IntMeter(0); |
---|
433 | |
---|
434 | Label panosnaytto = new Label(); |
---|
435 | panosnaytto.X = Screen.Right - 100; |
---|
436 | panosnaytto.Y = Screen.Top - 100; |
---|
437 | panosnaytto.TextColor = Color.Red; |
---|
438 | |
---|
439 | panosnaytto.BindTo(porkkana.Weapon.Ammo); |
---|
440 | Add(panosnaytto); |
---|
441 | |
---|
442 | Label panosteksti = new Label("Panoksia jäljellä: "); |
---|
443 | panosteksti.X = Screen.Right - 200; |
---|
444 | panosteksti.Y = Screen.Top - 100; |
---|
445 | panosteksti.TextColor = Color.Red; |
---|
446 | |
---|
447 | Add(panosteksti); |
---|
448 | } |
---|
449 | |
---|
450 | void LuoPisteLaskuri() |
---|
451 | { |
---|
452 | pistelaskuri = new IntMeter(0); |
---|
453 | |
---|
454 | Label pistenaytto = new Label(); |
---|
455 | pistenaytto.X = Screen.Right - 100; |
---|
456 | pistenaytto.Y = Screen.Top - 150; |
---|
457 | pistenaytto.TextColor = Color.Red; |
---|
458 | |
---|
459 | pistenaytto.BindTo(pistelaskuri); |
---|
460 | Add(pistenaytto); |
---|
461 | |
---|
462 | Label pisteteksti = new Label("Pisteitä: "); |
---|
463 | pisteteksti.X = Screen.Right - 200; |
---|
464 | pisteteksti.Y = Screen.Top - 150; |
---|
465 | pisteteksti.TextColor = Color.Red; |
---|
466 | |
---|
467 | Add(pisteteksti); |
---|
468 | } |
---|
469 | |
---|
470 | PhysicsObject LuoLipas() |
---|
471 | { |
---|
472 | lipas = new PhysicsObject(20, 20); |
---|
473 | lipas.Mass = 2.0; |
---|
474 | |
---|
475 | lipas.Image = lippaanKuva; |
---|
476 | |
---|
477 | lipas.IgnoresExplosions = true; |
---|
478 | |
---|
479 | lipas.Tag = "Lipas"; |
---|
480 | |
---|
481 | Add(lipas); |
---|
482 | return lipas; |
---|
483 | } |
---|
484 | |
---|
485 | void LuoLipas(double x, double y) |
---|
486 | { |
---|
487 | LuoLipas().Position = new Vector(x,y); |
---|
488 | } |
---|
489 | |
---|
490 | void LipasKranaattiOsuma(PhysicsObject porkkana, PhysicsObject kohde) |
---|
491 | { |
---|
492 | if (kohde.Tag.ToString() == "Lipas") |
---|
493 | { |
---|
494 | ((PlatformCharacter)porkkana).Weapon.Ammo.Value +=5; |
---|
495 | kohde.Destroy(); |
---|
496 | } |
---|
497 | if (kohde.Tag.ToString() == "Kranaatti") |
---|
498 | { |
---|
499 | kranaattiLaskuri.Value++; |
---|
500 | kohde.Destroy(); |
---|
501 | } |
---|
502 | |
---|
503 | } |
---|
504 | |
---|
505 | void HeitaKranaatti() |
---|
506 | { |
---|
507 | |
---|
508 | if (kranaattiLaskuri.Value > 0) |
---|
509 | { |
---|
510 | Grenade kranaatti = new Grenade(10.0); |
---|
511 | kranaatti.Image = kranaatinKuva; |
---|
512 | kranaatti.Mass = 1.0; |
---|
513 | kranaatti.ExplosionRadius = 100.0; |
---|
514 | |
---|
515 | kranaattiLaskuri.Value -= 1; |
---|
516 | |
---|
517 | kranaatti.X = porkkana.X; |
---|
518 | kranaatti.Y = porkkana.Y; |
---|
519 | Add(kranaatti); |
---|
520 | Vector kranaatinlento = Vector.FromLengthAndAngle(700.0, porkkana.Weapon.Angle); |
---|
521 | kranaatti.Hit(kranaatinlento); |
---|
522 | kranaatti.Destroyed += new Action(delegate { kranaatti_Destroyed(kranaatti); }); |
---|
523 | } |
---|
524 | } |
---|
525 | |
---|
526 | void kranaatti_Destroyed(Grenade s) |
---|
527 | { |
---|
528 | foreach (PhysicsObject item in rajahdyksenTomaattiUhrit) |
---|
529 | { |
---|
530 | if (Vector.Distance(s.Position, item.Position) < s.ExplosionRadius) |
---|
531 | { |
---|
532 | TomaattiKuoli(item); |
---|
533 | rajahdys.AddEffect(item.Position, 50); |
---|
534 | } |
---|
535 | } |
---|
536 | |
---|
537 | foreach (GameObject item in rajahdyksenPorkkanaUhri) |
---|
538 | { |
---|
539 | if (Vector.Distance(s.Position, item.Position) < s.ExplosionRadius) |
---|
540 | { |
---|
541 | porkkana.Stop(); |
---|
542 | |
---|
543 | MediaPlayer.Play("pling"); |
---|
544 | |
---|
545 | porkkana.Destroy(); |
---|
546 | |
---|
547 | LuoHautakivi(); |
---|
548 | |
---|
549 | LopetaPeli(); |
---|
550 | |
---|
551 | } |
---|
552 | } |
---|
553 | |
---|
554 | |
---|
555 | } |
---|
556 | |
---|
557 | void vihollinenAmpuu(Timer ampumaAjastin) |
---|
558 | { |
---|
559 | PlatformCharacter tomaatti2 = ampumaAjastin.Tag as PlatformCharacter; |
---|
560 | |
---|
561 | Vector ampumaSuunta = (porkkana.Position - tomaatti2.Position).Normalize(); |
---|
562 | tomaatti2.Weapon.Angle = ampumaSuunta.Angle; |
---|
563 | |
---|
564 | PhysicsObject vihu = ampumaAjastin.Tag as PhysicsObject; |
---|
565 | |
---|
566 | PhysicsObject ammus = tomaatti2.Weapon.Shoot(); |
---|
567 | |
---|
568 | if (ammus != null) |
---|
569 | { |
---|
570 | ammus.Size *= 5; |
---|
571 | ammus.MaximumLifetime = TimeSpan.FromSeconds(1.5); |
---|
572 | ammus.IgnoresGravity = true; |
---|
573 | } |
---|
574 | } |
---|
575 | |
---|
576 | void LuoKranaattiLaskuri () |
---|
577 | { |
---|
578 | kranaattiLaskuri= new IntMeter(3); |
---|
579 | kranaattiLaskuri.MinValue = 0; |
---|
580 | Label kranaattiNaytto = new Label(); |
---|
581 | kranaattiNaytto.X = Screen.Right - 100; |
---|
582 | kranaattiNaytto.Y = Screen.Top - 50; |
---|
583 | kranaattiNaytto.TextColor = Color.Red; |
---|
584 | |
---|
585 | |
---|
586 | kranaattiNaytto.BindTo(kranaattiLaskuri); |
---|
587 | Add( kranaattiNaytto); |
---|
588 | |
---|
589 | Label kranaattiteksti = new Label("Kranaatteja jäljellä: "); |
---|
590 | kranaattiteksti.X = Screen.Right - 200; |
---|
591 | kranaattiteksti.Y = Screen.Top - 50; |
---|
592 | kranaattiteksti.TextColor = Color.Red; |
---|
593 | |
---|
594 | Add(kranaattiteksti); |
---|
595 | } |
---|
596 | |
---|
597 | PhysicsObject LuoKranaatti() |
---|
598 | { |
---|
599 | uusikranaatti = new PhysicsObject(40, 20); |
---|
600 | uusikranaatti.Mass = 2.0; |
---|
601 | |
---|
602 | uusikranaatti.Image = uudenKranaatinKuva; |
---|
603 | |
---|
604 | uusikranaatti.IgnoresExplosions = true; |
---|
605 | |
---|
606 | uusikranaatti.Tag = "Kranaatti"; |
---|
607 | |
---|
608 | Add(uusikranaatti); |
---|
609 | return uusikranaatti; |
---|
610 | } |
---|
611 | |
---|
612 | void LuoKranaatti(double x, double y) |
---|
613 | { |
---|
614 | LuoKranaatti().Position = new Vector(x, y); |
---|
615 | } |
---|
616 | |
---|
617 | PhysicsObject LuoMaali() |
---|
618 | { |
---|
619 | PhysicsObject maali = new PhysicsObject(40, 40); |
---|
620 | maali.Image = maalinKuva; |
---|
621 | maali.Mass = 4.0; |
---|
622 | AddCollisionHandler(maali, MaaliinOsuma); |
---|
623 | return maali; |
---|
624 | } |
---|
625 | |
---|
626 | void MaaliinOsuma(PhysicsObject maali, PhysicsObject kohde) |
---|
627 | { |
---|
628 | if (kohde == porkkana) |
---|
629 | { |
---|
630 | kenttaLaskuri.Value++; |
---|
631 | } |
---|
632 | } |
---|
633 | |
---|
634 | void TallennaPisteet(Window sender) |
---|
635 | { |
---|
636 | DataStorage.Save<ScoreList>(topLista, "pisteet.xlm"); |
---|
637 | |
---|
638 | Label lopetus = new Label(); |
---|
639 | lopetus.Position = Screen.Center; |
---|
640 | lopetus.TextColor = Color.Orange; |
---|
641 | lopetus.Font = Font.DefaultLargeBold; |
---|
642 | Add(lopetus); |
---|
643 | lopetus.Text = " Game over.\n" + |
---|
644 | "Jos haluat aloittaa alusta, paina enter/A. Jos taas haluat lopettaa, paina Esc/start."; |
---|
645 | |
---|
646 | ClearControls(); |
---|
647 | |
---|
648 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu pelistä"); |
---|
649 | Keyboard.Listen(Key.Enter, ButtonState.Pressed, Begin, null); |
---|
650 | |
---|
651 | ControllerOne.Listen(Button.A, ButtonState.Down, Begin, "Poistu pelistä"); |
---|
652 | ControllerOne.Listen(Button.Start, ButtonState.Down, Exit, null); |
---|
653 | |
---|
654 | } |
---|
655 | |
---|
656 | void LuoRaha() |
---|
657 | { |
---|
658 | raha = new PhysicsObject(40, 40); |
---|
659 | raha.Mass = 4.0; |
---|
660 | raha.Image = RahanKuva; |
---|
661 | AddCollisionHandler(raha, RahaanTormays); |
---|
662 | return raha; |
---|
663 | } |
---|
664 | |
---|
665 | void RahaanTormays(PhysicsObject raha, PhysicsObject kohde) |
---|
666 | { |
---|
667 | if(kohde == porkkana) |
---|
668 | { |
---|
669 | pistelaskuri.Value += 1; |
---|
670 | } |
---|
671 | } |
---|
672 | } |
---|