forked from vavrekmichal/Strategy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cs
400 lines (359 loc) · 12.5 KB
/
Game.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
using System;
using Strategy.GameObjectControl;
using Strategy.GameObjectControl.GroupMgr;
using Strategy.MoveMgr;
using Strategy.FightMgr;
using Mogre;
using Strategy.TeamControl;
using Strategy.MogreControl;
using Mogre.TutorialFramework;
using MOIS;
using Strategy.GameGUI;
using Strategy.GameObjectControl.Game_Objects;
using Strategy.Sound;
using Strategy.GameObjectControl.RuntimeProperty;
using Strategy.MissionControl;
using Strategy.Exceptions;
namespace Strategy {
/// <summary>
/// The main class of the application. Allows static access to most managers and updates them.
/// </summary>
public class Game {
private MouseControl mouseControl;
/// <summary>
/// The name of the player team.
/// </summary>
public static string PlayerName = "Player";
/// <summary>
/// The relative path to the saved missions.
/// </summary>
public static string SavesGamePath = "../../Media/Mission/Saves";
/// <summary>
/// The relative path to the new missions.
/// </summary>
public static string NewGamePath = "../../Media/Mission";
private static SoundPlayer soundPlayer; // Background music
private static GameObjectManager gameObjectMgr;
private static IGameGUI gameGUI;
private static SceneManager sceneMgr;
private static Mission mission;
private static bool paused;
private static bool initialized = false;
#region Singleton, constructor and static properties and functions
private static Game instance;
/// <summary>
/// If the game is not initializes,so creates GUI, MouseControl, GameObjectManager ,SoundPlayer and mission.
/// Returns singleton instance.
/// </summary>
/// <param name="sceneManager">The Mogre SceneManager.</param>
/// <param name="c">The game CameraMan for the MouseControl.</param>
/// <param name="mWindow">The RednerWindow for sending the width and height of the window.</param>
/// <param name="mouse">The Mogre Mouse for GUI.</param>
/// <param name="keyboard">The Mogre Keyboard for GUI.</param>
/// <returns>Returns initializes Game singleton instance.</returns>
public static Game GetInstance(SceneManager sceneManager, CameraMan c, RenderWindow mWindow, Mouse mouse, Keyboard keyboard) {
if (instance == null) {
instance = new Game(sceneManager, c, mWindow, mouse, keyboard);
}
return instance;
}
/// <summary>
/// Initialzes the Game.
/// </summary>
/// <param name="sceneManager">The Mogre SceneManager.</param>
/// <param name="camera">The game CameraMan for the MouseControl.</param>
/// <param name="mWindow">The RednerWindow for sending the width and height of the window.</param>
/// <param name="mouse">The Mogre Mouse for GUI.</param>
/// <param name="keyboard">The Mogre Keyboard for GUI.</param>
/// <returns>Returns initializes Game singleton instance.</returns>
private Game(SceneManager sceneManager, CameraMan camera, RenderWindow mWindow, Mouse mouse, Keyboard keyboard) {
sceneMgr = sceneManager;
gameObjectMgr = GameObjectManager.GetInstance();
gameGUI = new GameGUI.GameGUI((int)mWindow.Width, (int)mWindow.Height, mouse, keyboard);
mouseControl = MouseControl.GetInstance(camera, (int)mWindow.Width, (int)mWindow.Height);
paused = true;
soundPlayer = new SoundPlayer(mWindow);
mission = new Mission();
}
/// <summary>
/// Checks if the IGameGUI is initializes (if not throws a exception) and if is initialized,
/// so returns it.
/// </summary>
/// <exception cref="System.NullReferenceException">Thrown when IGameGUI is not initialized.</exception>
public static IGameGUI IGameGUI {
get {
if (gameGUI == null) {
throw new NullReferenceException("IGameGUI is not initialized.");
}
return gameGUI;
}
}
/// <summary>
/// Returns instance of GroupManager from GameObjectManager. The manager contorls if it is
/// initialized and can throws a exception (if is not initialized).
/// </summary>
public static GroupManager GroupManager {
get {
return gameObjectMgr.GroupManager;
}
}
/// <summary>
/// Returns instance of IGameObjectCreator from GameObjectManager. The manager contorls if it is
/// initialized and can throws a exception (if is not initialized).
/// </summary>
public static IGameObjectCreator IGameObjectCreator {
get {
return gameObjectMgr.IGameObjectCreator;
}
}
/// <summary>
/// Returns instance of IFightManager from GameObjectManager. The manager contorls if it is
/// initialized and can throws a exception (if is not initialized).
/// </summary>
public static IFightManager IFightManager {
get {
return gameObjectMgr.IFightManager;
}
}
/// <summary>
/// Returns instance of IMoveManager from GameObjectManager. The manager contorls if it is
/// initialized and can throws a exception (if is not initialized).
/// </summary>
public static IMoveManager IMoveManager {
get {
return gameObjectMgr.IMoveManager;
}
}
/// <summary>
/// Returns instance of HitTest from GameObjectManager. The maSolanager contorls if it is
/// initialized and can throws a exception (if is not initialized).
/// </summary>
public static HitTest HitTest {
get {
return gameObjectMgr.HitTest;
}
}
/// <summary>
/// Returns instance of SolarSystemManager from GameObjectManager. The manager contorls if it is
/// initialized and can throws a exception (if is not initialized).
/// </summary>
public static SolarSystemManager SolarSystemManager {
get {
return gameObjectMgr.SolarSystemManager;
}
}
/// <summary>
/// Returns instance of TeamManager from GameObjectManager. The manager contorls if it is
/// initialized and can throws a exception (if is not initialized).
/// </summary>
public static TeamManager TeamManager {
get {
return gameObjectMgr.TeamManager;
}
}
/// <summary>
/// Returns instance of PropertyManager from GameObjectManager. The manager contorls if it is
/// initialized and can throws a exception (if is not initialized).
/// </summary>
public static PropertyManager PropertyManager {
get {
return gameObjectMgr.PropertyManager;
}
}
/// <summary>
/// Checks if the IEffectPlayer is initializes (if not throws a exception) and if is initialized,
/// so returns it.
/// </summary>
/// <exception cref="System.NullReferenceException">Thrown when IEffectPlayer is not initialized.</exception>
public static IEffectPlayer IEffectPlayer {
get {
if (soundPlayer == null) {
throw new NullReferenceException("IEffectPlayer is not initialized.");
}
return soundPlayer;
}
}
/// <summary>
/// Checks if the IGameMusicPlayer is initializes (if not throws a exception) and if is initialized,
/// so returns it.
/// </summary>
/// <exception cref="System.NullReferenceException">Thrown when IGameMusicPlayer is not initialized.</exception>
public static IGameMusicPlayer IGameSoundPlayer {
get {
if (soundPlayer == null) {
throw new NullReferenceException("IGameMusicPlayer is not initialized.");
}
return soundPlayer;
}
}
/// <summary>
/// Checks if the Mission is initializes (if not throws a exception) and if is initialized,
/// so returns it.
/// </summary>
/// <exception cref="System.NullReferenceException">Thrown when Mission is not initialized.</exception>
public static Mission Mission {
get {
if (mission == null) {
throw new NullReferenceException("Mission is not initialized.");
}
return mission;
}
}
/// <summary>
/// Returns the instance of SceneManager initialized by Mogre.
/// </summary>
public static SceneManager SceneManager {
get { return sceneMgr; }
}
/// <summary>
/// Returns the object if is controllable, else returns null.
/// The object is returns by GameObjectManager.
/// </summary>
/// <param name="name">The name of the searching object.</param>
/// <returns>Returns the game object searched by the given name.</returns>
public static IGameObject GetIGameObject(string name) {
if (gameObjectMgr.HitTest.IsObjectControllable(name)) {
return gameObjectMgr.HitTest.GetGameObject(name);
} else {
return null;
}
}
/// <summary>
/// Saves the current mission.
/// </summary>
/// <param name="name">The name of the creating save.</param>
public static void Save(string name) {
if (initialized) {
gameObjectMgr.GameSerializer.Save(name);
}
}
/// <summary>
/// Loads the mission by given name and initilazed whole game.
/// </summary>
/// <param name="name">The name of the loading game.</param>
public static void Load(string name) {
Inicialization(name);
}
/// <summary>
/// Prints the given text to the game console.
/// </summary>
/// <param name="text">The printing text.</param>
public static void PrintToGameConsole(string text) {
gameGUI.PrintToGameConsole(text);
}
/// <summary>
/// Shows the panel with the possible choices (all SolarSystems) and if the user selects any,
/// so is created a Traveler.
/// </summary>
/// <param name="gameObject">The possible traveler.</param>
public static void CreateInterstellarTravel(IGameObject gameObject) {
gameGUI.ShowTravelSelectionPanel(gameObjectMgr.SolarSystemManager.GetAllSolarSystemNames(), gameObject);
}
/// <summary>
/// Ends the current Mission and destoys the Mission.
/// </summary>
/// <param name="printText">The text with the reason of ending the mission.</param>
public static void EndMission(string printText) {
gameGUI.MissionEnd(printText);
DestroyMission();
}
/// <summary>
/// Remove the given game object from the game (destroys the object).
/// </summary>
/// <param name="gameObject">The removing game object.</param>
public static void RemoveObject(IGameObject gameObject) {
gameObjectMgr.RemoveObject(gameObject);
}
/// <summary>
/// Changes the game object Team (sets the given Team).
/// </summary>
/// <param name="gameObject">The object which will have the changed Team.</param>
/// <param name="newTeam">The new Team for the object.</param>
public static void ChangeObjectsTeam(IGameObject gameObject, Team newTeam) {
gameObjectMgr.ChangeObjectsTeam(gameObject, newTeam);
}
/// <summary>
/// Destroys the current Mission and initializes new empty game contol objets.
/// </summary>
public static void DestroyMission() {
gameGUI.ClearMissionData();
gameGUI.Enable = false;
paused = true;
mission = new Mission();
gameObjectMgr.DestroyGame();
initialized = false;
}
/// <summary>
/// Initializes the mission by the given name. Loads game objects and starts the game.
/// </summary>
/// <param name="missionFileName">The name of a file with the mission.</param>
private static void Inicialization(string missionFileName) {
gameObjectMgr.Initialize(missionFileName);
gameGUI.SetSolarSystemName(SolarSystemManager.GetSolarSystemName(0));
mission.Initialize();
gameGUI.Enable = true;
paused = false;
PrintToGameConsole("Game loaded");
initialized = true;
}
/// <summary>
/// Indicatas if the game is initialized.
/// </summary>
public static bool Initialized {
get { return initialized; }
}
/// <summary>
/// Indicatas if the game is paused or not (if objects are updating or not).
/// </summary>
public static bool Paused {
get { return paused; }
set {
if (value) {
PrintToGameConsole("Game is paused.");
} else {
PrintToGameConsole("Game continues.");
}
paused = value;
}
}
/// <summary>
/// Indicatas if the keyboard is captured.
/// </summary>
public static bool KeyboardCaptured { get; set; }
/// <summary>
/// Indicatas if the mouse is captured. Mouse is captured if any Panel is registed at
/// GUI's PopUpPanels.
/// </summary>
public static bool MouseCaptured {
get { return gameGUI.NumberOfPopUpPanels > 0; }
}
#endregion
/// <summary>
/// Updates GUI system and MusicPlayer if the game is not paused, so
/// </summary>
/// <param name="delay"></param>
public void Update(float delay) {
gameGUI.Update();
soundPlayer.Update(delay);
if (!Paused) {
gameObjectMgr.Update(delay);
mission.Update(delay);
}
}
/// <summary>
/// Returns the initialized MouseControl.
/// </summary>
/// <returns></returns>
public MouseControl GetMouseControl() {
return mouseControl;
}
/// <summary>
/// Quits this instance.
/// </summary>
/// <exception cref="ShutdownException">Throws to close whole application. Thrown always</exception>
public void Quit() {
gameGUI.Dispose();
throw new ShutdownException();
}
}
}