source: 2013/24/AamosR/GeneerinenAvaruuslentely/GeneerinenAvaruuslentely/GeneerinenAvaruuslentely/GeneerinenAvaruuslentely.cs @ 4158

Revision 4158, 15.8 KB checked in by juiitamm, 10 years ago (diff)

Talletus.

Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Assets;
5using Jypeli.Controls;
6using Jypeli.Effects;
7using Jypeli.Widgets;
8
9
10
11
12class alus : PhysicsObject
13{
14    private IntMeter elamaLaskuri = new IntMeter(3, 0, 3);
15    public IntMeter ElamaLaskuri { get { return elamaLaskuri; } }
16
17    public alus(double leveys, double korkeus)
18        : base(leveys, korkeus)
19    {
20        elamaLaskuri.LowerLimit += delegate { this.Destroy(); };
21    }
22}
23
24public class GeneerinenAvaruuslentely : PhysicsGame
25{
26    EasyHighScore topLista = new EasyHighScore();
27    Image aluskuva = LoadImage("alus");
28    Image asteroidikuva = LoadImage("asteroidi");
29    Image tausta = LoadImage("tausta");
30    Image vihollinenkuva = LoadImage("vihollinen");
31    Image hp = LoadImage("hp");
32    Image rajahdys = LoadImage("explosion0");
33    Image liekkikuva = LoadImage("liekki");
34    Image ammuskuva = LoadImage("ammus");
35    SoundEffect hurt = LoadSoundEffect("hit");
36    SoundEffect shoot = LoadSoundEffect("shoot");
37    SoundEffect explosion = LoadSoundEffect("explosion");
38    SoundEffect powerup = LoadSoundEffect("powerup");
39    Flame liekki;
40
41
42    alus alus;
43    PhysicsObject asteroidi;
44    PhysicsObject HP;
45    //PhysicsObject vihollinen;
46    IntMeter pisteLaskuri;
47    IntMeter ElamaLaskuri;
48
49    bool SaaAmpua = true;
50
51
52
53    public override void Begin()
54    {
55        // TODO: Kirjoita ohjelmakoodisi tähän
56        //IsFullScreen = true;
57        ClearAll();
58        Level.Background.Image = tausta;
59        //Level.Background.FitToLevel();
60
61        SetWindowSize(1024, 768, true);
62        //SetWindowSize(1920, 1080, true);
63       
64
65
66
67        MultiSelectWindow valikko = new MultiSelectWindow("Just some generic space game", "Start Game", "How to play", "High Score", "Quit");
68        //valikko.ItemSelected += PainettiinValikonNappia;
69
70        valikko.Color = Color.DarkGray;
71        valikko.SelectionColor = Color.DarkJungleGreen;
72        valikko.BorderColor = Color.DarkJungleGreen;
73        valikko.SetButtonTextColor(Color.SkyBlue);
74        //valikko.te
75
76        valikko.BorderColor = Color.Black;
77        valikko.AddItemHandler(0, AloitaPeli);
78        valikko.AddItemHandler(1, Ohjeet);
79        valikko.AddItemHandler(2, ParhaatPisteet);
80        valikko.AddItemHandler(3, ConfirmExit);
81
82        Add(valikko);
83
84    }
85
86    void ParhaatPisteet()
87    {
88        topLista.Show();
89        topLista.HighScoreWindow.Closed += delegate { Begin(); };
90    }
91
92    void AloitaPeli()
93    {
94        ClearAll();
95
96        MediaPlayer.Play("8bit Dungeon Boss");
97        MediaPlayer.IsRepeating = true;
98        LuoKentta();
99        AsteroidiAjastin();
100        VihollisAjastin();
101        HPAjastin();
102        AsetaOhjaimet();
103        LuoPisteLaskuri();
104        LuoElamaLaskuri();
105    }
106
107    void LuoElamaLaskuri()
108    {
109        ElamaLaskuri = new IntMeter(5, 0, 10);
110
111        Label ElamaNaytto = new Label();
112        ElamaNaytto.X = Screen.Left + 100;
113        ElamaNaytto.Y = Screen.Top - 100;
114        ElamaNaytto.TextColor = Color.White;
115        ElamaNaytto.Title = "HP";
116        ElamaNaytto.BindTo(ElamaLaskuri);
117
118        ElamaLaskuri.LowerLimit += ElamaLoppui;
119        ElamaLaskuri.LowerLimit += ElamaLaskuri.Reset;
120        //ElamaLaskuri.LowerLimit += topLista.EnterAndShow(pisteLaskuri.Value);
121        //ElamaLaskuri.LowerLimit += topLista.HighScoreWindow.Closed += Begin;
122
123        Add(ElamaNaytto);
124
125
126    }
127
128    void ElamaLoppui()
129    {
130        alus.Destroy();
131        liekki.Destroy();
132
133        explosion.Play();
134        ControllerOne.Vibrate(10.0, 10.0, 10.0, 10.0, 1.5);
135        //Begin();
136        Explosion rajahdys = new Explosion(1000);
137        rajahdys.Position = alus.Position;
138        rajahdys.Force = 9001;
139        Add(rajahdys);
140        MediaPlayer.Stop();
141        topLista.EnterAndShow(pisteLaskuri.Value);
142        topLista.HighScoreWindow.Closed += delegate { Begin(); };
143
144
145    }
146
147    void Ohjeet()
148    {
149        MessageWindow ikkuna = new MessageWindow("The goal of this game is to survive.\nYou get points when time passes and from destroying enemies.\nHealth Packs give you 2 HP.\n\n\nControls (Keyboard): \n \n WASD: move \n \n rCtrl: shoot \n \n Controls (Controller): \n \n Left Analog Stick: move \n \n Left Trigger: shoot \n \n Press Enter or A to continue \n");
150        ikkuna.Closed += delegate { Begin(); };
151        ikkuna.Message.SizeMode = TextSizeMode.AutoSize;
152        Add(ikkuna);
153        //Window ikkuna = new Window();
154        //ikkuna.
155        //ikkuna.Title.Text = "How to play:";
156        //Label teksti = new Label("The goal of this game is to survive. You get points when time passes and from destroying enemies. Controls: WASD: move rCtrl: shoot Press Enter to continue");
157        //teksti.Width = 300;
158        //teksti.SizeMode = TextSizeMode.Wrapped;
159        //ikkuna.Add(teksti);
160        //Add(ikkuna);
161        //Add(teksti);
162        //Keyboard.Listen(Key.Enter, ButtonState.Pressed, Begin, null);
163    }
164
165    void LuoPisteLaskuri()
166    {
167        pisteLaskuri = new IntMeter(0);
168
169        Timer pisteTimer = new Timer();
170        pisteTimer.Interval = 2;
171        pisteTimer.Timeout += delegate { pisteLaskuri.Value++; };
172        pisteTimer.Start();
173
174        Label pisteNaytto = new Label();
175        pisteNaytto.X = Screen.Right - 100;
176        pisteNaytto.Y = Screen.Top - 100;
177        pisteNaytto.TextColor = Color.White;
178        //pisteNaytto.Color = Color.White;
179        pisteNaytto.Title = "Points";
180
181
182        pisteNaytto.BindTo(pisteLaskuri);
183        Add(pisteNaytto);
184
185    }
186
187
188    void LuoKentta()
189    {
190        if (1920 == Window.ClientBounds.Width)
191        {
192            Level.Width = Screen.Width;
193            Level.Height = Screen.Height;
194        }
195        else
196        {
197            Level.Width = Screen.Width*1.5;
198            Level.Height = Screen.Height*1.5;
199
200        }
201        Level.CreateBorders();
202
203
204        Camera.ZoomToLevel();
205        Camera.StayInLevel = true;
206
207
208        alus = new alus(100, 100);
209        alus.Shape = Shape.Rectangle;
210        alus.X = 0;
211
212        alus.Y = 0;
213        alus.Image = aluskuva;
214        alus.AngularDamping = 0.5;
215        alus.LinearDamping = 0.95;
216        alus.ElamaLaskuri.Value--;
217       
218        liekki = new Flame(liekkikuva);
219        liekki.Position = alus.Position;
220        liekki.MinVelocity = 200;
221        liekki.MaxVelocity = 200;
222        liekki.MinAcceleration = 200;
223        liekki.MaxAcceleration = 200;
224        liekki.MinLifetime = 0.1;
225        liekki.MaxLifetime = 0.11;
226        liekki.IgnoreWind = false;
227       
228
229        Add(liekki, -1);
230
231        Level.Background.Image = tausta;
232        Level.Background.FitToLevel();
233
234        Add(alus);
235
236        //Level.AmbientLight = 0.2;
237        //Light valo = new Light();
238        //valo.Intensity = 0.8;
239        //valo.Distance = 150;
240        //valo.Position = alus.Position;
241        //Add(valo);
242
243        AddCollisionHandler(alus, "vihollinen", kuolema);
244       
245        AddCollisionHandler(alus, "hp", LisaaTerveys);
246    }
247
248
249
250    void AsetaOhjaimet()
251    {
252        PhoneBackButton.Listen(ConfirmExit, "Lopeta peli");
253        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
254        Keyboard.Listen(Key.A, ButtonState.Down, KaannaAlusta, null, 10.0);
255        Keyboard.Listen(Key.D, ButtonState.Down, KaannaAlusta, null, -10.0);
256        Keyboard.Listen(Key.W, ButtonState.Down, TyonnaAlusta, null);
257        Keyboard.Listen(Key.S, ButtonState.Down, VedaAlusta, null);
258        Keyboard.Listen(Key.RightControl, ButtonState.Down, Ammu, null);
259        Keyboard.Listen(Key.P, ButtonState.Pressed, Pause, null);
260        ControllerOne.ListenAnalog(AnalogControl.LeftStick, 0.1, KaannaTatilla, null);
261        ControllerOne.Listen(Button.Back, ButtonState.Pressed, ConfirmExit, null);
262        ControllerOne.Listen(Button.Start, ButtonState.Pressed, Pausetus, null);
263        ControllerOne.ListenAnalog(AnalogControl.LeftTrigger, 0.1, AmmuLiipasin, null);
264
265
266
267
268    }
269
270    void Pausetus()
271    {
272        Pause();
273       
274
275
276    }
277
278    protected override void Update(Time time)
279    {
280        if (liekki != null && alus != null)
281        {
282            Vector pelaajanSuunta = Vector.FromLengthAndAngle(2500.0, alus.Angle + Angle.StraightAngle);
283            liekki.Position = alus.AbsolutePosition + pelaajanSuunta.Normalize() * 50;
284            Wind = pelaajanSuunta;
285        }
286
287        base.Update(time);
288    }
289
290    void AmmuLiipasin(AnalogState liipasimenTila)
291    {
292        Ammu();
293    }
294
295
296    void KaannaTatilla(AnalogState tatinTila)
297    {
298        Vector tatinAsento = tatinTila.StateVector;
299        alus.AngularVelocity = tatinAsento.X * -10;
300        Vector pelaajanSuunta = Vector.FromLengthAndAngle(tatinAsento.Y * 2500, alus.Angle);
301        alus.Push(pelaajanSuunta);
302    }
303
304    void KaannaAlusta(double kulma)
305    {
306        alus.AngularVelocity = kulma;
307    }
308
309    void LisaaTerveys(PhysicsObject alus, PhysicsObject HP)
310    {
311        ElamaLaskuri.Value += 2;
312        HP.Destroy();
313        powerup.Play();
314    }
315
316    void TyonnaAlusta()
317    {
318        Vector pelaajanSuunta = Vector.FromLengthAndAngle(2500.0, alus.Angle);
319        alus.Push(pelaajanSuunta);
320
321    }
322
323    void VedaAlusta()
324    {
325        Vector pelaajanSuunta = Vector.FromLengthAndAngle(2500.0, alus.Angle);
326        alus.Push(pelaajanSuunta - pelaajanSuunta - pelaajanSuunta);
327
328    }
329
330    void kuolema(PhysicsObject alus, PhysicsObject vihollinen)
331    {
332        hurt.Play();
333        Explosion rajahdys = new Explosion(100);
334        rajahdys.Position = vihollinen.Position;
335        rajahdys.Force = 0;
336        //rajahdys.Image = rajahdys;
337        Add(rajahdys);
338        ControllerOne.Vibrate(0.25, 0.25, 1.0, 1.0, 0.25);
339        ElamaLaskuri.Value -= 1;
340        vihollinen.Destroy();
341    }
342
343
344    void HPAjastin()
345    {
346        Timer ajastin = new Timer();
347        ajastin.Interval = 20;
348        ajastin.Timeout += LisaaHP;
349        ajastin.Start();
350    }
351
352    void LisaaHP()
353    {
354        Vector kentanPiste = Level.GetRandomPosition();
355        HP = new PhysicsObject(50, 50);
356        HP.IgnoresCollisionResponse = true;
357        HP.Tag = "hp";
358        HP.Image = hp;
359        HP.Position = kentanPiste;
360        HP.LifetimeLeft = TimeSpan.FromSeconds(10);
361        Add(HP);
362    }
363
364    void LisaaAsteroidi()
365    {
366        //Vector kentanPiste = Level.GetRandomPosition();
367        //RandomGen.NextVector(
368
369        Vector kentanPiste = Vector.FromLengthAndAngle(Level.Width + 200, RandomGen.NextAngle());
370        asteroidi = new PhysicsObject(50, 50);
371        asteroidi.Position = kentanPiste;
372        asteroidi.Shape = Shape.Circle;
373        asteroidi.Color = Color.Black;
374        asteroidi.Image = asteroidikuva;
375        asteroidi.Tag = "vihollinen";
376        asteroidi.LifetimeLeft = TimeSpan.FromSeconds(10);
377        asteroidi.IgnoresCollisionResponse = true;
378        Add(asteroidi);
379        Angle asteroidikulma = (Vector.Zero - asteroidi.Position).Angle;
380        Vector asteroidivector = Vector.FromLengthAndAngle(500, RandomGen.NextAngle(asteroidikulma - Angle.FromDegrees(50), (asteroidikulma + Angle.FromDegrees(50))));
381        asteroidi.Hit(asteroidivector);
382    }
383
384    void AsteroidiAjastin()
385    {
386        Timer ajastin = new Timer();
387        ajastin.Interval = 0.25;
388        ajastin.Timeout += LisaaAsteroidi;
389        ajastin.Start();
390    }
391
392    void VihollisAjastin()
393    {
394        Timer ajastin = new Timer();
395        ajastin.Interval = 5;
396        ajastin.Timeout += LisaaVihollinen;
397        ajastin.Start();
398    }
399
400    void LisaaVihollinen()
401    {
402        Vector kentanPiste = Vector.FromLengthAndAngle(Level.Width + 200, RandomGen.NextAngle());
403        PhysicsObject vihollinen = new PhysicsObject(150, 150);
404        vihollinen.Position = kentanPiste;
405        vihollinen.Shape = Shape.Circle;
406        vihollinen.Color = Color.Black;
407        vihollinen.Image = vihollinenkuva;
408        vihollinen.Tag = "vihollinen";
409        //vihollinen.Tag = "vihu";
410        vihollinen.IgnoresCollisionResponse = true;
411        vihollinen.LifetimeLeft = TimeSpan.FromSeconds(10);
412
413        AssaultRifle pelaajan1Ase = new AssaultRifle(30, 10);
414        pelaajan1Ase.IsVisible = false;
415        pelaajan1Ase.FireRate = 9001;
416        //pelaajan1Ase.InfiniteAmmo = true;
417        pelaajan1Ase.Ammo.Value = 3;
418        pelaajan1Ase.Power.DefaultValue = 150;
419        pelaajan1Ase.Power.Value = 150;
420        pelaajan1Ase.ProjectileCollision = AmmusOsui;
421        vihollinen.Add(pelaajan1Ase);
422
423        Add(vihollinen);
424        Angle vihollinenkulma = (Vector.Zero - vihollinen.Position).Angle;
425        Vector vihollinenvector = Vector.FromLengthAndAngle(500, RandomGen.NextAngle(vihollinenkulma - Angle.FromDegrees(10), (vihollinenkulma + Angle.FromDegrees(10))));
426
427        vihollinen.Hit(vihollinenvector);
428
429        Timer ajastin = new Timer();
430        ajastin.Interval = 2;
431        ajastin.Timeout += delegate { VihollinenAmpuu(vihollinen, pelaajan1Ase, ajastin); };
432        ajastin.Start();
433
434    }
435
436    void AmmusOsui(PhysicsObject ammus, PhysicsObject kohde)
437    {
438        if (kohde == alus)
439        {
440            ElamaLaskuri.Value -= 1;
441            hurt.Play();
442            Explosion rajahdys = new Explosion(100);
443            ControllerOne.Vibrate(0.25, 0.25, 1.0, 1.0, 0.25);
444            rajahdys.Position = ammus.Position;
445            rajahdys.Force = 0;
446            Add(rajahdys);
447
448        }
449
450        ammus.Destroy();
451    }
452
453    void VihollinenAmpuu(PhysicsObject vihollinen, AssaultRifle ase, Timer ajastin)
454    {
455        if (vihollinen.IsDestroyed)
456        {
457            ajastin.Stop();
458            return;
459        }
460        Vector suunta = (alus.Position - vihollinen.Position).Normalize();
461        ase.Angle = suunta.Angle;
462        PhysicsObject ammus = ase.Shoot();
463
464        if (ammus != null)
465        {
466            ammus.IgnoresCollisionResponse = true;
467        }
468    }
469
470    void Ammu()
471    {
472        if (SaaAmpua == true)
473        {
474            SaaAmpua = false;
475            shoot.Play();
476            PhysicsObject ammus = new PhysicsObject(100, 10);
477
478            Vector pelaajanSuunta = Vector.FromLengthAndAngle(2500.0, alus.Angle);
479
480
481            ammus.Position = alus.Position + Vector.FromLengthAndAngle(80.0, alus.Angle);
482            ammus.Shape = Shape.Rectangle;
483            ammus.Color = Color.Yellow;
484            ammus.Image = ammuskuva;
485            ammus.Angle = alus.AbsoluteAngle;
486            ammus.Hit(pelaajanSuunta);
487
488            Add(ammus, -1);
489
490
491            ammus.CanRotate = false;
492            ammus.IgnoresCollisionResponse = true;
493            AddCollisionHandler(ammus, "vihollinen", vihollisenKuolema);
494
495            Timer poistumisAjastin = new Timer();
496            poistumisAjastin.Interval = 1;
497            poistumisAjastin.Timeout += delegate { ammusPoistuu(ammus); };
498            poistumisAjastin.Start();
499
500            Timer.SingleShot(0.2,
501                delegate { SaaAmpua = true; }
502            );
503        }
504    }
505
506    void ammusPoistuu(PhysicsObject ammus)
507    {
508        if (ammus.X > Level.Right)
509        {
510            ammus.Destroy();
511        }
512
513        else if (ammus.Y > Level.Top)
514        {
515            ammus.Destroy();
516        }
517    }
518
519    void vihollisenKuolema(PhysicsObject ammus, PhysicsObject vihollinen)
520    {
521        vihollinen.Destroy();
522        hurt.Play();
523        Explosion rajahdys = new Explosion(100);
524        rajahdys.Position = vihollinen.Position;
525        rajahdys.Force = 0;
526        Add(rajahdys);
527        ammus.Destroy();
528
529        pisteLaskuri.Value += 10;
530
531    }
532
533    void asteroidiKuolema(PhysicsObject ammus, PhysicsObject asteroidi)
534    {
535        asteroidi.Destroy();
536        ammus.Destroy();
537
538        pisteLaskuri.Value += 5;
539    }
540}
Note: See TracBrowser for help on using the repository browser.