source: 2011/23/ookantol/Haamuongelma/Haamuongelma/Peli.cs @ 1886

Revision 1886, 17.1 KB checked in by ookantol, 12 years ago (diff)
Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Assets;
5using Jypeli.Controls;
6using Jypeli.Effects;
7using Jypeli.Widgets;
8
9public class Peli : PhysicsGame
10{
11    const double nopeus = 300;
12    const double hyppyNopeus = 1000;
13    const int RUUDUN_KOKO = 40;
14
15    PlatformCharacter pelaaja1;
16
17    IntMeter laskuri;
18    IntMeter laskuri2;
19
20
21    Label naytto;
22    Label naytto2;
23
24
25    List<Label> Valikonkohdat;
26
27    Image pelaajanKuva = LoadImage("Buddy");
28    Image kelloKuva = LoadImage("kello");
29    Image kukkaKuva = LoadImage("kukka");
30    Image haamu1Kuva = LoadImage("haamu3");
31    Image haamu2Kuva = LoadImage("haamu4");
32
33
34
35    public override void Begin()
36    {
37        aloitusvalikko();
38        //MediaPlayer.Play("MTA");
39        //MediaPlayer.IsRepeating = true;
40    }
41
42
43
44    void luoKentta()
45    {
46        TileMap kentta = TileMap.FromFile("kentta1.txt");
47        kentta['-'] = lisaaTaso;
48        kentta['_'] = lisaaTaso2;
49        kentta['+'] = lisaaKello;
50        kentta['Y'] = lisaaKukka;
51        kentta['B'] = lisaaPelaaja;
52        kentta['1'] = lisaaHaamu1;
53        kentta['2'] = lisaaHaamu2;
54        kentta.Insert(RUUDUN_KOKO, RUUDUN_KOKO);
55        Level.CreateBorders(1.0, true);
56        Level.Background.CreateStars();
57    }
58    void luoKentta2()
59    {
60        TileMap kentta = TileMap.FromFile("kentta2.txt");
61        kentta['-'] = lisaaTaso;
62        kentta['_'] = lisaaTaso2;
63        kentta['+'] = lisaaKello;
64        kentta['Y'] = lisaaKukka;
65        kentta['B'] = lisaaPelaaja;
66        kentta['1'] = lisaaHaamu1;
67        kentta['2'] = lisaaHaamu2;
68        kentta.Insert(RUUDUN_KOKO, RUUDUN_KOKO);
69        Level.CreateBorders(1.0, true);
70        Level.Background.CreateStars();
71    }
72    void luoKentta3()
73    {
74        TileMap kentta = TileMap.FromFile("kentta3.txt");
75        kentta['-'] = lisaaTaso;
76        kentta['_'] = lisaaTaso2;
77        kentta['+'] = lisaaKello;
78        kentta['Y'] = lisaaKukka;
79        kentta['B'] = lisaaPelaaja;
80        kentta['1'] = lisaaHaamu1;
81        kentta['2'] = lisaaHaamu2;
82        kentta.Insert(RUUDUN_KOKO, RUUDUN_KOKO);
83        Level.CreateBorders(1.0, true);
84        Level.Background.CreateStars();
85    }
86
87
88
89    PhysicsObject lisaaTaso()
90    {
91        PhysicsObject taso = PhysicsObject.CreateStaticObject(3 * RUUDUN_KOKO, RUUDUN_KOKO);
92        taso.Image = LoadImage("pilvi");
93        return taso;
94    }
95    PhysicsObject lisaaTaso2()
96    {
97        PhysicsObject taso2 = PhysicsObject.CreateStaticObject(3 * RUUDUN_KOKO, RUUDUN_KOKO);
98        taso2.Image = LoadImage("ruoho");
99        return taso2;
100    }
101    PhysicsObject lisaaKello()
102    {
103        PhysicsObject kello = PhysicsObject.CreateStaticObject(RUUDUN_KOKO, RUUDUN_KOKO);
104        kello.Image = kelloKuva;
105        kello.CollisionIgnoreGroup = 1;
106        kello.Angle = RandomGen.NextAngle();
107        kello.Tag = "kello";
108        return kello;
109    }
110    PhysicsObject lisaaKukka()
111    {
112        PhysicsObject kukka = PhysicsObject.CreateStaticObject(RUUDUN_KOKO, RUUDUN_KOKO);
113        kukka.Image = kukkaKuva;
114        kukka.IgnoresCollisionResponse = true;
115        kukka.Tag = "kukka";
116        return kukka;
117    }
118    PlatformCharacter lisaaPelaaja()
119    {
120        pelaaja1 = new PlatformCharacter(27, 40);
121        pelaaja1.Mass = 6.0;
122        pelaaja1.Image = pelaajanKuva;
123        AddCollisionHandler(pelaaja1, osuKelloon);
124        AddCollisionHandler(pelaaja1, osuHaamuun);
125        AddCollisionHandler(pelaaja1, osuKukkaan);
126
127        return pelaaja1;
128    }
129    PhysicsObject lisaaHaamu1()
130    {
131        PhysicsObject haamu1 = new PhysicsObject(RUUDUN_KOKO, RUUDUN_KOKO);
132        haamu1.Image = LoadImage("haamu3");
133        haamu1.Tag = "haamu";
134        haamu1.CanRotate = false;
135        haamu1.CollisionIgnoreGroup = 1;
136        haamu1.IgnoresGravity = true;
137        Timer asetaAivot = new Timer();
138        asetaAivot.Tag = haamu1;
139        asetaAivot.Trigger += asetaHaamulleAivot;
140        asetaAivot.Interval = 1.5;
141        asetaAivot.Start();
142        return haamu1;
143    }
144    PhysicsObject lisaaHaamu2()
145    {
146        PhysicsObject haamu2 = new PhysicsObject(RUUDUN_KOKO, RUUDUN_KOKO);
147        haamu2.Image = LoadImage("haamu4");
148        haamu2.Tag = "haamu";
149        haamu2.CanRotate = false;
150        haamu2.CollisionIgnoreGroup = 1;
151        haamu2.IgnoresGravity = true;
152        Timer asetaAivot = new Timer();
153        asetaAivot.Tag = haamu2;
154        asetaAivot.Trigger += asetaHaamulleAivot;
155        asetaAivot.Interval = 2.0;
156        asetaAivot.Start();
157        return haamu2;
158    }
159
160
161    void lisaaNappaimet()
162    {
163        Keyboard.Listen(Key.F1, ButtonState.Pressed, ShowControlHelp, "Näytä ohjeet");
164        Keyboard.Listen(Key.Escape, ButtonState.Pressed, aloitusvalikko, "Päävalikkoon");
165
166        Keyboard.Listen(Key.Left, ButtonState.Down, liikuta, "Liikkuu vasemmalle", pelaaja1, -nopeus);
167        Keyboard.Listen(Key.Right, ButtonState.Down, liikuta, "Liikkuu vasemmalle", pelaaja1, nopeus);
168        Keyboard.Listen(Key.Up, ButtonState.Pressed, hyppaa, "Pelaaja hyppää", pelaaja1, hyppyNopeus);
169        Keyboard.Listen(Key.A, ButtonState.Down, liikuta, "Liikkuu vasemmalle", pelaaja1, -nopeus);
170        Keyboard.Listen(Key.D, ButtonState.Down, liikuta, "Liikkuu vasemmalle", pelaaja1, nopeus);
171        Keyboard.Listen(Key.W, ButtonState.Pressed, hyppaa, "Pelaaja hyppää", pelaaja1, hyppyNopeus);
172
173        ControllerOne.Listen(Button.Back, ButtonState.Pressed, Exit, "Poistu pelistä");
174    }
175    void liikuta(PlatformCharacter hahmo, double nopeus)
176    {
177        hahmo.Walk(nopeus);
178    }
179    void hyppaa(PlatformCharacter hahmo, double nopeus)
180    {
181        hahmo.Jump(nopeus);
182    }
183
184
185
186    IntMeter elamat()
187    {
188        laskuri = new IntMeter(5);
189        naytto = new Label(140, 70);
190        naytto.Title = "Lives left ";
191        naytto.Size = new Vector(140, 60);
192        naytto.X = Screen.Left + 90;
193        naytto.Y = Screen.Top - 80;
194        naytto.TextColor = Color.White;       
195        naytto.BindTo(laskuri);
196        Add(naytto);
197        return laskuri;
198
199    }
200    IntMeter kerattavat(int maxmaara)
201    {
202        laskuri2 = new IntMeter(0);
203        laskuri2.MaxValue = maxmaara;
204        laskuri2.Changed += new Meter<int>.OnChangeHandler(laskuri2_Changed);
205        laskuri2.UpperLimit += laskuriYlarajassa;
206        naytto2 = new Label(400, 70);
207        naytto2.Text = "Ghosts & clocks destroyed " + laskuri2.Value.ToString() + " / " + laskuri2.MaxValue.ToString();
208        naytto2.Size = new Vector(400, 70);
209        naytto2.X = Screen.Left + 190;
210        naytto2.Y = Screen.Top - 50;
211        naytto2.TextColor = Color.White;
212        Add(naytto2);
213        return laskuri2;
214
215    }
216    void laskuri2_Changed(int oldValue, int newValue)
217    {
218        naytto2.Text = "Ghosts & clocks destroyed " + laskuri2.Value.ToString() + " / " + laskuri2.MaxValue.ToString();
219    }
220    void laskuriYlarajassa(int arvo)
221    {
222        if (arvo == 38)
223        { 
224        Label Win = new Label ("Field 1 done!");
225        Win.Position = new Vector(10, 0);
226        Win.TextColor = RandomGen.NextColor();
227        Add(Win);
228        Timer.SingleShot(3.0, Aloita2);
229        }
230        else if (arvo == 40)
231        {
232            Label Win = new Label("Field 2 done!");
233            Win.Position = new Vector(10, 0);
234            Win.TextColor = RandomGen.NextColor();
235            Add(Win);
236            Timer.SingleShot(3.0, Aloita3);
237        }
238        else if (arvo == 46)
239        {
240            Label Win = new Label("Field 3 done! YOU WIN! YOU SAVED THE FLOWERS!");
241            Win.Position = new Vector(10, 0);
242            Win.TextColor = RandomGen.NextColor();
243            Add(Win);
244            Timer.SingleShot(3.0, aloitusvalikko);
245        }   
246    }
247
248
249
250    void osuKelloon(PhysicsObject hahmo, PhysicsObject kohde)
251    {
252        if (kohde.Tag.ToString() == "kello")
253        {
254            kohde.Destroy();
255            laskuri2.Value += 1;
256        }
257
258    }
259    void osuHaamuun(PhysicsObject hahmo, PhysicsObject kohde)
260    {
261        if (kohde.Tag.ToString() == "haamu")
262        {
263            if (kohde.Top < pelaaja1.Position.Y)
264            {
265                kohde.Destroy();
266                laskuri2.Value += 1;
267            }
268            else
269            {
270                laskuri.Value--;
271                Vector lento = hahmo.Position - kohde.Position;
272                hahmo.Hit(1000*lento);
273            }
274
275               if ( laskuri.Value == 0)
276            {
277                hahmo.Destroy();
278                Label naytto = new Label(700, - 600);
279                naytto.TextColor = RandomGen.NextColor();
280                naytto.Text = "Game Over";
281
282                Add(naytto);
283
284            }
285        }
286       
287    }
288    void osuKukkaan (PhysicsObject pelaaja1, PhysicsObject kohde)
289    {
290        if (kohde.Tag.ToString() == "kukka")
291        {
292            Label apua = new Label(600, - 800);
293            apua.X = Screen.Left + 500;
294            apua.Y = Screen.Top - 600;
295            apua.TextColor = Color.White;
296            apua.Text = "Help us! Collect the clocks and destroy evil ghosts!";
297            Timer.SingleShot(1.0, apua.Destroy);
298            Add(apua);
299        }
300    }
301
302
303
304    void aloitusvalikko()
305    {
306        ClearAll();
307        IsFullScreen = true;
308        Valikonkohdat = new List<Label>();
309        Level.Background.CreateGradient (Color.Orange, Color.White);
310
311        Label kohta = new Label("Ghost Troubles");
312        kohta.Position = new Vector(0, 140);
313        kohta.Font = Font.DefaultLargeBold;
314        kohta.TextColor = Color.Orange;
315        Add(kohta);
316
317        Label kohta1 = new Label("Start Game");
318        kohta1.Position = new Vector(0, 80);
319        Valikonkohdat.Add(kohta1);
320
321        Label kohta2 = new Label("Exit");
322        kohta2.Position = new Vector(0, -80);
323        Valikonkohdat.Add(kohta2);
324
325        Label kohta3 = new Label("Fields");
326        kohta3.Position = new Vector(0, 30);
327        Valikonkohdat.Add(kohta3);
328
329        Label kohta4 = new Label("Instructions");
330        kohta4.Position = new Vector(0, - 20);
331        Valikonkohdat.Add(kohta4);
332
333
334        foreach (Label Valikonkohta in Valikonkohdat)
335        {
336            Valikonkohta.TextColor = Color.White;
337            Add(Valikonkohta);
338        }
339
340        Mouse.ListenOn(kohta1, MouseButton.Left, ButtonState.Pressed, Start, null);
341        Mouse.ListenOn(kohta2, MouseButton.Left, ButtonState.Pressed, Lopeta, null);
342        Mouse.ListenOn(kohta3, MouseButton.Left, ButtonState.Pressed, kentat, null);
343        Mouse.ListenOn(kohta4, MouseButton.Left, ButtonState.Pressed, ohjeet, null);
344
345        Keyboard.Listen(Key.Enter, ButtonState.Pressed, Start, null);
346
347        Mouse.IsCursorVisible = true;
348        Mouse.ListenMovement(1.0, ValikossaLiikkuminen, null);
349
350        Keyboard.Listen(Key.Escape, ButtonState.Pressed, Lopeta, null);
351
352    }
353    void Start()
354    {
355        ClearAll();
356        IsFullScreen = true;
357        Valikonkohdat = new List<Label>();
358        Level.Background.CreateGradient(Color.Orange, Color.White);
359
360        Label kohta3 = new Label(" Just a normal day. Buddy is on his way home, when he sees mean ghosts. Lots of them. He tries to avoid them,       but then he meets dying flowers, which are dying     because the ghosts took all the good energy out of the air. To help them, Buddy must destroy all the ghosts and                     collect clocks, that reverse time...");
361        kohta3.Size = new Vector(600, 130);
362        kohta3.SizeMode = TextSizeMode.Wrapped;
363        kohta3.Position = new Vector(0, 100);
364        kohta3.TextColor = Color.Orange;
365        Add(kohta3);
366
367        Label kohta4 = new Label("Start");
368        kohta4.Position = new Vector(0, -40);
369        kohta4.TextColor = Color.White;
370        Valikonkohdat.Add(kohta4);
371
372        foreach (Label Valikonkohta in Valikonkohdat)
373        {
374            Valikonkohta.TextColor = Color.White;
375            Add(Valikonkohta);
376        }
377
378        Mouse.ListenOn(kohta4, MouseButton.Left, ButtonState.Pressed, Aloita, null);
379        Keyboard.Listen(Key.Escape, ButtonState.Pressed, aloitusvalikko, null);
380        Keyboard.Listen(Key.Enter, ButtonState.Pressed, Aloita, null);
381
382        Mouse.ListenMovement(1.0, ValikossaLiikkuminen2, null);
383
384        Mouse.IsCursorVisible = true;
385    }   
386
387    void kentat()
388    {
389        ClearAll();
390        IsFullScreen = true;
391        Valikonkohdat = new List<Label>();
392        Level.Background.CreateGradient(Color.Orange, Color.White);
393
394        Label kohta1 = new Label("Field 1");
395        kohta1.Position = new Vector(0, 80);
396        Valikonkohdat.Add(kohta1);
397
398        Label kohta2 = new Label("Field 2");
399        kohta2.Position = new Vector(0, 20);
400        Valikonkohdat.Add(kohta2);
401
402        Label kohta3 = new Label("Field 3");
403        kohta3.Position = new Vector(0, - 40);
404        Valikonkohdat.Add(kohta3);
405
406
407
408        foreach (Label Valikonkohta in Valikonkohdat)
409        {
410            Valikonkohta.TextColor = Color.White;
411            Add(Valikonkohta);
412        }
413
414        Mouse.ListenOn(kohta1, MouseButton.Left, ButtonState.Pressed, Aloita, null);
415        Mouse.ListenOn(kohta2, MouseButton.Left, ButtonState.Pressed, Aloita2, null);
416        Mouse.ListenOn(kohta3, MouseButton.Left, ButtonState.Pressed, Aloita3, null);
417
418        Mouse.IsCursorVisible = true;
419        Mouse.ListenMovement(1.0, ValikossaLiikkuminen, null);
420
421        Keyboard.Listen(Key.Escape, ButtonState.Pressed, aloitusvalikko, null);
422
423    }
424
425    void ohjeet()
426    {
427        ClearAll();
428        IsFullScreen = true;
429        Valikonkohdat = new List<Label>();
430        Level.Background.CreateGradient(Color.Orange, Color.White);
431
432        Label kohta1 = new Label("    Move Buddy with the arrow keys or with A, D and W         keys. Collect clocks, and destroy all the ghosts by                                    jumping on them.");
433        kohta1.Size = new Vector(600, 130); 
434        kohta1.SizeMode = TextSizeMode.Wrapped;
435        kohta1.Position = new Vector(0, 100);
436        kohta1.TextColor = Color.Orange;
437        Add(kohta1);
438
439        Label kohta2 = new Label("Back");
440        kohta2.Position = new Vector(0, -20);
441        kohta2.TextColor = Color.White;
442        Valikonkohdat.Add(kohta2);
443
444        foreach (Label Valikonkohta in Valikonkohdat)
445        {
446            Valikonkohta.TextColor = Color.White;
447            Add(Valikonkohta);
448        }
449
450        Mouse.ListenOn(kohta2, MouseButton.Left, ButtonState.Pressed, aloitusvalikko, null);
451        Keyboard.Listen(Key.Escape, ButtonState.Pressed, aloitusvalikko, null);
452
453        Mouse.ListenMovement(1.0, ValikossaLiikkuminen2, null);
454
455        Mouse.IsCursorVisible = true;
456    }   
457    void Lopeta()
458    {
459        Exit();
460    }
461
462
463    void Aloita()
464    {
465
466        ClearAll();
467        IsFullScreen = true;
468        Gravity = new Vector(0, -1000);
469
470        luoKentta();
471        lisaaNappaimet();
472        elamat();
473        kerattavat(38);
474
475        Camera.Follow(pelaaja1);
476        Camera.ZoomFactor = 1.8;
477        Camera.StayInLevel = true;
478   
479   
480    }
481    void Aloita2()
482    {
483
484        ClearAll();
485        IsFullScreen = true;
486        Gravity = new Vector(0, -1000);
487
488        luoKentta2();
489        lisaaNappaimet();
490        elamat();
491        kerattavat(40);
492
493        Camera.Follow(pelaaja1);
494        Camera.ZoomFactor = 1.8;
495        Camera.StayInLevel = true;
496
497
498    }
499    void Aloita3()
500    {
501
502        ClearAll();
503        IsFullScreen = true;
504        Gravity = new Vector(0, -1000);
505
506        luoKentta3();
507        lisaaNappaimet();
508        elamat();
509        kerattavat(46);
510
511        Camera.Follow(pelaaja1);
512        Camera.ZoomFactor = 1.8;
513        Camera.StayInLevel = true;
514
515
516    }
517
518
519
520    void ValikossaLiikkuminen(AnalogState hiirenTila)
521    {
522        foreach (Label kohta in Valikonkohdat)
523        {
524            if (Mouse.IsCursorOn(kohta))
525            {
526                kohta.TextColor = Color.Orange;
527            }
528            else
529            {
530                kohta.TextColor = Color.White;
531            }
532        }
533
534    }
535    void ValikossaLiikkuminen2(AnalogState hiirenTila)
536    {
537        foreach (Label kohta in Valikonkohdat)
538        {
539            if (Mouse.IsCursorOn(kohta))
540            {
541                if (kohta.TextColor == Color.White)
542                kohta.TextColor = Color.Orange;
543            }
544            else if(kohta.TextColor == Color.Orange)
545            {
546                kohta.TextColor = Color.White;
547            }
548        }
549
550    }
551
552
553    void asetaHaamulleAivot(Timer sender)
554    {
555        PathFollowerBrain aivot = new PathFollowerBrain(150);
556        aivot.Path = new List<Vector>() { 
557            new Vector((sender.Tag as PhysicsObject).Position.X - 100, (sender.Tag as PhysicsObject).Position.Y),
558            new Vector((sender.Tag as PhysicsObject).Position.X, (sender.Tag as PhysicsObject).Position.Y)};
559        aivot.Owner = (sender.Tag as PhysicsObject);
560        aivot.Loop = true;
561        aivot.Speed = 150;
562        aivot.Active = true;
563        sender.Stop();
564    }
565
566
567}
Note: See TracBrowser for help on using the repository browser.