Final-Bomber  0.1
Bomberman/Dynablaster remake in C# using XNA.
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Pages
Player.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Diagnostics;
4 using FBLibrary;
5 using FBLibrary.Core;
6 using FBLibrary.Core.BaseEntities;
7 using FBClient.Controls;
8 using FBClient.Core;
9 using FBClient.Core.Entities;
10 using FBClient.Entities;
11 using FBClient.Screens;
12 using FBClient.Screens.GameScreens;
13 using FBClient.Sprites;
14 using FBClient.TileEngine;
15 using FBClient.WorldEngine;
16 using Microsoft.Xna.Framework;
17 using Microsoft.Xna.Framework.Audio;
18 using Microsoft.Xna.Framework.Graphics;
19 
20 namespace FBClient.Entities
21 {
22  public abstract class Player : BasePlayer
23  {
24  #region Field Region
25 
26  private AnimatedSprite _playerDeathAnimation;
27  protected TimeSpan BombTimerSaved;
28 
29  // Bad item
30  protected float SpeedSaved;
31  private bool _cellTeleporting;
32  private float _invincibleBlinkFrequency;
33  private TimeSpan _invincibleBlinkTimer;
34 
35  public AnimatedSprite Sprite { get; protected set; }
36 
37  // Sounds
38  private SoundEffect _playerDeathSound;
39 
40  #endregion
41 
42  #region Property Region
43 
44  #endregion
45 
46  #region Constructor Region
47 
48  protected Player(int id)
49  : base(id)
50  {
51  Initiliaze();
52  }
53 
54  protected Player(int id, PlayerStats stats)
55  : base(id, stats)
56  {
57  Initiliaze();
58  }
59 
60  #endregion
61 
62  private void Initiliaze()
63  {
64  IsMoving = false;
65 
66  _invincibleBlinkFrequency = Config.InvincibleBlinkFrequency;
67  _invincibleBlinkTimer = TimeSpan.FromSeconds(_invincibleBlinkFrequency);
68  }
69 
70  #region XNA Method Region
71 
72  public void LoadContent()
73  {
74  const int animationFramesPerSecond = 10;
75  var animations = new Dictionary<AnimationKey, Animation>();
76 
77  var animation = new Animation(4, 23, 23, 0, 0, animationFramesPerSecond);
78  animations.Add(AnimationKey.Down, animation);
79 
80  animation = new Animation(4, 23, 23, 0, 23, animationFramesPerSecond);
81  animations.Add(AnimationKey.Left, animation);
82 
83  animation = new Animation(4, 23, 23, 0, 46, animationFramesPerSecond);
84  animations.Add(AnimationKey.Right, animation);
85 
86  animation = new Animation(4, 23, 23, 0, 69, animationFramesPerSecond);
87  animations.Add(AnimationKey.Up, animation);
88 
89  var spriteTexture = FinalBomber.Instance.Content.Load<Texture2D>("Graphics/Characters/player1");
90 
91  Sprite = new AnimatedSprite(spriteTexture, animations);
92  Sprite.ChangeFramesPerSecond(animationFramesPerSecond);
93 
94  var playerDeathTexture = FinalBomber.Instance.Content.Load<Texture2D>("Graphics/Characters/player1Death");
95  animation = new Animation(8, 23, 23, 0, 0, 4);
96  _playerDeathAnimation = new AnimatedSprite(playerDeathTexture, animation)
97  {
98  IsAnimating = false
99  };
100 
101  // Sounds
102  _playerDeathSound = FinalBomber.Instance.Content.Load<SoundEffect>("Audio/Sounds/playerDeath");
103  }
104 
105  public virtual void Update(GameTime gameTime, Map map, int[,] hazardMap)
106  {
107  if (IsAlive && !InDestruction)
108  {
109  PreviousDirection = CurrentDirection;
110 
111  Sprite.Update(gameTime);
112 
113  #region Invincibility
114 
115  if (!GameConfiguration.Invincible && IsInvincible)
116  {
117  if (InvincibleTimer >= TimeSpan.Zero)
118  {
119  if (_invincibleBlinkTimer >= TimeSpan.Zero)
120  _invincibleBlinkTimer -= gameTime.ElapsedGameTime;
121  else
122  _invincibleBlinkTimer = TimeSpan.FromSeconds(_invincibleBlinkFrequency);
123  }
124  }
125 
126  #endregion
127 
128  #region Moving
129 
130  Move(gameTime, map, hazardMap);
131 
132  #endregion
133 
134  #region Bomb
135 
136  #region Push a bomb
137 
138  if (Config.PlayerCanPush)
139  {
140  if (CurrentDirection != LookDirection.Idle)
141  {
142  Point direction = Point.Zero;
143  switch (CurrentDirection)
144  {
145  case LookDirection.Up:
146  direction = new Point(CellPosition.X, CellPosition.Y - 1);
147  break;
148  case LookDirection.Down:
149  direction = new Point(CellPosition.X, CellPosition.Y + 1);
150  break;
151  case LookDirection.Left:
152  direction = new Point(CellPosition.X - 1, CellPosition.Y);
153  break;
154  case LookDirection.Right:
155  direction = new Point(CellPosition.X + 1, CellPosition.Y);
156  break;
157  }
158  Bomb bomb = BombAt(direction);
159  if (bomb != null)
160  bomb.ChangeDirection(CurrentDirection, Id);
161  }
162  }
163 
164  #endregion
165 
166  #endregion
167 
168  #region Teleporter
169 
170  /*
171  if (!_cellTeleporting && FinalBomber.Instance.GamePlayScreen.World.Levels[FinalBomber.Instance.GamePlayScreen.World.CurrentLevel].
172  Map[CellPosition.X, CellPosition.Y] is Teleporter)
173  {
174  var teleporter = (Teleporter)(FinalBomber.Instance.GamePlayScreen.World.Levels[FinalBomber.Instance.GamePlayScreen.World.CurrentLevel].
175  Map[CellPosition.X, CellPosition.Y]);
176 
177  teleporter.ChangePosition(this);
178  _cellTeleporting = true;
179  }
180  */
181 
182  #endregion
183  }
184 
185  #region Death
186 
187  else if (InDestruction)
188  {
189  _playerDeathAnimation.Update(gameTime);
190  }
191  #endregion
192 
193  #region Edge wall gameplay
194  /*
195  else if (OnEdge &&
196  (!Config.ActiveSuddenDeath ||
197  (Config.ActiveSuddenDeath && !FinalBomber.Instance.GamePlayScreen.SuddenDeath.HasStarted)))
198  {
199  Sprite.Update(gameTime);
200 
201  MoveFromEdgeWall();
202  }
203  */
204  #endregion
205 
206  #region Camera
207  /*
208  Camera.Update(gameTime);
209 
210  if (Config.Debug)
211  {
212  if (InputHandler.KeyDown(Microsoft.Xna.Framework.Input.Keys.PageUp) ||
213  InputHandler.ButtonReleased(Buttons.LeftShoulder, PlayerIndex.One))
214  {
215  Camera.ZoomIn();
216  if (Camera.CameraMode == CameraMode.Follow)
217  Camera.LockToSprite(Sprite);
218  }
219  else if (InputHandler.KeyDown(Microsoft.Xna.Framework.Input.Keys.PageDown))
220  {
221  Camera.ZoomOut();
222  if (Camera.CameraMode == CameraMode.Follow)
223  Camera.LockToSprite(Sprite);
224  }
225  else if (InputHandler.KeyDown(Microsoft.Xna.Framework.Input.Keys.End))
226  {
227  Camera.ZoomReset();
228  }
229 
230  if (InputHandler.KeyReleased(Microsoft.Xna.Framework.Input.Keys.F))
231  {
232  Camera.ToggleCameraMode();
233  if (Camera.CameraMode == CameraMode.Follow)
234  Camera.LockToSprite(Sprite);
235  }
236 
237  if (Camera.CameraMode != CameraMode.Follow)
238  {
239  if (InputHandler.KeyReleased(Microsoft.Xna.Framework.Input.Keys.L))
240  {
241  Camera.LockToSprite(Sprite);
242  }
243  }
244  }
245 
246  if (IsMoving && Camera.CameraMode == CameraMode.Follow)
247  Camera.LockToSprite(Sprite);
248  */
249 
250  #endregion
251 
252  // Call DynamicEntity Update method
253  base.Update();
254  }
255 
256  public void Draw(GameTime gameTime)
257  {
258  var playerNamePosition = new Vector2(
259  Position.X + Engine.Origin.X + Sprite.Width / 2f -
260  ControlManager.SpriteFont.MeasureString(Name).X / 2 + 5,
261  Position.Y + Engine.Origin.Y - 25 -
262  ControlManager.SpriteFont.MeasureString(Name).Y / 2);
263 
264  if ((IsAlive && !InDestruction) || OnEdge)
265  {
266  if (IsInvincible)
267  {
268  if (_invincibleBlinkTimer > TimeSpan.FromSeconds(_invincibleBlinkFrequency * 0.5f))
269  {
270  Sprite.Draw(gameTime, FinalBomber.Instance.SpriteBatch, Position);
271  FinalBomber.Instance.SpriteBatch.DrawString(ControlManager.SpriteFont, Name,
272  playerNamePosition,
273  Color.Black);
274  }
275  }
276  else
277  {
278  Sprite.Draw(gameTime, FinalBomber.Instance.SpriteBatch, Position);
279  FinalBomber.Instance.SpriteBatch.DrawString(ControlManager.SpriteFont, Name, playerNamePosition,
280  Color.Black);
281  }
282  }
283  else
284  {
285  _playerDeathAnimation.Draw(gameTime, FinalBomber.Instance.SpriteBatch, Position);
286  FinalBomber.Instance.SpriteBatch.DrawString(ControlManager.SpriteFont, Name, playerNamePosition,
287  Color.Black);
288  }
289  }
290 
291  #endregion
292 
293  #region Method Region
294 
295  #region Private Method Region
296 
297  private Bomb BombAt(Point cell)
298  {
299  if (cell.X >= 0 && cell.Y >= 0 &&
300  cell.X <
302  .Size.X &&
303  cell.Y <
305  .Size.Y)
306  {
307  Bomb bomb = FinalBomber.Instance.GamePlayScreen.BombList.Find(b => b.CellPosition == cell);
308  return (bomb);
309  }
310  return null;
311  }
312 
313  private void Invincibility()
314  {
315  IsInvincible = true;
316  _invincibleBlinkFrequency = Config.InvincibleBlinkFrequency;
317  _invincibleBlinkTimer = TimeSpan.FromSeconds(_invincibleBlinkFrequency);
318  }
319 
320  #endregion
321 
322  #region Protected Method Region
323 
324  protected virtual void Move(GameTime gameTime, Map map, int[,] hazardMap)
325  {
326  if (IsMoving)
327  ComputeWallCollision(map);
328  }
329 
330  protected void UpdatePlayerPosition(Map map)
331  {
332  #region Update player's position
333 
334  if (IsChangingCell())
335  {
336  if (map.Board[PreviousCellPosition.X, PreviousCellPosition.Y] == this)
337  {
338  map.Board[PreviousCellPosition.X, PreviousCellPosition.Y] = null;
339  }
340 
341  if (_cellTeleporting)
342  _cellTeleporting = false;
343  }
344  else
345  {
346  if (map.Board[CellPosition.X, CellPosition.Y] == null)
347  {
348  map.Board[CellPosition.X, CellPosition.Y] = this;
349  }
350  }
351 
352  #endregion
353  }
354 
355  protected override float GetMovementSpeed()
356  {
357  var deltaTime = (float)TimeSpan.FromTicks(GameConfiguration.DeltaTime).TotalSeconds;
358  float speedValue = (Speed * deltaTime);
359  return speedValue;
360  }
361 
362  #endregion
363 
364  #region Override Method Region
365 
366  public override void Destroy()
367  {
368  if (!InDestruction)
369  {
370  Sprite.IsAnimating = false;
371  InDestruction = true;
372  _playerDeathSound.Play();
373  _playerDeathAnimation.IsAnimating = true;
374  }
375  }
376 
377  public override void Remove()
378  {
379  _playerDeathAnimation.IsAnimating = false;
380  InDestruction = false;
381  IsAlive = false;
382 
383  // Replacing for the gameplay on the edges
384  // Right side
385  if (Config.MapSize.X - CellPosition.X < Config.MapSize.X / 2)
386  {
387  Sprite.CurrentAnimation = AnimationKey.Left;
388  Position = new Vector2((Config.MapSize.X * Engine.TileWidth) - Engine.TileWidth, Position.Y);
389  }
390  // Left side
391  else
392  {
393  Sprite.CurrentAnimation = AnimationKey.Right;
394  Position = new Vector2(0, Position.Y);
395  }
396  }
397 
398  protected virtual void MoveFromEdgeWall()
399  {
400  }
401 
402  #endregion
403 
404  #endregion
405 
406 
407  public void Rebirth(Vector2 position)
408  {
409  IsAlive = true;
410  Sprite.IsAnimating = true;
411  InDestruction = false;
412  Position = position;
413  Sprite.CurrentAnimation = AnimationKey.Down;
414  _playerDeathAnimation.IsAnimating = false;
415 
416  Invincibility();
417  }
418 
419  public virtual void ChangeLookDirection(byte newLookDirection)
420  {
421  PreviousDirection = CurrentDirection;
422  CurrentDirection = (LookDirection)newLookDirection;
423  Debug.Print("New look direction: " + (LookDirection)newLookDirection);
424  }
425 
426  protected override int GetTime()
427  {
428  return TimeSpan.FromTicks(GameConfiguration.DeltaTime).Milliseconds;
429  }
430  }
431 }
432 
433 public class PlayerCollection : List<Player>
434 {
435  public Player GetPlayerByID(int playerID)
436  {
437  foreach (Player player in this)
438  {
439  if (player.Id == playerID)
440  return player;
441  }
442  return null;
443  }
444 }
void Draw(GameTime gameTime)
Definition: Player.cs:256
Player(int id, PlayerStats stats)
Definition: Player.cs:54
virtual void MoveFromEdgeWall()
Definition: Player.cs:398
override void Destroy()
Definition: Player.cs:366
virtual void Move(GameTime gameTime, Map map, int[,] hazardMap)
Definition: Player.cs:324
static Vector2 Origin
Definition: Engine.cs:75
override int GetTime()
Definition: Player.cs:426
Player GetPlayerByID(int playerID)
Definition: Player.cs:435
virtual void Update(GameTime gameTime, Map map, int[,] hazardMap)
Definition: Player.cs:105
static FinalBomber Instance
Definition: FinalBomber.cs:17
virtual void ChangeLookDirection(byte newLookDirection)
Definition: Player.cs:419
List< Map > Levels
Definition: World.cs:36
static int TileWidth
Definition: Engine.cs:65
override float GetMovementSpeed()
Definition: Player.cs:355
void Rebirth(Vector2 position)
Definition: Player.cs:407
override void Remove()
Definition: Player.cs:377
TimeSpan BombTimerSaved
Definition: Player.cs:27
void UpdatePlayerPosition(Map map)
Definition: Player.cs:330
readonly GamePlayScreen GamePlayScreen
Definition: FinalBomber.cs:66