-
Notifications
You must be signed in to change notification settings - Fork 0
/
BombermanGame.cs
363 lines (324 loc) · 11 KB
/
BombermanGame.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Bomberman.Utils;
using Bomberman.Game.Movable;
using Bomberman.IO;
namespace Bomberman
{
/// <summary>
/// This is the main type for your game
/// </summary>
public partial class BombermanGame : Microsoft.Xna.Framework.Game, IGame
{
#region Fields
//Graphics
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Matrix spriteScale;
//Input
InputHelper inputHelper;
//Managers
private UI.UIManager _UIManager;
private Game.GameManager _GameManager;
// Error Logger
ILoggable logger;
//settings
private OptionsSettings _settings;
public bool IsGameRunning { get; private set; }
public string PlayerName { get; private set; }
bool IsPlayerLoggedIn = false;
#endregion
#region XNAGame methods
public BombermanGame()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 900;
graphics.PreferredBackBufferHeight = 600;
GameConstants.SetDisplaySize(graphics);
Content.RootDirectory = "Content";
logger = new BoxLogger();
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
inputHelper = new InputHelper();
_settings = new OptionsSettings();
//Managers
_UIManager = new UI.UIManager(this, this.graphics);
_GameManager = Game.GameManager.GetInstance((IGame)this);
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
float xScale =
(float)graphics.GraphicsDevice.Viewport.Width / 900f;
float yScale =
(float)graphics.GraphicsDevice.Viewport.Height / 600f;
spriteScale = Matrix.CreateScale(xScale, yScale, 1);
_UIManager.LoadContent(this.Content);
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
inputHelper.Update();
var move = InputToMove(Keyboard.GetState());
if (IsGameRunning)
{
if (move == Moves.Pause)
{
IsGameRunning = false;
_UIManager.ShowPauseMenu();
}
else
{
var elapsedTime = (int)gameTime.ElapsedGameTime.Milliseconds;
try
{
_GameManager.Update(elapsedTime, move);
}
catch (BombermanException bombermanException)
{
logger.Log(bombermanException);
}
if (_GameManager.HasPlayerLost)
{
IsGameRunning = false;
_UIManager.ShowGameOverMenu();
}
if (_GameManager.HasPlayerWon)
{
IsGameRunning = false;
_UIManager.ShowVictoryMenu();
}
}
}
else
{
//display menu
try
{
if (IsPlayerLoggedIn)
{
_UIManager.ShowMainMenu();
_UIManager.Update(move);
}
else
{
_UIManager.ShowLoginMenu();
char input;
bool delete;
if (InputToChar(Keyboard.GetState(), out input, out delete))
_UIManager.Update(move, input, delete);
else
_UIManager.Update(move);
}
}
catch (BombermanException bombermanException)
{
logger.Log(bombermanException);
}
}
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(0, null, null, null, null, null, spriteScale);
if (IsGameRunning)
{
_GameManager.Draw(spriteBatch);
}
else
{
_UIManager.Draw(spriteBatch);
}
spriteBatch.End();
base.Draw(gameTime);
}
#endregion
private Moves InputToMove(KeyboardState keyboardState)
{
var keys = keyboardState.GetPressedKeys();
if (keys.Length < 1) return Moves.None;
var key = keys[0];
if (IsGameRunning)
{
if (this._settings.Arrows)
{
switch (key)
{
case Keys.Space:
return inputHelper.IsNewPress(key) ? Moves.Fire : Moves.None;
case Keys.Left:
return Moves.Left;
case Keys.Right:
return Moves.Right;
case Keys.Down:
return Moves.Down;
case Keys.Up:
return Moves.Up;
// game menu
case Keys.Escape:
return Moves.Pause;
}
}
else
{
//WSAD
switch (key)
{
case Keys.Space:
return inputHelper.IsNewPress(key) ? Moves.Fire : Moves.None;
case Keys.A:
return Moves.Left;
case Keys.D:
return Moves.Right;
case Keys.S:
return Moves.Down;
case Keys.W:
return Moves.Up;
// game menu
case Keys.Escape:
return Moves.Pause;
}
}
}
else if (inputHelper.IsNewPress(key))
{
switch (key)
{
case Keys.Enter:
return Moves.Enter;
case Keys.Down:
return Moves.Down;
case Keys.Up:
return Moves.Up;
}
}
return Moves.None;
}
private bool InputToChar(KeyboardState keyboardState, out char input, out bool delete)
{
input = ' ';
delete = false;
var keys = keyboardState.GetPressedKeys();
if (keys.Length < 1)
return false;
var key = keys[0];
if (inputHelper.IsNewPress(key))
{
if (key == Keys.Back)
{
delete = true;
return true;
}
input = key.ToString()[0];
return true;
}
else
{
return false;
}
}
#region IGame implementation
public void NewGame()
{
this.IsGameRunning = true;
this._GameManager.NewGame(PlayerName);
this._GameManager.LoadContent(this.Content);
}
public void SaveGame()
{
var gameState = _GameManager.GetGameState();
FileManager.SaveGameFile(gameState, PlayerName);
}
public void LoadGame(string filename)
{
var gameState = FileManager.LoadGameFile(PlayerName, filename);
_GameManager.LoadGame(gameState);
IsGameRunning = true;
this._GameManager.LoadContent(this.Content);
}
public void ResumeGame()
{
this.IsGameRunning = true;
}
public string[] GetSavedGames()
{
var saves = FileManager.GetSavedGames(PlayerName);
return saves;
}
public string[][] LoadMapFile(string mapName)
{
var mapFile = IO.FileManager.LoadMapFile(mapName);
return mapFile;
}
public Tuple<string, int>[] GetHighScores()
{
var highScores = FileManager.GetHighScores();
return highScores;
}
public void UpdateHighScores(string playerName, int score)
{
FileManager.AddHighScore(playerName, score);
}
public string ToggleOption(OptionType option)
{
var result = this._settings.Toggle(option);
return result;
}
public string GetOptionValue(OptionType option)
{
return this._settings.GetOptionValue(option); ;
}
public void Login(string nickname)
{
if (nickname.Length < 1)
{
throw new BombermanException("Name must be longer than 0");
}
PlayerName = nickname;
IsPlayerLoggedIn = true;
}
public void Quit()
{
this.Exit();
}
#endregion
}
}