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

Revision 8864, 7.0 KB checked in by npo17_40, 6 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    public Image puuimg = LoadImage("Tree_0");
14    public Image stoneimg = LoadImage("Stone_0");
15    private Image[] playerWalkR = LoadImages("PlayerWalkRight_0","PlayerWalkRight_1");
16    Image idleLeft;
17    private Image[] playerWalkL;
18    Image Bg = LoadImage("grass");
19    List<PhysicsObject> objektilista = new List<PhysicsObject>();
20    public override void Begin()
21    {
22        // TODO: Kirjoita ohjelmakoodisi tähän
23        SmoothTextures = false;
24        Player ukko = new Player(50, 50);
25        //ukko.MakeStatic();
26        ukko.Image = idleRight;
27        ukko.CanRotate = false;
28        ukko.PlayerImageRight = idleRight;
29        ukko.PlayerImageLeft = Image.Mirror(idleRight.Clone());
30        ukko.PlayerImage = ukko.PlayerImageRight;
31        //idleLeft = Image.Mirror(idleRight);
32        //playerWalkL = playerWalkR;
33        //playerWalkL[1] = Image.Mirror(playerWalkR[1]).Clone();
34        //playerWalkL[0] = Image.Mirror(playerWalkR[0]).Clone();
35        ukko.PlayerWalkR = new Animation(playerWalkR);
36        //ukko.PlayerWalkL = Animation.Mirror(ukko.PlayerWalkR.cl);
37        ukko.Restitution = 0;
38        Level.Background.Color = Color.Black;
39        Add(ukko);
40        Level.Background.Image = Bg;
41        Level.Background.TileToLevel();
42        Camera.ZoomToLevel();
43        LuoKiviRndm();
44        LuoPuuRndm();
45
46
47
48        Keyboard.Listen(Key.D, ButtonState.Down, MoveSideways, null, ukko, new Vector(Speed, 0));
49        Keyboard.Listen(Key.D, ButtonState.Released, MoveSideways, null, ukko, Vector.Zero);
50        Keyboard.Listen(Key.A, ButtonState.Down, MoveSideways, null, ukko, new Vector(-Speed, 0));
51        Keyboard.Listen(Key.A, ButtonState.Released, MoveSideways, null, ukko, Vector.Zero);
52        Keyboard.Listen(Key.W, ButtonState.Down,     MoveVertically, null, ukko, new Vector(0,Speed));
53        Keyboard.Listen(Key.W, ButtonState.Released, MoveVertically, null, ukko, Vector.Zero);
54        Keyboard.Listen(Key.S, ButtonState.Down,     MoveVertically, null, ukko, new Vector(0,-Speed));
55        Keyboard.Listen(Key.S, ButtonState.Released, MoveVertically, null, ukko, Vector.Zero);
56
57
58
59
60        PhoneBackButton.Listen(ConfirmExit, "Lopeta peli");
61        Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, "Lopeta peli");
62
63     
64    }
65    void MoveSideways(Player pelaaja, Vector direction)
66    {
67        pelaaja.Velocity = new Vector(direction.X, pelaaja.Velocity.Y);
68        PlayerAnimation(pelaaja, direction);
69    }
70
71    void MoveVertically(Player pelaaja, Vector direction)
72    {
73        pelaaja.Velocity = new Vector(pelaaja.Velocity.X, direction.Y);
74
75        PlayerAnimation(pelaaja, direction);
76    }
77
78    public void PlayerAnimation(Player player, Vector direction)
79    {
80        MessageDisplay.Clear();
81        MessageDisplay.Add("PlayerRight = " + player.PlayerRight + ", direction = " + direction);
82        if (direction.X != 0)
83        {
84            player.PlayerAnimation(direction.X);
85            return;
86        }
87        if (direction.Y != 0)
88        {
89            if (player.PlayerRight) player.Animation = player.PlayerWalkR;
90            else player.Animation = Animation.Mirror(player.PlayerWalkR);
91
92        }
93        else player.PlayerAnimation(0);
94
95        //if (direction == 0) player.Animation.Stop();
96        //else
97        //    player.Animation.Start();
98
99    }
100
101    void LuoKivi(double x,double y)
102    {
103        PhysicsObject stone = PhysicsObject.CreateStaticObject(40, 40);
104        stone.Image = stoneimg;
105        stone.Shape = Shape.Circle;
106        stone.X = x;
107        stone.Restitution = 0;
108        stone.Y = y;
109        Add(stone);
110        objektilista.Add(stone);
111        for (int i = 0; i < objektilista.Count-1; i++)
112        {
113            if ((stone.Position - objektilista[i].Position).Magnitude < 90 )
114            {
115                objektilista.Remove(stone);
116                stone.Destroy();
117            }
118        }
119    }
120
121    void LuoKiviRndm()
122    {
123        for (int i = 0; i < 20; i++)
124        {
125           
126            double x = RandomGen.NextDouble(Level.Left,Level.Right);
127            double y = RandomGen.NextDouble(Level.Bottom, Level.Top);
128            LuoKivi(x, y);
129        }
130    }
131    void LuoPuu(double x, double y)
132    {
133        PhysicsObject puu = PhysicsObject.CreateStaticObject(100, 100);
134        puu.Image = puuimg;
135        puu.X = x;
136        puu.Y = y;
137        Add(puu);
138        objektilista.Add(puu);
139        for (int i = 0; i < objektilista.Count - 1; i++)
140        {
141            if ((puu.Position - objektilista[i].Position).Magnitude < 90)
142            {
143                objektilista.Remove(puu);
144                puu.Destroy();
145            }
146        }
147    }
148
149    void LuoPuuRndm()
150    {
151        for (int i = 0; i < 20; i++)
152        {
153
154            double x = RandomGen.NextDouble(Level.Left, Level.Right);
155            double y = RandomGen.NextDouble(Level.Bottom, Level.Top);
156            LuoPuu(x, y);
157        }
158    }
159}
160public class Player : PhysicsObject
161{
162    public bool PlayerRight = true;
163    public Animation PlayerWalkR;
164    public Animation PlayerWalkL;
165    public Image PlayerImage;
166    public Image PlayerImageRight;
167    public Image PlayerImageLeft;
168
169    public Player(double leveys, double korkeus)
170        : base (leveys, korkeus)
171    {
172        //idleRight = Selviytyja.Move();
173 }
174    public void PlayerAnimation(double direction)
175    {
176
177        if (direction == 0 && PlayerImage == PlayerImageLeft)
178        {
179            this.Animation = null;
180            if (PlayerRight)
181            {
182                PlayerRight = true;
183                PlayerImage = (PlayerImageRight);
184            }
185            this.Image = (PlayerImage);
186           
187            return;
188        }
189        else if (direction == 0 && PlayerImage == PlayerImageRight)
190        {
191            this.Animation = null;
192            if (PlayerRight == false)
193            {
194                PlayerRight = false;
195                PlayerImage = (PlayerImageLeft);
196            }
197                this.Image = (PlayerImage);
198                     
199            return;
200        }
201
202        if (direction < 0)
203        {
204            PlayerRight = false;
205            if (this.Animation.IsPlaying == false)
206            {
207                this.Animation = Animation.Mirror(PlayerWalkR);
208                this.Animation.Start();
209            }
210        }
211        else if (direction > 0)
212        {
213            PlayerRight = true;
214            if (this.Animation.IsPlaying == false)
215            {
216                this.Animation = PlayerWalkR;
217                this.Animation.Start();
218            }
219        }
220        if (this.Animation != null) this.Animation.FPS = 3;
221
222
223
224
225
226       
227    }
228
229}
Note: See TracBrowser for help on using the repository browser.