1 | using System; |
---|
2 | using Jypeli; |
---|
3 | using Jypeli.Widgets; |
---|
4 | using Jypeli.Assets; |
---|
5 | using Jypeli.Effects; |
---|
6 | |
---|
7 | |
---|
8 | class Tasohyppely : PhysicsGame |
---|
9 | { |
---|
10 | const double nopeus = 200; |
---|
11 | const double hyppyVoima = 4000; |
---|
12 | int kenttaLaskuri = 1; |
---|
13 | const int ruudunLeveys = 50; |
---|
14 | const int ruudunKorkeus = 50; |
---|
15 | PlatformCharacter pelaaja1; |
---|
16 | PhysicsObject maali; |
---|
17 | IntMeter pisteLaskuri; |
---|
18 | GameObject[] alkupisteet = new GameObject[10]; |
---|
19 | GameObject[] loppupisteet = new GameObject[10]; |
---|
20 | int kaikkipisteet; |
---|
21 | |
---|
22 | ExplosionSystem rajahdys; |
---|
23 | |
---|
24 | |
---|
25 | protected override void Begin() |
---|
26 | { |
---|
27 | rajahdys = new ExplosionSystem(LoadImage("tahti"), 300); |
---|
28 | Add(rajahdys); |
---|
29 | |
---|
30 | Gravity = new Vector(0, -1000); |
---|
31 | //LuoMaali(); |
---|
32 | luoKentta1(); |
---|
33 | lisaaNappaimet(); |
---|
34 | |
---|
35 | Camera.Follow(pelaaja1); |
---|
36 | Camera.ZoomFactor = 2.0; |
---|
37 | Camera.StayInLevel = true; |
---|
38 | kaikkipisteet = 0; |
---|
39 | } |
---|
40 | |
---|
41 | void luoKentta1() |
---|
42 | { |
---|
43 | |
---|
44 | Label teksti2 = new Label("Taso 1"); |
---|
45 | teksti2.TextColor = Color.White; |
---|
46 | teksti2.X = Screen.Left + 1000; |
---|
47 | teksti2.Y = Screen.Top - 100; |
---|
48 | Add(teksti2); |
---|
49 | |
---|
50 | |
---|
51 | |
---|
52 | LuoLaskuri(); |
---|
53 | //Level.Background.CreateGradient(Color.White, Color.SkyBlue); |
---|
54 | Level.Background.CreateStars(); |
---|
55 | |
---|
56 | TileMap ruudut = TileMap.FromFile("kentta1.txt"); |
---|
57 | |
---|
58 | ruudut['-'] = lisaaTaso; |
---|
59 | ruudut['M'] = LuoMaaliTileMap; |
---|
60 | ruudut['X'] = lisaaPelaajatTileMap; |
---|
61 | //ruudut['V'] = LuoVihollinen; |
---|
62 | ruudut['L'] = LuoLaatikko; |
---|
63 | ruudut['A'] = LuoLaatikko2; |
---|
64 | ruudut['0'] = LuoReitti0; |
---|
65 | ruudut['1'] = LuoReitti1; |
---|
66 | ruudut['2'] = LuoReitti2; |
---|
67 | ruudut['3'] = LuoReitti3; |
---|
68 | ruudut['4'] = LuoReitti4; |
---|
69 | Gravity = new Vector(0, -1000); |
---|
70 | |
---|
71 | ruudut.Insert(ruudunLeveys, ruudunKorkeus); |
---|
72 | |
---|
73 | for (int i = 0; i < 10; i++) |
---|
74 | { |
---|
75 | if (alkupisteet[i] != null) |
---|
76 | { |
---|
77 | |
---|
78 | PhysicsObject vihollinen = LuoVihollinen(i); |
---|
79 | Add(vihollinen); |
---|
80 | } |
---|
81 | } |
---|
82 | } |
---|
83 | void luoKentta2() |
---|
84 | { |
---|
85 | |
---|
86 | Label teksti = new Label("Taso 2"); |
---|
87 | teksti.TextColor = Color.White; |
---|
88 | teksti.X = Screen.Left + 1000; |
---|
89 | teksti.Y = Screen.Top - 100; |
---|
90 | Add(teksti); |
---|
91 | |
---|
92 | ClearAll(); |
---|
93 | LuoLaskuri(); |
---|
94 | //Level.Background.CreateGradient(Color.White, Color.SkyBlue); |
---|
95 | Level.Background.CreateStars(); |
---|
96 | |
---|
97 | TileMap ruudut = TileMap.FromFile("kentta2.txt"); |
---|
98 | |
---|
99 | ruudut['-'] = lisaaTaso; |
---|
100 | ruudut['M'] = LuoMaaliTileMap; |
---|
101 | ruudut['X'] = lisaaPelaajatTileMap; |
---|
102 | //ruudut['V'] = LuoVihollinen; |
---|
103 | ruudut['L'] = LuoLaatikko; |
---|
104 | ruudut['A'] = LuoLaatikko2; |
---|
105 | ruudut['0'] = LuoReitti0; |
---|
106 | ruudut['1'] = LuoReitti1; |
---|
107 | ruudut['2'] = LuoReitti2; |
---|
108 | ruudut['3'] = LuoReitti3; |
---|
109 | ruudut['4'] = LuoReitti4; |
---|
110 | ruudut['5'] = LuoReitti5; |
---|
111 | ruudut['6'] = LuoReitti6; |
---|
112 | ruudut['7'] = LuoReitti7; |
---|
113 | ruudut['8'] = LuoReitti8; |
---|
114 | Gravity = new Vector(0, -1000); |
---|
115 | |
---|
116 | ruudut.Insert(ruudunLeveys, ruudunKorkeus); |
---|
117 | |
---|
118 | for (int i = 0; i < 10; i++) |
---|
119 | { |
---|
120 | if (alkupisteet[i] != null) |
---|
121 | { |
---|
122 | |
---|
123 | PhysicsObject vihollinen = LuoVihollinen(i); |
---|
124 | Add(vihollinen); |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | Camera.Follow(pelaaja1); |
---|
129 | Camera.ZoomFactor = 2.0; |
---|
130 | Camera.StayInLevel = true; |
---|
131 | lisaaNappaimet(); |
---|
132 | rajahdys = new ExplosionSystem(LoadImage("tahti"), 300); |
---|
133 | Add(rajahdys); |
---|
134 | } |
---|
135 | void luoKentta3() |
---|
136 | { |
---|
137 | ClearAll(); |
---|
138 | |
---|
139 | |
---|
140 | //Level.Background.CreateGradient(Color.White, Color.SkyBlue); |
---|
141 | Level.Background.CreateStars(); |
---|
142 | |
---|
143 | Label lopputeksti = new Label(); |
---|
144 | //lopputeksti.Image = Image." |
---|
145 | |
---|
146 | Gravity = new Vector(0, -1000); |
---|
147 | Camera.Follow(pelaaja1); |
---|
148 | Camera.ZoomFactor = 2.0; |
---|
149 | Camera.StayInLevel = true; |
---|
150 | lisaaNappaimet(); |
---|
151 | } |
---|
152 | |
---|
153 | void luoKentta4() |
---|
154 | { |
---|
155 | |
---|
156 | } |
---|
157 | |
---|
158 | void luoKentta5() |
---|
159 | { |
---|
160 | } |
---|
161 | |
---|
162 | void lisaaTasoPaikkaan(double x, double y) |
---|
163 | { |
---|
164 | PhysicsObject taso = PhysicsObject.CreateStaticObject(200, 18); |
---|
165 | taso.Color = Color.White; |
---|
166 | taso.X = x; |
---|
167 | taso.Y = y; |
---|
168 | Add(taso); |
---|
169 | } |
---|
170 | |
---|
171 | PhysicsObject lisaaTaso() |
---|
172 | { |
---|
173 | PhysicsObject taso = PhysicsObject.CreateStaticObject(200, 18); |
---|
174 | taso.Color = Color.White; |
---|
175 | |
---|
176 | return taso; |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | |
---|
181 | |
---|
182 | //void lisaaPelaajat() |
---|
183 | //{ |
---|
184 | // pelaaja1 = new PlatformCharacter(40, 40); |
---|
185 | // pelaaja1.Mass = 4.0; |
---|
186 | // pelaaja1.Image = LoadImage("tahti"); |
---|
187 | // pelaaja1.X = 0; |
---|
188 | // pelaaja1.Y = Level.Bottom + 100; |
---|
189 | // LaserGun laserPyssy = new LaserGun(20, 5); |
---|
190 | // //laserPyssy.LaserCollision = LaserSadeOsuu; |
---|
191 | // pelaaja1.Weapon = new LaserGun(0, 0); |
---|
192 | // Add(pelaaja1); |
---|
193 | // //laserPyssy.Ammo.Value = 100; |
---|
194 | // laserPyssy.IsVisible = false; |
---|
195 | //} |
---|
196 | |
---|
197 | void vaihdaKentta(int kenttaLaskuri) |
---|
198 | { |
---|
199 | kaikkipisteet += pisteLaskuri.Value; |
---|
200 | if (kenttaLaskuri == 1) |
---|
201 | { |
---|
202 | luoKentta1(); |
---|
203 | } |
---|
204 | if (kenttaLaskuri == 2) |
---|
205 | { |
---|
206 | luoKentta2(); |
---|
207 | } |
---|
208 | if (kenttaLaskuri == 3) |
---|
209 | { |
---|
210 | luoKentta3(); |
---|
211 | } |
---|
212 | |
---|
213 | } |
---|
214 | |
---|
215 | void lisaaNappaimet() |
---|
216 | { |
---|
217 | Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet"); |
---|
218 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu pelistä"); |
---|
219 | |
---|
220 | Keyboard.Listen(Key.Left, ButtonState.Down, liikuta, "Liikkuu vasemmalle", pelaaja1, -nopeus); |
---|
221 | Keyboard.Listen(Key.Right, ButtonState.Down, liikuta, "Liikkuu oikealle", pelaaja1, nopeus); |
---|
222 | Keyboard.Listen(Key.Space, ButtonState.Pressed, hyppaa, "Hyppää", pelaaja1, hyppyVoima); |
---|
223 | Keyboard.Listen(Key.RightControl, ButtonState.Down, AmmuAseella, "Ammu"); |
---|
224 | |
---|
225 | ControllerOne.Listen(Button.Back, ButtonState.Pressed, Exit, "Poistu pelistä"); |
---|
226 | |
---|
227 | ControllerOne.Listen(Button.DPadLeft, ButtonState.Down, liikuta, "Pelaaja liikkuu vasemmalle", pelaaja1, -nopeus); |
---|
228 | ControllerOne.Listen(Button.DPadRight, ButtonState.Down, liikuta, "Pelaaja liikkuu oikealle", pelaaja1, nopeus); |
---|
229 | ControllerOne.Listen(Button.A, ButtonState.Pressed, hyppaa, "Pelaaja hyppää", pelaaja1, hyppyVoima); |
---|
230 | } |
---|
231 | |
---|
232 | void liikuta(PlatformCharacter hahmo, double nopeus) |
---|
233 | { |
---|
234 | hahmo.Walk(nopeus); |
---|
235 | } |
---|
236 | |
---|
237 | void hyppaa(PlatformCharacter hahmo, double voima) |
---|
238 | { |
---|
239 | hahmo.Jump(voima); |
---|
240 | } |
---|
241 | void KasittelePelaajanTormays(PhysicsObject pelaaja, PhysicsObject kohde) |
---|
242 | { |
---|
243 | if (kohde.Tag.ToString() == "maali1") |
---|
244 | { |
---|
245 | kenttaLaskuri++; |
---|
246 | vaihdaKentta(kenttaLaskuri); |
---|
247 | |
---|
248 | } |
---|
249 | if (kohde.Tag.ToString() == "maali2") |
---|
250 | { |
---|
251 | kenttaLaskuri++; |
---|
252 | vaihdaKentta(kenttaLaskuri); |
---|
253 | } |
---|
254 | |
---|
255 | if (kohde.Tag.ToString() == "vihollinen") |
---|
256 | { |
---|
257 | rajahdys.MinScale = 0.01; |
---|
258 | rajahdys.MaxScale = 0.4; |
---|
259 | rajahdys.MinVelocity = 10; |
---|
260 | rajahdys.MaxVelocity = 200; |
---|
261 | rajahdys.MinLifetime = 0.1; |
---|
262 | rajahdys.MaxLifetime = 1.5; |
---|
263 | |
---|
264 | rajahdys.AddEffect(pelaaja.X, pelaaja.Y, 50); |
---|
265 | GameOver(); |
---|
266 | pelaaja.Destroy(); |
---|
267 | |
---|
268 | |
---|
269 | |
---|
270 | } |
---|
271 | |
---|
272 | } |
---|
273 | |
---|
274 | void KasitteleAmpuminen(PhysicsObject ammus, PhysicsObject kohde) |
---|
275 | { |
---|
276 | if (kohde.Tag.ToString() == "vihollinen") |
---|
277 | { |
---|
278 | Explosion rajahdys2 = new Explosion(25); |
---|
279 | rajahdys2.Position = kohde.Position; |
---|
280 | rajahdys2.Speed = 200.0; |
---|
281 | rajahdys2.Force = 0; |
---|
282 | rajahdys2.ShockwaveColor = Color.Lime; |
---|
283 | pisteLaskuri.Value++; |
---|
284 | pisteLaskuri.Value += 250; |
---|
285 | pisteLaskuri.Value = pisteLaskuri.Value + 249; |
---|
286 | |
---|
287 | |
---|
288 | Add(rajahdys2); |
---|
289 | kohde.Destroy(); |
---|
290 | |
---|
291 | } |
---|
292 | } |
---|
293 | void LuoMaali() |
---|
294 | { |
---|
295 | maali = PhysicsObject.CreateStaticObject(75.0, 120.0); |
---|
296 | maali.X = 150.0; |
---|
297 | maali.Y = 200.0; |
---|
298 | maali.Shape = Shapes.Rectangle; |
---|
299 | maali.Color = Color.Yellow; |
---|
300 | maali.Tag = "maali1"; |
---|
301 | Add(maali); |
---|
302 | |
---|
303 | } |
---|
304 | PhysicsObject lisaaPelaajatTileMap() |
---|
305 | { |
---|
306 | pelaaja1 = new PlatformCharacter(40, 40); |
---|
307 | pelaaja1.Mass = 4.0; |
---|
308 | pelaaja1.Image = LoadImage("tahti"); |
---|
309 | pelaaja1.X = 0; |
---|
310 | pelaaja1.Y = Level.Bottom + 100; |
---|
311 | LaserGun laserPyssy = new LaserGun(20, 5); |
---|
312 | pelaaja1.Weapon = new LaserGun(0, 0); |
---|
313 | Add(pelaaja1); |
---|
314 | laserPyssy.IsVisible = false; |
---|
315 | AddCollisionHandler(pelaaja1, KasittelePelaajanTormays); |
---|
316 | AddCollisionHandler(pelaaja1, KeraaPisteita); |
---|
317 | |
---|
318 | return pelaaja1; |
---|
319 | |
---|
320 | } |
---|
321 | PhysicsObject LuoMaaliTileMap() |
---|
322 | { |
---|
323 | maali = PhysicsObject.CreateStaticObject(75.0, 100.0); |
---|
324 | maali.X = 150.0; |
---|
325 | maali.Y = 200.0; |
---|
326 | maali.Shape = Shapes.Rectangle; |
---|
327 | maali.Color = Color.Yellow; |
---|
328 | maali.Tag = "maali2"; |
---|
329 | return maali; |
---|
330 | } |
---|
331 | |
---|
332 | PhysicsObject LuoVihollinen(int reitti) |
---|
333 | { |
---|
334 | PhysicsObject vihollinen = new PhysicsObject(50, 50); |
---|
335 | vihollinen.Shape = Shapes.Circle; |
---|
336 | vihollinen.Color = Color.DarkMagenta; |
---|
337 | vihollinen.Mass = 4.0; |
---|
338 | vihollinen.Tag = "vihollinen"; |
---|
339 | PathFollowerBrain aivo = new PathFollowerBrain(); |
---|
340 | //vihollinen.IgnoresCollisionResponse = true; |
---|
341 | vihollinen.IgnoresGravity = true; |
---|
342 | vihollinen.Position = alkupisteet[reitti].Position; |
---|
343 | |
---|
344 | Vector[] pisteet = new Vector[] |
---|
345 | { |
---|
346 | alkupisteet[reitti].Position, |
---|
347 | loppupisteet[reitti].Position, |
---|
348 | }; |
---|
349 | |
---|
350 | aivo.Path = pisteet; |
---|
351 | vihollinen.Brain = aivo; |
---|
352 | aivo.Speed = 100.0; |
---|
353 | return vihollinen; |
---|
354 | |
---|
355 | } |
---|
356 | |
---|
357 | void AmmuAseella() |
---|
358 | { |
---|
359 | PhysicsObject ammus = pelaaja1.Weapon.Shoot(); |
---|
360 | |
---|
361 | if (ammus != null) |
---|
362 | { |
---|
363 | ammus.Size *= 4; |
---|
364 | //ammus.Shape = Shapes.Circle; |
---|
365 | |
---|
366 | ammus.Color = Color.Lime; |
---|
367 | AddCollisionHandler(ammus, KasitteleAmpuminen); |
---|
368 | ammus.Tag = "ammus"; |
---|
369 | |
---|
370 | } |
---|
371 | } |
---|
372 | GameObject LuoReittiPiste(int reitti) |
---|
373 | { |
---|
374 | GameObject piste = new GameObject(1, 1); |
---|
375 | piste.IsVisible = false; |
---|
376 | if (alkupisteet[reitti] == null) |
---|
377 | { |
---|
378 | alkupisteet[reitti] = piste; |
---|
379 | } |
---|
380 | else |
---|
381 | { |
---|
382 | loppupisteet[reitti] = piste; |
---|
383 | } |
---|
384 | return piste; |
---|
385 | |
---|
386 | } |
---|
387 | |
---|
388 | GameObject LuoReitti0() |
---|
389 | { |
---|
390 | return LuoReittiPiste(0); |
---|
391 | } |
---|
392 | |
---|
393 | GameObject LuoReitti1() |
---|
394 | { |
---|
395 | return LuoReittiPiste(1); |
---|
396 | } |
---|
397 | GameObject LuoReitti2() |
---|
398 | { |
---|
399 | return LuoReittiPiste(2); |
---|
400 | } |
---|
401 | GameObject LuoReitti3() |
---|
402 | { |
---|
403 | return LuoReittiPiste(3); |
---|
404 | } |
---|
405 | GameObject LuoReitti4() |
---|
406 | { |
---|
407 | return LuoReittiPiste(4); |
---|
408 | |
---|
409 | } |
---|
410 | GameObject LuoReitti5() |
---|
411 | { |
---|
412 | return LuoReittiPiste(5); |
---|
413 | } |
---|
414 | GameObject LuoReitti6() |
---|
415 | { |
---|
416 | return LuoReittiPiste(6); |
---|
417 | } |
---|
418 | GameObject LuoReitti7() |
---|
419 | { |
---|
420 | return LuoReittiPiste(7); |
---|
421 | } |
---|
422 | GameObject LuoReitti8() |
---|
423 | { |
---|
424 | return LuoReittiPiste(8); |
---|
425 | } |
---|
426 | |
---|
427 | |
---|
428 | void LuoLaskuri() |
---|
429 | { |
---|
430 | pisteLaskuri = new IntMeter(kaikkipisteet); |
---|
431 | |
---|
432 | Label pisteNaytto = new Label(); |
---|
433 | pisteNaytto.X = Screen.Left + 175; |
---|
434 | pisteNaytto.Y = Screen.Top - 100; |
---|
435 | pisteNaytto.TextColor = Color.Red; |
---|
436 | |
---|
437 | pisteNaytto.BindTo(pisteLaskuri); |
---|
438 | Add(pisteNaytto); |
---|
439 | |
---|
440 | Label pisteTeksti = new Label("Pisteitä: "); |
---|
441 | pisteTeksti.X = Screen.Left + 100; |
---|
442 | pisteTeksti.Y = Screen.Top - 100; |
---|
443 | pisteTeksti.TextColor = Color.Red; |
---|
444 | Add(pisteTeksti); |
---|
445 | } |
---|
446 | |
---|
447 | void KeraaPisteita(PhysicsObject pelaaja, PhysicsObject kohde) |
---|
448 | { |
---|
449 | if (kohde.Tag.ToString() == "Laatikko") |
---|
450 | { |
---|
451 | pisteLaskuri.Value++; |
---|
452 | pisteLaskuri.Value += 50; |
---|
453 | pisteLaskuri.Value = pisteLaskuri.Value + 49; |
---|
454 | kohde.Destroy(); |
---|
455 | |
---|
456 | |
---|
457 | } |
---|
458 | if (kohde.Tag.ToString() == "Laatikko2") |
---|
459 | { |
---|
460 | pisteLaskuri.Value++; |
---|
461 | pisteLaskuri.Value += 500; |
---|
462 | pisteLaskuri.Value = pisteLaskuri.Value + 499; |
---|
463 | kohde.Destroy(); |
---|
464 | |
---|
465 | |
---|
466 | } |
---|
467 | } |
---|
468 | PhysicsObject LuoLaatikko() |
---|
469 | { |
---|
470 | PhysicsObject laatikko; |
---|
471 | laatikko = new PhysicsObject(20.0, 20.0); |
---|
472 | |
---|
473 | laatikko.Shape = Shapes.Rectangle; |
---|
474 | laatikko.Color = Color.Green; |
---|
475 | laatikko.Mass = 1000.0; |
---|
476 | laatikko.Tag = "Laatikko"; |
---|
477 | |
---|
478 | return laatikko; |
---|
479 | |
---|
480 | } |
---|
481 | void GameOver() |
---|
482 | { |
---|
483 | //ClearAll(); |
---|
484 | Label peliOhi = new Label("Game Over!"); |
---|
485 | |
---|
486 | peliOhi.TextColor = Color.Red; |
---|
487 | |
---|
488 | //peliOhi.Position = pelaaja1.Position; |
---|
489 | peliOhi.Position = Screen.Center; |
---|
490 | Add(peliOhi); |
---|
491 | } |
---|
492 | |
---|
493 | PhysicsObject LuoLaatikko2() |
---|
494 | { |
---|
495 | PhysicsObject laatikko2; |
---|
496 | laatikko2 = new PhysicsObject(35.0, 35.0); |
---|
497 | |
---|
498 | laatikko2.Shape = Shapes.Rectangle; |
---|
499 | laatikko2.Color = Color.Cyan; |
---|
500 | laatikko2.Mass = 1000.0; |
---|
501 | laatikko2.Tag = "Laatikko2"; |
---|
502 | |
---|
503 | return laatikko2; |
---|
504 | |
---|
505 | } |
---|
506 | |
---|
507 | } |
---|
508 | |
---|