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 | private static Image playerImage = Game.LoadImage("Player"); |
---|
12 | |
---|
13 | private G game; |
---|
14 | |
---|
15 | |
---|
16 | const double defaultSpeed = 1000; |
---|
17 | const int defaultPower = 10000; |
---|
18 | private double speed = defaultSpeed; |
---|
19 | private double power = defaultPower; |
---|
20 | |
---|
21 | private Weapon weapon; |
---|
22 | public IntMeter health; |
---|
23 | public bool canMove = true; |
---|
24 | |
---|
25 | //Handlers |
---|
26 | public delegate void EmptyHandler(); |
---|
27 | |
---|
28 | //Events |
---|
29 | public event EmptyHandler Dying; |
---|
30 | |
---|
31 | public Player(G game, double width, double height, int health, bool addDefaultControls) |
---|
32 | : base(width, height) |
---|
33 | { |
---|
34 | this.game = game; |
---|
35 | this.health = new IntMeter(health, 0, health); |
---|
36 | this.health.LowerLimit += delegate { if (Dying != null) Dying(); this.Destroy(); }; |
---|
37 | this.Tag = "player"; |
---|
38 | this.Shape = Shape.Rectangle; |
---|
39 | this.Image = playerImage; |
---|
40 | this.LinearDamping = 0.9; |
---|
41 | |
---|
42 | if (addDefaultControls) |
---|
43 | SetControls(Key.W, Key.S, Key.D, Key.A, MouseButton.Left); |
---|
44 | |
---|
45 | G.game.AddCollisionHandler(this, PlayerCollisionHanlder); |
---|
46 | this.IsUpdated = true; |
---|
47 | } |
---|
48 | |
---|
49 | #region Collision handlers |
---|
50 | |
---|
51 | private void PlayerCollisionHanlder(PhysicsObject player, PhysicsObject target) |
---|
52 | { |
---|
53 | if (target is Enemy) |
---|
54 | { |
---|
55 | health.Value -= 1; |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | private void Explosion(Vector pos) |
---|
60 | { |
---|
61 | Explosion explosion = new Explosion(50); |
---|
62 | explosion.Position = pos; |
---|
63 | game.Add(explosion); |
---|
64 | } |
---|
65 | |
---|
66 | private void ProjectileHanlder(PhysicsObject projectile, PhysicsObject target) //ToDo: Set collisions |
---|
67 | { |
---|
68 | if (target is Enemy) |
---|
69 | { |
---|
70 | Explosion(target.Position); |
---|
71 | if (weapon is Cannon) |
---|
72 | ((Enemy)target).Health(-100); |
---|
73 | else if (weapon is PlasmaCannon) |
---|
74 | ((Enemy)target).Health(-100); |
---|
75 | else if (weapon is LaserGun) |
---|
76 | ((Enemy)target).Health(-50); |
---|
77 | else if (weapon is AssaultRifle) |
---|
78 | ((Enemy)target).Health(-50); |
---|
79 | } |
---|
80 | projectile.Destroy(); |
---|
81 | } |
---|
82 | |
---|
83 | #endregion |
---|
84 | |
---|
85 | public bool PowerUp(String type, double value) |
---|
86 | { |
---|
87 | if (type == "speed") |
---|
88 | { |
---|
89 | speed = defaultSpeed * value; |
---|
90 | return true; |
---|
91 | } |
---|
92 | else if (type == "power") |
---|
93 | { |
---|
94 | power = defaultPower * value; |
---|
95 | return true; |
---|
96 | } |
---|
97 | |
---|
98 | return false; |
---|
99 | } |
---|
100 | |
---|
101 | public bool GiveWeapon(String itemName) |
---|
102 | { |
---|
103 | RemoveWeapon(); |
---|
104 | if (itemName.ToLower() == "cannon") |
---|
105 | { |
---|
106 | weapon = new Cannon(75, 25); |
---|
107 | } |
---|
108 | else if (itemName.ToLower() == "plasmacannon") |
---|
109 | weapon = new PlasmaCannon(75, 25); |
---|
110 | else if (itemName.ToLower() == "lasergun") |
---|
111 | weapon = new LaserGun(75, 25); |
---|
112 | else if (itemName.ToLower() == "assaultrifle") |
---|
113 | weapon = new AssaultRifle(75, 25); |
---|
114 | |
---|
115 | if (weapon != null) |
---|
116 | { |
---|
117 | weapon.Power.Value = power; |
---|
118 | weapon.ProjectileCollision = ProjectileHanlder; |
---|
119 | weapon.Angle = Angle.FromDegrees(90); |
---|
120 | weapon.CanHitOwner = true; |
---|
121 | this.Add(weapon); |
---|
122 | return true; |
---|
123 | } |
---|
124 | return false; |
---|
125 | } |
---|
126 | |
---|
127 | public bool RemoveWeapon() |
---|
128 | { |
---|
129 | if (weapon != null) |
---|
130 | { |
---|
131 | weapon.Destroy(); |
---|
132 | return true; |
---|
133 | } |
---|
134 | return false; |
---|
135 | } |
---|
136 | |
---|
137 | public void SetControls(Key forward, Key back, Key right, Key left, MouseButton shoot) |
---|
138 | { |
---|
139 | Game.Keyboard.Listen(forward, ButtonState.Down, delegate { MovePlayer(new Vector(0, speed)); }, null); |
---|
140 | Game.Keyboard.Listen(back, ButtonState.Down, delegate { MovePlayer(new Vector(0, -speed)); }, null); |
---|
141 | Game.Keyboard.Listen(right, ButtonState.Down, delegate { MovePlayer(new Vector(speed, 0)); }, null); |
---|
142 | Game.Keyboard.Listen(left, ButtonState.Down, delegate { MovePlayer(new Vector(-speed, 0)); }, null); |
---|
143 | Game.Mouse.Listen(shoot, ButtonState.Down, Shoot, null); |
---|
144 | } |
---|
145 | |
---|
146 | private void MovePlayer(Vector force) |
---|
147 | { |
---|
148 | if (canMove && !IsDestroyed) |
---|
149 | this.Push(force); |
---|
150 | } |
---|
151 | |
---|
152 | public void Shoot() |
---|
153 | { |
---|
154 | if (!IsDestroyed) |
---|
155 | { |
---|
156 | if (weapon != null) |
---|
157 | { |
---|
158 | weapon.Power.Value = power; |
---|
159 | weapon.Shoot(); |
---|
160 | } |
---|
161 | else |
---|
162 | Game.MessageDisplay.Add("PEW PEW!"); |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | public override void Update(Time time) |
---|
167 | { |
---|
168 | this.Angle = (Game.Mouse.PositionOnWorld - this.AbsolutePosition).Normalize().Angle - Angle.FromDegrees(90); |
---|
169 | base.Update(time); |
---|
170 | } |
---|
171 | } |
---|