1 | using System; |
---|
2 | using Jypeli; |
---|
3 | using Jypeli.ScreenObjects; |
---|
4 | using Jypeli.Assets; |
---|
5 | using System.Collections.Generic; |
---|
6 | |
---|
7 | |
---|
8 | namespace ShootEmUp |
---|
9 | { |
---|
10 | public class Peli : PhysicsGame |
---|
11 | { |
---|
12 | #region Julistukset |
---|
13 | |
---|
14 | |
---|
15 | //Liikkumisnopeudet |
---|
16 | private const double NOPEUS = 300; |
---|
17 | private Vector nopeusPysty = new Vector(0,NOPEUS); |
---|
18 | private Vector nopeusVaaka = new Vector(NOPEUS, 0); |
---|
19 | |
---|
20 | //Kentän rakenne |
---|
21 | private Alus pelaajaAlus; |
---|
22 | private PhysicsObject vasenLaita; |
---|
23 | private PhysicsObject oikeaLaita; |
---|
24 | private PhysicsObject pohja; |
---|
25 | private PhysicsObject katto; |
---|
26 | private PhysicsObject exitZone; |
---|
27 | |
---|
28 | //Viholliset |
---|
29 | private const int VIHOLLISKUVIA = 3; |
---|
30 | |
---|
31 | //Statistiikka |
---|
32 | private int kentannro = 0; |
---|
33 | private IntMeter score = new IntMeter(0); |
---|
34 | private Weapon[] weapons; |
---|
35 | |
---|
36 | #endregion |
---|
37 | |
---|
38 | protected override void Begin() |
---|
39 | { |
---|
40 | LataaKentta(kentannro); |
---|
41 | } |
---|
42 | |
---|
43 | #region Kentän lataaminen |
---|
44 | |
---|
45 | /// <summary> |
---|
46 | /// Tulee lataamaan numeroa vastaavan kentän |
---|
47 | /// Kentän tiedot siis tallennettaisiin level1.txt, level2.txt... |
---|
48 | /// </summary> |
---|
49 | /// <param name="kentanNumero">Ei vielä käytössä</param> |
---|
50 | void LataaKentta(int kentanNumero) |
---|
51 | { |
---|
52 | ClearAll(); |
---|
53 | Level.Width = 600; |
---|
54 | Level.Height = 800; |
---|
55 | |
---|
56 | Camera.Move(new Vector(250, 0)); |
---|
57 | |
---|
58 | //Laidat |
---|
59 | LisaData tieto; |
---|
60 | tieto.Tyyppi = "seina"; |
---|
61 | tieto.Rajahtaa = true; |
---|
62 | tieto.Damage = 5; |
---|
63 | vasenLaita = Level.CreateLeftBorder(); |
---|
64 | vasenLaita.Tag = tieto; |
---|
65 | oikeaLaita = Level.CreateRightBorder(); |
---|
66 | oikeaLaita.Tag = tieto; |
---|
67 | pohja = Level.CreateBottomBorder(); |
---|
68 | pohja.Tag = tieto; |
---|
69 | katto = Level.CreateTopBorder(); |
---|
70 | katto.Tag = tieto; |
---|
71 | |
---|
72 | //Exit-alueen tekeminen. |
---|
73 | exitZone = PhysicsObject.CreateStaticObject(Level.Width, 200); |
---|
74 | exitZone.Position = new Vector(Level.Center.X, Level.Bottom - exitZone.Size.Y / 2 - 5); |
---|
75 | exitZone.Tag = tieto; |
---|
76 | Add(exitZone); |
---|
77 | |
---|
78 | LuoPelaaja(); |
---|
79 | LataaKontrollit(pelaajaAlus); |
---|
80 | //LataaTausta(kentanNumero); |
---|
81 | LataaViholliset(20, 4,10); |
---|
82 | LataaHUD(); |
---|
83 | } |
---|
84 | |
---|
85 | /// <summary> |
---|
86 | /// Luo näytölle tarvittavat mittarit |
---|
87 | /// </summary> |
---|
88 | void LataaHUD() |
---|
89 | { |
---|
90 | ValueDisplay hpNaytto = new ValueDisplay(); |
---|
91 | hpNaytto.BindTo(pelaajaAlus.HpMeter); |
---|
92 | Add(hpNaytto); |
---|
93 | hpNaytto.Position = new Vector(Level.Right + 40, Level.Top - 80); |
---|
94 | hpNaytto.Text = "HP: "; |
---|
95 | |
---|
96 | ValueDisplay scoreNaytto = new ValueDisplay(); |
---|
97 | scoreNaytto.BindTo(score); |
---|
98 | Add(scoreNaytto); |
---|
99 | scoreNaytto.Position = new Vector(Level.Right + 40, Level.Top - 100); |
---|
100 | scoreNaytto.Text = "SCORE: "; |
---|
101 | |
---|
102 | } |
---|
103 | |
---|
104 | /// <summary> |
---|
105 | /// Luo pelaajan ja sijoittaa sen kentälle. |
---|
106 | /// Asettaa pelaajalle myös aseen. |
---|
107 | /// </summary> |
---|
108 | void LuoPelaaja() |
---|
109 | { |
---|
110 | pelaajaAlus = new Alus(60, 55, 10); |
---|
111 | pelaajaAlus.Shape = Shapes.Triangle; |
---|
112 | |
---|
113 | pelaajaAlus.Animation = new Animation(LoadImages("sankarialus/sankarialusThrust1", "sankarialus/sankarialusThrust2")); |
---|
114 | pelaajaAlus.Animation.Start(); |
---|
115 | |
---|
116 | LisaData tieto; |
---|
117 | tieto.Tyyppi = "oma"; |
---|
118 | tieto.Rajahtaa = true; |
---|
119 | tieto.Damage = 5; |
---|
120 | pelaajaAlus.Tag = tieto; |
---|
121 | |
---|
122 | pelaajaAlus.Angle += Angle.Degrees(90); |
---|
123 | |
---|
124 | pelaajaAlus.IgnoresCollisionResponse = false; |
---|
125 | pelaajaAlus.Y = Level.Bottom + 40; |
---|
126 | pelaajaAlus.KineticFriction = 1.0; |
---|
127 | pelaajaAlus.Mass = 100000; |
---|
128 | |
---|
129 | weapons = new Weapon[3]; |
---|
130 | |
---|
131 | weapons[0] = new Beam(1, 1); |
---|
132 | ((Beam)weapons[0]).BeamCollision = AmmusOsuu; |
---|
133 | weapons[0].Position = new Vector(0, -40); |
---|
134 | weapons[1] = new Missile(1, 1); |
---|
135 | ((Missile)weapons[1]).MissileCollision = AmmusOsuu; |
---|
136 | |
---|
137 | weapons[2] = new Photon(1, 1); |
---|
138 | ((Photon)weapons[2]).PhotonCollision = AmmusOsuu; |
---|
139 | |
---|
140 | for (int i = 0; i < weapons.Length; i++) |
---|
141 | { |
---|
142 | pelaajaAlus.Add(weapons[i]); |
---|
143 | } |
---|
144 | |
---|
145 | Add(pelaajaAlus); |
---|
146 | pelaajaAlus.SetWeapon(weapons[2]); |
---|
147 | |
---|
148 | AddCollisionHandler(pelaajaAlus, PelaajaTormaa); |
---|
149 | |
---|
150 | } |
---|
151 | |
---|
152 | #endregion |
---|
153 | #region Kontrollit ja liikkuminen |
---|
154 | /// <summary> |
---|
155 | /// Asettaa kontrollit ohjaamaan haluttua alusta |
---|
156 | /// </summary> |
---|
157 | /// <param name="pelaaja">Alus, jota halutaan ohjata</param> |
---|
158 | void LataaKontrollit(Alus pelaaja) |
---|
159 | { |
---|
160 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Lopettaa pelin"); |
---|
161 | Keyboard.Listen(Key.Up, ButtonState.Down, Liikuta, "Liikuttaa alusta ylös", pelaaja, nopeusPysty); |
---|
162 | Keyboard.Listen(Key.Up, ButtonState.Released, pelaaja.StopVertical, null); |
---|
163 | Keyboard.Listen(Key.Down, ButtonState.Down, Liikuta, "Liikuttaa alusta alas", pelaaja, -nopeusPysty); |
---|
164 | Keyboard.Listen(Key.Down, ButtonState.Released, pelaaja.StopVertical, null); |
---|
165 | Keyboard.Listen(Key.Left, ButtonState.Down, Liikuta, "Liikuttaa alusta vasemmalle", pelaaja, -nopeusVaaka); |
---|
166 | Keyboard.Listen(Key.Left, ButtonState.Released, pelaaja.StopHorizontal, null); |
---|
167 | Keyboard.Listen(Key.Right, ButtonState.Down, Liikuta, "Liikuttaa alusta oikealle", pelaaja, nopeusVaaka); |
---|
168 | Keyboard.Listen(Key.Right, ButtonState.Released, pelaaja.StopHorizontal, null); |
---|
169 | Keyboard.Listen(Key.LeftControl, ButtonState.Down, Ammu, "Lopettaa pelin", pelaajaAlus); |
---|
170 | Keyboard.Listen(Key.D1, ButtonState.Pressed, VaihdaAsetta, "Valitsee säde-aseen", 1); |
---|
171 | Keyboard.Listen(Key.D2, ButtonState.Pressed, VaihdaAsetta, "Valitsee ohjuksen", 2); |
---|
172 | Keyboard.Listen(Key.D3, ButtonState.Pressed, VaihdaAsetta, "Valitsee fotoniaseen", 2); |
---|
173 | } |
---|
174 | |
---|
175 | /// <summary> |
---|
176 | /// Liikuttaa alusta haluttuun suuntaan |
---|
177 | /// </summary> |
---|
178 | /// <param name="alus">Liikutettava alus</param> |
---|
179 | /// <param name="suunta">Liikutettava suunta</param> |
---|
180 | void Liikuta(Alus alus, Vector suunta) |
---|
181 | { |
---|
182 | if (-NOPEUS < alus.Velocity.Y && alus.Velocity.Y < NOPEUS && suunta.Y != 0) |
---|
183 | { |
---|
184 | alus.Velocity += suunta; |
---|
185 | } |
---|
186 | if (-NOPEUS < alus.Velocity.X && alus.Velocity.X < NOPEUS && suunta.X != 0) |
---|
187 | { |
---|
188 | alus.Velocity += suunta; |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | void VaihdaAsetta(int aseenNumero) |
---|
193 | { |
---|
194 | if (aseenNumero > 0 && aseenNumero < 4) |
---|
195 | pelaajaAlus.SetWeapon(weapons[aseenNumero-1]); |
---|
196 | } |
---|
197 | |
---|
198 | /// <summary> |
---|
199 | /// Ampuu aluksella |
---|
200 | /// </summary> |
---|
201 | /// <param name="alus">Alus joka ampuu</param> |
---|
202 | void Ammu(Alus alus) |
---|
203 | { |
---|
204 | alus.Shoot(); |
---|
205 | } |
---|
206 | |
---|
207 | #endregion |
---|
208 | #region Viholliset |
---|
209 | /// <summary> |
---|
210 | /// Lataa viholliset |
---|
211 | /// TODO: Kentän numeron huomioiminen |
---|
212 | /// </summary> |
---|
213 | void LataaViholliset(int maara, double min, double max) |
---|
214 | { |
---|
215 | List<AikaTapahtuma> vihollistenTuloajat = new List<AikaTapahtuma>(); |
---|
216 | /* |
---|
217 | for (int i = 0; i < maara; i++) |
---|
218 | { |
---|
219 | vihollistenTuloajat.Add(new AikaTapahtuma(2 + (i * 5), LahetaSatunnainenVihollislaivue, 4, 10)); |
---|
220 | } |
---|
221 | */ |
---|
222 | vihollistenTuloajat.Add(new AikaTapahtuma(1/*2 + (maara * 7)*/, LahetaLoppupomo, 1, 20)); |
---|
223 | |
---|
224 | foreach (AikaTapahtuma tapahtuma in vihollistenTuloajat) |
---|
225 | { |
---|
226 | Timer ajastin = new Timer(); |
---|
227 | ajastin.Tag = tapahtuma.Lahtopaikka; |
---|
228 | ajastin.Interval = tapahtuma.Aika; |
---|
229 | ajastin.Trigger += tapahtuma.Tapahtuma; |
---|
230 | Add(ajastin); |
---|
231 | ajastin.Start(1); |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | /// <summary> |
---|
236 | /// Luo alukset ja lähettää ne matkaan |
---|
237 | /// </summary> |
---|
238 | /// <param name="sender">Timerin tagissa on lähtökoordinaatti</param> |
---|
239 | /// <param name="lahetysX">X-koordinaatti, josta muodostelma lähtee</param> |
---|
240 | void LahetaLoppupomo(Timer sender) |
---|
241 | { |
---|
242 | double taso = ((Vector)sender.Tag).X; |
---|
243 | |
---|
244 | List<Alus> alukset = new List<Alus>(); |
---|
245 | Image vihollisenKuva = LoadImage("loppuvastus" + 1); |
---|
246 | alukset.Add(LuoVihollinen(vihollisenKuva, 400)); |
---|
247 | alukset[0].Position = new Vector(0, Level.Top + 200); |
---|
248 | alukset[0].Angle = Angle.Degrees(270); |
---|
249 | Add(alukset[0]); |
---|
250 | |
---|
251 | //Aseen säätö |
---|
252 | Photon ase = new Photon(10, 10); |
---|
253 | LisaData ammuksenData; |
---|
254 | ammuksenData.Damage = 20; |
---|
255 | ammuksenData.Rajahtaa = false; |
---|
256 | ammuksenData.Tyyppi = "vihollinen"; |
---|
257 | ase.Tag = ammuksenData; |
---|
258 | alukset[0].Add(ase); |
---|
259 | alukset[0].SetWeapon(ase); |
---|
260 | alukset[0].UsedWeapon.IsVisible = false; |
---|
261 | ase.PhotonCollision = AmmusOsuu; |
---|
262 | |
---|
263 | Timer ampumaAjastin = new Timer(); |
---|
264 | ampumaAjastin.Interval = 0.7; |
---|
265 | ampumaAjastin.Trigger += alukset[0].TimerShoot; |
---|
266 | Add(ampumaAjastin); |
---|
267 | ampumaAjastin.Start(); |
---|
268 | |
---|
269 | List<Vector> reitti = new List<Vector>(); |
---|
270 | reitti.Add(new Vector(-200, Level.Top - 100 )); |
---|
271 | reitti.Add(new Vector(200, Level.Top - 100)); |
---|
272 | |
---|
273 | Muodostelma muodostelma = new Muodostelma(alukset, reitti, LuoLahettaja()); |
---|
274 | muodostelma.MovingSpeed = 100; |
---|
275 | muodostelma.Start(); |
---|
276 | } |
---|
277 | |
---|
278 | /// <summary> |
---|
279 | /// Arpoo ja lähettää satunnaisen vihollislaivueen |
---|
280 | /// Laivueessa on tällä hetkellä 10 alusta |
---|
281 | /// Aluksen kuva arvotaan tiedostoista, joiden nimi on "vihollinen#.png" |
---|
282 | /// </summary> |
---|
283 | /// <param name="sender"></param> |
---|
284 | void LahetaSatunnainenVihollislaivue(Timer sender) |
---|
285 | { |
---|
286 | List<Alus> alukset = new List<Alus>(); |
---|
287 | Image vihollisenKuva = LoadImage("vihollinen" + RandomGen.NextInt(1,VIHOLLISKUVIA)); |
---|
288 | |
---|
289 | int min = (int)((Vector)sender.Tag).X; |
---|
290 | int max = (int)((Vector)sender.Tag).Y; |
---|
291 | int koko = RandomGen.NextInt(min, max); |
---|
292 | |
---|
293 | for (int i = 0; i < koko; i++) |
---|
294 | { |
---|
295 | alukset.Add(LuoVihollinen(vihollisenKuva, 40)); |
---|
296 | Add(alukset[i]); |
---|
297 | Angle alas = new Angle(); |
---|
298 | alas.Degree = 270; |
---|
299 | alukset[i].Angle = Angle.Degrees(270); |
---|
300 | alukset[i].Position = new Vector(0, Level.Top + 200); |
---|
301 | AddCollisionHandler(alukset[i], VihollinenTormaa); |
---|
302 | } |
---|
303 | |
---|
304 | List<Vector> reitti = ArvoReitti(RandomGen.NextInt(0,5)); |
---|
305 | Muodostelma muodostelma = new Muodostelma(alukset, reitti, LuoLahettaja()); |
---|
306 | muodostelma.MovingSpeed = 100; |
---|
307 | muodostelma.Start(); |
---|
308 | } |
---|
309 | |
---|
310 | /// <summary> |
---|
311 | /// Arpoo koneelle reitin, joka lähteee ylhäältä ja päätyy alas |
---|
312 | /// </summary> |
---|
313 | /// <param name="valipisteet">Välipisteiden määrä, 0 -></param> |
---|
314 | /// <returns></returns> |
---|
315 | List<Vector> ArvoReitti(int valipisteet) |
---|
316 | { |
---|
317 | List<Vector> reitti = new List<Vector>(); |
---|
318 | reitti.Add(new Vector(RandomGen.NextDouble(Level.Left + 30, Level.Right - 30),Level.Top + 80)); |
---|
319 | for (int i = 0; i < valipisteet; i++) |
---|
320 | { |
---|
321 | reitti.Add(new Vector(RandomGen.NextDouble(Level.Left + 30, Level.Right - 30), |
---|
322 | (RandomGen.NextDouble(Level.Bottom + 30, Level.Top - 100)))); |
---|
323 | } |
---|
324 | reitti.Add(new Vector(RandomGen.NextDouble(Level.Left + 30, Level.Right - 30), Level.Bottom - 35)); |
---|
325 | return reitti; |
---|
326 | } |
---|
327 | |
---|
328 | Alus LuoVihollinen(Image kuva, int hp) |
---|
329 | { |
---|
330 | |
---|
331 | Alus vihollinen = new Alus(kuva.Width/5, kuva.Height/5, hp); |
---|
332 | vihollinen.Image = kuva; |
---|
333 | LisaData tieto; |
---|
334 | tieto.Tyyppi = "vihollinen"; |
---|
335 | tieto.Rajahtaa = true; |
---|
336 | tieto.Damage = 5; |
---|
337 | vihollinen.Tag = tieto; |
---|
338 | return vihollinen; |
---|
339 | } |
---|
340 | |
---|
341 | /// <summary> |
---|
342 | /// Luo ajastimen |
---|
343 | /// Lisää myös ajastimen, joten voidaan kutsua suoraan Muodostelman muodostajassa. |
---|
344 | /// </summary> |
---|
345 | /// <returns></returns> |
---|
346 | Timer LuoLahettaja() |
---|
347 | { |
---|
348 | Timer ajastin = new Timer(); |
---|
349 | ajastin.Interval = 0.5; |
---|
350 | Add(ajastin); |
---|
351 | return ajastin; |
---|
352 | } |
---|
353 | #endregion |
---|
354 | #region Tapahtumat |
---|
355 | /// <summary> |
---|
356 | /// Pelaajan törmäyksen käsittelijä |
---|
357 | /// |
---|
358 | /// Katsoo myös, että pelaaja ei juutu seiniin |
---|
359 | /// </summary> |
---|
360 | /// <param name="tormaaja"></param> |
---|
361 | /// <param name="kohde"></param> |
---|
362 | void PelaajaTormaa(PhysicsObject tormaaja, PhysicsObject kohde) |
---|
363 | { |
---|
364 | if (((LisaData)kohde.Tag).Tyyppi == "vihollinen") |
---|
365 | { |
---|
366 | ((Alus)tormaaja).TakeHit(5); |
---|
367 | if (kohde is Alus) ((Alus)kohde).TakeHit(20); |
---|
368 | } |
---|
369 | |
---|
370 | if (kohde == vasenLaita) |
---|
371 | { |
---|
372 | tormaaja.X = Level.Left + tormaaja.Height / 2 + 1; |
---|
373 | tormaaja.StopHorizontal(); |
---|
374 | } |
---|
375 | |
---|
376 | if (kohde == oikeaLaita) |
---|
377 | { |
---|
378 | tormaaja.X = Level.Right - tormaaja.Height / 2 - 1; |
---|
379 | tormaaja.StopHorizontal(); |
---|
380 | } |
---|
381 | if (kohde == katto) |
---|
382 | { |
---|
383 | tormaaja.Y = Level.Top - tormaaja.Width / 2 - 1; |
---|
384 | tormaaja.StopVertical(); |
---|
385 | } |
---|
386 | if (kohde == pohja) |
---|
387 | { |
---|
388 | tormaaja.Y = Level.Bottom + tormaaja.Width/2 +1; |
---|
389 | tormaaja.StopVertical(); |
---|
390 | } |
---|
391 | |
---|
392 | } |
---|
393 | |
---|
394 | /// <summary> |
---|
395 | /// Mitä tapahtuu, kun vihollinen tulee ns. poistumisalueelle |
---|
396 | /// </summary> |
---|
397 | /// <param name="tormaaja"></param> |
---|
398 | /// <param name="kohde"></param> |
---|
399 | void VihollinenTormaa(PhysicsObject tormaaja, PhysicsObject kohde) |
---|
400 | { |
---|
401 | if (kohde == exitZone) Remove(tormaaja); |
---|
402 | |
---|
403 | } |
---|
404 | |
---|
405 | void AmmusOsuu(PhysicsObject ammus, PhysicsObject toinen) |
---|
406 | { |
---|
407 | |
---|
408 | if (toinen.Tag.ToString() == "seina" || ((LisaData)toinen.Tag).Tyyppi != ((LisaData)ammus.Tag).Tyyppi) |
---|
409 | { |
---|
410 | ammus.Destroy(); |
---|
411 | if (((LisaData)ammus.Tag).Rajahtaa) |
---|
412 | { |
---|
413 | Explosion rajahdys = new Explosion(100); |
---|
414 | rajahdys.Position = toinen.Position; |
---|
415 | Add(rajahdys); |
---|
416 | } |
---|
417 | |
---|
418 | if (((LisaData)toinen.Tag).Tyyppi == "vihollinen") |
---|
419 | { |
---|
420 | if (toinen is Alus) |
---|
421 | { |
---|
422 | ((Alus)toinen).TakeHit(((LisaData)ammus.Tag).Damage); |
---|
423 | if (toinen.IsDestroyed()) score.Value++; |
---|
424 | } |
---|
425 | |
---|
426 | } |
---|
427 | } |
---|
428 | |
---|
429 | |
---|
430 | } |
---|
431 | #endregion |
---|
432 | } |
---|
433 | |
---|
434 | public class AikaTapahtuma |
---|
435 | { |
---|
436 | public int Aika; |
---|
437 | public Timer.TriggerHandler Tapahtuma; |
---|
438 | private Vector lahtopaikka; |
---|
439 | |
---|
440 | public Vector Lahtopaikka |
---|
441 | { |
---|
442 | get{ return lahtopaikka; } |
---|
443 | } |
---|
444 | |
---|
445 | public AikaTapahtuma(int aika, Timer.TriggerHandler tapahtuma, double param1, double param2) |
---|
446 | { |
---|
447 | this.Aika = aika; |
---|
448 | this.Tapahtuma = tapahtuma; |
---|
449 | this.lahtopaikka = new Vector(param1, param2); |
---|
450 | } |
---|
451 | } |
---|
452 | } |
---|