-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameInterface.cs
224 lines (198 loc) · 7.66 KB
/
GameInterface.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
using Mogre;
using Mogre.TutorialFramework;
using System.Collections.Generic;
using System;
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 lifeArea;
private OverlayElement healthBar;
private OverlayElement shieldBar;
private Overlay overlay3D;
private Entity lifeEntity;
private List<SceneNode> lives;
private Timer time;
public Timer Time
{
set { time = value; }
}
private float hRatio;
private float sRatio;
private float lRatio;
private string score = "Score: ";
private string timeX = "Time Remaining: ";
/// <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)
: base(mSceneMgr, mWindow, playerStats) // this calls the constructor of the parent class
{
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>();
healthBar = OverlayManager.Singleton.GetOverlayElement("HealthBar");
hRatio = healthBar.Width / (float)characterStats.Health.Max;
shieldBar = OverlayManager.Singleton.GetOverlayElement("ShieldBar");
sRatio = shieldBar.Width / (float)characterStats.Shield.Max;
scoreText = OverlayManager.Singleton.GetOverlayElement("ScoreText");
scoreText.Caption = score;
scoreText.Left = mWindow.Width * 0.5f;
timeText = OverlayManager.Singleton.GetOverlayElement("TimeText");
timeText.Caption = timeX;
timeText.Left = mWindow.Width * 0.5f;
lifeArea = OverlayManager.Singleton.GetOverlayElement("LifeArea");
//hRatio = lifeBar.Width / (float)characterStats.Lives.Max;
panel =
(PanelOverlayElement) OverlayManager.Singleton.GetOverlayElement("GreenBackground");
panel.Width = mWindow.Width;
LoadOverlay3D();
}
/// <summary>
/// This method initalize a 3D overlay
/// </summary>
private void LoadOverlay3D()
{
overlay3D = OverlayManager.Singleton.Create("3DOverlay");
overlay3D.ZOrder = 15000;
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.15f, 0.15f, 0.15f));
//livesNode.Position = new Vector3(3f, 3f, -8) - n * 0.5f * Vector3.UNIT_X; ;
livesNode.Position = new Vector3(3f, 3.5f, -5.5f) - n * 0.5f * 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 (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);
}
healthBar.Width = hRatio * characterStats.Health.Value;
shieldBar.Width = sRatio * characterStats.Shield.Value;
scoreText.Caption = score + ((PlayerStats)characterStats).Score.Value;
timeText.Caption = timeX + (time);
}
/// <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();
}
}
}