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 Player : PhysicsObject |
---|
10 | { |
---|
11 | const double defaultMoveSpeed = 1000; |
---|
12 | double speed = defaultMoveSpeed; |
---|
13 | private static Image image = Game.LoadImage("Player"); |
---|
14 | |
---|
15 | public Player(double width, double height, bool addDefaultControls) : base(width, height) |
---|
16 | { |
---|
17 | this.Tag = "player"; |
---|
18 | this.Shape = Shape.Rectangle; |
---|
19 | this.Image = image; |
---|
20 | this.LinearDamping = 0.9; |
---|
21 | |
---|
22 | if (addDefaultControls) |
---|
23 | SetDefaultControls(); |
---|
24 | |
---|
25 | this.IsUpdated = true; |
---|
26 | |
---|
27 | |
---|
28 | } |
---|
29 | |
---|
30 | public bool GiveItem(String itemName) |
---|
31 | { |
---|
32 | if (itemName == "cannon") |
---|
33 | { |
---|
34 | Cannon weapon = new Cannon(75, 25); |
---|
35 | this.Add(weapon); |
---|
36 | return true; |
---|
37 | } |
---|
38 | return false; |
---|
39 | } |
---|
40 | |
---|
41 | public void SetDefaultControls() |
---|
42 | { |
---|
43 | Game.Keyboard.Listen(Key.W, ButtonState.Down, delegate { this.Push(new Vector(0, speed)); }, null); |
---|
44 | Game.Keyboard.Listen(Key.S, ButtonState.Down, delegate { this.Push(new Vector(0, -speed)); }, null); |
---|
45 | Game.Keyboard.Listen(Key.A, ButtonState.Down, delegate { this.Push(new Vector(-speed, 0)); }, null); |
---|
46 | Game.Keyboard.Listen(Key.D, ButtonState.Down, delegate { this.Push(new Vector(speed, 0)); }, null); |
---|
47 | Game.Mouse.Listen(MouseButton.Left, ButtonState.Pressed, Shoot, null); |
---|
48 | } |
---|
49 | |
---|
50 | public void Shoot() |
---|
51 | { |
---|
52 | Game.MessageDisplay.Add("PEW PEW!"); |
---|
53 | } |
---|
54 | |
---|
55 | public override void Update(Time time) |
---|
56 | { |
---|
57 | this.Angle = (Game.Mouse.PositionOnWorld - this.AbsolutePosition).Normalize().Angle - Angle.FromDegrees(90); |
---|
58 | base.Update(time); |
---|
59 | } |
---|
60 | } |
---|