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 Selviytyja : PhysicsGame |
---|
10 | { |
---|
11 | int Speed = 100; |
---|
12 | public bool menuopened = false; |
---|
13 | //public bool hakkaamassa = false; |
---|
14 | public Image idleRight = LoadImage("PlayerIdleRight_0"); |
---|
15 | public Image woodItem = LoadImage("WoodItem_0"); |
---|
16 | public Image inventoryimg = LoadImage("ToolBar_0"); |
---|
17 | public Image treeLeaves = LoadImage("TreeLeaves"); |
---|
18 | public Image treeTrunk = LoadImage("TreeTrunk"); |
---|
19 | public Image stoneTop = LoadImage("StoneTop"); |
---|
20 | public Image stoneBottom = LoadImage("StoneBottom"); |
---|
21 | public Image stoneimg = LoadImage("Stone_0"); |
---|
22 | public Image appleimg = LoadImage("Apple_0"); |
---|
23 | Drop valittuEsine = null; |
---|
24 | |
---|
25 | public Image stoneitemimg = LoadImage("StoneItem_0"); |
---|
26 | private Image[] playerWalkR = LoadImages("PlayerWalkRight_0", "PlayerWalkRight_1"); |
---|
27 | private Image[] playerHitR = LoadImages("PlayerHitRight_0", "PlayerHitRight_1"); |
---|
28 | Image idleLeft; |
---|
29 | public bool pausegame = false; |
---|
30 | GameObject menubutton; |
---|
31 | GameObject menu; |
---|
32 | GameObject resumebutton; |
---|
33 | GameObject quitbutton; |
---|
34 | GameObject darkfade; |
---|
35 | GameObject nappi; |
---|
36 | GameObject apple; |
---|
37 | public int slotitemsize = 30; |
---|
38 | Tavaraluettelo tavaraluettelo; |
---|
39 | Player ukko; |
---|
40 | |
---|
41 | PhysicsObject wood; |
---|
42 | PhysicsObject stone; |
---|
43 | Timer fader; |
---|
44 | private Image[] playerWalkL; |
---|
45 | Image Bg = LoadImage("grass"); |
---|
46 | Image menuimg = LoadImage("menu"); |
---|
47 | List<PhysicsObject> objektilista = new List<PhysicsObject>(); |
---|
48 | List<GameObject> menubuttonit = new List<GameObject>(); |
---|
49 | public byte fade = 1; |
---|
50 | public override void Begin() |
---|
51 | { |
---|
52 | tavaraluettelo = new Tavaraluettelo(657, 150, inventoryimg); |
---|
53 | Add(tavaraluettelo, 2); |
---|
54 | |
---|
55 | // TODO: Kirjoita ohjelmakoodisi tähän |
---|
56 | Level.Width = Window.Width; |
---|
57 | Level.Height = Window.Height; |
---|
58 | SmoothTextures = false; |
---|
59 | ukko = new Player(24, 52); |
---|
60 | |
---|
61 | //ukko.MakeStatic(); |
---|
62 | ukko.Image = idleRight; |
---|
63 | ukko.CanRotate = false; |
---|
64 | ukko.PlayerImageRight = idleRight; |
---|
65 | ukko.PlayerImageLeft = Image.Mirror(idleRight.Clone()); |
---|
66 | ukko.PlayerImage = ukko.PlayerImageRight; |
---|
67 | ukko.Tag = "ukko"; |
---|
68 | AddCollisionHandler(ukko, "drop", PelaajatTormaavat); |
---|
69 | //idleLeft = Image.Mirror(idleRight); |
---|
70 | //playerWalkL = playerWalkR; |
---|
71 | //playerWalkL[1] = Image.Mirror(playerWalkR[1]).Clone(); |
---|
72 | //playerWalkL[0] = Image.Mirror(playerWalkR[0]).Clone(); |
---|
73 | ukko.PlayerWalkR = new Animation(playerWalkR); |
---|
74 | ukko.PlayerHitR = new Animation(playerHitR); |
---|
75 | ukko.PlayerHitL = new Animation(Animation.Mirror(ukko.PlayerHitR)); |
---|
76 | //ukko.PlayerWalkL = Animation.Mirror(ukko.PlayerWalkR.cl); |
---|
77 | ukko.Restitution = 0; |
---|
78 | ukko.MaxVelocity = 0; |
---|
79 | Level.Background.Color = Color.Black; |
---|
80 | Add(ukko); |
---|
81 | Level.Background.Image = Bg; |
---|
82 | Level.Background.TileToLevel(); |
---|
83 | Camera.ZoomToLevel(); |
---|
84 | LuoKiviRndm(); |
---|
85 | LuoPuuRndm(); |
---|
86 | LuoPauseMenu(); |
---|
87 | fader = new Timer(); |
---|
88 | //inventory.Image = LoadImage("ToolBar_0"); |
---|
89 | //inventory.Y = Screen.Bottom + 100; |
---|
90 | //Add(inventory, 3); |
---|
91 | //inventory.AddItem(wood, woodItem, 1); |
---|
92 | //inventory.AddItem(wood, stoneimg, 2); |
---|
93 | tavaraluettelo.Y = -400; |
---|
94 | |
---|
95 | |
---|
96 | //ITEMIT |
---|
97 | wood = new PhysicsObject(40, 40); |
---|
98 | stone = new PhysicsObject(40, 40); |
---|
99 | apple = new PhysicsObject(40, 40); |
---|
100 | stone.Image = stoneitemimg; |
---|
101 | apple.Image = appleimg; |
---|
102 | wood.Tag = "wood"; |
---|
103 | //-------------------------------// |
---|
104 | |
---|
105 | |
---|
106 | Keyboard.Listen(Key.D, ButtonState.Down, MoveSideways, null, ukko, new Vector(Speed, 0)); |
---|
107 | Keyboard.Listen(Key.D, ButtonState.Released, MoveSideways, null, ukko, Vector.Zero); |
---|
108 | Keyboard.Listen(Key.A, ButtonState.Down, MoveSideways, null, ukko, new Vector(-Speed, 0)); |
---|
109 | Keyboard.Listen(Key.A, ButtonState.Released, MoveSideways, null, ukko, Vector.Zero); |
---|
110 | Keyboard.Listen(Key.W, ButtonState.Down, MoveVertically, null, ukko, new Vector(0, Speed)); |
---|
111 | Keyboard.Listen(Key.W, ButtonState.Released, MoveVertically, null, ukko, Vector.Zero); |
---|
112 | Keyboard.Listen(Key.S, ButtonState.Down, MoveVertically, null, ukko, new Vector(0, -Speed)); |
---|
113 | Keyboard.Listen(Key.S, ButtonState.Released, MoveVertically, null, ukko, Vector.Zero); |
---|
114 | Mouse.ListenMovement(10, KuunteleLiiketta, null); |
---|
115 | |
---|
116 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, AvaaPauseMenu, "Pause the game"); |
---|
117 | |
---|
118 | Mouse.Listen(MouseButton.Left, ButtonState.Pressed, HakkaaminenAlkaa, null, ukko); |
---|
119 | Mouse.Listen(MouseButton.Left, ButtonState.Released, HakkaaminenLoppuu, null, ukko); |
---|
120 | |
---|
121 | |
---|
122 | } |
---|
123 | void MoveSideways(Player pelaaja, Vector direction) |
---|
124 | { |
---|
125 | //pelaaja.Velocity = new Vector(direction.X, pelaaja.Velocity.Y); |
---|
126 | |
---|
127 | PlayerAnimation(pelaaja, direction); |
---|
128 | |
---|
129 | pelaaja.Velocity += direction; |
---|
130 | |
---|
131 | } |
---|
132 | |
---|
133 | void MoveVertically(Player pelaaja, Vector direction) |
---|
134 | { |
---|
135 | //pelaaja.Velocity = new Vector(pelaaja.Velocity.X, direction.Y); |
---|
136 | pelaaja.Velocity += direction; |
---|
137 | |
---|
138 | if (pelaaja.PelaajaHakkaamassa) return; |
---|
139 | |
---|
140 | PlayerAnimation(pelaaja, direction); |
---|
141 | |
---|
142 | } |
---|
143 | |
---|
144 | public void AvaaPauseMenu() |
---|
145 | { |
---|
146 | fader.Stop(); |
---|
147 | if (menuopened == true) |
---|
148 | { |
---|
149 | menu.IsVisible = false; |
---|
150 | menuopened = false; |
---|
151 | //Mouse.IsCursorVisible = false; |
---|
152 | foreach (GameObject nappi in menubuttonit) nappi.Color = Color.Transparent; |
---|
153 | FadeRuutu(false); |
---|
154 | |
---|
155 | Keyboard.EnableAll(); |
---|
156 | |
---|
157 | |
---|
158 | |
---|
159 | |
---|
160 | } |
---|
161 | else |
---|
162 | { |
---|
163 | menu.IsVisible = true; |
---|
164 | menuopened = true; |
---|
165 | Mouse.IsCursorVisible = true; |
---|
166 | Keyboard.Disable(Key.A); |
---|
167 | Keyboard.Disable(Key.W); |
---|
168 | Keyboard.Disable(Key.S); |
---|
169 | Keyboard.Disable(Key.D); |
---|
170 | FadeRuutu(true); |
---|
171 | Keyboard.Enable(Key.Escape); |
---|
172 | |
---|
173 | } |
---|
174 | } |
---|
175 | public void FadeRuutu(bool pelijatkuu) |
---|
176 | { |
---|
177 | //fader.Stop(); |
---|
178 | |
---|
179 | fader.Interval = 0.1; |
---|
180 | if (pelijatkuu) |
---|
181 | { |
---|
182 | fader.Timeout += RuudunTummennus; |
---|
183 | fader.Timeout -= RuudunVaalennus; |
---|
184 | fade = 0; |
---|
185 | } |
---|
186 | else |
---|
187 | { |
---|
188 | fader.Timeout += RuudunVaalennus; |
---|
189 | fader.Timeout -= RuudunTummennus; |
---|
190 | |
---|
191 | fade = 150; |
---|
192 | } |
---|
193 | |
---|
194 | fader.Start(); |
---|
195 | } |
---|
196 | |
---|
197 | public void RuudunTummennus() |
---|
198 | { |
---|
199 | pausegame = true; |
---|
200 | Color uusiVari = new Color(Color.Black, fade); |
---|
201 | darkfade.Color = uusiVari; |
---|
202 | fade += 20; |
---|
203 | if (fade > 140) fader.Stop(); |
---|
204 | } |
---|
205 | public void RuudunVaalennus() |
---|
206 | { |
---|
207 | pausegame = false; |
---|
208 | Color uusiVari = new Color(Color.Black, fade); |
---|
209 | darkfade.Color = uusiVari; |
---|
210 | fade -= 20; |
---|
211 | if (fade < 20) |
---|
212 | { |
---|
213 | fader.Stop(); |
---|
214 | darkfade.Color = Color.Transparent; |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | public void LuoPauseMenu() |
---|
219 | { |
---|
220 | |
---|
221 | menu = new GameObject(450, 500); |
---|
222 | menu.Image = menuimg; |
---|
223 | menu.IsVisible = false; |
---|
224 | Add(menu, 2); |
---|
225 | //menubutton = new GameObject(254, 134); |
---|
226 | resumebutton = new GameObject(254, 134); |
---|
227 | quitbutton = new GameObject(254, 134); |
---|
228 | darkfade = new GameObject(Screen.Width, Screen.Height); |
---|
229 | //menubutton.Y = 157; |
---|
230 | resumebutton.Y = 0; |
---|
231 | quitbutton.Y = -160; |
---|
232 | //Add(menubutton, 3); |
---|
233 | Add(resumebutton, 3); |
---|
234 | Add(quitbutton, 3); |
---|
235 | Add(darkfade, 2); |
---|
236 | darkfade.Color = (Color.Transparent); |
---|
237 | //menubuttonit.Add(menubutton); |
---|
238 | menubuttonit.Add(resumebutton); |
---|
239 | menubuttonit.Add(quitbutton); |
---|
240 | if (menuopened == false) |
---|
241 | { |
---|
242 | //menubutton.Color = Color.Transparent; |
---|
243 | resumebutton.Color = Color.Transparent; |
---|
244 | quitbutton.Color = Color.Transparent; |
---|
245 | |
---|
246 | |
---|
247 | } |
---|
248 | //menubutton.Color = Color.Transparent; |
---|
249 | resumebutton.Color = Color.Transparent; |
---|
250 | quitbutton.Color = Color.Transparent; |
---|
251 | |
---|
252 | } |
---|
253 | |
---|
254 | |
---|
255 | void KuunteleLiiketta(AnalogState hiirentila) |
---|
256 | { |
---|
257 | if (valittuEsine != null ) |
---|
258 | { |
---|
259 | valittuEsine.Position = Mouse.PositionOnWorld; |
---|
260 | return; |
---|
261 | } |
---|
262 | |
---|
263 | if (menuopened == false) |
---|
264 | { |
---|
265 | foreach (Slot slotti in tavaraluettelo.ruudut) |
---|
266 | { |
---|
267 | if (Mouse.IsCursorOn(slotti)) |
---|
268 | { |
---|
269 | slotti.Valittu = true; |
---|
270 | } |
---|
271 | else |
---|
272 | { |
---|
273 | slotti.Valittu = false; |
---|
274 | } |
---|
275 | } |
---|
276 | return; |
---|
277 | } |
---|
278 | foreach (GameObject nappi in menubuttonit) |
---|
279 | { |
---|
280 | if (Mouse.IsCursorOn(nappi) && (menuopened == true)) |
---|
281 | { |
---|
282 | nappi.Color = new Color(Color.DarkBrown, 100); |
---|
283 | |
---|
284 | } |
---|
285 | else |
---|
286 | { |
---|
287 | nappi.Color = Color.Transparent; |
---|
288 | } |
---|
289 | } |
---|
290 | } |
---|
291 | public void PlayerAnimation(Player player, Vector direction) |
---|
292 | { |
---|
293 | // MessageDisplay.Clear(); |
---|
294 | // MessageDisplay.Add("PlayerRight = " + player.PlayerRight + ", direction = " + direction); |
---|
295 | if (direction.X != 0) |
---|
296 | { |
---|
297 | player.PlayerAnimation(direction.X); |
---|
298 | return; |
---|
299 | } |
---|
300 | if (direction.Y != 0) |
---|
301 | { |
---|
302 | if (player.PlayerRight) player.Animation = player.PlayerWalkR; |
---|
303 | else player.Animation = Animation.Mirror(player.PlayerWalkR); |
---|
304 | |
---|
305 | } |
---|
306 | else player.PlayerAnimation(0); |
---|
307 | |
---|
308 | //if (direction == 0) player.Animation.Stop(); |
---|
309 | //else |
---|
310 | // player.Animation.Start(); |
---|
311 | |
---|
312 | } |
---|
313 | |
---|
314 | void HakkaaminenAlkaa(Player ukko) |
---|
315 | { |
---|
316 | if (pausegame == true) |
---|
317 | { |
---|
318 | if (Mouse.IsCursorOn(resumebutton) && (menuopened == true)) |
---|
319 | { |
---|
320 | AvaaPauseMenu(); |
---|
321 | } |
---|
322 | |
---|
323 | if (Mouse.IsCursorOn(quitbutton) && (menuopened == true)) |
---|
324 | { |
---|
325 | Exit(); |
---|
326 | } |
---|
327 | } |
---|
328 | |
---|
329 | if (menuopened == false) |
---|
330 | { |
---|
331 | foreach (Slot slotti in tavaraluettelo.ruudut) |
---|
332 | { |
---|
333 | if (slotti.Valittu && slotti.Resurssit.Count > 0) |
---|
334 | { |
---|
335 | Raahaa(slotti); |
---|
336 | return; |
---|
337 | } |
---|
338 | |
---|
339 | } |
---|
340 | } |
---|
341 | |
---|
342 | if (pausegame == false) |
---|
343 | { |
---|
344 | ukko.PelaajaHakkaamassa= true; |
---|
345 | //PlayerAnimationHit(ukko); |
---|
346 | ukko.Width = ukko.HakkaamisLeveys; |
---|
347 | |
---|
348 | List<GameObject> resurssit; |
---|
349 | double verticalAttackDistance = 1.5; |
---|
350 | double horizontalAttackDistance = 1.5; |
---|
351 | |
---|
352 | |
---|
353 | |
---|
354 | |
---|
355 | if (ukko.PlayerRight) |
---|
356 | { |
---|
357 | resurssit = GetObjectsBetween((ukko.Position + new Vector(ukko.Width * 0.8, ukko.Height * 0.2)), (ukko.Position + new Vector(ukko.Width * horizontalAttackDistance, ukko.Height + verticalAttackDistance))); |
---|
358 | ukko.Animation = ukko.PlayerHitR; |
---|
359 | |
---|
360 | } |
---|
361 | else |
---|
362 | { |
---|
363 | resurssit = GetObjectsBetween((ukko.Position - new Vector(ukko.Width * 0.8, ukko.Height * 0.2)), (ukko.Position - new Vector(ukko.Width * horizontalAttackDistance, -ukko.Height - verticalAttackDistance))); |
---|
364 | ukko.Animation = ukko.PlayerHitL; |
---|
365 | |
---|
366 | } |
---|
367 | ukko.Animation.Start(); |
---|
368 | if (resurssit.Count > 0) |
---|
369 | { |
---|
370 | foreach (GameObject resurssi in resurssit) |
---|
371 | { |
---|
372 | |
---|
373 | if (resurssi == null) return; |
---|
374 | if (resurssi.Tag.Equals("")) return; |
---|
375 | if (resurssi.Tag.Equals("Puu")) |
---|
376 | { |
---|
377 | PlaySound("Hit"); |
---|
378 | Tree puu = (Tree)resurssi; |
---|
379 | |
---|
380 | Slot slotti = tavaraluettelo.LisaaResurssi(wood, RandomGen.SelectOne(1, 2, 3)); |
---|
381 | puu.ElamaLaskuri.Value--; |
---|
382 | if (slotti != null) slotti.Image = woodItem; |
---|
383 | |
---|
384 | |
---|
385 | Slot slotti2 = null; |
---|
386 | const int OMENAMAHDOLLISUUS = 20; |
---|
387 | if (RandomGen.NextInt(OMENAMAHDOLLISUUS+1) >= OMENAMAHDOLLISUUS) slotti2 = tavaraluettelo.LisaaResurssi(apple, 1); |
---|
388 | |
---|
389 | if (slotti2 != null) slotti2.Image = apple.Image; |
---|
390 | |
---|
391 | |
---|
392 | |
---|
393 | //inventory.AddItem(wood, woodItem, RandomGen.SelectOne(5,6,7,8,9,10)); |
---|
394 | |
---|
395 | } |
---|
396 | if (resurssi.Tag.Equals("Kivi")) |
---|
397 | { |
---|
398 | PlaySound("Hit"); |
---|
399 | Stone stone = (Stone)resurssi; |
---|
400 | |
---|
401 | Slot slotti = tavaraluettelo.LisaaResurssi(stone, RandomGen.SelectOne(1, 2)); |
---|
402 | stone.ElamaLaskuri.Value -= 2; |
---|
403 | if (slotti != null) slotti.Image = stoneitemimg; |
---|
404 | |
---|
405 | |
---|
406 | |
---|
407 | //inventory.AddItem(wood, woodItem, RandomGen.SelectOne(5,6,7,8,9,10)); |
---|
408 | |
---|
409 | } |
---|
410 | |
---|
411 | } |
---|
412 | } |
---|
413 | |
---|
414 | } |
---|
415 | } |
---|
416 | |
---|
417 | void Raahaa(Slot slotti) |
---|
418 | { |
---|
419 | valittuEsine = new Drop(20,40); |
---|
420 | valittuEsine.Tag = "drop"; |
---|
421 | valittuEsine.luku = slotti.Maara; |
---|
422 | valittuEsine.Resurssit = new List<GameObject>(slotti.Resurssit); |
---|
423 | slotti.Maara = 0; |
---|
424 | slotti.Teksti.Text = ""; |
---|
425 | valittuEsine.Image = slotti.Image; |
---|
426 | slotti.Image = null; |
---|
427 | Add(valittuEsine,3); |
---|
428 | valittuEsine.Position = Mouse.PositionOnWorld; |
---|
429 | slotti.Resurssit.Clear(); |
---|
430 | slotti.Maara = 0; |
---|
431 | } |
---|
432 | |
---|
433 | void PelaajatTormaavat(PhysicsObject tormaaja, PhysicsObject kohde) |
---|
434 | { |
---|
435 | Drop maatavara = (Drop)kohde; |
---|
436 | Slot slotti = tavaraluettelo.LisaaResurssi(maatavara.Resurssit[0], maatavara.luku); |
---|
437 | slotti.Image = kohde.Image; |
---|
438 | kohde.Destroy(); |
---|
439 | MessageDisplay.Add("Bump!"); |
---|
440 | } |
---|
441 | |
---|
442 | public void HakkaaminenLoppuu(Player player) |
---|
443 | { |
---|
444 | if (valittuEsine != null) |
---|
445 | { |
---|
446 | foreach(Slot slotti in tavaraluettelo.ruudut) |
---|
447 | { |
---|
448 | double etaisyys = (slotti.AbsolutePosition - valittuEsine.Position).Magnitude; |
---|
449 | if (etaisyys < 100) |
---|
450 | { |
---|
451 | slotti.Image = valittuEsine.Image; |
---|
452 | |
---|
453 | MessageDisplay.Add("slotin " + tavaraluettelo.ruudut.IndexOf(slotti) + " etäisyys hiireen " + etaisyys); |
---|
454 | |
---|
455 | slotti.Resurssit = new List<GameObject>(valittuEsine.Resurssit); |
---|
456 | slotti.Teksti.Text = slotti.Resurssit.Count.ToString(); |
---|
457 | valittuEsine.luku = 0; |
---|
458 | valittuEsine.Resurssit.Clear(); |
---|
459 | valittuEsine.Destroy(); |
---|
460 | valittuEsine = null; |
---|
461 | foreach (Slot slotti2 in tavaraluettelo.ruudut) |
---|
462 | { |
---|
463 | if (slotti2.Resurssit.Count > 0) { |
---|
464 | MessageDisplay.Add("slotin " + tavaraluettelo.ruudut.IndexOf(slotti2) + " tavaroita " + slotti2.Resurssit[0].Tag.ToString() + " määrä " + slotti2.Resurssit.Count); |
---|
465 | } |
---|
466 | } |
---|
467 | return; |
---|
468 | } |
---|
469 | } |
---|
470 | valittuEsine = null; |
---|
471 | } |
---|
472 | |
---|
473 | ukko.PelaajaHakkaamassa = false; |
---|
474 | ukko.Animation.Stop(); |
---|
475 | ukko.Animation = null; |
---|
476 | ukko.Width = ukko.InitialLeveys; |
---|
477 | ukko.Image = ukko.PlayerImage; |
---|
478 | } |
---|
479 | public void PlayerAnimationHit(Player player) |
---|
480 | { |
---|
481 | // MessageDisplay.Clear(); |
---|
482 | // MessageDisplay.Add("PlayerRight = " + player.PlayerRight + ", direction = " + direction); |
---|
483 | |
---|
484 | return; |
---|
485 | if (player.Velocity.X != 0) |
---|
486 | { |
---|
487 | //player.PlayerAnimation(direction.X); |
---|
488 | |
---|
489 | } |
---|
490 | if (player.Velocity.Y != 0) |
---|
491 | { |
---|
492 | if (player.PlayerRight) player.Animation = player.PlayerHitR; |
---|
493 | else player.Animation = Animation.Mirror(player.PlayerHitR); |
---|
494 | |
---|
495 | } |
---|
496 | else player.PlayerAnimation(0); |
---|
497 | |
---|
498 | //if (direction == 0) player.Animation.Stop(); |
---|
499 | //else |
---|
500 | // player.Animation.Start(); |
---|
501 | |
---|
502 | } |
---|
503 | |
---|
504 | void LuoKivi(double x, double y) |
---|
505 | { |
---|
506 | Stone stone = new Stone(56, 50); |
---|
507 | stone.Image = stoneBottom; |
---|
508 | stone.TopImage = stoneTop; |
---|
509 | stone.MakeStatic(); |
---|
510 | stone.Tag = "Kivi"; |
---|
511 | stone.Shape = Shape.Circle; |
---|
512 | stone.Restitution = 0; |
---|
513 | stone.X = x; |
---|
514 | stone.Restitution = 0; |
---|
515 | stone.Y = y; |
---|
516 | Add(stone, 1); |
---|
517 | objektilista.Add(stone); |
---|
518 | for (int i = 0; i < objektilista.Count - 1; i++) |
---|
519 | { |
---|
520 | if ((stone.Position - objektilista[i].Position).Magnitude < 120) |
---|
521 | { |
---|
522 | objektilista.Remove(stone); |
---|
523 | stone.Destroy(); |
---|
524 | } |
---|
525 | } |
---|
526 | } |
---|
527 | |
---|
528 | void LuoKiviRndm() |
---|
529 | { |
---|
530 | for (int i = 0; i < 20; i++) |
---|
531 | { |
---|
532 | |
---|
533 | double x = RandomGen.NextDouble(Level.Left, Level.Right); |
---|
534 | double y = RandomGen.NextDouble(Level.Bottom, Level.Top); |
---|
535 | LuoKivi(x, y); |
---|
536 | } |
---|
537 | } |
---|
538 | void LuoPuu(double x, double y) |
---|
539 | { |
---|
540 | Tree puu = new Tree(15, 152); |
---|
541 | puu.MakeStatic(); |
---|
542 | puu.Image = treeTrunk; |
---|
543 | puu.Tag = "Puu"; |
---|
544 | puu.TopImage = treeLeaves; |
---|
545 | puu.Restitution = 0; |
---|
546 | puu.X = x; |
---|
547 | puu.Y = y; |
---|
548 | Add(puu, 1); |
---|
549 | objektilista.Add(puu); |
---|
550 | for (int i = 0; i < objektilista.Count - 1; i++) |
---|
551 | { |
---|
552 | if ((puu.Position - objektilista[i].Position).Magnitude < 80) |
---|
553 | { |
---|
554 | objektilista.Remove(puu); |
---|
555 | puu.Destroy(); |
---|
556 | } |
---|
557 | } |
---|
558 | } |
---|
559 | |
---|
560 | void LuoPuuRndm() |
---|
561 | { |
---|
562 | for (int i = 0; i < 150; i++) |
---|
563 | { |
---|
564 | |
---|
565 | double x = RandomGen.NextDouble(Level.Left, Level.Right); |
---|
566 | double y = RandomGen.NextDouble(Level.Bottom, Level.Top); |
---|
567 | LuoPuu(x, y); |
---|
568 | } |
---|
569 | } |
---|
570 | } |
---|
571 | public class Player : PhysicsObject |
---|
572 | { |
---|
573 | public bool PlayerRight = true; |
---|
574 | public Animation PlayerWalkR; |
---|
575 | public Animation PlayerWalkL; |
---|
576 | public Animation PlayerHitR; |
---|
577 | public Animation PlayerHitL; |
---|
578 | public Image PlayerImage; |
---|
579 | public Image PlayerImageRight; |
---|
580 | public Image PlayerImageLeft; |
---|
581 | public double InitialLeveys; |
---|
582 | public double HakkaamisLeveys; |
---|
583 | public bool PelaajaHakkaamassa = false; |
---|
584 | |
---|
585 | |
---|
586 | public Player(double leveys, double korkeus) |
---|
587 | : base(leveys, korkeus) |
---|
588 | { |
---|
589 | InitialLeveys = leveys; |
---|
590 | HakkaamisLeveys = leveys * 12 / 6; |
---|
591 | //idleRight = Selviytyja.Move(); |
---|
592 | } |
---|
593 | public void PlayerAnimation(double direction) |
---|
594 | { |
---|
595 | |
---|
596 | if (direction == 0 && PlayerImage == PlayerImageLeft) |
---|
597 | { |
---|
598 | |
---|
599 | if (PlayerRight) |
---|
600 | { |
---|
601 | PlayerRight = true; |
---|
602 | PlayerImage = (PlayerImageRight); |
---|
603 | } |
---|
604 | if (PelaajaHakkaamassa) return; |
---|
605 | this.Animation = null; |
---|
606 | this.Image = (PlayerImage); |
---|
607 | |
---|
608 | return; |
---|
609 | } |
---|
610 | else if (direction == 0 && PlayerImage == PlayerImageRight) |
---|
611 | { |
---|
612 | |
---|
613 | if (PlayerRight == false) |
---|
614 | { |
---|
615 | PlayerRight = false; |
---|
616 | PlayerImage = (PlayerImageLeft); |
---|
617 | } |
---|
618 | if (PelaajaHakkaamassa) return; |
---|
619 | this.Animation = null; |
---|
620 | this.Image = (PlayerImage); |
---|
621 | |
---|
622 | return; |
---|
623 | } |
---|
624 | |
---|
625 | if (direction < 0) |
---|
626 | { |
---|
627 | PlayerRight = false; |
---|
628 | if (PelaajaHakkaamassa) return; |
---|
629 | if (this.Animation.IsPlaying == false) |
---|
630 | { |
---|
631 | this.Animation = Animation.Mirror(PlayerWalkR); |
---|
632 | this.Animation.Start(); |
---|
633 | } |
---|
634 | } |
---|
635 | else if (direction > 0) |
---|
636 | { |
---|
637 | PlayerRight = true; |
---|
638 | if (PelaajaHakkaamassa) return; |
---|
639 | if (this.Animation.IsPlaying == false) |
---|
640 | { |
---|
641 | this.Animation = PlayerWalkR; |
---|
642 | this.Animation.Start(); |
---|
643 | } |
---|
644 | } |
---|
645 | if (this.Animation != null) this.Animation.FPS = 3; |
---|
646 | |
---|
647 | |
---|
648 | |
---|
649 | |
---|
650 | |
---|
651 | |
---|
652 | } |
---|
653 | |
---|
654 | } |
---|
655 | |
---|
656 | |
---|
657 | |
---|
658 | public class Drop : PhysicsObject |
---|
659 | { |
---|
660 | public int luku = 10; |
---|
661 | public List<GameObject> Resurssit = new List<GameObject>(); |
---|
662 | |
---|
663 | public Drop(double leveys, double korkeus) |
---|
664 | : base(leveys, korkeus * 0.5) |
---|
665 | { |
---|
666 | |
---|
667 | } |
---|
668 | |
---|
669 | } |
---|
670 | public class Tree : PhysicsObject |
---|
671 | { |
---|
672 | GameObject Lehti; |
---|
673 | private IntMeter elamaLaskuri = new IntMeter(15, 0, 15); |
---|
674 | public IntMeter ElamaLaskuri { get { return elamaLaskuri; } } |
---|
675 | |
---|
676 | public Image TopImage |
---|
677 | { |
---|
678 | get { return Lehti.Image; } |
---|
679 | set { Lehti.Image = value; } |
---|
680 | } |
---|
681 | |
---|
682 | |
---|
683 | public Tree(double leveys, double korkeus) |
---|
684 | : base(leveys, korkeus * 0.5) |
---|
685 | { |
---|
686 | elamaLaskuri.LowerLimit += delegate { this.Destroy(); }; |
---|
687 | |
---|
688 | this.Y -= korkeus * 0.25; |
---|
689 | Lehti = new GameObject(leveys * 5, korkeus * 0.5); |
---|
690 | Lehti.Bottom = this.Top; |
---|
691 | this.Add(Lehti); |
---|
692 | } |
---|
693 | |
---|
694 | } |
---|
695 | |
---|
696 | public class Stone : PhysicsObject |
---|
697 | { |
---|
698 | GameObject stonetop; |
---|
699 | PhysicsObject stonebottom; |
---|
700 | private IntMeter elamaLaskuri = new IntMeter(15, 0, 15); |
---|
701 | public IntMeter ElamaLaskuri { get { return elamaLaskuri; } } |
---|
702 | |
---|
703 | public Image TopImage |
---|
704 | { |
---|
705 | get { return stonetop.Image; } |
---|
706 | set { stonetop.Image = value; } |
---|
707 | } |
---|
708 | |
---|
709 | |
---|
710 | public Stone(double leveys, double korkeus) |
---|
711 | : base(leveys, korkeus * 0.5) |
---|
712 | { |
---|
713 | elamaLaskuri.LowerLimit += delegate { this.Destroy(); }; |
---|
714 | stonetop = new GameObject(leveys, korkeus * 0.5); |
---|
715 | stonetop.Bottom = this.Top; |
---|
716 | this.Add(stonetop); |
---|
717 | } |
---|
718 | |
---|
719 | } |
---|
720 | |
---|
721 | /// <summary> |
---|
722 | /// Esinevalikko. |
---|
723 | /// </summary> |
---|
724 | class Inventory : Widget |
---|
725 | { |
---|
726 | /// <summary> |
---|
727 | /// Tapahtuma, kun esine on valittu. |
---|
728 | /// </summary> |
---|
729 | public event Action<PhysicsObject> ItemSelected; |
---|
730 | |
---|
731 | /// <summary> |
---|
732 | /// Luo uuden esinevalikon. |
---|
733 | /// </summary> |
---|
734 | public Inventory() |
---|
735 | : base(new HorizontalLayout()) |
---|
736 | { |
---|
737 | } |
---|
738 | |
---|
739 | /// <summary> |
---|
740 | /// Lisää esineen. |
---|
741 | /// </summary> |
---|
742 | /// <param name="item">Lisättävä esine.</param> |
---|
743 | /// <param name="kuva">Esineen ikoni, joka näkyy valikossa.</param> |
---|
744 | public void AddItem(PhysicsObject item, Image kuva, int tavaraluku) |
---|
745 | { |
---|
746 | PushButton icon = new PushButton(kuva); |
---|
747 | Add(icon); |
---|
748 | icon.Clicked += delegate () { SelectItem(item); }; |
---|
749 | } |
---|
750 | |
---|
751 | void SelectItem(PhysicsObject item) |
---|
752 | { |
---|
753 | if (ItemSelected != null) |
---|
754 | { |
---|
755 | ItemSelected(item); |
---|
756 | } |
---|
757 | } |
---|
758 | } |
---|
759 | |
---|
760 | class Tavaraluettelo : GameObject |
---|
761 | { |
---|
762 | GameObject ruutu; |
---|
763 | public List<GameObject> ruudut = new List<GameObject>(); |
---|
764 | |
---|
765 | |
---|
766 | double koko = 100; |
---|
767 | double erotus = 20; |
---|
768 | public Tavaraluettelo(double leveys, double korkeus, Image kuva) |
---|
769 | : base(leveys, korkeus) |
---|
770 | { |
---|
771 | ruutu = new GameObject(leveys, korkeus); |
---|
772 | this.Image = kuva; |
---|
773 | Lisaaruudut(5); |
---|
774 | } |
---|
775 | |
---|
776 | void Lisaaruudut(int maara) |
---|
777 | { |
---|
778 | for (int i = 0; i < maara; i++) |
---|
779 | { |
---|
780 | Slot slot1 = new Slot(koko - erotus, koko - erotus); |
---|
781 | double xKoord = i * 1.22 * koko - 248; |
---|
782 | slot1.X = xKoord; |
---|
783 | slot1.Teksti.Position = slot1.Position; |
---|
784 | ruudut.Add(slot1); |
---|
785 | slot1.Color = Color.Transparent; |
---|
786 | //slot1.IsVisible = false;' |
---|
787 | slot1.initialpos = slot1.Position; |
---|
788 | Add(slot1); |
---|
789 | Add(slot1.Teksti); |
---|
790 | } |
---|
791 | } |
---|
792 | |
---|
793 | public Slot LisaaResurssi(GameObject resurssi, int count) |
---|
794 | { |
---|
795 | foreach (Slot slotti in ruudut) |
---|
796 | { |
---|
797 | if (slotti.Maara >= slotti.MaxMaara) slotti.Maara = slotti.MaxMaara; |
---|
798 | if (slotti.Resurssit.Count >= slotti.MaxMaara) slotti.Maara = slotti.MaxMaara; |
---|
799 | slotti.Teksti.Text = slotti.Maara.ToString(); |
---|
800 | if (slotti.Resurssit.Count == 0) |
---|
801 | slotti.Teksti.Text = ""; |
---|
802 | if (slotti.Resurssit.Count > 0 && slotti.Resurssit[0].Equals(resurssi)) |
---|
803 | { |
---|
804 | if (slotti.Maara >= slotti.MaxMaara) continue; |
---|
805 | for (int i = 0; i < count; i++) |
---|
806 | { |
---|
807 | slotti.Resurssit.Add(resurssi); |
---|
808 | slotti.Maara = slotti.Resurssit.Count; |
---|
809 | if (slotti.Maara > slotti.MaxMaara) slotti.Maara = slotti.MaxMaara; |
---|
810 | slotti.Teksti.Text = slotti.Maara.ToString(); |
---|
811 | } |
---|
812 | return slotti; |
---|
813 | } |
---|
814 | } |
---|
815 | foreach (Slot slotti in ruudut) |
---|
816 | { |
---|
817 | if (slotti.Resurssit.Count == 0) |
---|
818 | { |
---|
819 | for (int i = 0; i < count; i++) |
---|
820 | { |
---|
821 | slotti.Resurssit.Add(resurssi); |
---|
822 | slotti.Maara = slotti.Resurssit.Count; |
---|
823 | if (slotti.Maara > slotti.MaxMaara) slotti.Maara = slotti.MaxMaara; |
---|
824 | slotti.Teksti.Text = slotti.Maara.ToString(); |
---|
825 | |
---|
826 | } |
---|
827 | return slotti; |
---|
828 | } |
---|
829 | } |
---|
830 | return null; |
---|
831 | } |
---|
832 | } |
---|
833 | |
---|
834 | public class Slot : GameObject |
---|
835 | { |
---|
836 | public Label Teksti; |
---|
837 | public Vector initialpos; |
---|
838 | public int Maara = 0; |
---|
839 | public int MaxMaara = 255; |
---|
840 | public List<GameObject> Resurssit = new List<GameObject>(); |
---|
841 | public bool Valittu = false; |
---|
842 | public Slot(double leveys, double korkeus) |
---|
843 | : base(leveys, korkeus) |
---|
844 | { |
---|
845 | Teksti = new Label(); |
---|
846 | //Teksti.Text = Maara.ToString(); |
---|
847 | } |
---|
848 | } |
---|