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 | |
---|
195 | var text = new Label("Voitit muutes belin."); // Tekstin voisi vaihtaa. |
---|
196 | text.TextColor = Color.White; |
---|
197 | Add(text); |
---|
198 | } |
---|
199 | |
---|
200 | void BuildUI() |
---|
201 | { |
---|
202 | BuildRightBar(); |
---|
203 | BuildInventoryCycle(); |
---|
204 | BuildHealthBar(); |
---|
205 | UpdateItemCycleImages(); |
---|
206 | } |
---|
207 | |
---|
208 | void BuildHealthBar() |
---|
209 | { |
---|
210 | var bar = new ProgressBar(32 * 10, 32); |
---|
211 | bar.Right = Screen.Right - 50; |
---|
212 | bar.Top = Screen.Top - 50; |
---|
213 | bar.Image = PlayerHealthEmptyImage; |
---|
214 | bar.BarImage = PlayerHealthImage; |
---|
215 | bar.BindTo(player.Health); |
---|
216 | Add(bar); |
---|
217 | } |
---|
218 | |
---|
219 | void BuildInventoryCycle() |
---|
220 | { |
---|
221 | const int spacing = 20; |
---|
222 | |
---|
223 | prevItem = new CycleSlot(Screen.Left + 50, Screen.Top - 30); |
---|
224 | Add(prevItem); |
---|
225 | Add(prevItem.UsageLabel); |
---|
226 | |
---|
227 | currentItem = new CycleSlot(prevItem.Right + spacing, prevItem.Top); |
---|
228 | Add(currentItem); |
---|
229 | Add(currentItem.UsageLabel); |
---|
230 | |
---|
231 | nextItem = new CycleSlot(currentItem.Right + spacing, prevItem.Top); |
---|
232 | Add(nextItem); |
---|
233 | Add(nextItem.UsageLabel); |
---|
234 | |
---|
235 | // Ruutu jonka sisällä käytössä oleva kama on. |
---|
236 | var frame = new Label(); |
---|
237 | frame.Size = new Vector(60, 60); |
---|
238 | frame.Image = ActiveItemFrameImageX; |
---|
239 | frame.X = currentItem.X; |
---|
240 | frame.Top = currentItem.Top; |
---|
241 | Add(frame); |
---|
242 | |
---|
243 | // Miekan kuva. |
---|
244 | swordItem = new Label(); |
---|
245 | swordItem.Size = prevItem.Size; |
---|
246 | swordItem.Left = nextItem.Right + spacing * 2; |
---|
247 | swordItem.Top = currentItem.Top; |
---|
248 | Add(swordItem); |
---|
249 | |
---|
250 | // Miekan kuvan ympärillä oleva ruutu. |
---|
251 | var swordFrame = new Label(); |
---|
252 | swordFrame.Size = new Vector(60, 60); |
---|
253 | swordFrame.Image = ActiveItemFrameImageZ; |
---|
254 | swordFrame.X = swordItem.X; |
---|
255 | swordFrame.Top = swordItem.Top; |
---|
256 | Add(swordFrame); |
---|
257 | } |
---|
258 | |
---|
259 | void BuildRightBar() |
---|
260 | { |
---|
261 | for (int i = 0; i < STORYITEM_COUNT; i++) |
---|
262 | { |
---|
263 | Label member = new Label(FrameImage); |
---|
264 | member.Size = new Vector(40, 40); |
---|
265 | member.Position = new Vector(Screen.Right - FrameImage.Width, Screen.Top - Screen.Width * 0.25 - FrameImage.Height * i * 1.5); |
---|
266 | Add(member); |
---|
267 | |
---|
268 | GameObject inner = new GameObject(TILE_SIZE, TILE_SIZE); |
---|
269 | inner.Color = Color.Black; |
---|
270 | if (storyItems.Count > i && storyItems[i] != null) |
---|
271 | { |
---|
272 | inner.Image = storyItems[i].InventoryImage; |
---|
273 | Mouse.ListenOn(inner, MouseButton.Left, ButtonState.Pressed, ShowTextItem, "View those precious memories", storyItems[i]); |
---|
274 | } |
---|
275 | member.Add(inner); |
---|
276 | } |
---|
277 | } |
---|
278 | |
---|
279 | /// <summary> |
---|
280 | /// Lataa automaagisesti kaikki animaatiot. |
---|
281 | /// </summary> |
---|
282 | private void LoadAnimations() |
---|
283 | { |
---|
284 | // Haetaan tämän luokan kaikki ei-julkiset attribuutit. |
---|
285 | foreach (var field in GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance)) |
---|
286 | { |
---|
287 | // Jos attribuutille on laitettu AsseName, niin ladataan sen niminen animaatio. |
---|
288 | var assetAttr = Attribute.GetCustomAttribute(field, typeof(AssetNameAttribute)) as AssetNameAttribute; |
---|
289 | if (assetAttr == null) continue; |
---|
290 | var anim = LoadAnimation(assetAttr.AssetName); |
---|
291 | field.SetValue(this, assetAttr.Mirror ? MirrorAnimation(anim) : anim); |
---|
292 | } |
---|
293 | } |
---|
294 | |
---|
295 | Dictionary<Direction, Animation> DirectionalAnimations(Animation left, Animation right, Animation up, Animation down) |
---|
296 | { |
---|
297 | var dict = new Dictionary<Direction, Animation>(); |
---|
298 | dict[Direction.Left] = left; |
---|
299 | dict[Direction.Right] = right; |
---|
300 | dict[Direction.Up] = up; |
---|
301 | dict[Direction.Down] = down; |
---|
302 | return dict; |
---|
303 | } |
---|
304 | |
---|
305 | void Intro() |
---|
306 | { |
---|
307 | storyItems.Add(new StoryItem(new Queue<string>(new[] { "Oi, Alex", |
---|
308 | "", |
---|
309 | "Coyotes on a train.", |
---|
310 | "Coyotes on a train, that's all I'm sayin'", |
---|
311 | "-Gabriel" |
---|
312 | }), LetterImage)); |
---|
313 | |
---|
314 | ShowTextItem(storyItems[0]); |
---|
315 | } |
---|
316 | |
---|
317 | void ShowTextItem(StoryItem item) |
---|
318 | { |
---|
319 | viewing = true; |
---|
320 | messagesviewed = new Queue<string>(item.Message); |
---|
321 | |
---|
322 | Level.AmbientLight -= 0.75; |
---|
323 | |
---|
324 | frame = new GameObject(item.InventoryImage) { Width = Window.Width * 0.25, Height = Window.Height * 0.25, Position = Camera.Position }; |
---|
325 | Add(frame, 2); |
---|
326 | |
---|
327 | midpoint = 26 * item.Message.Count() * 0.5; |
---|
328 | Pause(); |
---|
329 | |
---|
330 | } |
---|
331 | |
---|
332 | /// <summary> |
---|
333 | /// Peilaa kaikki animaation kuvat. |
---|
334 | /// </summary> |
---|
335 | private static Animation MirrorAnimation(Animation anim) |
---|
336 | { |
---|
337 | return new Animation(anim.Select(Image.Mirror).ToArray()) { FPS = anim.FPS }; |
---|
338 | } |
---|
339 | |
---|
340 | private void StartGame() |
---|
341 | { |
---|
342 | ClearAll(); |
---|
343 | CreatePlayer(new Vector(0, -300), TILE_SIZE, TILE_SIZE, Angle.Zero, Shape.Rectangle, "", null); |
---|
344 | CreateLevel("level1"); |
---|
345 | //CreatePlayer(new Vector(-Level.Width/3, Level.Bottom + TILE_SIZE * 2)); |
---|
346 | SetControls(); |
---|
347 | |
---|
348 | Camera.Follow(player); |
---|
349 | Camera.ZoomFactor = 2.0; |
---|
350 | } |
---|
351 | |
---|
352 | void SetControls() |
---|
353 | { |
---|
354 | Keyboard.Listen(Key.Left, ButtonState.Down, player.Move, null, Direction.Left); |
---|
355 | Keyboard.Listen(Key.Right, ButtonState.Down, player.Move, null, Direction.Right); |
---|
356 | Keyboard.Listen(Key.Up, ButtonState.Down, player.Move, null, Direction.Up); |
---|
357 | Keyboard.Listen(Key.Down, ButtonState.Down, player.Move, null, Direction.Down); |
---|
358 | |
---|
359 | Keyboard.Listen(Key.X, ButtonState.Pressed, delegate { UseItem(player.ActiveItem, player.Sword, () => player.ActiveItem.UseKeyPressed()); }, null); |
---|
360 | Keyboard.Listen(Key.X, ButtonState.Released, delegate { UseItem(player.ActiveItem, player.Sword, () => player.ActiveItem.UseKeyReleased()); }, null); |
---|
361 | Keyboard.Listen(Key.X, ButtonState.Down, delegate { UseItem(player.ActiveItem, player.Sword, () => player.ActiveItem.UseKeyDown()); }, null); |
---|
362 | |
---|
363 | Keyboard.Listen(Key.Z, ButtonState.Pressed, delegate { UseItem(player.Sword, player.ActiveItem, () => player.Sword.UseKeyPressed()); }, null); |
---|
364 | Keyboard.Listen(Key.Z, ButtonState.Released, delegate { UseItem(player.Sword, player.ActiveItem, () => player.Sword.UseKeyReleased()); }, null); |
---|
365 | Keyboard.Listen(Key.Z, ButtonState.Down, delegate { UseItem(player.Sword, player.ActiveItem, () => player.Sword.UseKeyDown()); }, null); |
---|
366 | |
---|
367 | Keyboard.Listen(Key.Space, ButtonState.Pressed, CycleItems, null); |
---|
368 | |
---|
369 | Keyboard.Listen(Key.Escape, ButtonState.Pressed, ConfirmExit, null); |
---|
370 | } |
---|
371 | |
---|
372 | /// <summary> |
---|
373 | /// Luo pelaajan. |
---|
374 | /// </summary> |
---|
375 | void CreatePlayer(Vector position, double width, double height, Angle angle, Shape shape, string name, Dictionary<string, string> properties) |
---|
376 | { |
---|
377 | player = new Player(); |
---|
378 | player.CanRotate = false; |
---|
379 | player.MovementSpeed = new DoubleMeter(2300, 0, 2300); |
---|
380 | player.Position = position; |
---|
381 | player.MoveAnimations = DirectionalAnimations(playerWalkLeft, playerWalkRight, playerWalkUp, playerWalkDown); |
---|
382 | player.SwingAnimations = DirectionalAnimations(playerSwingLeft, playerSwingRight, playerSwingUp, playerSwingDown); |
---|
383 | player.ShootAnimations = DirectionalAnimations(playerShootLeft, playerShootRight, playerShootUp, playerShootDown); |
---|
384 | player.Image = playerWalkDown.CurrentFrame; |
---|
385 | Add(player, 1); |
---|
386 | |
---|
387 | player.Inventory.Add(new Monocle(player)); |
---|
388 | |
---|
389 | //player.Sword = new Sword(player); |
---|
390 | //player.Inventory.Add(new Pistol(player)); |
---|
391 | //player.Inventory.Add(new Grenade(player)); |
---|
392 | |
---|
393 | player.Health.Value = 5; |
---|
394 | //player.Health.Value = player.Health.MaxValue = 100; |
---|
395 | |
---|
396 | AddCollisionHandler(player, "exit", CollidesWithExit); |
---|
397 | AddCollisionHandler(player, "enemy", delegate(PhysicsObject p, PhysicsObject e) |
---|
398 | { |
---|
399 | player.Health.Value--; |
---|
400 | }); |
---|
401 | |
---|
402 | AddCollisionHandler(player, "ammobox", delegate(PhysicsObject p, PhysicsObject box) |
---|
403 | { |
---|
404 | box.Destroy(); |
---|
405 | var gun = player.Inventory.FirstOrDefault(i => i is Pistol); |
---|
406 | if (gun != null) |
---|
407 | { |
---|
408 | gun.Usages.Value += 5; |
---|
409 | } |
---|
410 | }); |
---|
411 | |
---|
412 | AddCollisionHandler(player, "grenadebox", delegate(PhysicsObject p, PhysicsObject box) |
---|
413 | { |
---|
414 | box.Destroy(); |
---|
415 | var grenades = player.Inventory.FirstOrDefault(i => i is Grenade); |
---|
416 | if (grenades != null) |
---|
417 | { |
---|
418 | grenades.Usages.Value += 5; |
---|
419 | } |
---|
420 | }); |
---|
421 | |
---|
422 | AddCollisionHandler(player, "storyTime", StoryTime); |
---|
423 | |
---|
424 | AddCollisionHandler(player, "pistol", delegate(PhysicsObject p, PhysicsObject item) |
---|
425 | { |
---|
426 | item.Destroy(); |
---|
427 | player.Inventory.Add(new Pistol(player)); |
---|
428 | UpdateItemCycleImages(); |
---|
429 | }); |
---|
430 | |
---|
431 | AddCollisionHandler(player, "grenade", delegate(PhysicsObject p, PhysicsObject item) |
---|
432 | { |
---|
433 | item.Destroy(); |
---|
434 | player.Inventory.Add(new Grenade(player)); |
---|
435 | UpdateItemCycleImages(); |
---|
436 | }); |
---|
437 | |
---|
438 | AddCollisionHandler(player, "mask", delegate(PhysicsObject p, PhysicsObject item) |
---|
439 | { |
---|
440 | item.Destroy(); |
---|
441 | player.Inventory.Add(new Mask(player)); |
---|
442 | UpdateItemCycleImages(); |
---|
443 | }); |
---|
444 | |
---|
445 | AddCollisionHandler(player, "chest", delegate(PhysicsObject p, PhysicsObject chest) |
---|
446 | { |
---|
447 | // Arkusta saa nyt aina uuden sydämen. |
---|
448 | ChestOpenSound.Play(); |
---|
449 | chest.Tag = null; |
---|
450 | |
---|
451 | Timer.SingleShot(1.0, delegate |
---|
452 | { |
---|
453 | chest.Image = ChestOpenImage; |
---|
454 | |
---|
455 | var heart = PhysicsObject.CreateStaticObject(20, 20); |
---|
456 | heart.IgnoresCollisionResponse = true; |
---|
457 | heart.Image = GoldenHeartImage; |
---|
458 | heart.Position = chest.Position; |
---|
459 | heart.Velocity = new Vector(0, 14); |
---|
460 | heart.LifetimeLeft = TimeSpan.FromSeconds(2.0); |
---|
461 | Add(heart, 3); |
---|
462 | |
---|
463 | player.Health.Value++; |
---|
464 | }); |
---|
465 | }); |
---|
466 | } |
---|
467 | |
---|
468 | void StoryTime(PhysicsObject pPlayer, PhysicsObject storyBlock) |
---|
469 | { |
---|
470 | storyItems.Add(new StoryItem(new Queue<string>(new[] { "Oi, boy!", |
---|
471 | "There something wrong with your head?", |
---|
472 | "It's dangerous out there - you did see the coyotes, right?", |
---|
473 | "", |
---|
474 | "Just... take this.", |
---|
475 | "You've used a sword before, right?", |
---|
476 | "", |
---|
477 | "-Hobo in a cave" |
---|
478 | }), FoodImage)); |
---|
479 | |
---|
480 | storyBlock.Tag = "none"; |
---|
481 | storyBlock.IgnoresCollisionResponse = true; |
---|
482 | |
---|
483 | if (player.Sword == null) |
---|
484 | { |
---|
485 | player.Sword = new Sword(player); |
---|
486 | UpdateItemCycleImages(); |
---|
487 | } |
---|
488 | |
---|
489 | ShowTextItem(storyItems[1]); |
---|
490 | BuildRightBar(); |
---|
491 | } |
---|
492 | |
---|
493 | void UseItem(Item item, Item otherItem, Action action) |
---|
494 | { |
---|
495 | if (player.IsDestroyed) |
---|
496 | return; |
---|
497 | |
---|
498 | bool inUse = false; |
---|
499 | if (otherItem != null) |
---|
500 | { |
---|
501 | inUse = otherItem.InUse; |
---|
502 | } |
---|
503 | |
---|
504 | if (item != null && !inUse) |
---|
505 | { |
---|
506 | action(); |
---|
507 | } |
---|
508 | } |
---|
509 | |
---|
510 | void CycleItems() |
---|
511 | { |
---|
512 | // Kaman vaihtossa simuloidaan että oltaisiin laskettu irti käyttönäppäimestä. |
---|
513 | if (player.ActiveItem != null) |
---|
514 | { |
---|
515 | player.ActiveItem.CancelUse(); |
---|
516 | } |
---|
517 | if (player.Sword != null) |
---|
518 | { |
---|
519 | player.Sword.CancelUse(); |
---|
520 | } |
---|
521 | |
---|
522 | player.CycleItems(); |
---|
523 | UpdateItemCycleImages(); |
---|
524 | } |
---|
525 | |
---|
526 | void UpdateItemCycleImages() |
---|
527 | { |
---|
528 | prevItem.UpdateImage(player.PrevItem); |
---|
529 | currentItem.UpdateImage(player.ActiveItem); |
---|
530 | nextItem.UpdateImage(player.NextItem); |
---|
531 | |
---|
532 | if (player.Sword != null) |
---|
533 | { |
---|
534 | swordItem.Image = BigSwordImage; |
---|
535 | } |
---|
536 | } |
---|
537 | |
---|
538 | void CollidesWithEnemy(PhysicsObject player, PhysicsObject enemy) |
---|
539 | { |
---|
540 | |
---|
541 | } |
---|
542 | |
---|
543 | /* |
---|
544 | void CollidesWithExit(PhysicsObject pPlayer, PhysicsObject pExit) |
---|
545 | { |
---|
546 | var oldExit = pExit as Exit; |
---|
547 | if (oldExit == null || oldExit.TargetLevel == null || pExit.IsDestroying || pExit.IsDestroyed) |
---|
548 | return; |
---|
549 | |
---|
550 | enemies.Clear(); |
---|
551 | ClearAll(); |
---|
552 | |
---|
553 | CreateLevel(oldExit.TargetLevel); |
---|
554 | var targetExit = exits.FirstOrDefault(e => e.Name == oldExit.TargetExitName); |
---|
555 | |
---|
556 | BuildUI(); |
---|
557 | SetControls(); |
---|
558 | player.Position = targetExit.Position + Direction.Inverse(targetExit.Position.Angle.MainDirection).GetVector() * TILE_SIZE * 2; |
---|
559 | Add(player); |
---|
560 | } |
---|
561 | */ |
---|
562 | |
---|
563 | void CollidesWithExit(PhysicsObject pPlayer, PhysicsObject pExit) |
---|
564 | { |
---|
565 | if (!canUseExit) |
---|
566 | return; |
---|
567 | |
---|
568 | canUseExit = false; |
---|
569 | Timer.SingleShot(2, () => canUseExit = true); |
---|
570 | |
---|
571 | var oldExit = pExit as Exit; |
---|
572 | if (oldExit == null || transition || oldExit.TargetLevel == null || pExit.IsDestroying || pExit.IsDestroyed) |
---|
573 | return; |
---|
574 | |
---|
575 | oldExit.Destroy(); |
---|
576 | |
---|
577 | var oldExitDirection = GetExitDirection(oldExit); |
---|
578 | transition = true; |
---|
579 | |
---|
580 | enemies.Clear(); |
---|
581 | |
---|
582 | // Otetaan vanhat objektit talteen. |
---|
583 | oldObjects.Clear(); |
---|
584 | foreach (var obj in GetObjects(o => o != player)) |
---|
585 | { |
---|
586 | if (obj != player) |
---|
587 | { |
---|
588 | oldObjects.Add(obj); |
---|
589 | } |
---|
590 | } |
---|
591 | |
---|
592 | // Luodaan seuraava kenttä. |
---|
593 | exits.Clear(); |
---|
594 | CreateLevel(oldExit.TargetLevel); |
---|
595 | |
---|
596 | // Etsitään seuraavan kentän kohde exitti johon siirrytään. |
---|
597 | var targetExit = exits.FirstOrDefault(e => e.Name == oldExit.TargetExitName); |
---|
598 | |
---|
599 | if (targetExit == null) |
---|
600 | { |
---|
601 | throw new Exception("Tarkisita kenttien exittien nimet."); |
---|
602 | } |
---|
603 | |
---|
604 | // Jompikumpi uloskäynti ei ole kentän laidalla, sulava siirtyminen ei ole mahdollista. |
---|
605 | if (GetExitDirection(targetExit) == Direction.None || oldExitDirection == Direction.None) |
---|
606 | { |
---|
607 | transition = false; |
---|
608 | oldObjects.ForEach(o => o.Destroy()); |
---|
609 | oldObjects.Clear(); |
---|
610 | |
---|
611 | BuildUI(); |
---|
612 | |
---|
613 | // Yritetään päätellä pelaajalle joku järkevä paikka. |
---|
614 | player.Position = targetExit.Position + Direction.Inverse(targetExit.Position.Angle.MainDirection).GetVector() * TILE_SIZE * 4; |
---|
615 | |
---|
616 | //Camera.Follow(player); |
---|
617 | //Camera.ZoomFactor = 2.0; |
---|
618 | return; |
---|
619 | } |
---|
620 | |
---|
621 | // Pysäytetään peli siirtymän ajaksi. |
---|
622 | Pause(); |
---|
623 | PhysicsEnabled = false; |
---|
624 | |
---|
625 | // Lasketaan siirtymävektorit. |
---|
626 | Direction dir = GetExitDirection(targetExit); |
---|
627 | Vector exitDelta = targetExit.Position - oldExit.Position; |
---|
628 | Vector transitionVector = Level.Size; |
---|
629 | if (dir == Direction.Left || dir == Direction.Right) |
---|
630 | { |
---|
631 | transitionVector.X *= dir.GetVector().X; |
---|
632 | transitionVector.Y = exitDelta.Y; |
---|
633 | } |
---|
634 | else |
---|
635 | { |
---|
636 | transitionVector.Y *= dir.GetVector().Y; |
---|
637 | transitionVector.X = exitDelta.X; |
---|
638 | } |
---|
639 | |
---|
640 | // Siirretään vanhoja objekteja ja pelaajaa. |
---|
641 | foreach (var obj in oldObjects) |
---|
642 | { |
---|
643 | obj.Position += transitionVector; |
---|
644 | } |
---|
645 | //player.Position += transitionVector + Direction.Inverse(dir).GetVector() * TILE_SIZE * 4; |
---|
646 | player.Position = targetExit.Position + Direction.Inverse(targetExit.Position.Angle.MainDirection).GetVector() * TILE_SIZE * 2; |
---|
647 | |
---|
648 | // Zoomataan kameraa sopivasti ja liikutetaan se vanhan kentän päälle, josta se sitten siirtyy uuden kentän päälle. |
---|
649 | Camera.Position += transitionVector; |
---|
650 | Vector pos = Camera.Position; |
---|
651 | //Camera.Follow(player); |
---|
652 | //Camera.ZoomFactor = 2.0; |
---|
653 | Camera.Position = pos; |
---|
654 | } |
---|
655 | |
---|
656 | /// <summary> |
---|
657 | /// Palauttaa suunnan millä puolella kenttää uloskäynti on. |
---|
658 | /// </summary> |
---|
659 | Direction GetExitDirection(Exit exit) |
---|
660 | { |
---|
661 | const double epsilon = 1e-3; |
---|
662 | Func<double, double, bool> isSame = (x, y) => Math.Abs(y - x) < epsilon; |
---|
663 | |
---|
664 | if (isSame(exit.Top, Level.Top)) |
---|
665 | { |
---|
666 | return Direction.Up; |
---|
667 | } |
---|
668 | if (isSame(exit.Bottom, Level.Bottom)) |
---|
669 | { |
---|
670 | return Direction.Down; |
---|
671 | } |
---|
672 | if (isSame(exit.Left, Level.Left)) |
---|
673 | { |
---|
674 | return Direction.Left; |
---|
675 | } |
---|
676 | if (isSame(exit.Right, Level.Right)) |
---|
677 | { |
---|
678 | return Direction.Right; |
---|
679 | } |
---|
680 | return Direction.None; |
---|
681 | } |
---|
682 | |
---|
683 | protected override void PausedUpdate(Time gameTime) |
---|
684 | { |
---|
685 | base.PausedUpdate(gameTime); |
---|
686 | double dt = gameTime.SinceLastUpdate.TotalSeconds; |
---|
687 | |
---|
688 | const double transitionSpeed = 400.0; |
---|
689 | |
---|
690 | if (transition && !viewing) |
---|
691 | { |
---|
692 | Camera.Position += Camera.Position.Normalize() * -transitionSpeed * dt; |
---|
693 | |
---|
694 | // Siirtymä on ohi, jatketaan peliä ja poistetaan edellinen kenttä. |
---|
695 | if (Camera.Position.Magnitude < transitionSpeed * dt) |
---|
696 | { |
---|
697 | transition = false; |
---|
698 | Pause(); |
---|
699 | PhysicsEnabled = true; |
---|
700 | Camera.Position = Vector.Zero; |
---|
701 | |
---|
702 | foreach (var obj in oldObjects) |
---|
703 | { |
---|
704 | obj.Destroy(); |
---|
705 | } |
---|
706 | oldObjects.Clear(); |
---|
707 | |
---|
708 | BuildUI(); |
---|
709 | } |
---|
710 | } |
---|
711 | |
---|
712 | if(viewing && !transition) //!transition perhaps being somewhat unnecessary, but now I do feel a bit safer. |
---|
713 | { |
---|
714 | second += dt; |
---|
715 | if(second > 1) |
---|
716 | { |
---|
717 | if (messagesviewed.Count < 1) //The letter's read, better head back. |
---|
718 | { |
---|
719 | Level.AmbientLight = 1; |
---|
720 | frame.Destroy(); |
---|
721 | GetObjectsWithTag("labelWaitingToDie").ForEach(g => g.Destroy()); |
---|
722 | viewing = false; |
---|
723 | Pause(); |
---|
724 | return; |
---|
725 | } |
---|
726 | |
---|
727 | second = 0; |
---|
728 | var storyLabel = new Label(messagesviewed.Dequeue()) { TextColor = Color.White }; |
---|
729 | storyLabel.Y = -midpoint + storyLabel.Height * messagesviewed.Count() + 1; |
---|
730 | storyLabel.Tag = "labelWaitingToDie"; |
---|
731 | Add(storyLabel); |
---|
732 | |
---|
733 | } |
---|
734 | } |
---|
735 | } |
---|
736 | |
---|
737 | protected override void Update(Time time) |
---|
738 | { |
---|
739 | player.UpdateCreature(time); |
---|
740 | enemies.ForEach(e => e.UpdateCreature(time)); |
---|
741 | base.Update(time); |
---|
742 | } |
---|
743 | } |
---|