source: 2013/23/Jon-ErikK/magnetaa.cs @ 3971

Revision 3971, 8.0 KB checked in by josaklin, 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 magnetaa : PhysicsGame
10{
11    PhysicsObject pelaaja1;
12    PhysicsObject pelaaja2;
13
14    PhysicsObject ylaraja;
15    PhysicsObject alaraja;
16    PhysicsObject oikearaja;
17    PhysicsObject vasenraja;
18
19    bool onkoMagneettinen1;
20    bool onkoMagneettinen2;
21
22    List<Ammus> ammukset;
23
24    Vector edellinenNopeusPelaaja1;
25    Vector edellinenNopeusPelaaja2;
26
27    Vector ylos = new Vector(0, 100);
28    Vector alas = new Vector(0, -100);
29    Vector oikea = new Vector(100, 0);
30    Vector vasen = new Vector(-100, 0);
31   
32    public override void Begin()
33    {
34        ammukset = new List<Ammus>();
35
36        onkoMagneettinen1 = true;
37        onkoMagneettinen1 = false;
38
39        edellinenNopeusPelaaja2 = new Vector(0, -400);
40        edellinenNopeusPelaaja1 = new Vector(0, 400);
41
42        luoKentta(0,100,0,-100);
43        asetaNappaimet();
44    }
45
46    public void luoKentta(double pelaaja1x, double pelaaja1y, double pelaaja2x, double pelaaja2y) {
47
48        pelaaja1 = luoPelaaja(pelaaja1x, pelaaja1y);
49        pelaaja2 = luoPelaaja(pelaaja2x, pelaaja2y);
50
51        Level.BackgroundColor = Color.Black;
52
53        alaraja = Level.CreateBottomBorder();
54        alaraja.IsVisible = true;
55
56        ylaraja = Level.CreateTopBorder();
57        ylaraja.IsVisible = true;
58
59        oikearaja = Level.CreateRightBorder();
60        oikearaja.IsVisible = true;
61
62        vasenraja = Level.CreateLeftBorder();
63        vasenraja.IsVisible = true;
64    }
65
66    public PhysicsObject luoPelaaja(double x, double y) {
67
68        PhysicsObject pelaaja = PhysicsObject.CreateStaticObject(50, 50);
69        pelaaja.Shape = Shape.Circle;
70        pelaaja.X = x;
71        pelaaja.Y = y;
72        pelaaja.MaxVelocity = 100.0;
73        Add(pelaaja);
74
75        return pelaaja;
76    }
77
78    public void asetaNappaimet() {
79
80        Keyboard.Listen(Key.Up, ButtonState.Down, asetaNopeus, null, pelaaja1, ylos);
81        Keyboard.Listen(Key.Up, ButtonState.Released, asetaNopeus, null, pelaaja1, Vector.Zero);
82        Keyboard.Listen(Key.Down, ButtonState.Down, asetaNopeus, null, pelaaja1, alas);
83        Keyboard.Listen(Key.Down, ButtonState.Released, asetaNopeus, null, pelaaja1, Vector.Zero);
84        Keyboard.Listen(Key.Left, ButtonState.Down, asetaNopeus, null, pelaaja1, vasen);
85        Keyboard.Listen(Key.Left, ButtonState.Released, asetaNopeus, null, pelaaja1, Vector.Zero);
86        Keyboard.Listen(Key.Right, ButtonState.Down, asetaNopeus, null, pelaaja1, oikea);
87        Keyboard.Listen(Key.Right, ButtonState.Released, asetaNopeus, null, pelaaja1, Vector.Zero);
88        Keyboard.Listen(Key.Period, ButtonState.Pressed, ammu, null, pelaaja1);
89        Keyboard.Listen(Key.Comma, ButtonState.Pressed, vaihdaMagneettisuus, null, pelaaja1);
90
91        Keyboard.Listen(Key.W, ButtonState.Down, asetaNopeus, null, pelaaja2, ylos);
92        Keyboard.Listen(Key.W, ButtonState.Released, asetaNopeus, null, pelaaja2, Vector.Zero);
93        Keyboard.Listen(Key.S, ButtonState.Down, asetaNopeus, null, pelaaja2, alas);
94        Keyboard.Listen(Key.S, ButtonState.Released, asetaNopeus, null, pelaaja2, Vector.Zero);
95        Keyboard.Listen(Key.A, ButtonState.Down, asetaNopeus, null, pelaaja2, vasen);
96        Keyboard.Listen(Key.A, ButtonState.Released, asetaNopeus, null, pelaaja2, Vector.Zero);
97        Keyboard.Listen(Key.D, ButtonState.Down, asetaNopeus, null, pelaaja2, oikea);
98        Keyboard.Listen(Key.D, ButtonState.Released, asetaNopeus, null, pelaaja2, Vector.Zero);
99        Keyboard.Listen(Key.Space, ButtonState.Pressed, ammu, null, pelaaja2);
100        Keyboard.Listen(Key.LeftShift, ButtonState.Pressed, vaihdaMagneettisuus, null, pelaaja2);
101
102        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
103
104    }
105
106    public void asetaMagneettitarkastusajastin()
107    {
108        while (true)
109        {
110            Timer.SingleShot(0.01, tarkistaMagneetit);
111        }
112    }
113
114    public void tarkistaMagneetit()
115    {
116    }
117
118    public void asetaNopeus(PhysicsObject pelaaja, Vector nopeus)
119    {
120
121        if (pelaaja == pelaaja1)
122        {
123            if (nopeus.X != 0 && nopeus.Y != 0)
124            {
125                edellinenNopeusPelaaja1 = nopeus * 4;
126            }
127            else
128            {
129                edellinenNopeusPelaaja1 = pelaaja.Velocity * 4;
130            }
131        }
132        if (pelaaja == pelaaja2)
133        {
134            if (nopeus.X != 0 && nopeus.Y != 0)
135            {
136                edellinenNopeusPelaaja2 = nopeus * 4;
137            }
138            else
139            {
140                edellinenNopeusPelaaja2 = pelaaja.Velocity * 4;
141            }
142        }
143       
144        pelaaja.Velocity = nopeus;   
145    }
146
147    public void ammu(PhysicsObject pelaaja)
148    { 
149        if (pelaaja == pelaaja1)
150        {
151            luoUusiAmmus(pelaaja.X + edellinenNopeusPelaaja1.X / 10, pelaaja.Y + edellinenNopeusPelaaja1.Y / 10, onkoMagneettinen1, edellinenNopeusPelaaja1);
152           
153        }
154        else
155        {
156            luoUusiAmmus(pelaaja.X + edellinenNopeusPelaaja2.X / 10, pelaaja.Y + edellinenNopeusPelaaja2.Y / 10, onkoMagneettinen2, edellinenNopeusPelaaja2);
157        }
158    }
159
160    public void luoUusiAmmus(double x, double y, bool onkoMagneettinen, Vector nopeus)
161    {
162        Ammus uusiAmmus = new Ammus(20, 20, onkoMagneettinen);
163
164        uusiAmmus.asetaNopeus(nopeus);
165        uusiAmmus.asetaSijainti(x, y);
166       
167        Add(uusiAmmus);
168
169        AddCollisionHandler(uusiAmmus, osumaAmmuksella);
170
171        ammukset.Add(uusiAmmus);
172    }
173
174    public void vaihdaMagneettisuus(PhysicsObject pelaaja)
175    {
176        if (pelaaja == pelaaja1)
177        {
178            if (onkoMagneettinen1)
179            {
180                onkoMagneettinen1 = false;
181            }
182            else
183            {
184                onkoMagneettinen1 = true;
185            }
186        }
187        else
188        {
189            if (onkoMagneettinen2)
190            {
191                onkoMagneettinen2 = false;
192            }
193            else
194            {
195                onkoMagneettinen2 = true;
196            }
197        }
198
199    }
200
201    public void osumaAmmuksella(PhysicsObject ammus, PhysicsObject osuja)
202    {
203        for(int i=0;i<ammukset.Count;i++){
204            if (ammukset[i] == osuja)
205            {
206                osuja.Destroy();
207                ammus.Destroy();
208            }
209        }
210
211        if (osuja == pelaaja1 || osuja == pelaaja2) {
212
213            ammus.Destroy();
214
215        }
216
217        if (osuja == ylaraja || osuja == alaraja)
218        {
219            ammus.Destroy();
220        }
221
222        if (osuja == oikearaja)
223        {
224            ammus.X = -ammus.X + 12;
225            Timer.SingleShot(0.01, delegate { ammus.Velocity = new Vector(400.0, 0); });
226               
227        } 
228
229        if (osuja == vasenraja)
230        {
231            ammus.X = -ammus.X - 12;
232            Timer.SingleShot(0.01, delegate { ammus.Velocity = new Vector(-400.0, 0); });
233        }
234
235    }
236   
237}
238
239class Ammus : PhysicsObject
240{
241    public PhysicsObject vaikutusalue;
242    public bool magneettisuus;
243
244    public Ammus(double leveys, double korkeus,bool magneettisuus)
245    :base(leveys,korkeus)
246    {
247        vaikutusalue=PhysicsObject.CreateStaticObject(leveys*2,korkeus*2);
248        this.magneettisuus = magneettisuus;
249
250        vaikutusalue.IsVisible = false;
251        if (magneettisuus == true)
252        {
253            base.Color = Color.Red;
254        }
255        else
256        {
257            base.Color = Color.Blue;
258        }
259
260        base.Shape = Shape.Circle;
261        vaikutusalue.Shape = Shape.Circle;
262
263
264    }
265
266    public void asetaNopeus(Vector nopeus) {
267        base.Velocity = nopeus;
268        vaikutusalue.Velocity = nopeus;
269    }
270
271    public void asetaSijainti(double x, double y) {
272
273        base.X = x;
274        base.Y = y;
275        vaikutusalue.X = x;
276        vaikutusalue.Y = y;
277
278    }
279}
Note: See TracBrowser for help on using the repository browser.