1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using System.Reflection; |
---|
5 | using Jypeli; |
---|
6 | using Jypeli.Assets; |
---|
7 | using Jypeli.Controls; |
---|
8 | using Jypeli.Effects; |
---|
9 | using Jypeli.Widgets; |
---|
10 | |
---|
11 | public class StoryItem |
---|
12 | { |
---|
13 | // Iso kuvake joka näkyy inventoryssä. |
---|
14 | public Image InventoryImage { get; set; } |
---|
15 | |
---|
16 | public Queue<string> Message { get; set; } |
---|
17 | |
---|
18 | public StoryItem(Queue<String> message, Image image) |
---|
19 | { |
---|
20 | this.InventoryImage = image; |
---|
21 | this.Message = message; |
---|
22 | } |
---|
23 | } |
---|
24 | |
---|
25 | class CycleSlot : Label |
---|
26 | { |
---|
27 | public Label UsageLabel { get; set; } |
---|
28 | |
---|
29 | public CycleSlot(double left, double top) |
---|
30 | { |
---|
31 | this.Left = left; |
---|
32 | this.Top = top; |
---|
33 | this.Size = new Vector(60, 40); |
---|
34 | |
---|
35 | UsageLabel = new Label("10"); |
---|
36 | UsageLabel.IsVisible = false; |
---|
37 | UsageLabel.TextColor = Color.White; |
---|
38 | UsageLabel.X = this.X + 10; |
---|
39 | UsageLabel.Top = this.Bottom; |
---|
40 | UsageLabel.Font = Font.DefaultSmallBold; |
---|
41 | } |
---|
42 | |
---|
43 | public void UpdateImage(Item item) |
---|
44 | { |
---|
45 | if (item != null) |
---|
46 | { |
---|
47 | this.Image = item.InventoryImage; |
---|
48 | UsageLabel.IsVisible = false; |
---|
49 | if (item.Usages.MaxValue > 0) |
---|
50 | { |
---|
51 | UsageLabel.IsVisible = true; |
---|
52 | UsageLabel.BindTo(item.Usages); |
---|
53 | } |
---|
54 | } |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | public partial class TheLegendOfGabriel : PhysicsGame |
---|
59 | { |
---|
60 | public const int TILE_SIZE = 20; |
---|
61 | |
---|
62 | private Player player; |
---|
63 | private List<Creature> enemies = new List<Creature>(); |
---|
64 | |
---|
65 | private bool transition; |
---|
66 | List<GameObject> oldObjects = new List<GameObject>(); |
---|
67 | List<Exit> exits = new List<Exit>(); |
---|
68 | |
---|
69 | private bool viewing; |
---|
70 | private double midpoint; |
---|
71 | private double second; |
---|
72 | GameObject frame; |
---|
73 | Queue<string> messagesviewed; |
---|
74 | |
---|
75 | private const int STORYITEM_COUNT = 3; |
---|
76 | List<StoryItem> storyItems = new List<StoryItem>(); |
---|
77 | |
---|
78 | private bool canUseExit = true; |
---|
79 | private bool woodDestroyed = false; |
---|
80 | |
---|
81 | public IntMeter BossHealth; |
---|
82 | private List<Vector> bossSpawnPositions = new List<Vector>(); |
---|
83 | |
---|
84 | private CycleSlot currentItem; // Laatikossa näkyvä nykyinen kama. |
---|
85 | private CycleSlot nextItem; // Seuraavan kaman kuva. |
---|
86 | private CycleSlot prevItem; // Edellisen kaman kuva. |
---|
87 | private Label swordItem; // Laatikossa näkyvä miekka. |
---|
88 | |
---|
89 | #region Resources |
---|
90 | |
---|
91 | public static SoundEffect SwordSound = LoadSoundEffect("swordsound"); |
---|
92 | public static SoundEffect GunSound = LoadSoundEffect("gunsound"); |
---|
93 | public static SoundEffect EnemyDieSound = LoadSoundEffect("enemydie"); |
---|
94 | public static SoundEffect ChestOpenSound = LoadSoundEffect("chestopen2"); |
---|
95 | |
---|
96 | public static Image GunImage = LoadImage("gun"); |
---|
97 | public static Image BulletImage = LoadImage("bullet"); |
---|
98 | public static Image LetterImage = LoadImage("letter"); |
---|
99 | public static Image FenceImage = LoadImage("fence"); |
---|
100 | public static Image FoodImage = LoadImage("food"); |
---|
101 | public static Image SmallSwordImage = LoadImage("smallsword"); |
---|
102 | public static Image MonocleImage = LoadImage("monocle"); |
---|
103 | public static Image BigMonocleImage = LoadImage("bigmonocle"); |
---|
104 | public static Image BigSwordImage = LoadImage("bigsword"); |
---|
105 | public static Image GrenadeImage = LoadImage("gran"); |
---|
106 | public static Image GrenadeSmallImage = LoadImage("gransmall"); |
---|
107 | public static Image MaskImage = LoadImage("helm"); |
---|
108 | public static Image MaskLargeImage = LoadImage("helmlarge"); |
---|
109 | public static Image GoldenHeartImage = LoadImage("goldenheart"); |
---|
110 | public static Image ChestImage = LoadImage("chest"); |
---|
111 | public static Image ChestOpenImage = LoadImage("chestopen"); |
---|
112 | |
---|
113 | public static Image crackedWallImage = LoadImage("cracked"); |
---|
114 | |
---|
115 | public static Image GrenadeBoxImage = LoadImage("grenadebox"); |
---|
116 | public static Image AmmoBoxImage = LoadImage("ammobox"); |
---|
117 | |
---|
118 | public static Image FrameImage = LoadImage("frame"); |
---|
119 | public static Image ActiveItemFrameImageX = LoadImage("activeitemframe"); |
---|
120 | public static Image ActiveItemFrameImageZ = LoadImage("activeitemframez"); |
---|
121 | |
---|
122 | public static Image PlayerHealthImage = LoadImage("playerhealth"); |
---|
123 | public static Image PlayerHealthEmptyImage = LoadImage("playerhealthempty"); |
---|
124 | |
---|
125 | [AssetName("walkright")] |
---|
126 | internal Animation playerWalkRight; |
---|
127 | [AssetName("walkright", mirror: true)] |
---|
128 | internal Animation playerWalkLeft; |
---|
129 | [AssetName("walkup")] |
---|
130 | private Animation playerWalkUp; |
---|
131 | [AssetName("walkdown")] |
---|
132 | private Animation playerWalkDown; |
---|
133 | |
---|
134 | [AssetName("nwalkright")] |
---|
135 | internal Animation nWalkRight; |
---|
136 | [AssetName("nwalkright", mirror: true)] |
---|
137 | internal Animation nWalkLeft; |
---|
138 | [AssetName("nwalkup")] |
---|
139 | private Animation nWalkUp; |
---|
140 | [AssetName("nwalkdown")] |
---|
141 | private Animation nWalkDown; |
---|
142 | |
---|
143 | [AssetName("swingup")] |
---|
144 | private Animation playerSwingUp; |
---|
145 | [AssetName("swingright")] |
---|
146 | private Animation playerSwingRight; |
---|
147 | [AssetName("swingright", mirror: true)] |
---|
148 | private Animation playerSwingLeft; |
---|
149 | [AssetName("swingdown")] |
---|
150 | private Animation playerSwingDown; |
---|
151 | |
---|
152 | [AssetName("shootup")] |
---|
153 | private Animation playerShootUp; |
---|
154 | [AssetName("shootright")] |
---|
155 | private Animation playerShootRight; |
---|
156 | [AssetName("shootright", mirror: true)] |
---|
157 | private Animation playerShootLeft; |
---|
158 | [AssetName("shootdown")] |
---|
159 | private Animation playerShootDown; |
---|
160 | |
---|
161 | [AssetName("coyoteup")] |
---|
162 | private Animation coyoteUp; |
---|
163 | [AssetName("coyoteright")] |
---|
164 | private Animation coyoteRight; |
---|
165 | [AssetName("coyoteright", mirror: true)] |
---|
166 | private Animation coyoteLeft; |
---|
167 | [AssetName("coyotedown")] |
---|
168 | private Animation coyoteDown; |
---|
169 | |
---|
170 | [AssetName("train")] |
---|
171 | private Animation trainAnimation; |
---|
172 | [AssetName("carriage")] |
---|
173 | private Animation carriageAnimation; |
---|
174 | |
---|
175 | [AssetName("smoke")] |
---|
176 | private Animation smokeAnimation; |
---|
177 | |
---|
178 | #endregion |
---|
179 | |
---|
180 | public override void Begin() |
---|
181 | { |
---|
182 | Mouse.IsCursorVisible = true; |
---|
183 | SmoothTextures = false; |
---|
184 | LoadAnimations(); |
---|
185 | StartGame(); |
---|
186 | BuildUI(); |
---|
187 | Intro(); |
---|
188 | } |
---|
189 | |
---|
190 | void Victory() |
---|
191 | { |
---|
192 | ClearAll(); |
---|
193 | Level.Background.Color = Color.Black; |
---|
194 | MediaPlayer.Stop(); |
---|
195 | |
---|
196 | var text = new Label("Voitit muutes belin."); // Tekstin voisi vaihtaa. |
---|
197 | text.TextColor = Color.White; |
---|
198 | Add(text); |
---|
199 | } |
---|
200 | |
---|
201 | void BuildUI() |
---|
202 | { |
---|
203 | BuildRightBar(); |
---|
204 | BuildInventoryCycle(); |
---|
205 | BuildHealthBar(); |
---|
206 | UpdateItemCycleImages(); |
---|
207 | } |
---|
208 | |
---|
209 | void BuildHealthBar() |
---|
210 | { |
---|
211 | var bar = new ProgressBar(32 * 10, 32); |
---|
212 | bar.Right = Screen.Right - 50; |
---|
213 | bar.Top = Screen.Top - 50; |
---|
214 | bar.Image = PlayerHealthEmptyImage; |
---|
215 | bar.BarImage = PlayerHealthImage; |
---|
216 | bar.BindTo(player.Health); |
---|
217 | Add(bar); |
---|
218 | } |
---|
219 | |
---|
220 | void BuildInventoryCycle() |
---|
221 | { |
---|
222 | const int spacing = 20; |
---|
223 | |
---|
224 | prevItem = new CycleSlot(Screen.Left + 50, Screen.Top - 30); |
---|
225 | Add(prevItem); |
---|
226 | Add(prevItem.UsageLabel); |
---|
227 | |
---|
228 | currentItem = new CycleSlot(prevItem.Right + spacing, prevItem.Top); |
---|
229 | Add(currentItem); |
---|
230 | Add(currentItem.UsageLabel); |
---|
231 | |
---|
232 | nextItem = new CycleSlot(currentItem.Right + spacing, prevItem.Top); |
---|
233 | Add(nextItem); |
---|
234 | Add(nextItem.UsageLabel); |
---|
235 | |
---|
236 | // Ruutu jonka sisällä käytössä oleva kama on. |
---|
237 | var frame = new Label(); |
---|
238 | frame.Size = new Vector(60, 60); |
---|
239 | frame.Image = ActiveItemFrameImageX; |
---|
240 | frame.X = currentItem.X; |
---|
241 | frame.Top = currentItem.Top; |
---|
242 | Add(frame); |
---|
243 | |
---|
244 | // Miekan kuva. |
---|
245 | swordItem = new Label(); |
---|
246 | swordItem.Size = prevItem.Size; |
---|
247 | swordItem.Left = nextItem.Right + spacing * 2; |
---|
248 | swordItem.Top = currentItem.Top; |
---|
249 | Add(swordItem); |
---|
250 | |
---|
251 | // Miekan kuvan ympärillä oleva ruutu. |
---|
252 | var swordFrame = new Label(); |
---|
253 | swordFrame.Size = new Vector(60, 60); |
---|
254 | swordFrame.Image = ActiveItemFrameImageZ; |
---|
255 | swordFrame.X = swordItem.X; |
---|
256 | swordFrame.Top = swordItem.Top; |
---|
257 | Add(swordFrame); |
---|
258 | } |
---|
259 | |
---|
260 | void BuildRightBar() |
---|
261 | { |
---|
262 | for (int i = 0; i < STORYITEM_COUNT; i++) |
---|
263 | { |
---|
264 | Label member = new Label(FrameImage); |
---|
265 | member.Size = new Vector(40, 40); |
---|
266 | member.Position = new Vector(Screen.Right - FrameImage.Width, Screen.Top - Screen.Width * 0.25 - FrameImage.Height * i * 1.5); |
---|
267 | Add(member); |
---|
268 | |
---|
269 | GameObject inner = new GameObject(TILE_SIZE, TILE_SIZE); |
---|
270 | inner.Color = Color.Black; |
---|
271 | if (storyItems.Count > i && storyItems[i] != null) |
---|
272 | { |
---|
273 | inner.Image = storyItems[i].InventoryImage; |
---|
274 | Mouse.ListenOn(inner, MouseButton.Left, ButtonState.Pressed, ShowTextItem, "View those precious memories", storyItems[i]); |
---|
275 | } |
---|
276 | member.Add(inner); |
---|
277 | } |
---|
278 | } |
---|
279 | |
---|
280 | /// <summary> |
---|
281 | /// Lataa automaagisesti kaikki animaatiot. |
---|
282 | /// </summary> |
---|
283 | private void LoadAnimations() |
---|
284 | { |
---|
285 | // Haetaan tämän luokan kaikki ei-julkiset attribuutit. |
---|
286 | foreach (var field in GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance)) |
---|
287 | { |
---|
288 | // Jos attribuutille on laitettu AsseName, niin ladataan sen niminen animaatio. |
---|
289 | var assetAttr = Attribute.GetCustomAttribute(field, typeof(AssetNameAttribute)) as AssetNameAttribute; |
---|
290 | if (assetAttr == null) continue; |
---|
291 | var anim = LoadAnimation(assetAttr.AssetName); |
---|
292 | field.SetValue(this, assetAttr.Mirror ? MirrorAnimation(anim) : anim); |
---|
293 | } |
---|
294 | } |
---|
295 | |
---|
296 | Dictionary<Direction, Animation> DirectionalAnimations(Animation left, Animation right, Animation up, Animation down) |
---|
297 | { |
---|
298 | var dict = new Dictionary<Direction, Animation>(); |
---|
299 | dict[Direction.Left] = left; |
---|
300 | dict[Direction.Right] = right; |
---|
301 | dict[Direction.Up] = up; |
---|
302 | dict[Direction.Down] = down; |
---|
303 | return dict; |
---|
304 | } |
---|
305 | |
---|
306 | void Intro() |
---|
307 | { |
---|
308 | storyItems.Add(new StoryItem(new Queue<string>(new[] { "Oi, Alex", |
---|
309 | "", |
---|
310 | "Coyotes on a train.", |
---|
311 | "Coyotes on a train, that's all I'm sayin'", |
---|
312 | "-Gabriel" |
---|
313 | }), LetterImage)); |
---|
314 | |
---|
315 | ShowTextItem(storyItems[0]); |
---|
316 | } |
---|
317 | |
---|
318 | void ShowTextItem(StoryItem item) |
---|
319 | { |
---|
320 | viewing = true; |
---|
321 | messagesviewed = new Queue<string>(item.Message); |
---|
322 | |
---|
323 | Level.AmbientLight -= 0.75; |
---|
324 | |
---|
325 | frame = new GameObject(item.InventoryImage) { Width = Window.Width * 0.25, Height = Window.Height * 0.25, Position = Camera.Position }; |
---|
326 | Add(frame, 2); |
---|
327 | |
---|
328 | midpoint = 26 * item.Message.Count() * 0.5; |
---|
329 | Pause(); |
---|
330 | |
---|
331 | } |
---|
332 | |
---|
333 | /// <summary> |
---|
334 | /// Peilaa kaikki animaation kuvat. |
---|
335 | /// </summary> |
---|
336 | private static Animation MirrorAnimation(Animation anim) |
---|
337 | { |
---|
338 | return new Animation(anim.Select(Image.Mirror).ToArray()) { FPS = anim.FPS }; |
---|
339 | } |
---|
340 | |
---|
341 | private void StartGame() |
---|
342 | { |
---|
343 | ClearAll(); |
---|
344 | CreatePlayer(new Vector(0, -300), TILE_SIZE, TILE_SIZE, Angle.Zero, Shape.Rectangle, "", null); |
---|
345 | CreateLevel("level1"); |
---|
346 | //CreatePlayer(new Vector(-Level.Width/3, Level.Bottom + TILE_SIZE * 2)); |
---|
347 | SetControls(); |
---|
348 | |
---|
349 | Camera.Follow(player); |
---|
350 | Camera.ZoomFactor = 2.0; |
---|
351 | } |
---|
352 | |
---|
353 | void SetControls() |
---|
354 | { |
---|
355 | Keyboard.Listen(Key.Left, ButtonState.Down, player.Move, null, Direction.Left); |
---|
356 | Keyboard.Listen(Key.Right, ButtonState.Down, player.Move, null, Direction.Right); |
---|
357 | Keyboard.Listen(Key.Up, ButtonState.Down, player.Move, null, Direction.Up); |
---|
358 | Keyboard.Listen(Key.Down, ButtonState.Down, player.Move, null, Direction.Down); |
---|
359 | |
---|
360 | Keyboard.Listen(Key.X, ButtonState.Pressed, delegate { UseItem(player.ActiveItem, player.Sword, () => player.ActiveItem.UseKeyPressed()); }, null); |
---|
361 | Keyboard.Listen(Key.X, ButtonState.Released, delegate { UseItem(player.ActiveItem, player.Sword, () => player.ActiveItem.UseKeyReleased()); }, null); |
---|
362 | Keyboard.Listen(Key.X, ButtonState.Down, delegate { UseItem(player.ActiveItem, player.Sword, () => player.ActiveItem.UseKeyDown()); }, null); |
---|
363 | |
---|
364 | Keyboard.Listen(Key.Z, ButtonState.Pressed, delegate { UseItem(player.Sword, player.ActiveItem, () => player.Sword.UseKeyPressed()); }, null); |
---|
365 | Keyboard.Listen(Key.Z, ButtonState.Released, delegate { UseItem(player.Sword, player.ActiveItem, () => player.Sword.UseKeyReleased()); }, null); |
---|
366 | Keyboard.Listen(Key.Z, ButtonState.Down, delegate { UseItem(player.Sword, player.ActiveItem, () => player.Sword.UseKeyDown()); }, null); |
---|
367 | |
---|
368 | Keyboard.Listen(Key.Space, ButtonState.Pressed, CycleItems, null); |
---|
369 | |
---|
370 | Keyboard.Listen(Key.F8, ButtonState.Pressed, GiveAllItems, null); |
---|
371 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, null); |
---|
372 | } |
---|
373 | |
---|
374 | void GiveAllItems() |
---|
375 | { |
---|
376 | if (player.Inventory.FirstOrDefault(i => i is Pistol) == null) player.Inventory.Add(new Pistol(player)); |
---|
377 | if (player.Inventory.FirstOrDefault(i => i is Grenade) == null) player.Inventory.Add(new Grenade(player)); |
---|
378 | if (player.Inventory.FirstOrDefault(i => i is Mask) == null) player.Inventory.Add(new Mask(player)); |
---|
379 | if (player.Sword == null) player.Sword = new Sword(player); |
---|
380 | UpdateItemCycleImages(); |
---|
381 | } |
---|
382 | |
---|
383 | /// <summary> |
---|
384 | /// Luo pelaajan. |
---|
385 | /// </summary> |
---|
386 | void CreatePlayer(Vector position, double width, double height, Angle angle, Shape shape, string name, Dictionary<string, string> properties) |
---|
387 | { |
---|
388 | player = new Player(); |
---|
389 | player.CanRotate = false; |
---|
390 | player.MovementSpeed = new DoubleMeter(2300, 0, 2300); |
---|
391 | player.Position = position; |
---|
392 | player.MoveAnimations = DirectionalAnimations(playerWalkLeft, playerWalkRight, playerWalkUp, playerWalkDown); |
---|
393 | player.SwingAnimations = DirectionalAnimations(playerSwingLeft, playerSwingRight, playerSwingUp, playerSwingDown); |
---|
394 | player.ShootAnimations = DirectionalAnimations(playerShootLeft, playerShootRight, playerShootUp, playerShootDown); |
---|
395 | player.Image = playerWalkDown.CurrentFrame; |
---|
396 | Add(player, 1); |
---|
397 | |
---|
398 | player.Inventory.Add(new Monocle(player)); |
---|
399 | |
---|
400 | //player.Sword = new Sword(player); |
---|
401 | //player.Inventory.Add(new Pistol(player)); |
---|
402 | //player.Inventory.Add(new Grenade(player)); |
---|
403 | |
---|
404 | player.Health.Value = 5; |
---|
405 | //player.Health.Value = player.Health.MaxValue = 100; |
---|
406 | |
---|
407 | AddCollisionHandler(player, "exit", CollidesWithExit); |
---|
408 | AddCollisionHandler(player, "enemy", delegate(PhysicsObject p, PhysicsObject e) |
---|
409 | { |
---|
410 | player.Health.Value--; |
---|
411 | }); |
---|
412 | |
---|
413 | AddCollisionHandler(player, "ammobox", delegate(PhysicsObject p, PhysicsObject box) |
---|
414 | { |
---|
415 | box.Destroy(); |
---|
416 | var gun = player.Inventory.FirstOrDefault(i => i is Pistol); |
---|
417 | if (gun != null) |
---|
418 | { |
---|
419 | gun.Usages.Value += 5; |
---|
420 | } |
---|
421 | }); |
---|
422 | |
---|
423 | AddCollisionHandler(player, "grenadebox", delegate(PhysicsObject p, PhysicsObject box) |
---|
424 | { |
---|
425 | box.Destroy(); |
---|
426 | var grenades = player.Inventory.FirstOrDefault(i => i is Grenade); |
---|
427 | if (grenades != null) |
---|
428 | { |
---|
429 | grenades.Usages.Value += 5; |
---|
430 | } |
---|
431 | }); |
---|
432 | |
---|
433 | AddCollisionHandler(player, "storyTime", StoryTime); |
---|
434 | |
---|
435 | AddCollisionHandler(player, "pistol", delegate(PhysicsObject p, PhysicsObject item) |
---|
436 | { |
---|
437 | item.Destroy(); |
---|
438 | player.Inventory.Add(new Pistol(player)); |
---|
439 | UpdateItemCycleImages(); |
---|
440 | }); |
---|
441 | |
---|
442 | AddCollisionHandler(player, "grenade", delegate(PhysicsObject p, PhysicsObject item) |
---|
443 | { |
---|
444 | item.Destroy(); |
---|
445 | player.Inventory.Add(new Grenade(player)); |
---|
446 | UpdateItemCycleImages(); |
---|
447 | }); |
---|
448 | |
---|
449 | AddCollisionHandler(player, "mask", delegate(PhysicsObject p, PhysicsObject item) |
---|
450 | { |
---|
451 | item.Destroy(); |
---|
452 | player.Inventory.Add(new Mask(player)); |
---|
453 | UpdateItemCycleImages(); |
---|
454 | }); |
---|
455 | |
---|
456 | AddCollisionHandler(player, "chest", delegate(PhysicsObject p, PhysicsObject chest) |
---|
457 | { |
---|
458 | // Arkusta saa nyt aina uuden sydämen. |
---|
459 | ChestOpenSound.Play(); |
---|
460 | chest.Tag = null; |
---|
461 | |
---|
462 | Timer.SingleShot(1.0, delegate |
---|
463 | { |
---|
464 | chest.Image = ChestOpenImage; |
---|
465 | |
---|
466 | var heart = PhysicsObject.CreateStaticObject(20, 20); |
---|
467 | heart.IgnoresCollisionResponse = true; |
---|
468 | heart.Image = GoldenHeartImage; |
---|
469 | heart.Position = chest.Position; |
---|
470 | heart.Velocity = new Vector(0, 14); |
---|
471 | heart.LifetimeLeft = TimeSpan.FromSeconds(2.0); |
---|
472 | Add(heart, 3); |
---|
473 | |
---|
474 | player.Health.Value++; |
---|
475 | }); |
---|
476 | }); |
---|
477 | } |
---|
478 | |
---|
479 | void StoryTime(PhysicsObject pPlayer, PhysicsObject storyBlock) |
---|
480 | { |
---|
481 | storyItems.Add(new StoryItem(new Queue<string>(new[] { "Oi, boy!", |
---|
482 | "There something wrong with your head?", |
---|
483 | "It's dangerous out there - you did see the coyotes, right?", |
---|
484 | "", |
---|
485 | "Just... take this.", |
---|
486 | "You've used a sword before, right?", |
---|
487 | "", |
---|
488 | "-Hobo in a cave" |
---|
489 | }), FoodImage)); |
---|
490 | |
---|
491 | storyBlock.Tag = "none"; |
---|
492 | storyBlock.IgnoresCollisionResponse = true; |
---|
493 | |
---|
494 | if (player.Sword == null) |
---|
495 | { |
---|
496 | player.Sword = new Sword(player); |
---|
497 | UpdateItemCycleImages(); |
---|
498 | } |
---|
499 | |
---|
500 | ShowTextItem(storyItems[1]); |
---|
501 | BuildRightBar(); |
---|
502 | } |
---|
503 | |
---|
504 | void UseItem(Item item, Item otherItem, Action action) |
---|
505 | { |
---|
506 | if (player.IsDestroyed) |
---|
507 | return; |
---|
508 | |
---|
509 | bool inUse = false; |
---|
510 | if (otherItem != null) |
---|
511 | { |
---|
512 | inUse = otherItem.InUse; |
---|
513 | } |
---|
514 | |
---|
515 | if (item != null && !inUse) |
---|
516 | { |
---|
517 | action(); |
---|
518 | } |
---|
519 | } |
---|
520 | |
---|
521 | void CycleItems() |
---|
522 | { |
---|
523 | // Kaman vaihtossa simuloidaan että oltaisiin laskettu irti käyttönäppäimestä. |
---|
524 | if (player.ActiveItem != null) |
---|
525 | { |
---|
526 | player.ActiveItem.CancelUse(); |
---|
527 | } |
---|
528 | if (player.Sword != null) |
---|
529 | { |
---|
530 | player.Sword.CancelUse(); |
---|
531 | } |
---|
532 | |
---|
533 | player.CycleItems(); |
---|
534 | UpdateItemCycleImages(); |
---|
535 | } |
---|
536 | |
---|
537 | void UpdateItemCycleImages() |
---|
538 | { |
---|
539 | prevItem.UpdateImage(player.PrevItem); |
---|
540 | currentItem.UpdateImage(player.ActiveItem); |
---|
541 | nextItem.UpdateImage(player.NextItem); |
---|
542 | |
---|
543 | if (player.Sword != null) |
---|
544 | { |
---|
545 | swordItem.Image = BigSwordImage; |
---|
546 | } |
---|
547 | } |
---|
548 | |
---|
549 | void CollidesWithEnemy(PhysicsObject player, PhysicsObject enemy) |
---|
550 | { |
---|
551 | |
---|
552 | } |
---|
553 | |
---|
554 | /* |
---|
555 | void CollidesWithExit(PhysicsObject pPlayer, PhysicsObject pExit) |
---|
556 | { |
---|
557 | var oldExit = pExit as Exit; |
---|
558 | if (oldExit == null || oldExit.TargetLevel == null || pExit.IsDestroying || pExit.IsDestroyed) |
---|
559 | return; |
---|
560 | |
---|
561 | enemies.Clear(); |
---|
562 | ClearAll(); |
---|
563 | |
---|
564 | CreateLevel(oldExit.TargetLevel); |
---|
565 | var targetExit = exits.FirstOrDefault(e => e.Name == oldExit.TargetExitName); |
---|
566 | |
---|
567 | BuildUI(); |
---|
568 | SetControls(); |
---|
569 | player.Position = targetExit.Position + Direction.Inverse(targetExit.Position.Angle.MainDirection).GetVector() * TILE_SIZE * 2; |
---|
570 | Add(player); |
---|
571 | } |
---|
572 | */ |
---|
573 | |
---|
574 | void CollidesWithExit(PhysicsObject pPlayer, PhysicsObject pExit) |
---|
575 | { |
---|
576 | if (!canUseExit) |
---|
577 | return; |
---|
578 | |
---|
579 | canUseExit = false; |
---|
580 | Timer.SingleShot(2, () => canUseExit = true); |
---|
581 | |
---|
582 | var oldExit = pExit as Exit; |
---|
583 | if (oldExit == null || transition || oldExit.TargetLevel == null || pExit.IsDestroying || pExit.IsDestroyed) |
---|
584 | return; |
---|
585 | |
---|
586 | oldExit.Destroy(); |
---|
587 | |
---|
588 | var oldExitDirection = GetExitDirection(oldExit); |
---|
589 | transition = true; |
---|
590 | |
---|
591 | enemies.Clear(); |
---|
592 | |
---|
593 | // Otetaan vanhat objektit talteen. |
---|
594 | oldObjects.Clear(); |
---|
595 | foreach (var obj in GetObjects(o => o != player)) |
---|
596 | { |
---|
597 | if (obj != player) |
---|
598 | { |
---|
599 | oldObjects.Add(obj); |
---|
600 | } |
---|
601 | } |
---|
602 | |
---|
603 | // Luodaan seuraava kenttä. |
---|
604 | exits.Clear(); |
---|
605 | CreateLevel(oldExit.TargetLevel); |
---|
606 | |
---|
607 | // Etsitään seuraavan kentän kohde exitti johon siirrytään. |
---|
608 | var targetExit = exits.FirstOrDefault(e => e.Name == oldExit.TargetExitName); |
---|
609 | |
---|
610 | if (targetExit == null) |
---|
611 | { |
---|
612 | throw new Exception("Tarkisita kenttien exittien nimet."); |
---|
613 | } |
---|
614 | |
---|
615 | // Jompikumpi uloskäynti ei ole kentän laidalla, sulava siirtyminen ei ole mahdollista. |
---|
616 | if (GetExitDirection(targetExit) == Direction.None || oldExitDirection == Direction.None) |
---|
617 | { |
---|
618 | transition = false; |
---|
619 | oldObjects.ForEach(o => o.Destroy()); |
---|
620 | oldObjects.Clear(); |
---|
621 | |
---|
622 | BuildUI(); |
---|
623 | |
---|
624 | // Yritetään päätellä pelaajalle joku järkevä paikka. |
---|
625 | player.Position = targetExit.Position + Direction.Inverse(targetExit.Position.Angle.MainDirection).GetVector() * TILE_SIZE * 4; |
---|
626 | |
---|
627 | //Camera.Follow(player); |
---|
628 | //Camera.ZoomFactor = 2.0; |
---|
629 | return; |
---|
630 | } |
---|
631 | |
---|
632 | // Pysäytetään peli siirtymän ajaksi. |
---|
633 | Pause(); |
---|
634 | PhysicsEnabled = false; |
---|
635 | |
---|
636 | // Lasketaan siirtymävektorit. |
---|
637 | Direction dir = GetExitDirection(targetExit); |
---|
638 | Vector exitDelta = targetExit.Position - oldExit.Position; |
---|
639 | Vector transitionVector = Level.Size; |
---|
640 | if (dir == Direction.Left || dir == Direction.Right) |
---|
641 | { |
---|
642 | transitionVector.X *= dir.GetVector().X; |
---|
643 | transitionVector.Y = exitDelta.Y; |
---|
644 | } |
---|
645 | else |
---|
646 | { |
---|
647 | transitionVector.Y *= dir.GetVector().Y; |
---|
648 | transitionVector.X = exitDelta.X; |
---|
649 | } |
---|
650 | |
---|
651 | // Siirretään vanhoja objekteja ja pelaajaa. |
---|
652 | foreach (var obj in oldObjects) |
---|
653 | { |
---|
654 | obj.Position += transitionVector; |
---|
655 | } |
---|
656 | //player.Position += transitionVector + Direction.Inverse(dir).GetVector() * TILE_SIZE * 4; |
---|
657 | player.Position = targetExit.Position + Direction.Inverse(targetExit.Position.Angle.MainDirection).GetVector() * TILE_SIZE * 2; |
---|
658 | |
---|
659 | // Zoomataan kameraa sopivasti ja liikutetaan se vanhan kentän päälle, josta se sitten siirtyy uuden kentän päälle. |
---|
660 | Camera.Position += transitionVector; |
---|
661 | Vector pos = Camera.Position; |
---|
662 | //Camera.Follow(player); |
---|
663 | //Camera.ZoomFactor = 2.0; |
---|
664 | Camera.Position = pos; |
---|
665 | } |
---|
666 | |
---|
667 | /// <summary> |
---|
668 | /// Palauttaa suunnan millä puolella kenttää uloskäynti on. |
---|
669 | /// </summary> |
---|
670 | Direction GetExitDirection(Exit exit) |
---|
671 | { |
---|
672 | const double epsilon = 1e-3; |
---|
673 | Func<double, double, bool> isSame = (x, y) => Math.Abs(y - x) < epsilon; |
---|
674 | |
---|
675 | if (isSame(exit.Top, Level.Top)) |
---|
676 | { |
---|
677 | return Direction.Up; |
---|
678 | } |
---|
679 | if (isSame(exit.Bottom, Level.Bottom)) |
---|
680 | { |
---|
681 | return Direction.Down; |
---|
682 | } |
---|
683 | if (isSame(exit.Left, Level.Left)) |
---|
684 | { |
---|
685 | return Direction.Left; |
---|
686 | } |
---|
687 | if (isSame(exit.Right, Level.Right)) |
---|
688 | { |
---|
689 | return Direction.Right; |
---|
690 | } |
---|
691 | return Direction.None; |
---|
692 | } |
---|
693 | |
---|
694 | protected override void PausedUpdate(Time gameTime) |
---|
695 | { |
---|
696 | base.PausedUpdate(gameTime); |
---|
697 | double dt = gameTime.SinceLastUpdate.TotalSeconds; |
---|
698 | |
---|
699 | const double transitionSpeed = 400.0; |
---|
700 | |
---|
701 | if (transition && !viewing) |
---|
702 | { |
---|
703 | Camera.Position += Camera.Position.Normalize() * -transitionSpeed * dt; |
---|
704 | |
---|
705 | // Siirtymä on ohi, jatketaan peliä ja poistetaan edellinen kenttä. |
---|
706 | if (Camera.Position.Magnitude < transitionSpeed * dt) |
---|
707 | { |
---|
708 | transition = false; |
---|
709 | Pause(); |
---|
710 | PhysicsEnabled = true; |
---|
711 | Camera.Position = Vector.Zero; |
---|
712 | |
---|
713 | foreach (var obj in oldObjects) |
---|
714 | { |
---|
715 | obj.Destroy(); |
---|
716 | } |
---|
717 | oldObjects.Clear(); |
---|
718 | |
---|
719 | BuildUI(); |
---|
720 | } |
---|
721 | } |
---|
722 | |
---|
723 | if(viewing && !transition) //!transition perhaps being somewhat unnecessary, but now I do feel a bit safer. |
---|
724 | { |
---|
725 | second += dt; |
---|
726 | if(second > 1) |
---|
727 | { |
---|
728 | if (messagesviewed.Count < 1) //The letter's read, better head back. |
---|
729 | { |
---|
730 | Level.AmbientLight = 1; |
---|
731 | frame.Destroy(); |
---|
732 | GetObjectsWithTag("labelWaitingToDie").ForEach(g => g.Destroy()); |
---|
733 | viewing = false; |
---|
734 | Pause(); |
---|
735 | return; |
---|
736 | } |
---|
737 | |
---|
738 | second = 0; |
---|
739 | var storyLabel = new Label(messagesviewed.Dequeue()) { TextColor = Color.White }; |
---|
740 | storyLabel.Y = -midpoint + storyLabel.Height * messagesviewed.Count() + 1; |
---|
741 | storyLabel.Tag = "labelWaitingToDie"; |
---|
742 | Add(storyLabel); |
---|
743 | |
---|
744 | } |
---|
745 | } |
---|
746 | } |
---|
747 | |
---|
748 | protected override void Update(Time time) |
---|
749 | { |
---|
750 | player.UpdateCreature(time); |
---|
751 | enemies.ForEach(e => e.UpdateCreature(time)); |
---|
752 | base.Update(time); |
---|
753 | } |
---|
754 | } |
---|