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