Final-Bomber  0.1
Bomberman/Dynablaster remake in C# using XNA.
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Pages
GameManager.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using FBClient.Core.Entities;
4 using FBClient.Entities;
5 using FBClient.WorldEngine;
6 using FBLibrary.Core;
7 using FBLibrary.Core.BaseEntities;
8 using Microsoft.Xna.Framework;
9 using Microsoft.Xna.Framework.Audio;
10 using Microsoft.Xna.Framework.Media;
11 
12 namespace FBClient.Core
13 {
17  public abstract class GameManager : BaseGameManager
18  {
19  // Game logic
20 
21  // Players
22  public readonly PlayerCollection Players;
23 
24  // Collections
25  public readonly List<Bomb> BombList;
26  protected readonly List<PowerUp> PowerUpList;
27  protected readonly List<Wall> WallList;
28 
29  // Map
30  private readonly Map _currentMap;
31 
32  // Songs & sounds effect
33  private Song _mapHurrySong;
34  private Song _mapSong;
35 
36  // Timer
37  private TimeSpan _gameTimer;
38 
39  protected GameTime GameTime;
40 
41  // Sudden Death
42  private SuddenDeath _suddenDeath;
43 
44  #region Properties
45 
46  public Map CurrentMap
47  {
48  get { return _currentMap; }
49  }
50 
51  public TimeSpan GameTimer
52  {
53  get { return _gameTimer; }
54  }
55 
56  #endregion
57 
58  protected GameManager()
59  {
60  Players = new PlayerCollection();
61 
62  WallList = new List<Wall>();
63  PowerUpList = new List<PowerUp>();
64  BombList = new List<Bomb>();
65 
66  _currentMap = new Map();
67  BaseCurrentMap = _currentMap;
68 
69  _gameTimer = TimeSpan.Zero;
70 
71  GameTime = new GameTime();
72  }
73 
74  public void Initialize()
75  {
76  MediaPlayer.Play(_mapSong);
77 
78  _gameTimer = TimeSpan.Zero;
79 
80  var origin = new Vector2((FinalBomber.Instance.GraphicsDevice.Viewport.Width - 234) / 2f - ((32 * _currentMap.Size.X) / 2f),
81  FinalBomber.Instance.GraphicsDevice.Viewport.Height / 2 - ((32 * _currentMap.Size.Y) / 2));
82 
83  Engine.Origin = origin;
84 
85  _suddenDeath = new SuddenDeath(FinalBomber.Instance, Config.PlayersPositions[0]);
86  }
87 
88  public override void Reset()
89  {
90  MediaPlayer.Play(_mapSong);
91 
92  _gameTimer = TimeSpan.Zero;
93 
94  Players.Clear();
95 
96  WallList.Clear();
97  PowerUpList.Clear();
98  BombList.Clear();
99 
100  base.Reset();
101  }
102 
103  public void LoadContent()
104  {
105  // Musics
106  _mapSong = FinalBomber.Instance.Content.Load<Song>("Audio/Musics/map2");
107  _mapHurrySong = FinalBomber.Instance.Content.Load<Song>("Audio/Musics/map1_hurry");
108 
109  CurrentMap.LoadContent();
110  }
111 
112  public void Update(GameTime gameTime)
113  {
114  _gameTimer += gameTime.ElapsedGameTime;
115 
116  GameTime = gameTime;
117 
118  base.Update();
119  }
120 
121  #region Update entities
122  protected override void UpdateWalls()
123  {
124  for (int i = 0; i < WallList.Count; i++)
125  {
126  WallList[i].Update(GameTime);
127 
128  // We clean the obsolete elements
129  if (!WallList[i].IsAlive)
130  {
131  RemoveWall(WallList[i]);
132  }
133  }
134 
135  base.UpdateWalls();
136  }
137 
138  protected override void UpdateBombs()
139  {
140  for (int i = 0; i < BombList.Count; i++)
141  {
142  BombList[i].Update(GameTime);
143 
144  // Do we delete the bomb
145  if (!BombList[i].IsAlive)
146  {
147  RemoveBomb(BombList[i]);
148  }
149  }
150  base.UpdateBombs();
151  }
152 
153  protected override void UpdatePowerUps()
154  {
155  for (int i = 0; i < PowerUpList.Count; i++)
156  {
157  PowerUpList[i].Update(GameTime);
158 
159  if (!PowerUpList[i].IsAlive)
160  {
161  RemovePowerUp(PowerUpList[i]);
162  }
163  }
164 
165  base.UpdatePowerUps();
166  }
167 
168  protected override void UpdatePlayers()
169  {
170  for (int i = 0; i < Players.Count; i++)
171  {
172  if (Players[i].IsAlive)
173  {
174  Players[i].Update(GameTime, CurrentMap, HazardMap);
175  }
176  else
177  {
178  if (!BasePlayerList[i].OnEdge)
179  {
180  if (BaseCurrentMap.Board[BasePlayerList[i].CellPosition.X, BasePlayerList[i].CellPosition.Y] is BasePlayer)
181  {
182  RemovePlayer(BasePlayerList[i]);
183  }
184  }
185 
186  if (true)//(Config.ActiveSuddenDeath && SuddenDeath.HasStarted))
187  {
188  //BasePlayerList.Remove(BasePlayerList[i]);
189  }
190  else
191  {
192  BasePlayerList[i].OnEdge = true;
193  }
194  }
195  }
196 
197  base.UpdatePlayers();
198  }
199  #endregion
200 
201  public void Draw(GameTime gameTime, Camera2D camera)
202  {
203  CurrentMap.Draw(gameTime, FinalBomber.Instance.SpriteBatch, camera);
204 
205  foreach (Wall wall in WallList)
206  wall.Draw(gameTime);
207 
208  foreach (PowerUp powerUp in PowerUpList)
209  powerUp.Draw(gameTime);
210 
211  foreach (Bomb bomb in BombList)
212  bomb.Draw(gameTime);
213 
214  foreach (Player player in Players)
215  {
216  if (player.IsAlive)
217  player.Draw(gameTime);
218  }
219  }
220 
221  #region Entity methods
222 
223  #region Player methods
224 
225  public void AddPlayer(Player player)
226  {
227  Players.Add(player);
228 
229  base.AddPlayer(player);
230  }
231 
232  protected override void DestroyPlayer(int playerId)
233  {
234  var player = Players.Find(p => p.Id == playerId);
235 
236  base.DestroyPlayer(player);
237  }
238 
239  public void RemovePlayer(Player player)
240  {
241  Players.Remove(player);
242 
243  base.RemovePlayer(player);
244  }
245 
246  #endregion
247 
248  #region Wall methods
249 
250  public override void AddWall(Point position)
251  {
252  var wall = new Wall(position);
253  WallList.Add(wall);
254 
255  base.AddWall(wall);
256  }
257 
258  public void AddWall(Wall wall)
259  {
260  WallList.Add(wall);
261 
262  base.AddWall(wall);
263  }
264 
265  public void AddWalls(IEnumerable<Point> wallPositions)
266  {
267  foreach (var position in wallPositions)
268  {
269  AddWall(position);
270  }
271  }
272 
273  protected override void DestroyWall(Point position)
274  {
275  var wall = WallList.Find(w => w.CellPosition == position);
276 
277  base.DestroyWall(wall);
278  }
279 
280  protected virtual void RemoveWall(Wall wall)
281  {
282  WallList.Remove(wall);
283 
284  base.RemoveWall(wall);
285  }
286 
287  #endregion
288 
289  #region Bomb methods
290 
291  public void AddBomb(Bomb bomb)
292  {
293  BombList.Add(bomb);
294 
295  base.AddBomb(bomb);
296  }
297 
298  protected override void DestroyBomb(Point position)
299  {
300  var bomb = BombList.Find(b => b.CellPosition == position);
301 
302  base.DestroyBomb(bomb);
303  }
304 
305  private void RemoveBomb(Bomb bomb)
306  {
307  BombList.Remove(bomb);
308 
309  base.RemoveBomb(bomb);
310  }
311 
312  #endregion
313 
314  #region Power up methods
315 
316  public override void AddPowerUp(Point position)
317  {
318  var powerUp = new PowerUp(position);
319  PowerUpList.Add(powerUp);
320 
321  base.AddPowerUp(powerUp);
322  }
323 
324  public void AddPowerUp(PowerUpType type, Point position)
325  {
326  var powerUp = new PowerUp(position, type);
327  PowerUpList.Add(powerUp);
328 
329  base.AddPowerUp(powerUp);
330  }
331 
332  protected override void PickUpPowerUp(BasePlayer player, BasePowerUp powerUp)
333  {
334  powerUp.ApplyEffect(player);
335  powerUp.PickUp();
336 
337  powerUp.Remove();
338  }
339 
340  protected override void DestroyPowerUp(Point position)
341  {
342  var powerUp = PowerUpList.Find(pu => pu.CellPosition == position);
343 
344  base.DestroyPowerUp(powerUp);
345  }
346 
347  private void RemovePowerUp(PowerUp powerUp)
348  {
349  PowerUpList.Remove(powerUp);
350 
351  base.RemovePowerUp(powerUp);
352  }
353 
354  #endregion
355 
356  #endregion
357  }
358 }
override void UpdatePlayers()
Definition: GameManager.cs:168
void AddPowerUp(PowerUpType type, Point position)
Definition: GameManager.cs:324
override void PickUpPowerUp(BasePlayer player, BasePowerUp powerUp)
Definition: GameManager.cs:332
readonly List< PowerUp > PowerUpList
Definition: GameManager.cs:26
override void UpdateWalls()
Definition: GameManager.cs:122
override void UpdateBombs()
Definition: GameManager.cs:138
void AddWalls(IEnumerable< Point > wallPositions)
Definition: GameManager.cs:265
void Draw(GameTime gameTime, Camera2D camera)
Definition: GameManager.cs:201
override void UpdatePowerUps()
Definition: GameManager.cs:153
readonly List< Bomb > BombList
Definition: GameManager.cs:25
virtual void RemoveWall(Wall wall)
Definition: GameManager.cs:280
override void Reset()
Definition: GameManager.cs:88
override void AddPowerUp(Point position)
Definition: GameManager.cs:316
static FinalBomber Instance
Definition: FinalBomber.cs:17
void Update(GameTime gameTime)
Definition: GameManager.cs:112
readonly PlayerCollection Players
Definition: GameManager.cs:22
override void AddWall(Point position)
Definition: GameManager.cs:250
This class is used to regroup common behaviors between network and local game logic.
Definition: GameManager.cs:17
override void DestroyPlayer(int playerId)
Definition: GameManager.cs:232
void AddWall(Wall wall)
Definition: GameManager.cs:258
void AddPlayer(Player player)
Definition: GameManager.cs:225
void AddBomb(Bomb bomb)
Definition: GameManager.cs:291
override void DestroyWall(Point position)
Definition: GameManager.cs:273
void RemovePlayer(Player player)
Definition: GameManager.cs:239
override void DestroyBomb(Point position)
Definition: GameManager.cs:298
override void DestroyPowerUp(Point position)
Definition: GameManager.cs:340
readonly List< Wall > WallList
Definition: GameManager.cs:27