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