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 Peli : PhysicsGame |
---|
10 | { |
---|
11 | Vector nopeusYlos = new Vector(0, 90); |
---|
12 | Vector nopeusAlas = new Vector(0, -90); |
---|
13 | Vector nopeusStop = new Vector(0, 0); |
---|
14 | Vector nopeusVasen = new Vector(-90, 0); |
---|
15 | Vector nopeusOikea = new Vector(90, 0); |
---|
16 | //Vector viholinen = new Vector(); |
---|
17 | |
---|
18 | AssaultRifle isoase; |
---|
19 | |
---|
20 | Inventory inventory; |
---|
21 | Image puukuva = LoadImage("puu"); |
---|
22 | Image kultakuva = LoadImage("kulta2"); |
---|
23 | Image ironkuva = LoadImage("iron2"); |
---|
24 | Image timantikuva = LoadImage("timanti3"); |
---|
25 | |
---|
26 | List<Widget> sydamet = new List<Widget>(); |
---|
27 | |
---|
28 | PhysicsObject Miner; |
---|
29 | Image olionKuva = LoadImage("player 1 version 4"); |
---|
30 | Image[] KavelyKuvat = LoadImages("player 1", "player 1 frame2"); |
---|
31 | |
---|
32 | Image[] zombiekavelykuvat = LoadImages("zombie animatio osa 1", "zombie animatio osa 2"); |
---|
33 | Image olionKuva1 = LoadImage("zombie seiso"); |
---|
34 | |
---|
35 | IntMeter pisteLaskuri; |
---|
36 | |
---|
37 | //Image[] KavelyKuvatPeilattu; |
---|
38 | public override void Begin() |
---|
39 | { |
---|
40 | tesydämet(); |
---|
41 | TileMap ruudut = TileMap.FromLevelAsset("kentaa"); |
---|
42 | ruudut.SetTileMethod('=', LouPuu); |
---|
43 | ruudut.SetTileMethod('$', Loukulta); |
---|
44 | ruudut.SetTileMethod('*', LouPuu); |
---|
45 | ruudut.SetTileMethod('P', LouMiner); |
---|
46 | ruudut.SetTileMethod('#', Louiron); |
---|
47 | ruudut.SetTileMethod('T', Loutimanti); |
---|
48 | ruudut.SetTileMethod('z', Louzombie); |
---|
49 | ruudut.Execute(50, 50); |
---|
50 | |
---|
51 | inventory = new Inventory(); |
---|
52 | inventory.Left = Screen.Left; |
---|
53 | inventory.Top = Screen.Top; |
---|
54 | Add(inventory); |
---|
55 | |
---|
56 | |
---|
57 | //Camera.ZoomToLevel(); |
---|
58 | //KavelyKuvatPeilattu = Image.Mirror(KavelyKuvat); |
---|
59 | |
---|
60 | // TODO: Kirjoita ohjelmakoodisi tähän |
---|
61 | |
---|
62 | Level.BackgroundColor = Color.ForestGreen; |
---|
63 | |
---|
64 | Camera.Follow(Miner); |
---|
65 | Camera.Zoom(9.0); |
---|
66 | |
---|
67 | AsetaOhjaimet(); |
---|
68 | louzombierandompaikka(); |
---|
69 | LuoLaskuri(); |
---|
70 | //Louzombie(); |
---|
71 | |
---|
72 | //Cannon tykki = new Cannon(50, 10); |
---|
73 | //tykki.Power.Value += 1000; |
---|
74 | } |
---|
75 | void AsetaOhjaimet() |
---|
76 | { |
---|
77 | Keyboard.Listen(Key.Up, ButtonState.Down, LiikutaaMinerYlos, "Pelaaja 1: Liikuta Miner ylös"); |
---|
78 | Keyboard.Listen(Key.Up, ButtonState.Released, PysaytaMiner, null); |
---|
79 | |
---|
80 | Keyboard.Listen(Key.Down, ButtonState.Down, LiikutaaMinerAlas, "Pelaaja 1: Liikuta Miner Alas"); |
---|
81 | Keyboard.Listen(Key.Down, ButtonState.Released, PysaytaMiner, null); |
---|
82 | |
---|
83 | Keyboard.Listen(Key.Left, ButtonState.Down, LiikutaaMinerVasen, "Pelaaja 1: Liikuta Miner Vasemalle"); |
---|
84 | Keyboard.Listen(Key.Left, ButtonState.Released, PysaytaMiner, null); |
---|
85 | |
---|
86 | Keyboard.Listen(Key.Right, ButtonState.Down, LiikutaaMinerOikea, "Pelaaja 1: Liikuta Miner Oikealle"); |
---|
87 | Keyboard.Listen(Key.Right, ButtonState.Released, PysaytaMiner, null); |
---|
88 | |
---|
89 | Keyboard.Listen(Key.Space, ButtonState.Released, rikkokivi, "Pelaaja 1: rikko kiven"); |
---|
90 | Keyboard.Listen(Key.Space, ButtonState.Released, rikkokulta, "Pelaaja 1: rikko kultan"); |
---|
91 | Keyboard.Listen(Key.Space, ButtonState.Released, rikkotimanti, "Pelaaja 1: rikko timantin"); |
---|
92 | |
---|
93 | Keyboard.Listen(Key.K, ButtonState.Released, tekasiase, "Pelaaja 1: teke kasiasen"); |
---|
94 | Keyboard.Listen(Key.O, ButtonState.Released, teisoase, "Pelaaja 1: teke isonasen"); |
---|
95 | |
---|
96 | Keyboard.Listen(Key.Space, ButtonState.Down, ammu, "Pelaaja 1: ammpu"); |
---|
97 | //Keyboard.Listen(Key.Space, ButtonState.Released, alaammu, "Pelaaja 1: loppeta ampumisen"); |
---|
98 | |
---|
99 | |
---|
100 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, Exit, "Poistu"); |
---|
101 | } |
---|
102 | void PysaytaMiner() |
---|
103 | { |
---|
104 | Miner.Velocity = nopeusStop; |
---|
105 | Miner.Animation.Stop(); |
---|
106 | } |
---|
107 | void LiikutaaMinerYlos() |
---|
108 | { |
---|
109 | Miner.Velocity = nopeusYlos; |
---|
110 | Miner.Animation.Resume(); |
---|
111 | Miner.Angle = Angle.FromDegrees(90); |
---|
112 | } |
---|
113 | void LiikutaaMinerAlas() |
---|
114 | { |
---|
115 | Miner.Velocity = nopeusAlas; |
---|
116 | Miner.Animation.Resume(); |
---|
117 | //Miner.Animation.FPS = 10; |
---|
118 | Miner.Angle = Angle.FromDegrees(-90); |
---|
119 | } |
---|
120 | void LiikutaaMinerVasen() |
---|
121 | { |
---|
122 | Miner.Velocity = nopeusVasen; |
---|
123 | Miner.Animation.Resume(); |
---|
124 | Miner.Angle = Angle.FromDegrees(180); |
---|
125 | |
---|
126 | } |
---|
127 | void LiikutaaMinerOikea() |
---|
128 | { |
---|
129 | Miner.Velocity = nopeusOikea; |
---|
130 | Miner.Animation.Resume(); |
---|
131 | Miner.Angle = Angle.FromDegrees(0); |
---|
132 | } |
---|
133 | void Louzombie(Vector paikka, double leveys, double korkeus) |
---|
134 | { |
---|
135 | Vihulainen zombie = new Vihulainen(10, 10); |
---|
136 | Add(zombie); |
---|
137 | zombie.Image = olionKuva1; |
---|
138 | zombie.Position = paikka; |
---|
139 | |
---|
140 | FollowerBrain seuraajanAivot = new FollowerBrain(Miner, 10); |
---|
141 | seuraajanAivot.Target = Miner; |
---|
142 | zombie.Brain = seuraajanAivot; |
---|
143 | zombie.Tag = "zombie"; |
---|
144 | } |
---|
145 | void LouPuu(Vector paikka, double leveys, double korkeus) |
---|
146 | { |
---|
147 | PhysicsObject Puu = PhysicsObject.CreateStaticObject(leveys, korkeus); |
---|
148 | Puu.Position = paikka; |
---|
149 | Puu.Shape = Shape.Circle; |
---|
150 | Puu.Color = Color.Green; |
---|
151 | Add(Puu); |
---|
152 | Puu.Image = puukuva; |
---|
153 | puut.Add(Puu); |
---|
154 | |
---|
155 | |
---|
156 | } |
---|
157 | void LouMiner(Vector paikka, double leveys, double korkeus) |
---|
158 | { |
---|
159 | Miner = new PhysicsObject(10.0, 10.0); |
---|
160 | Add(Miner); |
---|
161 | Miner.Shape = Shape.Rectangle; |
---|
162 | Miner.Color = Color.Black; |
---|
163 | Miner.Image = olionKuva; |
---|
164 | //Miner.Animation = new Animation(KavelyKuvat); |
---|
165 | Miner.Animation.FPS = 2; |
---|
166 | Miner.Position = paikka; |
---|
167 | Miner.Restitution = 0.0; |
---|
168 | Miner.CanRotate = false; |
---|
169 | AddCollisionHandler(Miner, tormays); |
---|
170 | } |
---|
171 | void tesydämet() |
---|
172 | { |
---|
173 | HorizontalLayout asettelu = new HorizontalLayout(); |
---|
174 | asettelu.Spacing = 10; |
---|
175 | |
---|
176 | Widget sydamet = new Widget(asettelu); |
---|
177 | sydamet.Color = Color.Transparent; |
---|
178 | sydamet.X = Screen.Center.X; |
---|
179 | sydamet.Y = Screen.Top - 30; |
---|
180 | Add(sydamet); |
---|
181 | |
---|
182 | for (int i = 0; i < 10; i++) |
---|
183 | { |
---|
184 | Widget sydan = new Widget(30, 30, Shape.Heart); |
---|
185 | sydan.Color = Color.Red; |
---|
186 | sydamet.Add(sydan); |
---|
187 | this.sydamet.Add(sydan); |
---|
188 | |
---|
189 | } |
---|
190 | |
---|
191 | } |
---|
192 | |
---|
193 | void Loukulta(Vector paikka, double leveys, double korkeus) |
---|
194 | { |
---|
195 | PhysicsObject kulta = PhysicsObject.CreateStaticObject(20, 20); |
---|
196 | kulta.Position = paikka; |
---|
197 | kullat.Add(kulta); |
---|
198 | Add(kulta); |
---|
199 | kulta.Image = kultakuva; |
---|
200 | |
---|
201 | } |
---|
202 | |
---|
203 | void Louiron(Vector paikka, double leveys, double korkeus) |
---|
204 | { |
---|
205 | PhysicsObject iron = PhysicsObject.CreateStaticObject(20, 20); |
---|
206 | iron.Position = paikka; |
---|
207 | irons.Add(iron); |
---|
208 | Add(iron); |
---|
209 | iron.Image = ironkuva; |
---|
210 | } |
---|
211 | void Loutimanti(Vector paikka, double leveys, double korkeus) |
---|
212 | { |
---|
213 | PhysicsObject timanti = PhysicsObject.CreateStaticObject(20, 20); |
---|
214 | timanti.Position = paikka; |
---|
215 | timantit.Add(timanti); |
---|
216 | Add(timanti); |
---|
217 | timanti.Image = timantikuva; |
---|
218 | |
---|
219 | } |
---|
220 | //PhysicsObject zombie; |
---|
221 | List<PhysicsObject> puut = new List<PhysicsObject>(); |
---|
222 | List<PhysicsObject> irons = new List<PhysicsObject>(); |
---|
223 | List<PhysicsObject> kullat = new List<PhysicsObject>(); |
---|
224 | List<PhysicsObject> timantit = new List<PhysicsObject>(); |
---|
225 | void rikkokivi() |
---|
226 | { |
---|
227 | foreach (var iron in irons) |
---|
228 | { |
---|
229 | |
---|
230 | if (Vector.Distance(iron.Position, Miner.Position) < 22 && !iron.IsDestroyed) |
---|
231 | { |
---|
232 | iron.Destroy(); |
---|
233 | pisteLaskuri.Value += 50; |
---|
234 | inventory.AddItem( iron, ironkuva ); |
---|
235 | } |
---|
236 | |
---|
237 | } |
---|
238 | } |
---|
239 | void rikkokulta() |
---|
240 | { |
---|
241 | foreach (var kulta in kullat ) |
---|
242 | { |
---|
243 | |
---|
244 | if (Vector.Distance(kulta.Position, Miner.Position) < 22 &&! kulta.IsDestroyed) |
---|
245 | { |
---|
246 | kulta.Destroy(); |
---|
247 | pisteLaskuri.Value += 200; |
---|
248 | inventory.AddItem(kulta, kultakuva); |
---|
249 | } |
---|
250 | } |
---|
251 | } |
---|
252 | void rikkotimanti() |
---|
253 | { |
---|
254 | foreach (var timanti in timantit ) |
---|
255 | { |
---|
256 | |
---|
257 | if (Vector.Distance(timanti.Position, Miner.Position) < 22 && !timanti.IsDestroyed) |
---|
258 | { |
---|
259 | timanti.Destroy(); |
---|
260 | pisteLaskuri.Value += 500; |
---|
261 | inventory.AddItem(timanti, timantikuva); |
---|
262 | } |
---|
263 | } |
---|
264 | } |
---|
265 | |
---|
266 | void teisoase() |
---|
267 | { |
---|
268 | if (inventory.inventlaskin(ironkuva) >= 3) |
---|
269 | { |
---|
270 | int poistettuja = 0; |
---|
271 | while (poistettuja > 3) |
---|
272 | { |
---|
273 | if (inventory.Items[poistettuja].Image == ironkuva) |
---|
274 | { |
---|
275 | inventory.Items.Remove(inventory.Items[poistettuja]); |
---|
276 | poistettuja++; |
---|
277 | } |
---|
278 | } |
---|
279 | |
---|
280 | isoase = new AssaultRifle(20, 5); |
---|
281 | isoase.ProjectileCollision = ammusOsuu; |
---|
282 | Miner.Add(isoase); |
---|
283 | pisteLaskuri.Value += 50; |
---|
284 | } |
---|
285 | } |
---|
286 | |
---|
287 | void ammusOsuu(PhysicsObject ammus, PhysicsObject kohde) |
---|
288 | { |
---|
289 | if (kohde is Vihulainen) |
---|
290 | { |
---|
291 | Vihulainen k = (Vihulainen)kohde; |
---|
292 | k.HP--; |
---|
293 | if (k.HP < 0) |
---|
294 | { |
---|
295 | k.Destroy(); |
---|
296 | pisteLaskuri.Value += 100; |
---|
297 | } |
---|
298 | } |
---|
299 | ammus.Destroy(); |
---|
300 | } |
---|
301 | void tekasiase() |
---|
302 | { |
---|
303 | |
---|
304 | } |
---|
305 | void ammu() |
---|
306 | { |
---|
307 | if (isoase != null) |
---|
308 | { |
---|
309 | PhysicsObject ammus = isoase.Shoot(); |
---|
310 | |
---|
311 | if (ammus != null) |
---|
312 | { |
---|
313 | ammus.Size *= 0.1; |
---|
314 | } |
---|
315 | } |
---|
316 | } |
---|
317 | void alaammu() |
---|
318 | { |
---|
319 | |
---|
320 | } |
---|
321 | void tormays(PhysicsObject Miner, PhysicsObject kohde) |
---|
322 | { |
---|
323 | if (kohde.Tag.ToString() == "zombie") |
---|
324 | { |
---|
325 | if (sydamet.Count == 1) |
---|
326 | { |
---|
327 | Miner.Destroy(); |
---|
328 | Label tekstikentta = new Label("Game over"); |
---|
329 | Add(tekstikentta); |
---|
330 | } |
---|
331 | else |
---|
332 | { |
---|
333 | int viimeinenSydan = sydamet.Count - 1; |
---|
334 | sydamet[viimeinenSydan].Destroy(); |
---|
335 | sydamet.RemoveAt(viimeinenSydan); |
---|
336 | |
---|
337 | } |
---|
338 | } |
---|
339 | } |
---|
340 | void louzombierandompaikka() |
---|
341 | { |
---|
342 | int i = 1000; |
---|
343 | bool valmis = false; |
---|
344 | |
---|
345 | while (i > 0) |
---|
346 | { |
---|
347 | Vector paikka = Level.GetRandomPosition(); |
---|
348 | foreach (var Puu in puut) |
---|
349 | { |
---|
350 | if (!Puu.IsInside(paikka)) |
---|
351 | { |
---|
352 | valmis = true; |
---|
353 | } |
---|
354 | else |
---|
355 | valmis = false; |
---|
356 | } |
---|
357 | |
---|
358 | if (valmis) |
---|
359 | { |
---|
360 | Louzombie(paikka, 30, 30); |
---|
361 | i--; |
---|
362 | } |
---|
363 | |
---|
364 | |
---|
365 | } |
---|
366 | } |
---|
367 | void LuoLaskuri() |
---|
368 | { |
---|
369 | pisteLaskuri = new IntMeter(0); |
---|
370 | |
---|
371 | Label pisteNaytto = new Label(); |
---|
372 | pisteNaytto.X = Screen.Left + 100; |
---|
373 | pisteNaytto.Y = Screen.Top - 100; |
---|
374 | pisteNaytto.TextColor = Color.Red; |
---|
375 | |
---|
376 | pisteNaytto.BindTo(pisteLaskuri); |
---|
377 | Add(pisteNaytto); |
---|
378 | } |
---|
379 | |
---|
380 | } |
---|
381 | |
---|
382 | class Inventory : Widget |
---|
383 | { |
---|
384 | public List<Widget> Items = new List<Widget>(); |
---|
385 | |
---|
386 | /// <summary> |
---|
387 | /// Tapahtuma, kun esine on valittu. |
---|
388 | /// </summary> |
---|
389 | public event Action<PhysicsObject> ItemSelected; |
---|
390 | |
---|
391 | /// <summary> |
---|
392 | /// Luo uuden esinevalikon. |
---|
393 | /// </summary> |
---|
394 | public Inventory() |
---|
395 | : base(new HorizontalLayout()) |
---|
396 | { |
---|
397 | } |
---|
398 | |
---|
399 | /// <summary> |
---|
400 | /// Lisää esineen. |
---|
401 | /// </summary> |
---|
402 | /// <param name="item">Lisättävä esine.</param> |
---|
403 | /// <param name="kuva">Esineen ikoni, joka näkyy valikossa.</param> |
---|
404 | public void AddItem(PhysicsObject item, Image kuva) |
---|
405 | { |
---|
406 | PushButton icon = new PushButton(kuva); |
---|
407 | Add(icon); |
---|
408 | Items.Add(icon); |
---|
409 | icon.Clicked += delegate() { SelectItem(item); }; |
---|
410 | } |
---|
411 | |
---|
412 | void SelectItem(PhysicsObject item) |
---|
413 | { |
---|
414 | if (ItemSelected != null) |
---|
415 | { |
---|
416 | ItemSelected(item); |
---|
417 | } |
---|
418 | } |
---|
419 | |
---|
420 | public int inventlaskin( Image itemType ) |
---|
421 | { |
---|
422 | int laskuri = 0; |
---|
423 | |
---|
424 | foreach (var item in Items) |
---|
425 | { |
---|
426 | if (item.Image == itemType) |
---|
427 | laskuri++; |
---|
428 | } |
---|
429 | |
---|
430 | return laskuri; |
---|
431 | } |
---|
432 | |
---|
433 | } |
---|
434 | |
---|
435 | public class Vihulainen : PhysicsObject |
---|
436 | { |
---|
437 | public int HP = 3; |
---|
438 | public Vihulainen(double width, double height) : base(width, height) { } |
---|
439 | } |
---|