source: 2017/27/EeliN/Selviytyja/Selviytyja/Selviytyja/Selviytyja.cs @ 8842

Revision 8842, 3.9 KB checked in by npo17_40, 2 years ago (diff)
Line 
1using System;
2using System.Collections.Generic;
3using Jypeli;
4using Jypeli.Assets;
5using Jypeli.Controls;
6using Jypeli.Effects;
7using Jypeli.Widgets;
8
9public class Selviytyja : PhysicsGame
10{
11    int Speed = 100;
12    public Image idleRight = LoadImage("PlayerIdleRight_0");
13    private Image[] playerWalkR = LoadImages("PlayerWalkRight_0","PlayerWalkRight_1");
14    Image idleLeft;
15    private Image[] playerWalkL;
16    public override void Begin()
17    {
18        // TODO: Kirjoita ohjelmakoodisi tähän
19        SmoothTextures = false;
20        Player ukko = new Player(50, 50);
21        ukko.Image = idleRight;
22        //idleLeft = Image.Mirror(idleRight);
23        //playerWalkL = playerWalkR;
24        //playerWalkL[1] = Image.Mirror(playerWalkR[1]).Clone();
25        //playerWalkL[0] = Image.Mirror(playerWalkR[0]).Clone();
26        ukko.PlayerWalkR = new Animation(playerWalkR);
27        //ukko.PlayerWalkL = Animation.Mirror(ukko.PlayerWalkR);
28        Level.Background.Color = Color.Black;
29        Add(ukko);
30
31
32        Keyboard.Listen(Key.D, ButtonState.Down, MoveSideways, null, ukko, new Vector(Speed, 0));
33        Keyboard.Listen(Key.D, ButtonState.Released, MoveSideways, null, ukko, Vector.Zero);
34        Keyboard.Listen(Key.A, ButtonState.Down, MoveSideways, null, ukko, new Vector(-Speed, 0));
35        Keyboard.Listen(Key.A, ButtonState.Released, MoveSideways, null, ukko, Vector.Zero);
36        Keyboard.Listen(Key.W, ButtonState.Down,     MoveVertically, null, ukko, new Vector(0,Speed));
37        Keyboard.Listen(Key.W, ButtonState.Released, MoveVertically, null, ukko, Vector.Zero);
38        Keyboard.Listen(Key.S, ButtonState.Down,     MoveVertically, null, ukko, new Vector(0,-Speed));
39        Keyboard.Listen(Key.S, ButtonState.Released, MoveVertically, null, ukko, Vector.Zero);
40
41
42
43        PhoneBackButton.Listen(ConfirmExit, "Lopeta peli");
44        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
45    }
46    void MoveSideways(Player pelaaja, Vector direction)
47    {
48        pelaaja.Velocity = new Vector(direction.X, pelaaja.Velocity.Y);
49        PlayerAnimation(pelaaja, direction.X);
50    }
51
52    void MoveVertically(Player pelaaja, Vector direction)
53    {
54        pelaaja.Velocity = new Vector(pelaaja.Velocity.X, direction.Y);
55        //PlayerAnimation(pelaaja, direction.X);
56    }
57
58    public void PlayerAnimation(Player player, double direction)
59    {
60        player.PlayerAnimation(direction);
61        //if (direction == 0) player.Animation.Stop();
62        //else
63        //    player.Animation.Start();
64       
65    }
66}
67public class Player : PhysicsObject
68{
69    public bool PlayerRight = true;
70    public Animation PlayerWalkR;
71    public Animation PlayerWalkL;
72    public Image PlayerImage;
73
74    public Player(double leveys, double korkeus)
75        : base (leveys, korkeus)
76    {
77        //idleRight = Selviytyja.Move();
78    }
79    public void PlayerAnimation(double direction)
80    {
81        if (direction < 0)
82        {
83            this.Animation = Animation.Mirror(PlayerWalkR);
84            this.Animation.Start();
85        }
86        else if (direction > 0)
87        {
88            this.Animation = PlayerWalkR;
89            this.Animation.Start();
90        }
91
92
93        if (PlayerRight == true)
94        {
95            PlayerImage = Image.Mirror(this.Image);
96            this.Image = Image.Mirror(this.Image);
97            PlayerRight = false;
98        }
99        else if (PlayerRight == false)
100        {
101            PlayerImage = Image.Mirror(this.Image);
102            this.Image = Image.Mirror(this.Image);
103            PlayerRight = true;
104        }
105
106
107
108        if (direction == 0)
109        {
110            //this.Animation = this.Image;
111            this.Animation = null;
112            this.Image = PlayerImage;
113            //return;
114        }
115        if (this.Animation != null) this.Animation.FPS = 3;
116    }
117}
Note: See TracBrowser for help on using the repository browser.