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 | const double defaultWeaponPower = 10; |
---|
13 | private static Image playerImage = Game.LoadImage("Player"); |
---|
14 | |
---|
15 | Weapon weapon; |
---|
16 | double speed = defaultMoveSpeed; |
---|
17 | double power = defaultWeaponPower; |
---|
18 | IntMeter healt; |
---|
19 | |
---|
20 | public Player(double width, double height, int healt, bool addDefaultControls) : base(width, height) |
---|
21 | { |
---|
22 | this.Tag = "player"; |
---|
23 | this.Shape = Shape.Rectangle; |
---|
24 | this.Image = playerImage; |
---|
25 | this.LinearDamping = 0.9; |
---|
26 | |
---|
27 | healt = new IntMeter(healt, 0, healt); |
---|
28 | |
---|
29 | if (addDefaultControls) |
---|
30 | SetControls(Key.W, Key.S, Key.D, Key.A, MouseButton.Left); |
---|
31 | |
---|
32 | this.IsUpdated = true; |
---|
33 | } |
---|
34 | |
---|
35 | public bool PowerUp(String type, int value) |
---|
36 | { |
---|
37 | if (type == "speed") |
---|
38 | { |
---|
39 | speed = defaultMoveSpeed + value; |
---|
40 | return true; |
---|
41 | } |
---|
42 | |
---|
43 | return false; |
---|
44 | } |
---|
45 | |
---|
46 | public bool GiveWeapon(String itemName) |
---|
47 | { |
---|
48 | RemoveWeapon(); |
---|
49 | if (itemName.ToLower() == "cannon") |
---|
50 | weapon = new Cannon(75, 25); |
---|
51 | else if (itemName.ToLower() == "plasmacannon") |
---|
52 | weapon = new PlasmaCannon(75, 25); |
---|
53 | |
---|
54 | if (weapon != null) |
---|
55 | { |
---|
56 | weapon.Angle = Angle.FromDegrees(90); |
---|
57 | this.Add(weapon); |
---|
58 | return true; |
---|
59 | } |
---|
60 | return false; |
---|
61 | } |
---|
62 | |
---|
63 | public bool RemoveWeapon() |
---|
64 | { |
---|
65 | if (weapon != null) |
---|
66 | { |
---|
67 | weapon.Destroy(); |
---|
68 | return true; |
---|
69 | } |
---|
70 | return false; |
---|
71 | } |
---|
72 | |
---|
73 | public void SetControls(Key forward, Key back, Key right, Key left, MouseButton shoot) |
---|
74 | { |
---|
75 | Game.Keyboard.Listen(forward, ButtonState.Down, delegate { MovePlayer(new Vector(0, speed)); }, null); |
---|
76 | Game.Keyboard.Listen(back, ButtonState.Down, delegate { MovePlayer(new Vector(0, -speed)); }, null); |
---|
77 | Game.Keyboard.Listen(right, ButtonState.Down, delegate { MovePlayer(new Vector(speed, 0)); }, null); |
---|
78 | Game.Keyboard.Listen(left, ButtonState.Down, delegate { MovePlayer(new Vector(-speed, 0)); }, null); |
---|
79 | Game.Mouse.Listen(shoot, ButtonState.Pressed, Shoot, null); |
---|
80 | } |
---|
81 | |
---|
82 | private void MovePlayer(Vector force) |
---|
83 | { |
---|
84 | this.Push(force); |
---|
85 | } |
---|
86 | |
---|
87 | public void Shoot() |
---|
88 | { |
---|
89 | if (weapon != null) |
---|
90 | { |
---|
91 | weapon.Shoot(); |
---|
92 | } |
---|
93 | else |
---|
94 | Game.MessageDisplay.Add("Weapon missing"); |
---|
95 | } |
---|
96 | |
---|
97 | public override void Update(Time time) |
---|
98 | { |
---|
99 | this.Angle = (Game.Mouse.PositionOnWorld - this.AbsolutePosition).Normalize().Angle - Angle.FromDegrees(90); |
---|
100 | base.Update(time); |
---|
101 | } |
---|
102 | } |
---|