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 | public Vector movementVector = Vector.Zero; |
---|
17 | public bool isMoving = false; |
---|
18 | private Keyboard keyboard; |
---|
19 | public double speed = 200; |
---|
20 | |
---|
21 | public Player(Game game, Vector pos) |
---|
22 | : base(playerWidth, playerHeight) |
---|
23 | { |
---|
24 | keyboard = game.Keyboard; |
---|
25 | CanRotate = false; |
---|
26 | // MakeStatic(); |
---|
27 | setupKeys(); |
---|
28 | } |
---|
29 | |
---|
30 | private void setupKeys() |
---|
31 | { |
---|
32 | keyboard.Listen(Key.W, ButtonState.Down, move, "Move Up", new Vector(0, speed)); |
---|
33 | keyboard.Listen(Key.A, ButtonState.Down, move, "Move Left", new Vector(-speed, 0)); |
---|
34 | keyboard.Listen(Key.S, ButtonState.Down, move, "Move Down", new Vector(0, -speed)); |
---|
35 | keyboard.Listen(Key.D, ButtonState.Down, move, "Move Right", new Vector(speed, 0)); |
---|
36 | |
---|
37 | keyboard.Listen(Key.W, ButtonState.Released, removeMovement, null, new Vector(0, speed)); |
---|
38 | keyboard.Listen(Key.A, ButtonState.Released, removeMovement, null, new Vector(-speed, 0)); |
---|
39 | keyboard.Listen(Key.S, ButtonState.Released, removeMovement, null, new Vector(0, -speed)); |
---|
40 | keyboard.Listen(Key.D, ButtonState.Released, removeMovement, null, new Vector(speed, 0)); |
---|
41 | } |
---|
42 | |
---|
43 | public override void Update(Time time) |
---|
44 | { |
---|
45 | if (!isMoving) |
---|
46 | { |
---|
47 | movementVector = Vector.Zero; |
---|
48 | Stop(); |
---|
49 | } |
---|
50 | |
---|
51 | isMoving = false; |
---|
52 | |
---|
53 | base.Update(time); |
---|
54 | } |
---|
55 | |
---|
56 | public void move(Vector vec) |
---|
57 | { |
---|
58 | if (vec.X != 0) |
---|
59 | movementVector.X = vec.X; |
---|
60 | if (vec.Y != 0) |
---|
61 | movementVector.Y = vec.Y; |
---|
62 | |
---|
63 | Move(movementVector); |
---|
64 | isMoving = true; |
---|
65 | } |
---|
66 | |
---|
67 | public void removeMovement(Vector vec) |
---|
68 | { |
---|
69 | movementVector = VecMath.sub(movementVector, vec); |
---|
70 | } |
---|
71 | } |
---|
72 | } |
---|