source: 2014/30/AleksiK/TToDHS/TToDHS/TToDHS/TToDHS.cs @ 5636

Revision 5636, 19.2 KB checked in by iimlart, 9 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 TToDHS : PhysicsGame
10{
11    PhysicsObject stein;
12    PhysicsObject taistoStein;
13    //VAIHDA NIMI NAISESTA NIMEKSI
14    PhysicsObject nainen;
15    PhysicsObject taistoNainen;
16    PhysicsObject mustaTakki;
17    PhysicsObject taistoMustaTakki;
18    PhysicsObject oviEteen;
19    PhysicsObject oviTaakse;
20    PhysicsObject inventory;
21    double ruutuNelionSivu;
22
23    Light valo;
24    Timer valoAjastin = new Timer();
25    Vector steinLuontiPiste = new Vector(0, 0);
26
27    IntMeter huoneLaskuri = new IntMeter(0);
28    IntMeter potionLaskuri = new IntMeter(0);
29    IntMeter steinHp = new IntMeter(20);
30    IntMeter steinDmg = new IntMeter(6);
31    IntMeter steinDef = new IntMeter(4);
32    IntMeter steinLvl = new IntMeter(1);
33    IntMeter steinExp = new IntMeter(0);
34    IntMeter mustaTakkiHp = new IntMeter(10);
35    IntMeter mustaTakkiDmg = new IntMeter(6);
36    IntMeter mustaTakkiDef = new IntMeter(3);
37
38    List<Label> alkuKohdat;
39    List<Label> taistoKohdat;
40    Label luetDocua;
41    Label potionInInv;
42    Label taisto1;
43    Label taisto2;
44    Label taisto3;
45
46    //TO DO: TutorialBattle, Tarinaa ->, muut huoneet, itemit, battlet
47
48    public override void Begin()
49    {
50        //IsFullScreen = true;
51        ruutuNelionSivu = Screen.Height / 20;
52        AlkuValikko();
53
54        Level.Background.Color = Color.Black;
55        Level.AmbientLight = 0;
56
57        valo = new Light();
58        valo.Intensity = 0;
59        valo.Distance = 1000;
60        Add(valo);
61
62        PotionienLaskuri();
63
64        steinExp.MaxValue = 32;
65
66        //Tee tälle oma aliohjelmansa, joka kutsutaan, kun stein voittaa taistelun!
67
68        steinExp.UpperLimit += delegate { steinLvl.Value += 1; };
69        steinExp.UpperLimit += delegate { steinHp.Value += RandomGen.NextInt(1 - 5); };
70        steinExp.UpperLimit += delegate { steinDmg.Value += 1; };
71        steinExp.UpperLimit += delegate { steinDef.Value += 1; };
72        steinExp.UpperLimit += delegate { steinExp.MaxValue *= steinLvl.Value; };
73        steinExp.UpperLimit += delegate { steinExp.Reset(); };
74        mustaTakkiHp.MinValue = 0;
75    }
76
77    void AlkuValikko()
78    {
79        Mouse.IsCursorVisible = true;
80
81        Label otsikko = new Label("The Tale of Dr Harvard Stein");
82        otsikko.Position = new Vector(0, 140);
83        otsikko.Font = Font.DefaultLargeBold;
84        otsikko.TextColor = Color.BrightGreen;
85        Add(otsikko);
86
87        alkuKohdat = new List<Label>();
88
89        Label alku1 = new Label("New Game");
90        alku1.Position = new Vector(0, 80);
91        alku1.Font = Font.DefaultLarge;
92        alkuKohdat.Add(alku1);
93
94        Label alku2 = new Label("Exit");
95        alku2.Position = new Vector(0, 30);
96        alku2.Font = Font.DefaultLarge;
97        alkuKohdat.Add(alku2);
98
99
100        foreach (Label alkuKohta in alkuKohdat)
101        {
102            Add(alkuKohta);
103        }
104
105        Mouse.ListenOn(alku1, MouseButton.Left, ButtonState.Pressed, HuoneenLaskuri, null, 2);
106        Mouse.ListenOn(alku2, MouseButton.Left, ButtonState.Pressed, Exit, null);
107        Mouse.ListenMovement(1.0, AlkuKohtaHiiri, null);
108    }
109
110    void AlkuKohtaHiiri(AnalogState hiiri)
111    {
112        foreach (Label alkuKohta in alkuKohdat)
113        {
114            if (Mouse.IsCursorOn(alkuKohta))
115            {
116                alkuKohta.TextColor = Color.Red;
117            }
118
119            else
120            {
121                alkuKohta.TextColor = Color.Blue;
122            }
123        }
124    }
125
126    void HuoneenLaskuri(int x)
127    {
128        Mouse.IsCursorVisible = false;
129
130        huoneLaskuri.MinValue = 0;
131        huoneLaskuri.MaxValue = 10;
132
133        if (huoneLaskuri.Value == 0)
134        {
135            if (x == 1)
136            {
137                steinLuontiPiste = new Vector(100, 100);
138            }
139
140            LuoHuone("Labrahuone");
141        }
142
143        else if (huoneLaskuri.Value == 1)
144        {
145            if (x == 0)
146            {
147                steinLuontiPiste = new Vector(-200, 200);
148            }
149
150            else if (x == 1)
151            {
152                steinLuontiPiste = new Vector(200, -200);
153            }
154
155            LuoHuone("INSERT ROOM 2 HERE");
156        }
157    }
158
159    void LuoHuone(String tekstinNimi)
160    {
161        ClearGameObjects();
162        TileMap huone = TileMap.FromLevelAsset(tekstinNimi);
163        huone.SetTileMethod('X', LuoSeina);
164        huone.SetTileMethod('S', LuoStein);
165        huone.SetTileMethod('D', LuoDoc);
166        huone.SetTileMethod('V', LuoVitriini);
167        huone.SetTileMethod('A', LuoPotionhuone1);
168        huone.SetTileMethod('F', LuoOviEteen);
169        huone.SetTileMethod('B', LuoOviTaakse);
170        huone.Execute(ruutuNelionSivu, ruutuNelionSivu);
171
172        HuoneenValo(0, 1);
173    }
174
175    void LuoSeina(Vector paikka, double leveys, double korkeus)
176    {
177        PhysicsObject seina = PhysicsObject.CreateStaticObject(leveys, korkeus);
178        seina.Position = paikka;
179        seina.Color = Color.Gray;
180        seina.CollisionIgnoreGroup = 1;
181        seina.Restitution = 0;
182        AddCollisionHandler(seina, "stein", PalautaStein);
183        Add(seina);
184    }
185
186    void LuoStein(Vector paikka, double leveys, double korkeus)
187    {
188        stein = new PhysicsObject(leveys * 0.8, korkeus * 0.8);
189        stein.Position = steinLuontiPiste;
190        stein.Color = Color.Green;
191        stein.Restitution = 0;
192        stein.CanRotate = false;
193        stein.Tag = "stein";
194        Add(stein);
195
196        Camera.Follow(stein);
197        Camera.Zoom(0.75);
198
199        Timer.SingleShot(1, delegate { SteinKontrollit(0); });
200    }
201
202    void SteinKontrollit(int x)
203    {
204        if (x == 1)
205        {
206            luetDocua.Destroy();
207        }
208
209        ClearControls();
210
211        //Jos muutat nopeutta, moni Timer.Singleshot menee väärään aikaan, mm. PalautaStein, SteinNopeus ja LuetDoc
212        Keyboard.Listen(Key.D, ButtonState.Down, AsiaNopeus, null, stein, ruutuNelionSivu * 5, 0.0, 0.0);
213        Keyboard.Listen(Key.A, ButtonState.Down, AsiaNopeus, null, stein, -ruutuNelionSivu * 5, 0.0, 0.0);
214        Keyboard.Listen(Key.W, ButtonState.Down, AsiaNopeus, null, stein, 0.0, ruutuNelionSivu * 5, 0.0);
215        Keyboard.Listen(Key.S, ButtonState.Down, AsiaNopeus, null, stein, 0.0, -ruutuNelionSivu * 5, 0.0);
216        Keyboard.Listen(Key.E, ButtonState.Pressed, InventorinTilanMuutos, null, 0, 0);
217    }
218
219    void AsiaNopeus(PhysicsObject liikkuja, double x, double y, double z)
220    {
221        liikkuja.Velocity = new Vector(x, y);
222
223        if ((x + 1 ) * (y + 1) > 2)
224        {
225            ClearControls();
226            Timer.SingleShot(0.2, delegate { AsiaNopeus(liikkuja, 0, 0, 0); });
227        }
228
229        else if ((x + 1) * (y + 1) < 1)
230        {
231            ClearControls();
232            Timer.SingleShot(0.2, delegate { AsiaNopeus(liikkuja, 0, 0, 0); });
233        }
234
235        else if (z == 0)
236        {
237            SteinKontrollit(0);
238        }
239    }
240
241    void PalautaStein(PhysicsObject wall, PhysicsObject dr)
242    {
243        dr.Velocity = dr.Velocity * -1;
244        //Määritetty Steinin koosta ja nopeudesta
245        Timer.SingleShot( 0.02, delegate { AsiaNopeus(dr, 0, 0, 1); });
246    }
247
248    void LuoDoc(Vector paikka, double leveys, double korkeus)
249    {
250        PhysicsObject doc = PhysicsObject.CreateStaticObject(leveys, korkeus);
251        doc.Position = paikka;
252        doc.Color = Color.White;
253        doc.CollisionIgnoreGroup = 1;
254        doc.Restitution = 0;
255        AddCollisionHandler(doc, "stein", PalautaStein);
256        AddCollisionHandler(doc, "stein", LuetDoc);
257        Add(doc);
258    }
259
260    void LuetDoc(PhysicsObject doku, PhysicsObject dr)
261    {
262        luetDocua = new Label("It's full of complicated calculations. Better leave it alone.");
263        luetDocua.Y = Screen.Bottom + 100;
264        luetDocua.Font = Font.DefaultLarge;
265        luetDocua.TextColor = Color.Green;
266        luetDocua.Color = Color.DarkGray;
267        luetDocua.BorderColor = Color.Gray;
268        Add(luetDocua);
269
270        ClearControls();
271        Timer.SingleShot(0.042, delegate { ClearControls(); });
272        Timer.SingleShot(0.161, delegate { ClearControls(); });
273
274        Timer.SingleShot(1, delegate { Keyboard.Listen(Key.Space, ButtonState.Pressed, SteinKontrollit, null, 1); });
275    }
276
277    void LuoVitriini(Vector paikka, double leveys, double korkeus)
278    {
279        PhysicsObject vitriini = PhysicsObject.CreateStaticObject(leveys * 3, korkeus);
280        vitriini.Position = paikka;
281        vitriini.CollisionIgnoreGroup = 1;
282        vitriini.Restitution = 0;
283        AddCollisionHandler(vitriini, "stein", PalautaStein);
284        Add(vitriini);
285    }
286
287    void LuoPotionhuone1(Vector paikka, double leveys, double korkeus)
288    {
289        PhysicsObject potionhuone1 = PhysicsObject.CreateStaticObject(leveys, korkeus);
290        potionhuone1.Position = paikka;
291        potionhuone1.Color = Color.Red;
292        potionhuone1.IgnoresCollisionResponse = true;
293        AddCollisionHandler(potionhuone1, "stein", SaitPotioninHuoneessa1);
294        Add(potionhuone1);
295    }
296
297    void SaitPotioninHuoneessa1(PhysicsObject esine, PhysicsObject dr)
298    {
299        esine.Destroy();
300        potionLaskuri.Value += 1;
301
302        ClearControls();
303        Timer.SingleShot(0.201, delegate { ClearControls(); });
304        Timer.SingleShot(2, Cutscene1);
305    }
306
307    //Säädä CUTSCENE!!!
308
309    void Cutscene1()
310    {
311        LuoNainen(new Vector(-ruutuNelionSivu * 12, -ruutuNelionSivu * 1), ruutuNelionSivu, ruutuNelionSivu);
312        nainen.Velocity = new Vector(ruutuNelionSivu * 5, 0);
313        Timer.SingleShot(1, delegate { AsiaNopeus(nainen, 0, 0, 1);
314                                       nainen.Velocity = new Vector(0, -ruutuNelionSivu * 5); });
315        Timer.SingleShot(1.8, delegate { AsiaNopeus(nainen, 0, 0, 1);
316                                         nainen.Velocity = new Vector(ruutuNelionSivu * 5, 0); });
317        Timer.SingleShot(3.2, delegate { AsiaNopeus(nainen, 0, 0, 1); });
318
319        Timer.SingleShot(0.4, delegate { LuoMustaTakki(new Vector(-ruutuNelionSivu * 12, -ruutuNelionSivu * 1), ruutuNelionSivu, ruutuNelionSivu);
320                                         mustaTakki.Velocity = new Vector(ruutuNelionSivu * 5, 0); });
321        Timer.SingleShot(1.4, delegate { AsiaNopeus(mustaTakki, 0, 0, 1);
322                                         mustaTakki.Velocity = new Vector(0, -ruutuNelionSivu * 5); });
323        Timer.SingleShot(2.4, delegate { AsiaNopeus(mustaTakki, 0, 0, 1);
324                                         mustaTakki.Velocity = new Vector(ruutuNelionSivu * 5, 0); });
325        Timer.SingleShot(4, delegate { AsiaNopeus(mustaTakki, 0, 0, 1);
326                                       mustaTakki.Velocity = new Vector(0, ruutuNelionSivu * 5); });
327        Timer.SingleShot(4.02, delegate { AsiaNopeus(mustaTakki, 0, 0, 1); });
328    }
329
330    //Vaihda nimi Luo[NaisenNimi]
331    //Muista parametrit!
332
333    void LuoNainen(Vector paikka, double leveys, double korkeus)
334    {
335        nainen = new PhysicsObject(leveys, korkeus);
336        nainen.Position = paikka;
337        nainen.Color = Color.Red;
338        nainen.Restitution = 0;
339        nainen.CanRotate = false;
340        Add(nainen);
341    }
342
343    //Muista parametrit!
344
345    void LuoMustaTakki(Vector paikka, double leveys, double korkeus)
346    {
347        mustaTakki = new PhysicsObject(leveys, korkeus);
348        mustaTakki.Position = paikka;
349        mustaTakki.Color = Color.DarkGray;
350        mustaTakki.Restitution = 0;
351        mustaTakki.CanRotate = false;
352        AddCollisionHandler(mustaTakki, "stein", AloitaTaistelu);
353        AddCollisionHandler(mustaTakki, "stein", PalautaStein);
354        Add(mustaTakki);
355    }
356
357    void LuoOviEteen(Vector paikka, double leveys, double korkeus)
358    {
359        oviEteen = PhysicsObject.CreateStaticObject(leveys, korkeus);
360        oviEteen.Position = paikka;
361        oviEteen.IgnoresCollisionResponse = true;
362        AddCollisionHandler(oviEteen, "stein", SeuraavaHuone);
363        Add(oviEteen);
364    }
365
366    void SeuraavaHuone(PhysicsObject door, PhysicsObject dr)
367    {
368        huoneLaskuri.Value = huoneLaskuri.Value + 1;
369        HuoneenValo(0, 2);
370        Timer.SingleShot(1.5, delegate { HuoneenLaskuri(0); });
371    }
372
373    void LuoOviTaakse(Vector paikka, double leveys, double korkeus)
374    {
375        oviTaakse = PhysicsObject.CreateStaticObject(leveys, korkeus);
376        oviTaakse.Position = paikka;
377        oviTaakse.IgnoresCollisionResponse = true;
378        AddCollisionHandler(oviTaakse, "stein", EdellinenHuone);
379        Add(oviTaakse);
380    }
381
382    void EdellinenHuone(PhysicsObject door, PhysicsObject dr)
383    {
384        huoneLaskuri.Value = huoneLaskuri.Value - 1;
385        HuoneenValo(0, 2);
386        Timer.SingleShot(1.5, delegate { HuoneenLaskuri(1); });
387    }
388
389    void HuoneenValo(int x, int y)
390    {
391        if (x == 1)
392        {
393            valo.Intensity += 0.067;
394        }
395
396        else if (x == 2)
397        {
398            valo.Intensity -= 0.067;
399        }
400
401        else if (x == 0)
402        {
403            valoAjastin.Interval = 0.05;
404            valoAjastin.Timeout += delegate { HuoneenValo(y, 0); };
405            valoAjastin.Start(15);
406        }
407    }
408
409    void PotionienLaskuri()
410    {
411        potionLaskuri.MinValue = 0;
412        potionLaskuri.MaxValue = 10;
413    }
414
415    //Laita Label("Inventory") Inventoryn yläosaan!
416
417    void InventorinTilanMuutos(int x, int y)
418    {
419        if (x == 0 && y == 0)
420        {
421            ClearControls();
422            Keyboard.Listen(Key.E, ButtonState.Pressed, InventorinTilanMuutos, null, 1, 0);
423
424            inventory = PhysicsObject.CreateStaticObject(ruutuNelionSivu * 10, ruutuNelionSivu * 20);
425            inventory.Color = Color.LightGray;
426            inventory.IgnoresCollisionResponse = true;
427            inventory.CollisionIgnoreGroup = 1;
428            inventory.Position = new Vector(0, 0);
429            Add(inventory);
430
431            if (potionLaskuri.Value > 0)
432            {
433                potionInInv = new Label("Potion");
434                potionInInv.Position = new Vector(-inventory.Width / 4, inventory.Height / 7 * 2.3);
435                potionInInv.Font = Font.DefaultLarge;
436                potionInInv.TextColor = Color.Red;
437                Add(potionInInv);
438            }
439        }
440
441        else if (x == 1 && y == 0 && potionLaskuri.Value > 0)
442        {
443            inventory.Destroy();
444            potionInInv.Destroy();
445            SteinKontrollit(0);
446        }
447
448        else if (x== 1 && y == 0)
449        {
450            inventory.Destroy(); 
451            SteinKontrollit(0);
452        }
453
454        else if (x == 0 && y == 1)
455        {
456            taisto1.Destroy();
457            taisto2.Destroy();
458            taisto3.Destroy();
459           
460            Keyboard.Listen(Key.E, ButtonState.Pressed, InventorinTilanMuutos, null, 1, 1);
461
462            inventory = PhysicsObject.CreateStaticObject(ruutuNelionSivu * 10, ruutuNelionSivu * 20);
463            inventory.Color = Color.LightGray;
464            inventory.IgnoresCollisionResponse = true;
465            inventory.CollisionIgnoreGroup = 1;
466            inventory.Position = taistoStein.Position;
467            Add(inventory);
468
469            if (potionLaskuri.Value > 0)
470            {
471                potionInInv = new Label("Potion");
472                potionInInv.Position = new Vector(-inventory.Width / 4, inventory.Height / 7 * 2.3);
473                potionInInv.Font = Font.DefaultLarge;
474                potionInInv.TextColor = Color.Red;
475                Add(potionInInv);
476            }
477        }
478
479        else if (x == 1 && y == 1 && potionLaskuri.Value > 0)
480        {
481            inventory.Destroy();
482            potionInInv.Destroy();
483            ClearControls();
484            LuoTaistoValikko();
485        }
486
487        else if (x == 1 && y == 1)
488        {
489            inventory.Destroy();
490            ClearControls();
491            LuoTaistoValikko();
492        }
493    }
494
495    //Selvitä miksi valo ei vähene HuoneenValo(0,2); -komennolla
496
497    void AloitaTaistelu(PhysicsObject vastus, PhysicsObject dr)
498    {
499        steinLuontiPiste = stein.Position;
500        //HuoneenValo(0, 2);
501        ClearGameObjects();
502        //HuoneenValo(0, 1);
503        TileMap taistelu = TileMap.FromLevelAsset("Taistelu");
504        taistelu.SetTileMethod('V', LuoTaistoon, 0);
505        taistelu.SetTileMethod('S', LuoTaistoon, 1);
506        taistelu.SetTileMethod('N', LuoTaistoon, 2);
507        taistelu.Execute(ruutuNelionSivu, ruutuNelionSivu);
508        LuoTaistoValikko();
509    }
510
511    void LuoTaistoon(Vector paikka, double leveys, double korkeus, int x)
512    {
513        if (x == 0)
514        {
515            taistoMustaTakki = LuoTaistoOtus(paikka, leveys, korkeus, Color.DarkGray);
516        }
517
518        else if (x == 1)
519        {
520            taistoStein = LuoTaistoOtus(paikka, leveys, korkeus, Color.Green);
521        }
522
523        else if (x == 2)
524        {
525            taistoNainen = LuoTaistoOtus(paikka, leveys, korkeus, Color.Red);
526        }
527    }
528
529    PhysicsObject LuoTaistoOtus(Vector paikka, double leveys, double korkeus, Color vari)
530    {
531        PhysicsObject taistelija = new PhysicsObject(leveys, korkeus);
532        taistelija.Position = paikka;
533        taistelija.Color = vari;
534        Add(taistelija);
535        return (taistelija);
536    }
537
538    void LuoTaistoValikko()
539    {
540        Mouse.IsCursorVisible = true;
541
542        taistoKohdat = new List<Label>();
543
544        taisto1 = new Label("Attack");
545        taisto1.Position = new Vector(-160, -30);
546        taisto1.Font = Font.DefaultLarge;
547        taistoKohdat.Add(taisto1);
548
549        taisto2 = new Label("Defend");
550        taisto2.Position = new Vector(-10, -30);
551        taisto2.Font = Font.DefaultLarge;
552        taistoKohdat.Add(taisto2);
553
554        taisto3 = new Label("Inventory");
555        taisto3.Position = new Vector(140, -30);
556        taisto3.Font = Font.DefaultLarge;
557        taistoKohdat.Add(taisto3);
558
559        foreach (Label taistoKohta in taistoKohdat)
560        {
561            Add(taistoKohta);
562        }
563
564        LuoTaistoValikonHiiri();
565    }
566
567    void LuoTaistoValikonHiiri()
568    {
569        Mouse.ListenOn(taisto1, MouseButton.Left, ButtonState.Pressed, SteinAttack, null);
570        Mouse.ListenOn(taisto2, MouseButton.Left, ButtonState.Pressed, SteinDefend, null);
571        Mouse.ListenOn(taisto3, MouseButton.Left, ButtonState.Pressed, InventorinTilanMuutos, null, 0, 1);
572        Mouse.ListenMovement(1.0, TaistoKohtaHiiri, null);
573    }
574
575    void TaistoKohtaHiiri(AnalogState hiiri)
576    {
577        foreach (Label taistoKohta in taistoKohdat)
578        {
579            if (Mouse.IsCursorOn(taistoKohta))
580            {
581                taistoKohta.TextColor = Color.Red;
582            }
583
584            else
585            {
586                taistoKohta.TextColor = Color.Blue;
587            }
588        }
589    }
590
591    //ERROR: MaxValue must be greater than zero. -Kohdassa mustaTakki.Hp -= jne...
592
593    void SteinAttack()
594    {
595        mustaTakkiHp.Value -= (steinDmg.Value -= mustaTakkiDef.Value += RandomGen.NextInt(1 - 4));
596        mustaTakkiHp.LowerLimit += delegate { taistoMustaTakki.Destroy(); };
597    }
598
599    void SteinDefend()
600    {
601        steinDef.Value += 1;
602    }
603}
Note: See TracBrowser for help on using the repository browser.