2 using System.Collections.Generic;
3 using System.Diagnostics;
5 using FBLibrary.Core.BaseEntities;
6 using FBClient.Sprites;
7 using Microsoft.Xna.Framework;
8 using Microsoft.Xna.Framework.Audio;
9 using Microsoft.Xna.Framework.Graphics;
11 namespace FBClient.Core.Entities
17 private readonly
Animation[] _explosionAnimations;
18 private readonly Dictionary<Point, ExplosionDirection> _explosionAnimationsDirection;
19 private readonly Texture2D _explosionSpriteTexture;
22 private SoundEffect _bombExplosionSound;
28 private bool _cellTeleporting;
29 private int _lastPlayerThatPushIt;
31 private enum ExplosionDirection
44 #region Constructor Region
46 public Bomb(
int playerId, Point cellPosition,
int pow, TimeSpan timer,
float playerSpeed)
47 : base(playerId, cellPosition, pow, timer, playerSpeed)
51 var spriteTexture = FinalBomber.Instance.Content.Load<Texture2D>(
"Graphics/Characters/bomb");
52 var animation =
new Animation(3, 32, 32, 0, 0, 3);
59 _explosionSpriteTexture = FinalBomber.Instance.Content.Load<Texture2D>(
"Graphics/Characters/explosion");
60 const int explosionAnimationsFramesPerSecond = 10;
61 _explosionAnimations =
new[]
63 new Animation(4, 32, 32, 0, 0, explosionAnimationsFramesPerSecond),
64 new Animation(4, 32, 32, 0, 32, explosionAnimationsFramesPerSecond),
65 new Animation(4, 32, 32, 0, 64, explosionAnimationsFramesPerSecond),
66 new Animation(4, 32, 32, 0, 96, explosionAnimationsFramesPerSecond),
67 new Animation(4, 32, 32, 0, 128, explosionAnimationsFramesPerSecond),
68 new Animation(4, 32, 32, 0, 160, explosionAnimationsFramesPerSecond),
69 new Animation(4, 32, 32, 0, 192, explosionAnimationsFramesPerSecond)
72 _explosionAnimationsDirection =
new Dictionary<Point, ExplosionDirection>();
75 _cellTeleporting =
false;
76 _lastPlayerThatPushIt = -1;
79 _bombExplosionSound = FinalBomber.Instance.Content.Load<SoundEffect>(
"Audio/Sounds/boom");
86 public void Update(GameTime gameTime)
88 Sprite.Update(gameTime);
119 foreach (
Animation animation
in _explosionAnimations)
120 animation.Update(gameTime);
123 Debug.Print(Timer.ToString());
127 #region When the bomb moves
252 public void Draw(GameTime gameTime)
256 if (_explosionAnimationsDirection.Count == 0)
258 foreach (Point p
in ActionField)
261 if (!CollisionLayer[p.X, p.Y] || p == CellPosition)
264 _explosionAnimationsDirection[p] = ComputeExplosionSpriteDirections(p, HazardMap);
270 foreach (Point p
in _explosionAnimationsDirection.Keys)
272 FinalBomber.Instance.SpriteBatch.Draw(_explosionSpriteTexture,
274 _explosionAnimations[(int) _explosionAnimationsDirection[p]].CurrentFrameRect, Color.White);
279 Sprite.Draw(gameTime, FinalBomber.Instance.SpriteBatch, Position);
284 #region Method Region
286 #region Private Method Region
288 #region Moving utils methods
290 private bool WallAt(Point cell)
292 if (cell.X >= 0 && cell.Y >= 0 && cell.X < MapSize.X &&
294 return (CollisionLayer[cell.X, cell.Y]);
298 private bool MoreTopSide()
300 return Position.Y < ((CellPosition.Y * Engine.TileHeight) - (Speed / 2));
303 private bool MoreBottomSide()
305 return Position.Y > ((CellPosition.Y * Engine.TileHeight) + (Speed / 2));
308 private bool MoreLeftSide()
310 return Position.X < ((CellPosition.X * Engine.TileWidth) - (Speed / 2));
313 private bool MoreRightSide()
315 return Position.X > ((CellPosition.X * Engine.TileWidth) + (Speed / 2));
321 private ExplosionDirection ComputeExplosionSpriteDirections(Point cell,
int[,] hazardMap)
323 int downCell = cell.Y + 1, leftCell = cell.X - 1, rightCell = cell.X + 1, upCell = cell.Y - 1;
326 if (cell.X == CellPosition.X && cell.Y == CellPosition.Y)
327 return ExplosionDirection.Middle;
332 if (hazardMap[cell.X, upCell] == 0 &&
333 (ActionField.Find(c => c.X == cell.X && c.Y == downCell) != Point.Zero))
334 return ExplosionDirection.Up;
336 if ((ActionField.Find(c => c.X == cell.X && c.Y == downCell) != Point.Zero) &&
337 (ActionField.Find(c => c.X == cell.X && c.Y == upCell) != Point.Zero))
338 return ExplosionDirection.Vertical;
340 if (hazardMap[cell.X, downCell] == 0 &&
341 (ActionField.Find(c => c.X == cell.X && c.Y == upCell) != Point.Zero))
342 return ExplosionDirection.Down;
347 if (hazardMap[leftCell, cell.Y] == 0 &&
348 (ActionField.Find(c => c.X == rightCell && c.Y == cell.Y) != Point.Zero))
349 return ExplosionDirection.Left;
351 if ((ActionField.Find(c => c.X == rightCell && c.Y == cell.Y) != Point.Zero) &&
352 (ActionField.Find(c => c.X == leftCell && c.Y == cell.Y) != Point.Zero))
353 return ExplosionDirection.Horizontal;
355 if (hazardMap[rightCell, cell.Y] == 0 &&
356 (ActionField.Find(c => c.X == leftCell && c.Y == cell.Y) != Point.Zero))
357 return ExplosionDirection.Right;
362 if (cell.Y == 1 && cell.X == 1)
365 if (ActionField.Find(c => c.X == rightCell && c.Y == cell.Y) != Point.Zero)
366 return ExplosionDirection.Left;
368 return ExplosionDirection.Up;
371 if (cell.Y == MapSize.Y - 2 && cell.X == 1)
374 return ActionField.Find(c => c.X == rightCell && c.Y == cell.Y) != Point.Zero
375 ? ExplosionDirection.Left
376 : ExplosionDirection.Down;
380 if (cell.X == MapSize.X - 2 && cell.Y == 1)
383 return ActionField.Find(c => c.X == leftCell && c.Y == cell.Y) != Point.Zero
384 ? ExplosionDirection.Right
385 : ExplosionDirection.Up;
388 if (cell.Y == MapSize.Y - 2 && cell.X == MapSize.X - 2)
391 return ActionField.Find(c => c.X == leftCell && c.Y == cell.Y) != Point.Zero
392 ? ExplosionDirection.Right
393 : ExplosionDirection.Down;
396 return ExplosionDirection.Middle;
401 #region Public Method Region
405 Point pos = Point.Zero;
408 case LookDirection.Up:
409 pos =
new Point(CellPosition.X, CellPosition.Y - 1);
411 case LookDirection.Down:
412 pos =
new Point(CellPosition.X, CellPosition.Y + 1);
414 case LookDirection.Left:
415 pos =
new Point(CellPosition.X - 1, CellPosition.Y);
417 case LookDirection.Right:
418 pos =
new Point(CellPosition.X + 1, CellPosition.Y);
422 if (!CollisionLayer[pos.X, pos.Y])
424 CurrentDirection = lD;
425 _lastPlayerThatPushIt = playerId;
426 foreach (Point p
in ActionField)
428 HazardMap[p.X, p.Y] = 0;
432 CurrentDirection = LookDirection.Idle;
437 _bombExplosionSound.Play();
void Update(GameTime gameTime)
void ChangeDirection(LookDirection lD, int playerId)
Bomb(int playerId, Point cellPosition, int pow, TimeSpan timer, float playerSpeed)
void Draw(GameTime gameTime)