source: 2015/23/HenriK/dungeoncrawlar/dungeoncrawlar/dungeoncrawlar/dungeoncrawlar.cs @ 5865

Revision 5865, 10.7 KB checked in by sieerinn, 8 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 dungeoncrawlar : PhysicsGame
10{
11    PhysicsObject ovi;
12    PhysicsObject player;
13    PhysicsObject ammus;
14    PhysicsObject avain;
15    PhysicsObject vihollinen;
16    PhysicsObject ase96;
17
18    Listener aseNyt;
19
20    int avaintenLkm;
21    int l96 = 0;
22    int kilpi;
23
24    //Surface alaReuna, vasenReuna, oikeaReuna, yläReuna;
25
26    //PhysicsObject alaReuna, vasenReuna, oikeaReuna, yläReuna;
27
28    public override void Begin()
29    {
30           
31        luohuone(1);
32       
33       
34        avaintenLkm = 0;
35       
36
37        //AddCollisionHandler(player, "seina", PelaajaTormaaSeinaan);
38
39        //PhoneBackButton.Listen(ConfirmExit, "Lopeta peli");
40       
41    }
42    void LuoAse96(Vector paikka, double leveys, double korkeus)
43    {
44        ase96 = new PhysicsObject(100, 100);
45        Image c = LoadImage("l96");
46       
47       
48        ase96.Image = c;
49        ase96.Tag = "ase96";
50        Add(ase96);
51
52       
53    }
54    void PoimiAse(PhysicsObject tormaaja, PhysicsObject tormatty)
55    {
56        ase96.Destroy();
57        l96 += 1;
58        aseet();
59    }
60    void Vihu(Vector paikka, double leveys, double korkeus)
61    {
62        PhysicsObject vihu = new PhysicsObject(20, 20);
63        vihu.Position = RandomGen.NextVector(0, 500);
64        vihu.LinearDamping = 0.5;
65        Add(vihu);
66        Image c = LoadImage("morko");
67        vihu.Shape = Shape.FromImage(c);
68        vihu.Image = c;
69
70        FollowerBrain aivo = new FollowerBrain(player);
71        aivo.Speed = 300;
72        vihu.Brain = aivo;
73
74        Timer ajastin = new Timer();
75        ajastin.Interval = 0.05;
76        ajastin.Timeout += delegate
77        {
78            GameObject väistä = null;
79            double väistöEtäisyys = 10;
80            foreach (GameObject ammus in GetObjectsWithTag("ammus"))
81            {
82                double etäisyys = Vector.Distance(ammus.Position, vihu.Position);
83                if (Vector.Distance(ammus.Position, vihu.Position) < väistöEtäisyys)
84                {
85                    väistöEtäisyys = etäisyys;
86                    väistä = ammus;
87                }
88            }
89
90            if (väistä != null)
91            {
92                vihu.Brain = null;
93                vihu.Push((vihu.Position - väistä.Position) * 500);
94            }
95            else
96            {
97                vihu.Brain = aivo;
98            }
99        };
100        ajastin.Start();
101    }
102   
103    void aseet()
104    {
105
106        if (l96 > 0)
107        {
108            Keyboard.Listen(Key.D1, ButtonState.Pressed, aseenVaihto, "aseen vaihto", 1);
109        }   
110           
111       
112        Keyboard.Listen(Key.D2, ButtonState.Pressed, aseenVaihto, "aseen vaihto", 2);
113        Keyboard.Listen(Key.D3, ButtonState.Pressed, aseenVaihto, "aseen vaihto", 3);
114       
115       
116    }
117    void aseenVaihto(int ase)
118    {
119        if (aseNyt != null)
120        {
121            aseNyt.Destroy();
122        }
123       
124        switch (ase)
125        {
126            case 1:         
127                aseNyt = Mouse.Listen(MouseButton.Left, ButtonState.Pressed, ammu, "pum pum");
128                break;
129            case 2:
130                aseNyt = Mouse.Listen(MouseButton.Left, ButtonState.Pressed, lyö, "pum pum");
131                break;
132            case 3:
133                aseNyt = Mouse.Listen(MouseButton.Left, ButtonState.Down, block, "pum pum");
134                break;
135        }   
136    }
137    void block()
138    {     
139        Image c = LoadImage("characterovi");
140        player.Shape = Shape.FromImage(c);
141        player.Image = c;
142
143        ammus = new PhysicsObject(10.0, 100.0);
144        ammus.Shape = Shape.Rectangle;
145        ammus.Color = Color.Gray;
146        ammus.Position = player.Position + Vector.FromLengthAndAngle(40, player.Angle);
147        ammus.CollisionIgnoreGroup = 2;
148        ammus.Angle = player.Angle;
149        Add(ammus);
150        ammus.Tag = "ammus";
151        ammus.IsVisible = false;
152       
153        //ammus.Hit(impulssi);
154        ammus.MaximumLifetime = new TimeSpan(20000);
155
156    }
157    void lyö()
158    {
159        Image c = LoadImage("charactersword");
160        player.Shape = Shape.FromImage(c);
161        player.Image = c;
162
163        ammus = new PhysicsObject(100.0, 10.0);
164        ammus.Shape = Shape.Circle;
165        ammus.Color = Color.Gray;
166        ammus.Position = player.Position;
167        ammus.CollisionIgnoreGroup = 1;
168        ammus.Angle = player.Angle;
169        Add(ammus);
170        Vector impulssi = Vector.FromLengthAndAngle(500, player.Angle);
171        ammus.Hit(impulssi);
172        ammus.MaximumLifetime = new TimeSpan(2000000);
173
174    }
175    void ammu()
176    {
177       
178        Image c = LoadImage("characterl96");
179        player.Shape = Shape.FromImage(c);
180        player.Image = c;
181       
182       
183
184        ammus = new PhysicsObject(10.0, 10.0);
185        ammus.Shape = Shape.Circle;
186        ammus.Color = Color.Gold;
187        ammus.Position = player.Position;
188        ammus.CollisionIgnoreGroup = 1;
189        Image d = LoadImage("bullet");
190        ammus.Shape = Shape.FromImage(d);
191        ammus.Image = d;
192        ammus.Angle = player.Angle;
193        ammus.Tag = "ammus";
194        Add(ammus);
195
196        Vector impulssi = Vector.FromLengthAndAngle(2500, player.Angle);
197        ammus.Hit(impulssi);
198        ammus.MaximumLifetime = new TimeSpan(20000000);
199       
200    }
201
202   
203    void luohuone(int huoneNumero)
204    {
205        Window.Width = 1350;
206        Window.Height = 850;
207        Camera.Zoom(0.5);
208        Image b = LoadImage("blackwhitegorund");
209
210       
211       
212       
213        TileMap ruudut = TileMap.FromLevelAsset("mapeasy lol");
214
215       
216        ruudut.SetTileMethod('p', LuoPelaaja);
217        ruudut.SetTileMethod('w', LuoTaso);
218        ruudut.SetTileMethod('h', LuoOvi, Angle.FromDegrees(0));
219        ruudut.SetTileMethod('u', LuoOvi, Angle.FromDegrees(90));
220        ruudut.SetTileMethod('k', LuoOvi, Angle.FromDegrees(180));
221        ruudut.SetTileMethod('m', LuoOvi, Angle.FromDegrees(270));
222        ruudut.SetTileMethod('r', luoAvain);
223       
224        ruudut.SetTileMethod('a', LuoAse96);
225       
226        //3. Execute luo kentän
227        //   Parametreina leveys ja korkeus
228        ruudut.Execute(64, 64);
229
230        TileMap ruudut2 = TileMap.FromLevelAsset("mapeasy lol");
231        ruudut2.SetTileMethod('v', Vihu);
232        ruudut2.Execute(64, 64);
233        Level.Background.Image = b;
234        Level.Background.TileToLevel();
235
236
237        Camera.StayInLevel = true;
238        näppäimet();
239        aseet();
240        avaintenLkm = 0;
241       
242       
243       
244
245    }
246   
247    void AvaintenKerays(PhysicsObject tormaaja, PhysicsObject tormatty)
248    {
249       /*witch(kohde)
250        {
251            case "avain":
252                avaintenLkm += 1;
253                tormatty.Destroy();
254
255                break;
256            default:
257                break;
258        }*/
259
260        avaintenLkm++;
261        tormatty.Destroy();
262           
263    }
264    void LuoTaso(Vector paikka, double leveys, double korkeus)
265    {
266        PhysicsObject taso = PhysicsObject.CreateStaticObject(leveys, korkeus);
267        taso.Position = paikka; 
268        //taso.CollisionIgnoreGroup = 1;
269        taso.Color = Color.Gray;
270        Image a = LoadImage("wall");
271       
272        taso.Image = a;
273        Add(taso);
274    }
275    void luoAvain(Vector paikka, double leveys, double korkeus)
276    {
277        if (RandomGen.NextDouble(0, 100) < 80)
278        {
279            avain = new PhysicsObject(50, 50);
280            Image c = LoadImage("gaykey");
281
282            avain.Image = c;
283            avain.Position = paikka;
284            avain.Tag = "avain";
285            Add(avain);
286        }
287       
288
289
290        //delegate(PhysicsObject a, PhysicsObject b) {kerays(a, b, "avain");}
291    }
292    void OvenAvaus(PhysicsObject tormaaja, PhysicsObject ovi)
293    {
294        if (avaintenLkm >= 1)
295        {
296            ClearAll();
297            luohuone(1);
298
299
300        }
301    }
302    void LuoPelaaja(Vector paikka, double leveys, double korkeus)
303    {
304        player = new PhysicsObject(50, 50);
305        Image c = LoadImage("thecharacter");
306        player.Shape = Shape.FromImage(c);
307        player.Image = c;
308       
309        //player.
310     
311        player.Position = paikka;
312        player.LinearDamping = 0.6;
313        player.AngularDamping = 0.6;
314       
315        player.MaxAngularVelocity = 8000.0;
316        player.Color = Color.Brown;
317        player.Mass = 1;
318        Add(player);
319        AddCollisionHandler(player, "avain", AvaintenKerays);
320        AddCollisionHandler(player, "ovi", OvenAvaus);
321        AddCollisionHandler(player, "ase96", PoimiAse);
322
323        //player.CollisionIgnoreGroup = 1;
324        Camera.Follow(player);
325    }
326    void näppäimet()
327    {
328        //Keyboard.Listen(Key.W, ButtonState.Down, liike, "liiku", Vector.FromLengthAndAngle(1500, player.Angle));
329        //Keyboard.Listen(Key.S, ButtonState.Down, liike, "liiku", Vector.FromLengthAndAngle(-500, player.Angle));
330        //Keyboard.Listen(Key.D, ButtonState.Down, liike, "liiku", Vector.FromLengthAndAngle(-1000, player.Angle + Angle.FromDegrees(90)));
331        //Keyboard.Listen(Key.A, ButtonState.Down, liike, "liiku", Vector.FromLengthAndAngle(1000, player.Angle + Angle.FromDegrees(90)));
332
333        Keyboard.Listen(Key.W, ButtonState.Down, liike, "liiku", new Vector(0, 500));
334        Keyboard.Listen(Key.S, ButtonState.Down, liike, "liiku", new Vector(0, -250));
335        Keyboard.Listen(Key.D, ButtonState.Down, liike, "liiku", new Vector(500, 0));
336        Keyboard.Listen(Key.A, ButtonState.Down, liike, "liiku", new Vector(-500, 0));
337
338        Mouse.IsCursorVisible = true;
339        Mouse.ListenMovement(0.0, kaanny, "");
340       
341        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
342    }
343    void kaanny(AnalogState hiirenLiike)
344    {
345        Vector suunta = Mouse.PositionOnWorld - player.AbsolutePosition;
346
347        if (suunta.Magnitude != 0)
348        {
349            suunta.Normalize();
350            player.Angle = suunta.Angle;
351        }
352    }
353    void liike(Vector vektori)
354    {
355        player.Move((player.Angle + vektori.Angle + Angle.FromDegrees(-90)).GetVector() * vektori.Magnitude);
356    }
357
358    void LuoOvi(Vector paikka, double leveys, double korkeus, Angle Kulma)
359    {
360        PhysicsObject ovi = PhysicsObject.CreateStaticObject(leveys, korkeus);
361        ovi.Position = paikka;
362        Image c = LoadImage("door");
363       
364        ovi.Image = c;
365        ovi.Tag = "ovi";
366       
367
368       
369        Add(ovi);
370
371    }
372
373}
Note: See TracBrowser for help on using the repository browser.