-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGameInterface.cs
More file actions
375 lines (296 loc) · 11.6 KB
/
Copy pathGameInterface.cs
File metadata and controls
375 lines (296 loc) · 11.6 KB
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
using System;
using System.Collections.Generic;
using Mogre;
namespace RaceGame
{
/// <summary>
/// This class implements an example of interface
/// </summary>
class GameInterface : HMD // Game interface inherits form the Head Mounted Dispaly (HMD) class
{
private PanelOverlayElement panel;
private OverlayElement scoreText;
private OverlayElement timeText;
private OverlayElement gameOver;
private OverlayElement guns;
private OverlayElement level;
private OverlayElement healthBar;
private OverlayElement shieldBar;
private Overlay overlay3D;
private Entity lifeEntity;
private List<SceneNode> lives;
private Timer timer;
private Timer timer2;
ulong timer_countdown;
int timeLimit = 40000;
private Boolean isGameEnded = false;
private string gameOverText = "Game over";
public Timer Time
{
set { timer = value; }
}
private float hRatio;
private float sRatio;
private string score = "Score: ";
private string timeDisplay = "Time left: ";
private Armoury playerArmoury;
private CharacterStats playerStats
{
get;
set;
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="mSceneMgr">A reference of a scene manager</param>
/// <param name="playerStats">A reference to a character stats</param>
public GameInterface(SceneManager mSceneMgr,
RenderWindow mWindow, CharacterStats playerStats, Armoury playerArmoury)
: base(mSceneMgr, mWindow, playerStats) // this calls the constructor of the parent class
{
this.playerArmoury = playerArmoury;
this.playerStats = playerStats;
Load("GameInterface");
}
/// <summary>
/// This method initializes the element of the interface
/// </summary>
/// <param name="name"> A name to pass to generate the overaly </param>
protected override void Load(string name)
{
base.Load(name);
lives = new List<SceneNode>();
timer = new Timer();
gameOver = OverlayManager.Singleton.GetOverlayElement("gameOver");
gameOver.Left = mWindow.Width * 0.5f;
level = OverlayManager.Singleton.GetOverlayElement("level");
level.Left = mWindow.Width * 0.5f;
healthBar = OverlayManager.Singleton.GetOverlayElement("HealthBar");
hRatio = healthBar.Width / (float)characterStats.Health.Max;
shieldBar = OverlayManager.Singleton.GetOverlayElement("ShieldBar");
sRatio = shieldBar.Width / (float)characterStats.Shield.Max;
guns = OverlayManager.Singleton.GetOverlayElement("guns");
guns.Caption = "no guns loaded";
guns.Left = mWindow.Width * 0.2f;
timeText = OverlayManager.Singleton.GetOverlayElement("timeText");
//timeText.Caption = timeDisplay;
//timeText.Left = mWindow.Width * 0.5f;
scoreText = OverlayManager.Singleton.GetOverlayElement("ScoreText");
scoreText.Caption = score;
scoreText.Left = mWindow.Width * 0.3f;
panel =
(PanelOverlayElement)OverlayManager.Singleton.GetOverlayElement("GreenBackground");
LoadOverlay3D();
}
/// <summary>
/// This method initalize a 3D overlay
/// </summary>
private void LoadOverlay3D()
{
overlay3D = OverlayManager.Singleton.Create("3DOverlay");
overlay3D.ZOrder = 10000;
CreateHearts();
overlay3D.Show();
}
/// <summary>
/// This method generate as many hearts as the number of lives left
/// </summary>
private void CreateHearts()
{
for (int i = 0; i < characterStats.Lives.Value; i++)
AddHeart(i);
}
/// <summary>
/// This method add an heart to the 3D overlay
/// </summary>
/// <param name="n"> A numeric tag</param>
private void AddHeart(int n)
{
SceneNode livesNode = CreateHeart(n);
lives.Add(livesNode);
overlay3D.Add3D(livesNode);
}
/// <summary>
/// This method remove from the 3D overlay and destries the passed scene node
/// </summary>
/// <param name="life"></param>
private void RemoveAndDestroyLife(SceneNode life)
{
overlay3D.Remove3D(life);
lives.Remove(life);
MovableObject heart = life.GetAttachedObject(0);
life.DetachAllObjects();
life.Dispose();
heart.Dispose();
}
/// <summary>
/// This method initializes the heart node and entity
/// </summary>
/// <param name="n"> A numeric tag used to determine the heart postion on sceen </param>
/// <returns></returns>
private SceneNode CreateHeart(int n)
{
lifeEntity = mSceneMgr.CreateEntity("Heart.mesh");
lifeEntity.SetMaterialName("HeartHMD");
SceneNode livesNode;
livesNode = new SceneNode(mSceneMgr);
livesNode.AttachObject(lifeEntity);
livesNode.Scale(new Vector3(0.2f, 0.2f, 0.2f));
livesNode.Position = new Vector3(5f, 5f, -8) - n * 0.7f * Vector3.UNIT_X; ;
livesNode.SetVisible(true);
return livesNode;
}
/// <summary>
/// This method converts milliseconds in to minutes and second format mm:ss
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
private string convertTime(float time)
{
string convTime;
float secs = time / 1000f;
int min = (int)(secs / 60);
secs = (int)secs % 60f;
if (secs < 10)
convTime = min + ":0" + secs;
else
convTime = min + ":" + secs;
return convTime;
}
/// <summary>
/// This method updates the interface
/// </summary>
/// <param name="evt"></param>
public override void Update(FrameEvent evt)
{
base.Update(evt);
Animate(evt);
//if (((PlayerStats)characterStats).CurrentLevel == 1)
//{
// if (((PlayerStats)characterStats).Score.Value >= 2)
// {
// gameOver.Caption = "You entered Level 2";
// ((PlayerStats)characterStats).CurrentLevel = 2;
// //if (convertTime(100 - timer2.Milliseconds) == "0:00" ){
// // gameOver.Caption = "";
// //}
// }
// else
// {
// gameOver.Caption = "";
// }
//}
//if (((PlayerStats)characterStats).CurrentLevel == 2)
//{
// if (((PlayerStats)characterStats).Score.Value >= 4)
// {
// gameOver.Caption = "You entered Level 3";
// ((PlayerStats)characterStats).CurrentLevel = 3;
// //if (convertTime(100 - timer2.Milliseconds) == "0:00" ){
// // gameOver.Caption = "";
// //}
// }
// else
// {
// gameOver.Caption = "";
// }
//}
if (((PlayerStats)characterStats).enemyKilled == 10)
{
isGameEnded = true;
gameOverText = "Congratulations, YOU Won ! Woooooo";
panel.Width = mWindow.Width;
panel.Height = mWindow.Height;
}
if (((PlayerStats)characterStats).CurrentLevel == 1)
{
level.Caption = "LV 1";
}
if (((PlayerStats)characterStats).CurrentLevel == 2)
{
level.Caption = "LV 2";
}
if (((PlayerStats)characterStats).CurrentLevel == 3)
{
level.Caption = "LV 3";
}
if (playerArmoury.activeGun != null){
guns.Caption = "ammo: " + playerArmoury.activeGun.Ammo.Value;
}
if (lives.Count > characterStats.Lives.Value && characterStats.Lives.Value >= 0)
{
SceneNode life = lives[lives.Count - 1];
RemoveAndDestroyLife(life);
}
if (lives.Count < characterStats.Lives.Value)
{
AddHeart(characterStats.Lives.Value);
}
if (convertTime(timeLimit - timer.Milliseconds) == "0:00")
{
isGameEnded = true;
gameOver.Caption = gameOverText;
panel.Width = mWindow.Width;
panel.Height = mWindow.Height;
}
if (isGameEnded)
{
timeText.Caption = "" ;
level.Caption = "";
guns.Caption = "";
scoreText.Caption = "";
score = "";
playerStats.Lives.Value = 0;
playerStats.Health.Value = 0;
playerStats.Shield.Value = 0;
this.Dispose();
}
else
{
timeText.Caption = "Time Left:" + convertTime(timeLimit - timer.Milliseconds);
scoreText.Caption = score + ((PlayerStats)characterStats).Score.Value;
}
if (((PlayerStats)characterStats).Lives.Value == 0)
{
isGameEnded = true;
gameOver.Caption = gameOverText;
panel.Width = mWindow.Width;
panel.Height = mWindow.Height;
}
healthBar.Width = hRatio * characterStats.Health.Value;
shieldBar.Width = sRatio * characterStats.Shield.Value;
}
/// <summary>
/// This method animates the heart rotation
/// </summary>
/// <param name="evt"></param>
protected override void Animate(FrameEvent evt)
{
foreach (SceneNode sn in lives)
sn.Yaw(evt.timeSinceLastFrame);
}
/// <summary>
/// This method disposes of the elements generated in the interface
/// </summary>
public override void Dispose()
{
List<SceneNode> toRemove = new List<SceneNode>();
foreach (SceneNode life in lives)
{
toRemove.Add(life);
}
foreach (SceneNode life in toRemove)
{
RemoveAndDestroyLife(life);
}
lifeEntity.Dispose();
toRemove.Clear();
shieldBar.Dispose();
healthBar.Dispose();
scoreText.Dispose();
panel.Dispose();
overlay3D.Dispose();
base.Dispose();
}
}
}