1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using System.Text; |
---|
5 | using Jypeli; |
---|
6 | using Jypeli.Assets; |
---|
7 | |
---|
8 | |
---|
9 | abstract class Item |
---|
10 | { |
---|
11 | protected Player player; |
---|
12 | |
---|
13 | // Iso kuvake joka näkyy inventoryssä. |
---|
14 | public Image InventoryImage { get; set; } |
---|
15 | |
---|
16 | public IntMeter Usages { get; set; } |
---|
17 | |
---|
18 | // Onko kama tällä hetkellä aktiivisessa käytössä. |
---|
19 | public virtual bool InUse { get; set; } |
---|
20 | |
---|
21 | // Jos true niin pelaaja ei koske animaatioihinsa. |
---|
22 | public virtual bool OverrideAnimation { get; set; } |
---|
23 | |
---|
24 | // Tapahtuu kokoajan, jos kama on pelaajalla valittuna tavarana. |
---|
25 | public virtual void UpdateItem(Time time) { } |
---|
26 | |
---|
27 | // Kaman käyttönappia painettiin. |
---|
28 | public virtual void UseKeyPressed() { } |
---|
29 | |
---|
30 | // Kaman käyttönappia pidetään pohjassa. |
---|
31 | public virtual void UseKeyDown() { } |
---|
32 | |
---|
33 | // Kaman käyttönappi laskettiin irti. |
---|
34 | public virtual void UseKeyReleased() { } |
---|
35 | |
---|
36 | public virtual void CancelUse() { } |
---|
37 | |
---|
38 | protected Item(Player player) |
---|
39 | { |
---|
40 | Usages = new IntMeter(0, 0, 0); |
---|
41 | this.player = player; |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | class Sword : Item |
---|
46 | { |
---|
47 | public Direction SwingDirection { get; set; } |
---|
48 | |
---|
49 | public PhysicsObject SwordObject { get; set; } |
---|
50 | |
---|
51 | private readonly Angle[] swordAngles; |
---|
52 | |
---|
53 | public override bool OverrideAnimation |
---|
54 | { |
---|
55 | get { return InUse; } |
---|
56 | } |
---|
57 | |
---|
58 | public Sword(Player player) |
---|
59 | : base(player) |
---|
60 | { |
---|
61 | InventoryImage = TheLegendOfGabriel.SmallSwordImage; |
---|
62 | swordAngles = new Angle[5]; |
---|
63 | for (var i = 0; i < 5; i++) |
---|
64 | { |
---|
65 | swordAngles[i] = Angle.FromDegrees(45 + -45 * 0.5 * i); |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | public override void UpdateItem(Time time) |
---|
70 | { |
---|
71 | if (SwordObject != null && SwordObject.IsDestroyed) |
---|
72 | { |
---|
73 | SwordObject = null; |
---|
74 | } |
---|
75 | if (SwordObject != null && InUse) |
---|
76 | { |
---|
77 | if (player.Animation.CurrentFrameIndex < swordAngles.Length) |
---|
78 | { |
---|
79 | try |
---|
80 | { |
---|
81 | var dir = SwingDirection; |
---|
82 | var swordAngle = swordAngles[player.Animation.CurrentFrameIndex]; |
---|
83 | if (dir == Direction.Left) |
---|
84 | swordAngle = Angle.FromDegrees(swordAngle.Degrees * -1); |
---|
85 | SwordObject.Position = player.Position + (dir.Angle + swordAngle).GetVector() * 15; |
---|
86 | SwordObject.Angle = swordAngle + |
---|
87 | Angle.FromDegrees(dir == Direction.Left || dir == Direction.Right ? 90 : 0); |
---|
88 | SwordObject.Angle += Angle.FromDegrees(dir == Direction.Right || dir == Direction.Down ? 180 : 0); |
---|
89 | } |
---|
90 | catch (IndexOutOfRangeException ex) |
---|
91 | { |
---|
92 | |
---|
93 | } |
---|
94 | } |
---|
95 | } |
---|
96 | base.UpdateItem(time); |
---|
97 | } |
---|
98 | |
---|
99 | public override void UseKeyPressed() |
---|
100 | { |
---|
101 | InUse = true; |
---|
102 | |
---|
103 | TheLegendOfGabriel.SwordSound.Play(); |
---|
104 | |
---|
105 | if (SwordObject != null) |
---|
106 | SwordObject.Destroy(); |
---|
107 | |
---|
108 | SwordObject = PhysicsObject.CreateStaticObject(6, 16); |
---|
109 | SwordObject.Tag = "playerattack"; |
---|
110 | SwordObject.CollisionIgnoreGroup = player.CollisionIgnoreGroup; |
---|
111 | SwordObject.Position = player.Position; |
---|
112 | SwordObject.Image = TheLegendOfGabriel.SmallSwordImage; |
---|
113 | Game.Instance.Add(SwordObject); |
---|
114 | |
---|
115 | SwingDirection = player.Velocity.Angle.MainDirection; |
---|
116 | player.Animation = new Animation(player.SwingAnimations[SwingDirection]); |
---|
117 | player.Animation.StopOnLastFrame = true; |
---|
118 | player.Animation.Played += delegate |
---|
119 | { |
---|
120 | InUse = false; |
---|
121 | player.SetWalkAnimation(); |
---|
122 | SwordObject.Destroy(); |
---|
123 | }; |
---|
124 | player.Animation.Start(1); |
---|
125 | |
---|
126 | base.UseKeyPressed(); |
---|
127 | } |
---|
128 | |
---|
129 | public override void CancelUse() |
---|
130 | { |
---|
131 | InUse = false; |
---|
132 | player.SetWalkAnimation(); |
---|
133 | if (SwordObject != null) |
---|
134 | { |
---|
135 | SwordObject.Destroy(); |
---|
136 | SwordObject = null; |
---|
137 | } |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | class Pistol : Item |
---|
142 | { |
---|
143 | private bool isCharging; |
---|
144 | |
---|
145 | public Direction ShootDirection { get; set; } |
---|
146 | |
---|
147 | // Montako sekuntia pistoolilla on tähdätty. Täydellä latauksella voisi ampua isomman ammuksen. |
---|
148 | public DoubleMeter Charge { get; set; } |
---|
149 | |
---|
150 | public override bool OverrideAnimation |
---|
151 | { |
---|
152 | get { return InUse; } |
---|
153 | } |
---|
154 | |
---|
155 | public Pistol(Player player) |
---|
156 | : base(player) |
---|
157 | { |
---|
158 | Usages.MaxValue = 15; |
---|
159 | Usages.Value = 15; |
---|
160 | Charge = new DoubleMeter(0, 0, 4); |
---|
161 | InventoryImage = TheLegendOfGabriel.GunImage; |
---|
162 | } |
---|
163 | |
---|
164 | public override void UpdateItem(Time time) |
---|
165 | { |
---|
166 | if (isCharging) |
---|
167 | { |
---|
168 | Charge.Value += time.SinceLastUpdate.TotalSeconds; |
---|
169 | } |
---|
170 | base.UpdateItem(time); |
---|
171 | } |
---|
172 | |
---|
173 | public override void UseKeyPressed() |
---|
174 | { |
---|
175 | if (Usages.Value == 0) |
---|
176 | { |
---|
177 | base.UseKeyPressed(); |
---|
178 | return; |
---|
179 | } |
---|
180 | |
---|
181 | InUse = true; |
---|
182 | isCharging = true; // Ase alkaa latautumaan. |
---|
183 | Charge.Value = Charge.MinValue; // Aseen lataus aluksi nollaan. |
---|
184 | |
---|
185 | // Hidastetaan pelaajaa samalla kun se tähtää. |
---|
186 | player.MovementSpeed.Value = player.MovementSpeed.MaxValue * 0.333; |
---|
187 | |
---|
188 | ShootDirection = player.Velocity.Angle.MainDirection; |
---|
189 | player.Animation = new Animation(player.ShootAnimations[ShootDirection]); |
---|
190 | player.Animation.StopOnLastFrame = true; |
---|
191 | player.Animation.Start(1); |
---|
192 | base.UseKeyPressed(); |
---|
193 | } |
---|
194 | |
---|
195 | public override void UseKeyReleased() |
---|
196 | { |
---|
197 | // Pistoolilla voi ampua vain jos tähtäysanimaatio on ehtinyt loppuun. |
---|
198 | if (InUse && Usages.Value > 0 && player.Animation.CurrentFrameIndex == player.Animation.FrameCount - 1) |
---|
199 | { |
---|
200 | Usages.Value--; |
---|
201 | TheLegendOfGabriel.GunSound.Play(); |
---|
202 | |
---|
203 | var bullet = new PhysicsObject(4, 4); |
---|
204 | bullet.Tag = "playerattack"; |
---|
205 | bullet.Image = TheLegendOfGabriel.BulletImage; |
---|
206 | bullet.Position = player.Position + ShootDirection.GetVector() * player.Width * 0.7; |
---|
207 | bullet.Hit(ShootDirection.GetVector() * 500); |
---|
208 | Game.Instance.Add(bullet); |
---|
209 | |
---|
210 | ((TheLegendOfGabriel)Game.Instance).AddCollisionHandler(bullet, |
---|
211 | delegate(PhysicsObject b, PhysicsObject e) |
---|
212 | { |
---|
213 | bullet.Destroy(); |
---|
214 | }); |
---|
215 | } |
---|
216 | CancelUse(); |
---|
217 | |
---|
218 | base.UseKeyReleased(); |
---|
219 | } |
---|
220 | |
---|
221 | public override void CancelUse() |
---|
222 | { |
---|
223 | InUse = false; |
---|
224 | isCharging = false; |
---|
225 | player.MovementSpeed.Value = player.MovementSpeed.MaxValue; |
---|
226 | player.SetWalkAnimation(); |
---|
227 | base.CancelUse(); |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | class Grenade : Item |
---|
232 | { |
---|
233 | public Grenade(Player player) |
---|
234 | : base(player) |
---|
235 | { |
---|
236 | Usages.MaxValue = 15; |
---|
237 | Usages.Value = 15; |
---|
238 | InventoryImage = TheLegendOfGabriel.GrenadeImage; |
---|
239 | } |
---|
240 | |
---|
241 | public override void UseKeyPressed() |
---|
242 | { |
---|
243 | if (Usages.Value > 0) |
---|
244 | { |
---|
245 | Usages.Value--; |
---|
246 | |
---|
247 | var grenade = new PhysicsObject(6, 6); |
---|
248 | grenade.Image = TheLegendOfGabriel.GrenadeSmallImage; |
---|
249 | grenade.Position = player.Position + player.LastDirection.GetVector() * player.Width * 0.7; |
---|
250 | grenade.Hit(player.LastDirection.GetVector() * 500); |
---|
251 | grenade.ApplyTorque(9000); |
---|
252 | grenade.LinearDamping = 0.9; |
---|
253 | grenade.AngularDamping = 0.9; |
---|
254 | Game.Instance.Add(grenade); |
---|
255 | |
---|
256 | Timer.SingleShot(1.5, delegate |
---|
257 | { |
---|
258 | var expl = new Explosion(50); |
---|
259 | expl.Position = grenade.Position; |
---|
260 | expl.ShockwaveReachesObject += delegate(IPhysicsObject o, Vector vector) |
---|
261 | { |
---|
262 | var cre = o as Creature; |
---|
263 | if (cre != null) |
---|
264 | { |
---|
265 | cre.Health.Value--; |
---|
266 | } |
---|
267 | }; |
---|
268 | Game.Instance.Add(expl); |
---|
269 | grenade.Destroy(); |
---|
270 | }); |
---|
271 | } |
---|
272 | base.UseKeyPressed(); |
---|
273 | } |
---|
274 | } |
---|
275 | |
---|
276 | class Monocle : Item |
---|
277 | { |
---|
278 | private GameObject bigMonocle; |
---|
279 | |
---|
280 | public Monocle(Player player) |
---|
281 | : base(player) |
---|
282 | { |
---|
283 | InventoryImage = TheLegendOfGabriel.MonocleImage; |
---|
284 | } |
---|
285 | |
---|
286 | public override void UpdateItem(Time time) |
---|
287 | { |
---|
288 | if (bigMonocle != null) |
---|
289 | bigMonocle.Position = player.Position; |
---|
290 | base.UpdateItem(time); |
---|
291 | } |
---|
292 | |
---|
293 | public override void UseKeyPressed() |
---|
294 | { |
---|
295 | InUse = true; |
---|
296 | CreateMonocle(); |
---|
297 | base.UseKeyPressed(); |
---|
298 | } |
---|
299 | |
---|
300 | public override void UseKeyDown() |
---|
301 | { |
---|
302 | if (bigMonocle == null) |
---|
303 | return; |
---|
304 | |
---|
305 | foreach (var tile in Game.Instance.GetObjectsWithTag("cover")) |
---|
306 | { |
---|
307 | tile.IsVisible = Vector.Distance(player.Position, tile.Position) > bigMonocle.Width * 0.5; |
---|
308 | } |
---|
309 | base.UseKeyDown(); |
---|
310 | } |
---|
311 | |
---|
312 | public override void UseKeyReleased() |
---|
313 | { |
---|
314 | CancelUse(); |
---|
315 | base.UseKeyReleased(); |
---|
316 | } |
---|
317 | |
---|
318 | public override void CancelUse() |
---|
319 | { |
---|
320 | InUse = false; |
---|
321 | if (bigMonocle != null) |
---|
322 | { |
---|
323 | bigMonocle.Destroy(); |
---|
324 | bigMonocle = null; |
---|
325 | } |
---|
326 | foreach (var tile in Game.Instance.GetObjectsWithTag("cover")) |
---|
327 | { |
---|
328 | tile.IsVisible = true; |
---|
329 | } |
---|
330 | base.CancelUse(); |
---|
331 | } |
---|
332 | |
---|
333 | private void CreateMonocle() |
---|
334 | { |
---|
335 | bigMonocle = new GameObject(150, 150); |
---|
336 | bigMonocle.Image = TheLegendOfGabriel.BigMonocleImage; |
---|
337 | bigMonocle.Position = player.Position; |
---|
338 | Game.Instance.Add(bigMonocle, 3); |
---|
339 | } |
---|
340 | } |
---|