source: 2013/26/OtsoR/Projekti/Projekti/Projekti/Projekti.cs @ 4297

Revision 4297, 30.3 KB checked in by juiitamm, 10 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 Player : PhysicsObject
10{
11    private IntMeter health = new IntMeter(100, 0, 100);
12    public IntMeter Health { get { return health; } }
13    public Color Color1;
14    public Color Color2;
15
16    public Image cannonImage;
17
18    public Player(double width, double height, Shape shape)
19        : base(width, height, shape)
20    {
21        ProgressBar playerhealth = new ProgressBar(8, 3);
22        playerhealth.BindTo(health);
23        playerhealth.BarColor = Color.SpringGreen;
24        playerhealth.BorderColor = Color.Red;
25        playerhealth.X = -8;
26        playerhealth.Angle = Angle.RightAngle;
27        Add(playerhealth);
28        health.LowerLimit += delegate
29        {
30            this.Destroy();
31        };
32    }
33}
34class Enemy : PhysicsObject
35{
36    private IntMeter health = new IntMeter(5, 0, 5);
37    public IntMeter Health { get { return health; } }
38
39    public Enemy(double width, double height, Shape shape, double barwidth, double barheight, double barx)
40        : base(width, height, shape)
41    {
42        ProgressBar enemyhealth = new ProgressBar(barwidth, barheight);
43        enemyhealth.BindTo(health);
44        enemyhealth.X = barx;
45        enemyhealth.Angle = Angle.RightAngle;
46        Add(enemyhealth);
47        health.LowerLimit += delegate
48        {
49            this.Destroy();
50        };
51    }
52}
53public class Projekti : PhysicsGame
54{
55    Image[] shipimages = new Image[100];
56    Color[] shipcolors = { Color.DarkGray, Color.Silver, Color.DarkOrange, Color.Blue };
57    Color[] shipcolors2 = { Color.Ruby, Color.Azure, Color.Emerald, Color.Orange };
58    Image bigenemy = LoadImage("bigenemy");
59    Image shell = LoadImage("bullet");
60    Image pointer = LoadImage("pointer");
61    Image rdamage = LoadImage("rdamage");
62    Image rrepair = LoadImage("rrepair");
63    Image homing = LoadImage("missile");
64
65    String[] weaponnames = { "projectile", "laser", "plasma", "missile" };
66
67    PhysicsObject topedge;
68    PhysicsObject bottomedge;
69    PhysicsObject leftedge;
70    PhysicsObject rightedge;
71
72    Player ship1;
73    Weapon weapon;
74    Player ship2;
75    Weapon weapon2;
76    //PhysicsObject hostile;
77    Weapon hostilew;
78    GameObject line;
79    PhysicsObject ammus;
80
81    Label number;
82    Label number2;
83    Label colour;
84    Label weaponlabel;
85    Label weaponlabel2;
86    Widget aluskuva;
87    Widget aluskuva2;
88    IntMeter shipimg;
89    IntMeter shipcolor1;
90    IntMeter shipwpn;
91    IntMeter shipimg2;
92    IntMeter shipcolor2;
93    IntMeter shipWeapon2;
94    IntMeter points;
95    int shipcounter;
96    int bigshipcounter;
97    int bosscounter;
98    int runecounter;
99    public override void Begin()
100    {
101        for (int i = 1; i <= 100; i++)
102        {
103            shipimages[i - 1] = LoadImage("s" + i);
104        }
105        IsMouseVisible = true;
106        MultiSelectWindow numberofplayersmenu = new MultiSelectWindow("Select number of players", "1 Player", "2 Players", "Quit");
107        numberofplayersmenu.ItemSelected += mainmenu;
108        Add(numberofplayersmenu);
109    }
110    void mainmenu(int choice)
111    {
112        switch (choice)
113        {
114            case 0:
115                ClearAll();
116                singleplayer();
117                break;
118            case 1:
119                ClearAll();
120                coop();
121                break;
122            case 2:
123                Exit();
124                break;
125        }
126    }
127
128    void singleplayer()
129    {
130        aluskuva = new Widget(50, 50);
131        aluskuva.X = 0;
132        aluskuva.Y = 250;
133        aluskuva.Image = shipimages[0];
134        Add(aluskuva);
135
136        shipimg = new IntMeter(1, 1, 100);
137        shipimg.Changed += Changeship1;
138        shipwpn = new IntMeter(1, 1, 4);
139        shipwpn.Changed += delegate { weaponlabel.Text = weaponnames[shipwpn.Value - 1]; };
140        shipcolor1 = new IntMeter(0, 0, 3);
141        shipcolor1.Changed += Changeship1;
142
143        number = new Label();
144        number.BindTo(shipimg);
145        number.X = 0;
146        number.Y = 300;
147        Add(number);
148
149        colour = new Label("style");
150        colour.X = -125;
151        colour.Y = 125;
152        Add(colour);
153
154        weaponlabel = new Label(weaponnames[0]);
155        weaponlabel.X = 125;
156        weaponlabel.Y = 125;
157        Add(weaponlabel);
158
159        Slider shiptype = new Slider(550, 10);
160        shiptype.X = 0;
161        shiptype.Y = 200;
162        shiptype.BindTo(shipimg);
163        Slider shipcolour = new Slider(125, 15);
164        shipcolour.X = -125;
165        shipcolour.Y = 150;
166        shipcolour.BindTo(shipcolor1);
167        Slider shipweapon = new Slider(125, 15);
168        shipweapon.X = 125;
169        shipweapon.Y = 150;
170        shipweapon.BindTo(shipwpn);
171
172        Add(shiptype);
173        Add(shipcolour);
174        Add(shipweapon);
175
176        List<Label> valikonKohdat;
177        valikonKohdat = new List<Label>(); // Alustetaan lista, johon valikon kohdat tulevat
178        Label kohta1 = new Label("Start");  // Luodaan uusi Label-olio, joka toimii uuden pelin aloituskohtana
179        kohta1.Position = new Vector(0, -100);  // Asetetaan valikon ensimmäinen kohta hieman kentän keskikohdan yläpuolelle
180        valikonKohdat.Add(kohta1);  // Lisätään luotu valikon kohta listaan jossa kohtia säilytetään
181        // Lisätään kaikki luodut kohdat peliin foreach-silmukalla
182        foreach (Label valikonKohta in valikonKohdat)
183        {
184            Add(valikonKohta);
185        }
186        Mouse.ListenOn(kohta1, MouseButton.Left, ButtonState.Pressed, singleplayerbase, null);
187    }
188
189    void Changeship1(int vanha, int uusi)
190    {
191        aluskuva.Image = shipimages[shipimg.Value - 1].Clone();
192        aluskuva.Image.ReplaceColor(Color.White, shipcolors[shipcolor1.Value]);
193        aluskuva.Image.ReplaceColor(Color.Black, shipcolors2[shipcolor1.Value]);
194    }
195    void Changeship2(int vanha, int uusi)
196    {
197        aluskuva2.Image = shipimages[shipimg2.Value - 1].Clone();
198        aluskuva2.Image.ReplaceColor(Color.White, shipcolors[shipcolor2.Value]);
199        aluskuva2.Image.ReplaceColor(Color.Black, shipcolors2[shipcolor2.Value]);
200    }
201
202    void Createship(double x, double y)
203    {
204        ship1 = new Player(14, 14, Shape.FromImage(aluskuva.Image));
205        ship1.Image = aluskuva.Image;
206
207        ship1.Restitution = 1.0;
208        ship1.Color1 = shipcolors[shipcolor1.Value];
209        ship1.Color2 = shipcolors2[shipcolor1.Value];
210        ship1.cannonImage = LoadImage("missile");
211        ship1.cannonImage.ReplaceColor(Color.White, ship1.Color1);
212        ship1.cannonImage.ReplaceColor(Color.Black, ship1.Color2);
213
214        ship1.X = x;
215        ship1.Y = y;
216        ship1.KineticFriction = 0.0;
217        ship1.MomentOfInertia = Double.PositiveInfinity;
218        ship1.LinearDamping = 0.95;
219        ship1.AngularDamping = 0.95;
220        Add(ship1);
221        choice(shipwpn.Value);
222        ship1.Add(weapon);
223        ship1.CollisionIgnoreGroup = 1;
224        ship1.Tag = "player";
225        ship1.Destroyed += playerdeath;
226        if (weapon is LaserGun)
227        {
228            line = new GameObject(5000, 1.25);
229            line.Image = pointer;
230            line.X = 2500;
231            ship1.Add(line);
232        }
233    }
234    void Createship2(double x, double y)
235    {
236        ship2 = new Player(14, 14, Shape.FromImage(aluskuva2.Image));
237        ship2.Image = aluskuva2.Image;
238        ship2.X = x;
239        ship2.Y = y;
240        ship2.Color1 = shipcolors[shipcolor2.Value];
241        ship2.Color2 = shipcolors2[shipcolor2.Value];
242        ship2.cannonImage = LoadImage("missile");
243        ship2.cannonImage.ReplaceColor(Color.White, ship2.Color1);
244        ship2.cannonImage.ReplaceColor(Color.Black, ship2.Color2);
245
246        ship2.Restitution = 1.0;
247        ship2.KineticFriction = 0.0;
248        ship2.MomentOfInertia = Double.PositiveInfinity;
249        ship2.LinearDamping = 0.95;
250        ship2.AngularDamping = 0.95;
251        Add(ship2);
252        choice2(shipWeapon2.Value);
253        ship2.Add(weapon2);
254        ship2.CollisionIgnoreGroup = 1;
255        ship2.Tag = "player";
256        ship2.Destroyed += playerdeath;
257        if (weapon is LaserGun)
258        {
259            line = new GameObject(5000, 1.25);
260            line.Image = pointer;
261            line.X = 2500;
262            ship1.Add(line);
263        }
264    }
265    void Spawnhostiles()
266    {
267        while (shipcounter < 39)
268        {
269        int hostileship = RandomGen.NextInt(0, 100);
270        double hostiley = 0.0;
271        double hostilex = 0.0;
272        if (RandomGen.NextBool())
273        {
274            hostiley = Level.Bottom + 2 * Level.Top * RandomGen.NextInt(0, 2);
275            hostilex = RandomGen.NextDouble(Level.Left, Level.Right);
276        }
277        else
278        {
279            hostiley = RandomGen.NextDouble(Level.Bottom, Level.Top);
280            hostilex = Level.Left + 2 * Level.Right * RandomGen.NextInt(0, 2);
281        }
282        Createhostile(hostileship, hostiley, hostilex);
283        shipcounter++;
284        }
285        while (bigshipcounter < 4)
286        {
287            double bighostiley = 0.0;
288            double bighostilex = 0.0;
289            if (RandomGen.NextBool())
290            {
291                bighostiley = Level.Bottom + 2 * Level.Top * RandomGen.NextInt(0, 2);
292                bighostilex = RandomGen.NextDouble(Level.Left, Level.Right);
293            }
294            else
295            {
296                bighostiley = RandomGen.NextDouble(Level.Bottom, Level.Top);
297                bighostilex = Level.Left + 2 * Level.Right * RandomGen.NextInt(0, 2);
298            }
299            Createbighostile(bighostiley, bighostilex);
300            bigshipcounter++;
301        }
302    }
303    void Createhostile(int randomship, double randomy, double randomx)
304    {
305        Enemy hostile = new Enemy(14, 14, Shape.FromImage(shipimages[randomship]), 5, 2.5, -8);
306        hostile.Image = shipimages[randomship];
307        hostile.Y = randomy;
308        hostile.X = randomx;
309        hostile.Restitution = 1.0;
310        hostile.KineticFriction = 0.0;
311        hostile.MomentOfInertia = Double.PositiveInfinity;
312        hostile.LinearDamping = 0.95;
313        hostile.AngularDamping = 1;
314        hostile.Tag = "hostile";
315        hostile.Health.MaxValue = 5;
316        hostile.Health.Value = 5;
317        hostile.CollisionIgnoreGroup = 2;
318        hostile.Image.ReplaceColor(Color.White, Color.Ultramarine);
319        hostile.Image.ReplaceColor(Color.Black, Color.Green);
320        Add(hostile);
321
322        hostile.Destroyed += enemydeath;
323
324        AssaultRifle hostilew = new AssaultRifle(0, 0);
325        hostilew.ProjectileCollision = delegate(PhysicsObject ammus, PhysicsObject kohde) { weaponhit(ammus, kohde, 10, 0); };
326        hostilew.FireRate = 0.5;
327        hostilew.Power.DefaultValue = 15;
328        hostilew.Power.Value = 15;
329        hostile.Add(hostilew);
330
331        FollowerBrain brain = new FollowerBrain("player");
332        brain.Speed = 20;
333        brain.DistanceFar = 10000;
334        brain.DistanceClose = 125;
335        brain.StopWhenTargetClose = false;
336        brain.TargetClose += delegate { fire(hostilew, null); };
337        brain.TurnWhileMoving = true;
338        hostile.Brain = brain;
339    }
340    void Createbighostile(double y, double x)
341    {
342        Enemy Big1 = new Enemy(50, 28, Shape.FromImage(bigenemy), 20, 2.5, -28);
343        Big1.Image = bigenemy;
344        Big1.X = x;
345        Big1.Y = y;
346        Big1.Restitution = 1.0;
347        Big1.KineticFriction = 0.0;
348        Big1.MomentOfInertia = Double.PositiveInfinity;
349        Big1.LinearDamping = 0.95;
350        Big1.AngularDamping = 1;
351        Big1.Tag = "hostile";
352        Big1.Health.MaxValue = 40;
353        Big1.Health.Value = 40;
354        Big1.CollisionIgnoreGroup = 2;
355        Big1.Image.ReplaceColor(Color.White, Color.Ultramarine);
356        Big1.Image.ReplaceColor(Color.Black, Color.Green);
357        Add(Big1);
358        Big1.Destroyed += bigenemydeath;
359
360        Cannon Big1w = new Cannon(0, 0);
361        Big1w.ProjectileCollision = delegate(PhysicsObject ammus, PhysicsObject kohde) { weaponhit(ammus, kohde, 3, 0); };
362        Big1w.FireRate = 0.3;
363        Big1w.CanHitOwner = false;
364        Big1w.Power.DefaultValue = 1500;
365        Big1w.Power.Value = 1500;
366        Big1.Add(Big1w);
367
368        FollowerBrain bigbrain = new FollowerBrain("player");
369        bigbrain.Speed = 15;
370        bigbrain.DistanceFar = 10000;
371        bigbrain.DistanceClose = 325;
372        bigbrain.TurnSpeed = UnlimitedAngle.FromDegrees(25);
373        bigbrain.StopWhenTargetClose = false;
374        bigbrain.TargetClose += delegate { firehoming(Big1w); };
375        bigbrain.TurnWhileMoving = true;
376        Big1.Brain = bigbrain;
377    }
378    void playerdeath()
379    {
380        MessageDisplay.Add("Ship lost");
381        //MediaPlayer.Play("playerdeath");
382    }
383    void enemydeath()
384    {
385        shipcounter--;
386        points.Value += 5;
387    }
388    void bigenemydeath()
389    {
390        bigshipcounter--;
391        points.Value += 40;
392    }
393    void boss1death()
394    {
395    }
396    void boss2death()
397    {
398    }
399    void createedges()
400    {
401        PhysicsObject leftedge = Level.CreateLeftBorder();
402        leftedge.Restitution = 1.0;
403        leftedge.KineticFriction = 0.0;
404        leftedge.IsVisible = false;
405        leftedge.CollisionIgnoreGroup = 2;
406        PhysicsObject rightedge = Level.CreateRightBorder();
407        rightedge.Restitution = 1.0;
408        rightedge.KineticFriction = 0.0;
409        rightedge.IsVisible = false;
410        rightedge.CollisionIgnoreGroup = 2;
411        PhysicsObject topedge = Level.CreateTopBorder();
412        topedge.Restitution = 1.0;
413        topedge.KineticFriction = 0.0;
414        topedge.IsVisible = false;
415        topedge.CollisionIgnoreGroup = 2;
416        PhysicsObject bottomedge = Level.CreateBottomBorder();
417        bottomedge.Restitution = 1.0;
418        bottomedge.IsVisible = false;
419        bottomedge.KineticFriction = 0.0;
420        topedge.CollisionIgnoreGroup = 2;
421    }
422    void coop()
423    {
424        aluskuva = new Widget(50, 50);
425        aluskuva.X = -300;
426        aluskuva.Y = 250;
427        aluskuva.Image = shipimages[0];
428        Add(aluskuva);
429
430        aluskuva2 = new Widget(50, 50);
431        aluskuva2.X = 300;
432        aluskuva2.Y = 250;
433        aluskuva2.Image = shipimages[0];
434        Add(aluskuva2);
435
436        shipimg = new IntMeter(1, 1, 100);
437        shipimg.Changed += Changeship1;
438        shipwpn = new IntMeter(1, 1, 4);
439        shipwpn.Changed += delegate { weaponlabel.Text = weaponnames[shipwpn.Value - 1]; };
440
441        Slider shipColorP1 = new Slider(125, 15);
442        shipColorP1.X = -425;
443        shipColorP1.Y = 150;
444        shipcolor1 = new IntMeter(0, 0, 3);
445        shipcolor1.Changed += Changeship1;
446        shipColorP1.BindTo(shipcolor1);
447
448        Slider shipcolour2 = new Slider(125, 15);
449        shipcolour2.X = 225;
450        shipcolour2.Y = 150;
451        shipcolor2 = new IntMeter(0, 0, 3);
452        shipcolor2.Changed += Changeship2;
453        shipcolour2.BindTo(shipcolor2); 
454               
455        shipimg2 = new IntMeter(1, 1, 100);
456        shipimg2.Changed += Changeship2;
457        shipWeapon2 = new IntMeter(1, 1, 4);
458        shipWeapon2.Changed += delegate
459        {
460            weaponlabel2.Text = weaponnames[shipWeapon2.Value - 1];
461        };
462        number = new Label();
463        number.BindTo(shipimg);
464        number.X = -300;
465        number.Y = 300;
466        Add(number);
467
468        number2 = new Label();
469        number2.BindTo(shipimg2);
470        number2.X = 300;
471        number2.Y = 300;
472        Add(number2);
473
474        weaponlabel = new Label(weaponnames[0]);
475        weaponlabel.X = -225;
476        weaponlabel.Y = 125;
477        Add(weaponlabel);
478        weaponlabel2 = new Label(weaponnames[0]);
479        weaponlabel2.X = 425;
480        weaponlabel2.Y = 125;
481        Add(weaponlabel2);
482
483        Slider shiptype = new Slider(550, 10);
484        shiptype.X = -300;
485        shiptype.Y = 200;
486        shiptype.BindTo(shipimg);
487       
488        Slider shipweapon = new Slider(125, 15);
489        shipweapon.X = -225;
490        shipweapon.Y = 150;
491        shipweapon.BindTo(shipwpn);
492
493        Slider shiptype2 = new Slider(550, 10);
494        shiptype2.X = 300;
495        shiptype2.Y = 200;
496        shiptype2.BindTo(shipimg2);
497       
498        Slider shipweapon2 = new Slider(125, 15);
499        shipweapon2.X = 425;
500        shipweapon2.Y = 150;
501        shipweapon2.BindTo(shipWeapon2);
502        Add(shiptype);
503        Add(shipColorP1);
504        Add(shipweapon);
505        Add(shiptype2);
506        Add(shipcolour2);
507        Add(shipweapon2);
508        List<Label> valikonKohdat2;
509        valikonKohdat2 = new List<Label>(); // Alustetaan lista, johon valikon kohdat tulevat
510        Label kohta2 = new Label("Start");  // Luodaan uusi Label-olio, joka toimii uuden pelin aloituskohtana
511        kohta2.Position = new Vector(0, -100);  // Asetetaan valikon ensimmäinen kohta hieman kentän keskikohdan yläpuolelle
512        valikonKohdat2.Add(kohta2);  // Lisätään luotu valikon kohta listaan jossa kohtia säilytetään
513        // Lisätään kaikki luodut kohdat peliin foreach-silmukalla
514        foreach (Label valikonKohta2 in valikonKohdat2)
515        {
516            Add(valikonKohta2);
517        }
518        Mouse.ListenOn(kohta2, MouseButton.Left, ButtonState.Pressed, coopbase, null);
519    }
520    void choice(int choice)
521    {
522        switch (choice)
523        {
524            case 1:
525                weapon = new AssaultRifle(0, 0);
526                weapon.ProjectileCollision = delegate(PhysicsObject ammus, PhysicsObject kohde) { weaponhit(ammus, kohde, 0, 1); };
527                weapon.FireRate = 4.5;
528                weapon.Power.Value = 25;
529                weapon.Power.DefaultValue = 25;
530                break;
531            case 2:
532                weapon = new LaserGun(0, 0);
533                weapon.ProjectileCollision = delegate(PhysicsObject ammus, PhysicsObject kohde) { weaponhit(ammus, kohde, 0, 5); };
534                weapon.FireRate = 0.8;
535                weapon.Power.DefaultValue = 1000;
536                weapon.Power.Value = 1000;
537                break;
538            case 3:
539                weapon = new PlasmaCannon(0, 0);
540                weapon.ProjectileCollision = delegate(PhysicsObject ammus, PhysicsObject kohde) { weaponhit(ammus, kohde, 0, 3); };
541                weapon.FireRate = 1.5;
542                weapon.Power.Value = 400;
543                weapon.Power.DefaultValue = 400;
544                break;
545            case 4:
546                weapon = new Cannon(0, 0);
547                weapon.ProjectileCollision = delegate(PhysicsObject ammus, PhysicsObject kohde) { weaponhit(ammus, kohde, 0, 5); };
548                weapon.FireRate = 0.8;
549                weapon.Power.DefaultValue = 1500;
550                weapon.Power.Value = 1500;
551                break;
552        }
553    }
554    void choice2(int choice)
555    {
556        switch (choice)
557        {
558            case 1:
559                weapon2 = new AssaultRifle(0, 0);
560                weapon2.ProjectileCollision = delegate(PhysicsObject ammus, PhysicsObject kohde) { weaponhit(ammus, kohde, 0, 1); };
561                weapon2.FireRate = 4.5;
562                weapon2.Power.Value = 25;
563                weapon2.Power.DefaultValue = 25;
564                break;
565            case 2:
566                weapon2 = new LaserGun(0, 0);
567                weapon2.ProjectileCollision = delegate(PhysicsObject ammus, PhysicsObject kohde) { weaponhit(ammus, kohde, 0, 5); };
568                weapon2.FireRate = 1;
569                weapon2.Power.DefaultValue = 1000;
570                weapon2.Power.Value = 1000;
571                break;
572            case 3:
573                weapon2 = new PlasmaCannon(0, 0);
574                weapon2.ProjectileCollision = delegate(PhysicsObject ammus, PhysicsObject kohde) { weaponhit(ammus, kohde, 0, 3); };
575                weapon2.FireRate = 1.5;
576                weapon2.Power.Value = 400;
577                weapon2.Power.DefaultValue = 400;
578                break;
579            case 4:
580                weapon2 = new Cannon(0, 0);
581                weapon2.ProjectileCollision = delegate(PhysicsObject ammus, PhysicsObject kohde) { weaponhit(ammus, kohde, 0, 5); };
582                weapon2.FireRate = 0.8;
583                weapon2.Power.DefaultValue = 1500;
584                weapon2.Power.Value = 1500;
585                break;
586        }
587    }
588    void fire(Weapon ase, Player ship)
589    {
590        PhysicsObject ammus = ase.Shoot();
591        if (ammus != null)
592        {
593            //Add(ammus, 2);
594            //ammus.Size *= ;
595            //ammus.Image = ...;
596            //ammus.MaximumLifetime = TimeSpan.FromSeconds(2.0);
597            ammus.MaximumLifetime = TimeSpan.FromSeconds(10.0);
598            if (ase is AssaultRifle)
599            {
600                ammus.IgnoresCollisionResponse = true;
601                ammus.Image = shell;
602                ammus.Size *= 0.4;
603                ammus.CollisionIgnoreGroup = 4;
604                AddCollisionHandler(ammus, AmmusOsui);
605            }
606            if (ase is LaserGun)
607            {
608                ammus.Size *= 2;
609                ammus.IgnoresCollisionResponse = true;
610                ammus.Tag = "laser";
611            }
612            if (ase is PlasmaCannon)
613            {
614                ammus.Size *= 0.9;
615                AddCollisionHandler(ammus, AmmusOsui);
616                ammus.IgnoresCollisionResponse = true;
617                ammus.CollisionIgnoreGroup = 3;
618            }
619            if (ase is Cannon)
620            {
621                ammus.Size *= 0.8;
622                // ammus.Image = homing;
623                ammus.Image = ship.cannonImage;
624                AddCollisionHandler(ammus, missilehit);
625                ammus.AngularDamping = 1.0;
626                ammus.IgnoresCollisionResponse = true;
627
628                FollowerBrain ammusbrain = new FollowerBrain("hostile", "laser");
629                //ammusbrain.DistanceFar = 125;
630                ammusbrain.DistanceFar = 50;
631                ammusbrain.Speed = 80;
632                ammusbrain.TurnSpeed = UnlimitedAngle.FromDegrees(360);
633                ammusbrain.StopWhenTargetClose = false;
634                ammusbrain.TurnWhileMoving = true;
635                ammus.Brain = ammusbrain;
636            }
637        }
638    }
639    void AmmusOsui(PhysicsObject ammus, PhysicsObject kohde)
640    {
641        if (kohde.Tag == "powerup")
642        {
643            return;
644        }
645        ammus.Destroy();
646    }
647
648    void missilehit(PhysicsObject tormaaja, PhysicsObject kohde)
649    {
650        if (kohde.Tag == "powerup")
651        {
652            return;
653        }
654        Explosion missilecrash = new Explosion(8);
655        missilecrash.Position = tormaaja.Position;
656        missilecrash.UseShockWave = false;
657        Add(missilecrash);
658        tormaaja.Destroy();
659    }
660    void firehoming(Weapon ase)
661    {
662        PhysicsObject ammus = ase.Shoot();
663        if (ammus != null)
664        {
665            //Add(ammus, 2);
666            //ammus.Size *= ;
667            //ammus.Image = ...;
668            //ammus.MaximumLifetime = TimeSpan.FromSeconds(2.0);
669            ammus.MaximumLifetime = TimeSpan.FromSeconds(5.0);
670            ammus.Image = homing;
671            AddCollisionHandler(ammus, missilehit);
672            ammus.IgnoresCollisionResponse = true;
673            ammus.Size *= 0.8;
674            ammus.AngularDamping = 1.0;
675            ammus.Image.ReplaceColor(Color.White, Color.Ultramarine);
676            ammus.Image.ReplaceColor(Color.Black, Color.Green);
677            AddCollisionHandler(ammus, "player1", CollisionHandler.DestroyObject);
678            FollowerBrain ammusbrain = new FollowerBrain("player", "laser");
679            //ammusbrain.DistanceFar = 125;
680            ammusbrain.DistanceFar = 50;
681            ammusbrain.Speed = 80;
682            ammusbrain.TurnSpeed = UnlimitedAngle.FromDegrees(360);
683            ammusbrain.StopWhenTargetClose = false;
684            ammusbrain.TurnWhileMoving = true;
685            ammus.Brain = ammusbrain;
686        }
687    }
688    void weaponhit(PhysicsObject ammus, PhysicsObject kohde, int playerDamage, int enemyDamage)
689    {
690        if (kohde.Tag == "player")
691        {
692            Player ship = kohde as Player;
693            //ship.Health.Value -= 3;
694            ship.Health.Value -= playerDamage;
695        }
696        if (kohde.Tag == "hostile")
697        {
698            Enemy ship = kohde as Enemy;
699            //ship.Health.Value -= 1;
700            ship.Health.Value -= enemyDamage;
701        }
702    }
703    void Controlsship()
704    {
705        Keyboard.Listen(Key.W, ButtonState.Down, movement, null, 0, 100, ship1);
706        Keyboard.Listen(Key.S, ButtonState.Down, movement, null, 0, -100, ship1);
707        Keyboard.Listen(Key.A, ButtonState.Down, movement, null, -100, 0, ship1);
708        Keyboard.Listen(Key.D, ButtonState.Down, movement, null, 100, 0, ship1);
709        Keyboard.Listen(Key.Space, ButtonState.Down, fire, "", weapon, ship1);
710        Mouse.ListenMovement(0, Aim, null, ship1);
711        Mouse.Listen(MouseButton.Left, ButtonState.Down, fire, null, weapon, ship1);
712        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, null);
713    }
714    void Aim(AnalogState hiirenLiike, Player ship)
715    {
716        Vector suunta = (Mouse.PositionOnWorld - ship.AbsolutePosition).Normalize();
717        ship.Angle = suunta.Angle;
718    }
719    void movement(int x, int y, PhysicsObject ship)
720    {
721        ship.Push(new Vector(x, y));
722    }
723    void Controlsship1()
724    {
725        Keyboard.Listen(Key.W, ButtonState.Down, thrust, "", 1.0, ship1);
726        Keyboard.Listen(Key.S, ButtonState.Down, thrust, "", -1.0, ship1);
727        Keyboard.Listen(Key.A, ButtonState.Down, tilt, "", 1.0, ship1);
728        Keyboard.Listen(Key.D, ButtonState.Down, tilt, "", -1.0, ship1);
729        Keyboard.Listen(Key.Space, ButtonState.Down, fire, "", weapon, ship1);
730        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
731    }
732    void Controlsship2()
733    {
734        Keyboard.Listen(Key.NumPad8, ButtonState.Down, thrust, "", 1.0, ship2);
735        Keyboard.Listen(Key.NumPad5, ButtonState.Down, thrust, "", -1.0, ship2);
736        Keyboard.Listen(Key.NumPad4, ButtonState.Down, tilt, "", 1.0, ship2);
737        Keyboard.Listen(Key.NumPad6, ButtonState.Down, tilt, "", -1.0, ship2);
738        Keyboard.Listen(Key.Add, ButtonState.Down, fire, "", weapon2, ship2);
739    }
740    void thrust(double direction, Player ship)
741    {
742        Vector shipdirection = Vector.FromLengthAndAngle(100.0 * direction, ship.Angle);
743        ship.Push(shipdirection);
744    }
745    void tilt(double direction, Player ship)
746    {
747        ship.AngularAcceleration = 10.0 * direction;
748    }
749    void singleplayerbase()
750    {
751        ClearAll();
752        Level.Background.CreateStars();
753        Level.CreateBorders();
754        Camera.ZoomToLevel();
755        Createship(0, 0);
756        Spawnhostiles();
757        Controlsship();
758
759        Timer spawnrune = new Timer();
760        spawnrune.Interval = 15;
761        spawnrune.Timeout += Spawnrunes;
762        spawnrune.Start();
763
764        points = new IntMeter(0);
765        Label pointsdisplay = new Label();
766        pointsdisplay.Title = "Points";
767        pointsdisplay.X = Screen.Right - 100;
768        pointsdisplay.Y = Screen.Top - 100;
769        pointsdisplay.TextColor = Color.White;
770        pointsdisplay.Color = Color.Black;
771        pointsdisplay.BindTo(points);
772        Add(pointsdisplay);
773
774        Timer a = new Timer();
775        a.Interval = 5;
776        a.Timeout += Spawnhostiles;
777        a.Start();
778    }
779    void coopbase()
780    {
781        ClearAll();
782        Level.Background.CreateStars();
783        Level.CreateBorders();
784        Camera.ZoomToLevel();
785        Createship(-100, 0);
786        Createship2(100, 0);
787        Spawnhostiles();
788        Controlsship1();
789        Controlsship2();
790
791        Timer spawnrune = new Timer();
792        spawnrune.Interval = 15;
793        spawnrune.Timeout += Spawnrunes;
794        spawnrune.Start();
795
796        points = new IntMeter(0);
797        Label pointsdisplay = new Label();
798        pointsdisplay.Title = "Points";
799        pointsdisplay.X = Screen.Right - 100;
800        pointsdisplay.Y = Screen.Top - 100;
801        pointsdisplay.TextColor = Color.White;
802        pointsdisplay.Color = Color.Black;
803        pointsdisplay.BindTo(points);
804        Add(pointsdisplay);
805
806        Timer spawnenemy = new Timer();
807        spawnenemy.Interval = 5;
808        spawnenemy.Timeout += Spawnhostiles;
809        spawnenemy.Start();
810    }
811    void Spawnrunes()
812    {
813        if (runecounter < 5)
814        {
815            int runetype = RandomGen.NextInt(1, 3);
816            double runey = 0.0;
817            double runex = 0.0;
818            Vector paikka = Level.GetRandomFreePosition(5.0);
819            runey = paikka.Y;
820            runex = paikka.X;
821            Createrune(runetype, runey, runex);
822            runecounter++;
823        }
824    }
825    void Createrune(int runetype, double y, double x)
826    {
827        PhysicsObject Rune = new PhysicsObject(10, 10);
828        Rune.X = x;
829        Rune.Y = y;
830        Rune.CollisionIgnoreGroup = 2 & 3 & 4;
831        Rune.IgnoresCollisionResponse = true;
832        Rune.MakeStatic();
833        Rune.Tag = "powerup";
834        Rune.AngularVelocity = 5;
835        Add(Rune);
836        switch (runetype)
837        {
838            case 1:
839                //Addcollision
840                Rune.Image = rdamage;
841                Rune.Height = 15;
842                Rune.Width = 10;
843                AddCollisionHandler(Rune, "player", delegate(PhysicsObject tormaaja, Player ship) { damagerune(tormaaja, ship); });
844                break;
845            case 2:
846                //Addcollision
847                Rune.Image = rrepair;
848                AddCollisionHandler(Rune, "player", delegate(PhysicsObject tormaaja, Player ship) { repairrune(tormaaja, ship); });
849                Rune.Height = 14;
850                Rune.Width = 14;
851                break;
852            case 3:
853                //Addcollision
854                //Rune.Image = ...;
855                break;
856        }
857    }
858    void damagerune(PhysicsObject rune, Player ship)
859    {
860        rune.Destroy();
861        weapon.FireRate *= 2;
862        Timer.SingleShot(10, delegate{ weapon.FireRate *=0.5; });
863        runecounter--;
864    }
865    void repairrune(PhysicsObject rune, Player ship)
866    {
867        ship.Health.Value += 100;
868        rune.Destroy();
869        runecounter--;
870    }
871    //void rune3(PhysicsObject rune, Player ship)
872    //{
873    //
874    //}
875}
Note: See TracBrowser for help on using the repository browser.