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