source: 2010/24/vihoskon/vantaa 2001 mappi testi/Peli.cs @ 10337

Revision 912, 6.6 KB checked in by vihoskon, 13 years ago (diff)
Line 
1using System;
2using Jypeli;
3using Jypeli.ScreenObjects;
4using Jypeli.Assets;
5
6public class Peli : PhysicsGame
7{
8   PlatformCharacter Player1;
9   PlatformCharacter Player2;
10   IntMeter LifeP1;
11   IntMeter LifeP2;
12   ValueDisplay LifeMeterP1;
13   ValueDisplay LifeMeterP2;
14
15   const double Speed = 200;
16   const double JumpStr = 2000;
17   const int TileWidth = 50;
18   const int TileHeight = 50;
19
20   Vector RespawnCoord;
21
22   protected override void Begin()
23   {
24       SetWindowSize(1024, 720, false);
25
26       LifeP1 = new IntMeter( 3 ); // Pelaajan 1 osumat
27       LifeP2 = new IntMeter( 3 ); // Pelaajan 2 osumat
28       LifeMeterP1 = new ValueDisplay();
29       LifeMeterP2 = new ValueDisplay();
30       LifeMeterP1.X = Screen.Left + 100;
31       LifeMeterP1.Y = Screen.Top - 100;
32       LifeMeterP2.X = Screen.Right - 500;
33       LifeMeterP2.Y = Screen.Top - 100;
34       LifeMeterP1.BindTo(LifeP1);
35       LifeMeterP2.BindTo(LifeP2);
36
37       StartCoop();
38   }
39
40   void ShowMenu()
41   {
42
43   }
44
45
46
47   void StartGame()
48   {
49       Gravity = new Vector(0, -1000);
50       CreateLevel();
51       CreatePlayer();
52       Camera.Follow(Player1);
53
54       Controls();
55   }
56
57   void StartCoop()
58   {
59       Gravity = new Vector(0, -1000);
60       CreateLevel();
61       Add(LifeMeterP1);
62       Add(LifeMeterP2);
63       Camera.Follow(Player1);
64
65       Controls();
66   }
67
68
69
70   PlatformCharacter CreatePlayer()
71   {
72       PlatformCharacter Player = new PlatformCharacter(40, 60);
73       Player.Mass = 4.0;
74       Add(Player);
75       Player.Color = Color.Black;
76       Player.KineticFriction = 1.0;
77       Player.StaticFriction = 0;
78       return Player;
79
80   }
81
82   PhysicsObject CreateEnemy()
83   {
84       PhysicsObject Enemy = new PhysicsObject(40, 65);
85       Enemy.Mass = 3.0;
86       Enemy.Color = Color.Red;
87       Enemy.KineticFriction = 1;
88       Enemy.StaticFriction = 1;
89       AddCollisionHandler(Enemy, EnemyPlayerCollision);
90       Enemy.CanRotate = false;
91       Add(Enemy);
92       return Enemy;
93
94   }
95
96   void CreateLevel()
97   {
98
99       TileMap ruudut = TileMap.FromFile("kentta.txt");
100       ruudut['x'] = LuoPalikka;
101       ruudut['z'] = LuoSeina;
102       ruudut['c'] = LuoKatto;
103       ruudut['v'] = luokatonalus;
104       ruudut['f'] = CreateFloor;
105       ruudut['r'] = CreateRespawn;
106       ruudut['e'] = CreateEnemy;
107       ruudut['1'] = StartP1;
108       ruudut['2'] = StartP2;
109       ruudut.Insert(TileWidth, TileHeight);
110   }
111    //troloolololo
112
113
114   void Controls()
115   {
116       Keyboard.Listen(Key.A, ButtonState.Down, Walk, "Move left",
117Player1, -Speed);
118       Keyboard.Listen(Key.D, ButtonState.Down, Walk, "Move right",
119Player1, Speed);
120       Keyboard.Listen(Key.W, ButtonState.Down, Jump, "Jump",
121Player1, JumpStr);
122
123       Keyboard.Listen(Key.Left, ButtonState.Down, Walk, "Move left",
124Player2, -Speed);
125       Keyboard.Listen(Key.Right, ButtonState.Down, Walk, "Move right", Player2, Speed);
126       Keyboard.Listen(Key.Up, ButtonState.Down, Jump, "Jump",
127Player2, JumpStr);
128   }
129
130   void Walk(PlatformCharacter Character, double Speed)
131   {
132       Character.Walk(Speed);
133   }
134
135   void Jump(PlatformCharacter Character, double JumpStr)
136   {
137       Character.Jump(JumpStr);
138   }
139
140
141
142   PhysicsObject CreateFloor()
143   {
144       PhysicsObject Floor = PhysicsObject.CreateStaticObject(50.0, 50.0);
145       return Floor;
146   }
147
148   PhysicsObject CreateRespawn()
149   {
150       PhysicsObject Respawn = PhysicsObject.CreateStaticObject(50.0, 100.0);
151       Respawn.IgnoresCollisionResponse = true;
152       Respawn.IsVisible = false;
153       AddCollisionHandler(Respawn, RespawnCollision);
154       return Respawn;
155   }
156
157   void EnemyPlayerCollision(PhysicsObject Enemy, PhysicsObject Target)
158   {
159       if (Target is PlatformCharacter)
160       {
161           // Tarkista onko elämiä jäljellä
162           // Aloita kuolemattomuusjakso esim. 5 sekuntia (tarvitaan ajastin)
163           if (Target == Player1)
164           {
165               LifeP1.Value--;
166               Death(Player1);
167               if (Player2.IsDestroyed() == false)
168               {
169                   Camera.Follow(Player2);
170               }
171
172           }
173           else if (Target == Player2)
174           {
175               LifeP2.Value--;
176               Death(Player2);
177           }
178         }
179     }
180
181
182   void Death(PlatformCharacter Player)
183   {
184       Player.Destroy();
185       if ((Player1.IsDestroyed()) && (Player2.IsDestroyed()))
186       {
187           RespawnPlayers(RespawnCoord.X, RespawnCoord.Y+10);
188       }
189
190   }
191
192   void RespawnPlayers(double x, double y)
193   {
194       if(LifeP1 > 0)
195       {
196           Player1 = CreatePlayer();
197           Player1.X = x+30;
198           Player1.Y = y;
199           Camera.Follow(Player1);
200       }
201       if (LifeP2 > 0)
202       {
203           Player2 = CreatePlayer();
204           Player2.X = x - 30;
205           Player2.Y = y;
206           if (LifeP1 == 0)
207           {
208               Camera.Follow(Player2);
209           }
210           Controls();
211       }
212   }
213
214   void RespawnCollision(PhysicsObject RespawnLoc, PhysicsObject Player)
215   {
216       if ((Player == Player1) || (Player == Player2))
217       {
218           RespawnCoord = RespawnLoc.Position;
219       }
220   }
221
222   PhysicsObject StartP1()
223   {
224       Player1 = CreatePlayer();
225       return Player1;
226   }
227
228   PhysicsObject StartP2()
229   {
230       Player2 = CreatePlayer();
231       return Player2;
232   }
233
234    PhysicsObject LuoPalikka()
235    {
236        PhysicsObject palikka = PhysicsObject.CreateStaticObject(50.0, 50.0);
237        palikka.Shape = Shapes.Rectangle;
238        palikka.Color = Color.Gray;
239        palikka.Image = LoadImage("x-tekstuuri");
240       
241        return palikka;
242    }
243
244    PhysicsObject LuoSeina()
245    {
246        PhysicsObject seina = PhysicsObject.CreateStaticObject(50.0, 50.0);
247        seina.Shape = Shapes.Rectangle;
248        seina.Color = Color.Gray;
249        seina.Image = LoadImage("z-tekstuuri");
250
251        return seina;
252    }
253    PhysicsObject LuoKatto()
254    {
255        PhysicsObject katto = PhysicsObject.CreateStaticObject(50.0, 50.0);
256        katto.Shape = Shapes.Rectangle;
257        katto.Color = Color.Gray;
258
259        katto.Image = LoadImage("c-tekstuuri");
260       
261
262
263        return katto;
264    }
265    PhysicsObject luokatonalus()
266    {
267        PhysicsObject katonalus = PhysicsObject.CreateStaticObject(50.0, 50.0);
268        katonalus.Shape = Shapes.Rectangle;
269        katonalus.Color = Color.Gray;
270
271        katonalus.Image = LoadImage("v-tekstuuri");
272
273
274
275        return katonalus;
276    }
277
278
279
280}
281
282
Note: See TracBrowser for help on using the repository browser.