1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using System.Text; |
---|
5 | using Jypeli; |
---|
6 | using Jypeli.Controls; |
---|
7 | using MathHelper; |
---|
8 | |
---|
9 | namespace Entity |
---|
10 | { |
---|
11 | class Player : PhysicsObject |
---|
12 | { |
---|
13 | public const double playerWidth = 75; |
---|
14 | public const double playerHeight = 75; |
---|
15 | // private PhysicsObject head; |
---|
16 | private Vector movementVector = Vector.Zero; |
---|
17 | private bool isMoving = false; |
---|
18 | private Keyboard keyboard; |
---|
19 | private double speed = 200; |
---|
20 | |
---|
21 | public Player(Vector pos) |
---|
22 | : base(playerWidth, playerHeight) |
---|
23 | { |
---|
24 | keyboard = Game.Keyboard; |
---|
25 | setupKeys(); |
---|
26 | } |
---|
27 | |
---|
28 | private void setupKeys() |
---|
29 | { |
---|
30 | keyboard.Listen(Key.W, ButtonState.Down, move, "Move Up", new Vector(0, speed)); |
---|
31 | keyboard.Listen(Key.A, ButtonState.Down, move, "Move Left", new Vector(-speed, 0)); |
---|
32 | keyboard.Listen(Key.S, ButtonState.Down, move, "Move Down", new Vector(0, -speed)); |
---|
33 | keyboard.Listen(Key.D, ButtonState.Down, move, "Move Right", new Vector(speed, 0)); |
---|
34 | |
---|
35 | keyboard.Listen(Key.W, ButtonState.Released, removeMovement, null, new Vector(0, speed)); |
---|
36 | keyboard.Listen(Key.A, ButtonState.Released, removeMovement, null, new Vector(-speed, 0)); |
---|
37 | keyboard.Listen(Key.S, ButtonState.Released, removeMovement, null, new Vector(0, -speed)); |
---|
38 | keyboard.Listen(Key.D, ButtonState.Released, removeMovement, null, new Vector(speed, 0)); |
---|
39 | } |
---|
40 | |
---|
41 | public override void Update(Time time) |
---|
42 | { |
---|
43 | if (!isMoving) |
---|
44 | { |
---|
45 | movementVector = Vector.Zero; |
---|
46 | Stop(); |
---|
47 | } |
---|
48 | |
---|
49 | isMoving = false; |
---|
50 | base.Update(time); |
---|
51 | } |
---|
52 | |
---|
53 | public void move(Vector vec) |
---|
54 | { |
---|
55 | if (vec.X != 0) |
---|
56 | movementVector.X = vec.X; |
---|
57 | if (vec.Y != 0) |
---|
58 | movementVector.Y = vec.Y; |
---|
59 | |
---|
60 | Move(movementVector); |
---|
61 | isMoving = true; |
---|
62 | } |
---|
63 | |
---|
64 | public void removeMovement(Vector vec) |
---|
65 | { |
---|
66 | movementVector = VecMath.sub(movementVector, vec); |
---|
67 | } |
---|
68 | } |
---|
69 | } |
---|