1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using Jypeli; |
---|
4 | using Jypeli.Assets; |
---|
5 | using Jypeli.Controls; |
---|
6 | using Jypeli.Effects; |
---|
7 | using Jypeli.Widgets; |
---|
8 | |
---|
9 | public class Alus : PhysicsObject |
---|
10 | { |
---|
11 | private IntMeter pommiLaskuri = new IntMeter(0, 0, 100); |
---|
12 | public IntMeter PommiLaskuri { get { return pommiLaskuri; } } |
---|
13 | |
---|
14 | private IntMeter ammoLaskuri = new IntMeter(0, 0, 60); |
---|
15 | public IntMeter AmmoLaskuri { get { return ammoLaskuri; } } |
---|
16 | |
---|
17 | private DoubleMeter elamaLaskuri = new DoubleMeter(100, 0, 100); |
---|
18 | public DoubleMeter ElamaLaskuri { get { return elamaLaskuri; } } |
---|
19 | |
---|
20 | private AssaultRifle ase; |
---|
21 | public AssaultRifle Ase { get { return ase; } } |
---|
22 | |
---|
23 | public Alus(double leveys, double korkeus) |
---|
24 | : base(leveys, korkeus) |
---|
25 | { |
---|
26 | ase = new AssaultRifle(60, 60); |
---|
27 | ase.IsVisible = false; |
---|
28 | ase.ProjectileCollision = PanosOsui; |
---|
29 | Add(ase); |
---|
30 | } |
---|
31 | |
---|
32 | void PanosOsui(PhysicsObject panos, PhysicsObject kohde) |
---|
33 | { |
---|
34 | panos.Destroy(); |
---|
35 | if (kohde.Tag == "alus") |
---|
36 | { |
---|
37 | Alus alus = (Alus)kohde; |
---|
38 | alus.elamaLaskuri.Value -= 4; |
---|
39 | |
---|
40 | } |
---|
41 | } |
---|
42 | |
---|
43 | } |
---|
44 | |
---|
45 | public class Turborace : PhysicsGame |
---|
46 | { |
---|
47 | Vector[] reittiPisteet = new Vector[] |
---|
48 | { |
---|
49 | new Vector( -700.0, 200.0 ), |
---|
50 | new Vector( -500.0, 300.0 ), |
---|
51 | new Vector( 100.0, 320.0 ), |
---|
52 | new Vector( 500.0, 220.0 ), |
---|
53 | new Vector( 600.0, 170.0 ), |
---|
54 | new Vector( 700.0, -100.0 ), |
---|
55 | new Vector( 600.0, -300.0 ), |
---|
56 | new Vector( 400.0, -430.0 ), |
---|
57 | new Vector( 100.0, -400.0 ), |
---|
58 | new Vector( -300.0, -300.0 ), |
---|
59 | new Vector( -700.0, -250.0 ), |
---|
60 | new Vector( -900.0, 0.0 ), |
---|
61 | new Vector( -670.0, 210.0 ), |
---|
62 | }; |
---|
63 | |
---|
64 | |
---|
65 | |
---|
66 | Image aluskuva1 = LoadImage("Alus1"); |
---|
67 | Image aluskuva2 = LoadImage("Alus2"); |
---|
68 | Image aluskuva3 = LoadImage("Alus3"); |
---|
69 | Image aluskuva4 = LoadImage("Alus4"); |
---|
70 | Image boostikuva = LoadImage("Boosti"); |
---|
71 | Image pommikuplakuva = LoadImage("PommiKupla"); |
---|
72 | Image pommikuva = LoadImage("Pommi"); |
---|
73 | Image laserkuplakuva = LoadImage("LaserKupla"); |
---|
74 | Image laserkuva = LoadImage("Laser"); |
---|
75 | |
---|
76 | Alus alus1, alus2, alus3, alus4; |
---|
77 | ProgressBar elama1, elama2, elama3, elama4; |
---|
78 | |
---|
79 | public override void Begin() |
---|
80 | { |
---|
81 | IsFullScreen = true; |
---|
82 | Level.Height = 1000; |
---|
83 | Level.Width = 2000; |
---|
84 | PhysicsObject vasenReuna = Level.CreateLeftBorder(); |
---|
85 | vasenReuna.Restitution = 1.0; |
---|
86 | vasenReuna.KineticFriction = 0.0; |
---|
87 | vasenReuna.IsVisible = false; |
---|
88 | |
---|
89 | PhysicsObject oikeaReuna = Level.CreateRightBorder(); |
---|
90 | oikeaReuna.Restitution = 1.0; |
---|
91 | oikeaReuna.KineticFriction = 0.0; |
---|
92 | oikeaReuna.IsVisible = false; |
---|
93 | |
---|
94 | PhysicsObject ylaReuna = Level.CreateTopBorder(); |
---|
95 | ylaReuna.Restitution = 1.0; |
---|
96 | ylaReuna.KineticFriction = 0.0; |
---|
97 | ylaReuna.IsVisible = false; |
---|
98 | |
---|
99 | PhysicsObject alaReuna = Level.CreateBottomBorder(); |
---|
100 | alaReuna.Restitution = 1.0; |
---|
101 | alaReuna.IsVisible = false; |
---|
102 | alaReuna.KineticFriction = 0.0; |
---|
103 | |
---|
104 | LuoRata(); |
---|
105 | |
---|
106 | Timer ajastin = new Timer(); |
---|
107 | ajastin.Interval = 2.3; |
---|
108 | ajastin.Timeout += delegate { |
---|
109 | int i = 1 + RandomGen.NextInt(reittiPisteet.Length - 1); |
---|
110 | luoBoosti(reittiPisteet[i], reittiPisteet[i - 1]); |
---|
111 | }; |
---|
112 | ajastin.Start(); |
---|
113 | |
---|
114 | Timer ajastin2 = new Timer(); |
---|
115 | ajastin2.Interval = 5; |
---|
116 | ajastin2.Timeout += delegate |
---|
117 | { |
---|
118 | int i = 1 + RandomGen.NextInt(reittiPisteet.Length - 1); |
---|
119 | luoPommiKupla(reittiPisteet[i], reittiPisteet[i - 1]); |
---|
120 | }; |
---|
121 | ajastin2.Start(); |
---|
122 | |
---|
123 | Timer ajastin3 = new Timer(); |
---|
124 | ajastin3.Interval = 4.2; |
---|
125 | ajastin3.Timeout += delegate |
---|
126 | { |
---|
127 | int i = 1 + RandomGen.NextInt(reittiPisteet.Length - 1); |
---|
128 | luoLaserKupla(reittiPisteet[i], reittiPisteet[i - 1]); |
---|
129 | }; |
---|
130 | ajastin3.Start(); |
---|
131 | |
---|
132 | // TODO: Kirjoita ohjelmakoodisi tähän |
---|
133 | alus1 = luoAlus(aluskuva1, -150); |
---|
134 | alus2 = luoAlus(aluskuva2, -100); |
---|
135 | alus3 = luoAlus(aluskuva3, -50); |
---|
136 | alus4 = luoAlus(aluskuva4, 0); |
---|
137 | |
---|
138 | |
---|
139 | PhoneBackButton.Listen(ConfirmExit, "Lopeta peli"); |
---|
140 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli"); |
---|
141 | Keyboard.Listen(Key.Up, ButtonState.Down, LiikutaAlusta, null, alus1, 1000.0); |
---|
142 | Keyboard.Listen(Key.Left, ButtonState.Down, KaannaAlusta, null, alus1, 5.0); |
---|
143 | Keyboard.Listen(Key.Right, ButtonState.Down, KaannaAlusta, null, alus1, -5.0); |
---|
144 | Keyboard.Listen(Key.Down, ButtonState.Down, LiikutaAlusta, null, alus1, -400.0); |
---|
145 | Keyboard.Listen(Key.K, ButtonState.Pressed, TiputaPommi, null, alus1); |
---|
146 | Keyboard.Listen(Key.J, ButtonState.Down, LiikutaAlusta2, null, alus1, 90.0); |
---|
147 | Keyboard.Listen(Key.L, ButtonState.Down, LiikutaAlusta2, null, alus1, -90.0); |
---|
148 | Keyboard.Listen(Key.I, ButtonState.Down, AmmuLaser, null, alus1); |
---|
149 | |
---|
150 | Keyboard.Listen(Key.W, ButtonState.Down, LiikutaAlusta, null, alus2, 1000.0); |
---|
151 | Keyboard.Listen(Key.A, ButtonState.Down, KaannaAlusta, null, alus2, 5.0); |
---|
152 | Keyboard.Listen(Key.D, ButtonState.Down, KaannaAlusta, null, alus2, -5.0); |
---|
153 | Keyboard.Listen(Key.S, ButtonState.Down, LiikutaAlusta, null, alus2, -400.0); |
---|
154 | Keyboard.Listen(Key.G, ButtonState.Pressed, TiputaPommi, null, alus2); |
---|
155 | Keyboard.Listen(Key.F, ButtonState.Down, LiikutaAlusta2, null, alus2, 90.0); |
---|
156 | Keyboard.Listen(Key.H, ButtonState.Down, LiikutaAlusta2, null, alus2, -90.0); |
---|
157 | Keyboard.Listen(Key.T, ButtonState.Down, AmmuLaser, null, alus2); |
---|
158 | |
---|
159 | ControllerOne.Listen(Button.A, ButtonState.Down, LiikutaAlusta, null, alus3, 1000.0); |
---|
160 | ControllerOne.Listen(Button.DPadLeft, ButtonState.Down, KaannaAlusta, null, alus3, 5.0); |
---|
161 | ControllerOne.Listen(Button.DPadRight, ButtonState.Down, KaannaAlusta, null, alus3, -5.0); |
---|
162 | ControllerOne.Listen(Button.X, ButtonState.Down, LiikutaAlusta, null, alus3, -400.0); |
---|
163 | ControllerOne.Listen(Button.LeftTrigger, ButtonState.Down, LiikutaAlusta2, null, alus3, 90.0); |
---|
164 | ControllerOne.Listen(Button.RightTrigger, ButtonState.Down, LiikutaAlusta2, null, alus3, -90.0); |
---|
165 | ControllerOne.Listen(Button.B, ButtonState.Down, TiputaPommi, null, alus3); |
---|
166 | ControllerOne.Listen(Button.RightShoulder, ButtonState.Down, AmmuLaser, null, alus3); |
---|
167 | |
---|
168 | ControllerTwo.Listen(Button.A, ButtonState.Down, LiikutaAlusta, null, alus4, 1000.0); |
---|
169 | ControllerTwo.Listen(Button.DPadLeft, ButtonState.Down, KaannaAlusta, null, alus4, 5.0); |
---|
170 | ControllerTwo.Listen(Button.DPadRight, ButtonState.Down, KaannaAlusta, null, alus4, -5.0); |
---|
171 | ControllerTwo.Listen(Button.X, ButtonState.Down, LiikutaAlusta, null, alus4, -400.0); |
---|
172 | ControllerTwo.Listen(Button.LeftTrigger, ButtonState.Down, LiikutaAlusta2, null, alus4, 90.0); |
---|
173 | ControllerTwo.Listen(Button.RightTrigger, ButtonState.Down, LiikutaAlusta2, null, alus4, -90.0); |
---|
174 | ControllerTwo.Listen(Button.B, ButtonState.Down, TiputaPommi, null, alus4); |
---|
175 | ControllerTwo.Listen(Button.RightShoulder, ButtonState.Down, AmmuLaser, null, alus4); |
---|
176 | |
---|
177 | elama1 = new ProgressBar(50, 5); |
---|
178 | elama1.Position = alus1.Position + new Vector(0, 30); |
---|
179 | elama1.BindTo(alus1.ElamaLaskuri); |
---|
180 | elama1.BorderColor = Color.Black; |
---|
181 | Add(elama1); |
---|
182 | |
---|
183 | elama2 = new ProgressBar(50, 5); |
---|
184 | elama2.Position = alus2.Position + new Vector(0, 30); |
---|
185 | elama2.BindTo(alus2.ElamaLaskuri); |
---|
186 | elama2.BorderColor = Color.Black; |
---|
187 | Add(elama2); |
---|
188 | |
---|
189 | elama3 = new ProgressBar(50, 5); |
---|
190 | elama3.Position = alus3.Position + new Vector(0, 30); |
---|
191 | elama3.BindTo(alus3.ElamaLaskuri); |
---|
192 | elama3.BorderColor = Color.Black; |
---|
193 | Add(elama3); |
---|
194 | |
---|
195 | elama4 = new ProgressBar(50, 5); |
---|
196 | elama4.Position = alus4.Position + new Vector(0, 30); |
---|
197 | elama4.BindTo(alus4.ElamaLaskuri); |
---|
198 | elama4.BorderColor = Color.Black; |
---|
199 | Add(elama4); |
---|
200 | |
---|
201 | Timer palkkilaskuri = new Timer(); |
---|
202 | palkkilaskuri.Interval = 0.01; |
---|
203 | palkkilaskuri.Timeout += delegate |
---|
204 | { |
---|
205 | elama1.Position = alus1.Position + new Vector(0, 30); |
---|
206 | elama2.Position = alus2.Position + new Vector(0, 30); |
---|
207 | elama3.Position = alus3.Position + new Vector(0, 30); |
---|
208 | elama4.Position = alus4.Position + new Vector(0, 30); |
---|
209 | }; |
---|
210 | palkkilaskuri.Start(); |
---|
211 | |
---|
212 | |
---|
213 | |
---|
214 | } |
---|
215 | |
---|
216 | public void AmmuLaser(Alus alus) |
---|
217 | { |
---|
218 | PhysicsObject panos = alus.Ase.Shoot(); |
---|
219 | //panos.Image = laserkuva; |
---|
220 | } |
---|
221 | |
---|
222 | public void TiputaPommi(PhysicsObject alus) |
---|
223 | { |
---|
224 | Alus pelaaja = (Alus)alus; |
---|
225 | if (pelaaja.PommiLaskuri.Value > 0) |
---|
226 | { |
---|
227 | PhysicsObject pommi = new PhysicsObject(25, 25); |
---|
228 | pommi.Image = pommikuva; |
---|
229 | pommi.Position = alus.Position + alus.AbsoluteAngle.GetVector().Normalize() * -70; |
---|
230 | pommi.AbsoluteAngle = alus.AbsoluteAngle + Angle.FromDegrees(90); |
---|
231 | AddCollisionHandler(pommi, "alus", pommiRajahtaa); |
---|
232 | PomminPudotusPois(pelaaja); |
---|
233 | Add(pommi); |
---|
234 | pelaaja.PommiLaskuri.Value--; |
---|
235 | pommi.IgnoresExplosions = true; |
---|
236 | } |
---|
237 | } |
---|
238 | |
---|
239 | public void pommiRajahtaa(PhysicsObject pommi, PhysicsObject alus) |
---|
240 | { |
---|
241 | Explosion rajahdys = new Explosion(100); |
---|
242 | rajahdys.Position = pommi.Position; |
---|
243 | rajahdys.AddShockwaveHandler("alus", PommiOsuu); |
---|
244 | pommi.Destroy(); |
---|
245 | Add(rajahdys); |
---|
246 | |
---|
247 | } |
---|
248 | |
---|
249 | public void PommiOsuu(IPhysicsObject olio, Vector shokki) |
---|
250 | { |
---|
251 | Alus alus = (Alus)olio; |
---|
252 | alus.ElamaLaskuri.Value -= 20; |
---|
253 | } |
---|
254 | |
---|
255 | public void LiikutaAlusta(PhysicsObject alus, double nopeus) |
---|
256 | { |
---|
257 | Vector aluksenSuunta = Vector.FromLengthAndAngle(nopeus, alus.Angle); |
---|
258 | alus.Push(aluksenSuunta); |
---|
259 | } |
---|
260 | |
---|
261 | public void KaannaAlusta(PhysicsObject alus, double nopeus) |
---|
262 | { |
---|
263 | Angle kulma = new Angle(); |
---|
264 | kulma.Degrees = nopeus; |
---|
265 | alus.Angle += kulma; |
---|
266 | } |
---|
267 | |
---|
268 | Alus luoAlus(Image kuva, double poikkeama) |
---|
269 | { |
---|
270 | Alus alus = new Alus(60, 40); |
---|
271 | alus.Shape = Shape.Rectangle; |
---|
272 | alus.Image = kuva; |
---|
273 | Add(alus); |
---|
274 | alus.Restitution = 0.2; |
---|
275 | alus.AngularDamping = 0.5; |
---|
276 | alus.LinearDamping = 0.97; |
---|
277 | alus.Tag = "alus"; |
---|
278 | |
---|
279 | alus.AbsoluteAngle = (reittiPisteet[1] - reittiPisteet[0]).Angle; |
---|
280 | alus.Position = reittiPisteet[0] + (alus.AbsoluteAngle + Angle.FromDegrees(90)).GetVector() * poikkeama; |
---|
281 | |
---|
282 | AddCollisionHandler(alus, "boosti", Boosti); |
---|
283 | AddCollisionHandler(alus, "PommiKupla", KeraaPommi); |
---|
284 | AddCollisionHandler(alus, "LaserKupla", KeraaLaser); |
---|
285 | |
---|
286 | /*Vector pelaajanSuunta = Vector.FromLengthAndAngle(10000.0, alus.Angle); |
---|
287 | alus.Push(pelaajanSuunta); |
---|
288 | alus.AngularVelocity = 5.0;*/ |
---|
289 | |
---|
290 | return alus; |
---|
291 | } |
---|
292 | |
---|
293 | public void KeraaLaser(PhysicsObject alus, PhysicsObject laserkupla) |
---|
294 | { |
---|
295 | ((Alus)alus).AmmoLaskuri.Value += 30; |
---|
296 | laserkupla.Destroy(); |
---|
297 | } |
---|
298 | |
---|
299 | public void KeraaPommi(PhysicsObject alus, PhysicsObject pommikupla) |
---|
300 | { |
---|
301 | ((Alus)alus).PommiLaskuri.Value++; |
---|
302 | pommikupla.IgnoresExplosions = true; |
---|
303 | pommikupla.Destroy(); |
---|
304 | } |
---|
305 | |
---|
306 | public void Boosti(PhysicsObject alus, PhysicsObject boosti) |
---|
307 | { |
---|
308 | //alus.Velocity *= 2; |
---|
309 | alus.Hit(alus.AbsoluteAngle.GetVector() * 500); |
---|
310 | boosti.Destroy(); |
---|
311 | } |
---|
312 | |
---|
313 | public void LiikutaAlusta2(PhysicsObject alus, double kulma) |
---|
314 | { |
---|
315 | Vector suunta = (alus.Angle+Angle.FromDegrees(kulma)).GetVector()*10; |
---|
316 | |
---|
317 | alus.Hit (suunta); |
---|
318 | } |
---|
319 | |
---|
320 | void LuoRata() |
---|
321 | { |
---|
322 | //RoadMap tie = new RoadMap(reittiPisteet); |
---|
323 | //tie.DefaultWidth = 210.0; |
---|
324 | //tie.CreateSegmentFunction = LuoKaiteenPatka; |
---|
325 | //tie.Insert(); |
---|
326 | |
---|
327 | Vector[] ulkoKaidePisteet = new Vector[reittiPisteet.Length]; |
---|
328 | for (int i = 0; i < reittiPisteet.Length; i++) |
---|
329 | { |
---|
330 | ulkoKaidePisteet[i] = reittiPisteet[i] + reittiPisteet[i].Normalize() * 100; |
---|
331 | } |
---|
332 | |
---|
333 | RoadMap ulkoKaide = new RoadMap(ulkoKaidePisteet); |
---|
334 | ulkoKaide.DefaultWidth = 20.0; |
---|
335 | ulkoKaide.CreateSegmentFunction = LuoKaiteenPatka; |
---|
336 | ulkoKaide.Insert(); |
---|
337 | |
---|
338 | Vector[] sisäKaidePisteet = new Vector[reittiPisteet.Length]; |
---|
339 | for (int i = 0; i < reittiPisteet.Length; i++) |
---|
340 | { |
---|
341 | sisäKaidePisteet[i] = reittiPisteet[i] - reittiPisteet[i].Normalize() * 250; |
---|
342 | } |
---|
343 | |
---|
344 | RoadMap sisaKaide = new RoadMap(sisäKaidePisteet); |
---|
345 | sisaKaide.DefaultWidth = 20.0; |
---|
346 | sisaKaide.CreateSegmentFunction = LuoKaiteenPatka; |
---|
347 | Level.Background.Color = Color.Gray; |
---|
348 | sisaKaide.Insert(); |
---|
349 | } |
---|
350 | |
---|
351 | PhysicsObject LuoKaiteenPatka(double width, double height, Shape shape) |
---|
352 | { |
---|
353 | PhysicsObject patka = PhysicsObject.CreateStaticObject(width, height, shape); |
---|
354 | patka.Color = Color.Black; |
---|
355 | Add(patka); |
---|
356 | return patka; |
---|
357 | } |
---|
358 | |
---|
359 | void luoBoosti(Vector paikka, Vector edellinenPiste) |
---|
360 | { |
---|
361 | PhysicsObject boosti = new PhysicsObject(50, 50); |
---|
362 | boosti.Image = boostikuva; |
---|
363 | boosti.Position = paikka - paikka.Normalize() * 100; |
---|
364 | boosti.IgnoresCollisionResponse = true; |
---|
365 | boosti.Tag = "boosti"; |
---|
366 | boosti.IgnoresExplosions = true; |
---|
367 | Add(boosti); |
---|
368 | |
---|
369 | Vector suunta = paikka - edellinenPiste; |
---|
370 | boosti.AbsoluteAngle = suunta.Angle - Angle.FromDegrees(90); |
---|
371 | |
---|
372 | boosti.LifetimeLeft = TimeSpan.FromSeconds(6); |
---|
373 | } |
---|
374 | |
---|
375 | void luoPommiKupla(Vector paikka, Vector edellinenPiste) |
---|
376 | { |
---|
377 | PhysicsObject pommiKupla = new PhysicsObject(50, 50); |
---|
378 | pommiKupla.Image = pommikuplakuva; |
---|
379 | pommiKupla.Position = paikka - paikka.Normalize() * 100; |
---|
380 | pommiKupla.IgnoresCollisionResponse = true; |
---|
381 | pommiKupla.IgnoresExplosions = true; |
---|
382 | pommiKupla.Tag = "PommiKupla"; |
---|
383 | Add(pommiKupla); |
---|
384 | pommiKupla.LifetimeLeft = TimeSpan.FromSeconds(6); |
---|
385 | } |
---|
386 | |
---|
387 | void PomminPudotusPois(Alus alus) |
---|
388 | { |
---|
389 | if (alus == alus1) |
---|
390 | { |
---|
391 | Keyboard.Disable(Key.K); |
---|
392 | Timer.SingleShot(2.5, delegate { Keyboard.Enable(Key.K); }); |
---|
393 | } |
---|
394 | |
---|
395 | if (alus == alus2) |
---|
396 | { |
---|
397 | Keyboard.Disable(Key.G); |
---|
398 | Timer.SingleShot(2.5, delegate { Keyboard.Enable(Key.G); }); |
---|
399 | } |
---|
400 | |
---|
401 | if (alus == alus3) |
---|
402 | { |
---|
403 | ControllerOne.Disable(Button.B); |
---|
404 | Timer.SingleShot(2.5, delegate { ControllerOne.Enable(Button.B); }); |
---|
405 | } |
---|
406 | |
---|
407 | if (alus == alus4) |
---|
408 | { |
---|
409 | ControllerTwo.Disable(Button.B); |
---|
410 | Timer.SingleShot(2.5, delegate { ControllerTwo.Enable(Button.B); }); |
---|
411 | } |
---|
412 | } |
---|
413 | |
---|
414 | void luoLaserKupla(Vector paikka, Vector edellinenPiste) |
---|
415 | { |
---|
416 | PhysicsObject laserKupla = new PhysicsObject(50, 50); |
---|
417 | laserKupla.Image = laserkuplakuva; |
---|
418 | laserKupla.Position = paikka - paikka.Normalize() * 100; |
---|
419 | laserKupla.IgnoresCollisionResponse = true; |
---|
420 | laserKupla.IgnoresExplosions = true; |
---|
421 | laserKupla.Tag = "LaserKupla"; |
---|
422 | laserKupla.LifetimeLeft = TimeSpan.FromSeconds(6); |
---|
423 | Add(laserKupla); |
---|
424 | |
---|
425 | } |
---|
426 | |
---|
427 | } |
---|