Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
AroriaNetwork
GitHub Repository: AroriaNetwork/3kho-backup
Path: blob/main/projects/missiles/src/hud.js
1835 views
1
MG.hud = (function () {
2
var mRootNode;
3
4
var mRadar;
5
var mSpeedometer;
6
var mLevelIndicator;
7
var mProgressIndicator;
8
var mLifeCounter;
9
10
return {
11
12
init: function () {
13
mRootNode = document.getElementById('hud');
14
15
// ----------------------------------------------------------- Radar
16
17
mRadar = (function () {
18
var mMissilePositionDot = document.getElementById('hud-radar-scope-missile');
19
var mMissileTargetDot = document.getElementById('hud-radar-scope-missile-target');
20
21
var mMissileTarget = {x: 0.0, y: 0.0};
22
var mMissilePosition = {x: 0.0, y: 0.0};
23
24
return {
25
update: function (dt) {
26
mMissileTarget = MG.missile.getTarget();
27
mMissilePosition = MG.missile.getPosition();
28
// PASS
29
},
30
updateDOM: function () {
31
var x,y;
32
var scopeRadius = 0.5;
33
34
/* Set the position of the dot indicating the intended target of the missile */
35
x = scopeRadius + 0.95 * scopeRadius * mMissileTarget.x
36
/ MG.TUNNEL_RADIUS;
37
y = scopeRadius + 0.95 * scopeRadius * mMissileTarget.y
38
/ MG.TUNNEL_RADIUS;
39
40
mMissileTargetDot.setAttribute('cx', String(x));
41
mMissileTargetDot.setAttribute('cy', String(y));
42
43
/* Set the position of the dot indicating the actual position of the missile */
44
x = scopeRadius + 0.95 * scopeRadius * mMissilePosition.x
45
/ MG.TUNNEL_RADIUS;
46
y = scopeRadius + 0.95 * scopeRadius * mMissilePosition.y
47
/ MG.TUNNEL_RADIUS;
48
49
mMissilePositionDot.setAttribute('cx', String(x));
50
mMissilePositionDot.setAttribute('cy', String(y));
51
}
52
};
53
}());
54
55
56
57
// ----------------------------------------------------- Speedometer
58
59
mSpeedometer = (function () {
60
var mBarNode = document.getElementById('hud-speedometer-bar');
61
62
var mTextNode = document.createTextNode('');
63
document.getElementById('hud-speedometer-speed-text').appendChild(mTextNode);
64
65
var mSpeed = 0.0;
66
67
return {
68
update: function (dt) {
69
mSpeed = MG.missile.getVelocity();
70
},
71
updateDOM: function () {
72
mTextNode.data = mSpeed.toFixed(0);
73
74
// TODO (possibly) work out the maximum speed properly and put a cap on the level with a nice victory screen
75
mBarNode.setAttribute('x', mSpeed/2000 - 1);
76
}
77
};
78
} ());
79
80
// ------------------------------------------------- Level Indicator
81
mLevelIndicator = (function () {
82
var mTextNode = document.createTextNode('');
83
document.getElementById('hud-level-indicator').appendChild(mTextNode);
84
85
return {
86
update: function (dt) {
87
// PASS
88
},
89
90
updateDOM: function () {
91
mTextNode.data = MG.game.getLevelString();
92
}
93
};
94
} ());
95
96
// ---------------------------------------------- Progress Indicator
97
mProgressIndicator = (function () {
98
var mProgressMarkNode = document.getElementById('hud-progress-indicator-progress');
99
var mBestProgressMarkNode = document.getElementById('hud-progress-indicator-best-progress');
100
101
var mProgress = 0.0;
102
var mBestProgress = 0.0;
103
104
return {
105
update: function (dt) {
106
mProgress = MG.game.getProgress();
107
mBestProgress = MG.game.getBestProgress();
108
},
109
110
updateDOM: function () {
111
mProgressMarkNode.setAttribute('transform', 'translate(0,'+mProgress+')');
112
mBestProgressMarkNode.setAttribute('transform', 'translate(0,'+mBestProgress+')');
113
}
114
};
115
} ());
116
// ---------------------------------------------------- Life Counter
117
118
mLifeCounter = (function () {
119
var i;
120
121
var mInfiniteLivesNode = document.getElementById('hud-lives-indicator-infinite');
122
var mZeroLivesNode = document.getElementById('hud-lives-indicator-none');
123
124
var mLivesNodes = [];
125
for (i=0; i<5; i++) {
126
mLivesNodes[i] = document.getElementById('hud-lives-indicator-missile-' + (i+1));
127
}
128
129
var mLives = -1;
130
131
return {
132
update: function (dt) {
133
mLives = MG.game.getNumLives();
134
},
135
136
updateDOM: function () {
137
var i;
138
139
switch (mLives) {
140
case Infinity:
141
mInfiniteLivesNode.setAttribute('visibility', 'visible');
142
mZeroLivesNode.setAttribute('visibility', 'hidden');
143
144
for (i=0; i<5; i++) {
145
mLivesNodes[i].setAttribute('visibility', 'hidden');
146
}
147
break;
148
case 0:
149
mInfiniteLivesNode.setAttribute('visibility', 'hidden');
150
mZeroLivesNode.setAttribute('visibility', 'visible');
151
152
for (i=0; i<5; i++) {
153
mLivesNodes[i].setAttribute('visibility', 'hidden');
154
}
155
break;
156
default:
157
mInfiniteLivesNode.setAttribute('visibility', 'hidden');
158
mZeroLivesNode.setAttribute('visibility', 'hidden');
159
160
for (i=0; i<5; i++) {
161
mLivesNodes[i].setAttribute('visibility', i<mLives ? 'visible' : 'hidden');
162
}
163
break;
164
}
165
}
166
};
167
} ());
168
169
// --------------------------------------------- Framerate Indicator
170
mFrameRateIndicator = (function () {
171
var mTextNode = document.createTextNode('');
172
document.getElementById('hud-frame-rate-indicator').appendChild(mTextNode);
173
174
var mFrameRate = 0
175
176
return {
177
update: function (dt) {
178
if (dt === 0) {
179
return;
180
}
181
182
if (mFrameRate <= 0) {
183
/* Assume game has just started and use first time
184
step as an initial guess for the frame rate */
185
mFrameRate = 1/dt ;
186
} else {
187
/* Smooth out the frame rate readings over a number of frames */
188
mFrameRate = 0.99*mFrameRate + 0.01/dt;
189
}
190
},
191
192
updateDOM: function () {
193
mTextNode.data = mFrameRate.toPrecision(3) + 'FPS' ;
194
}
195
};
196
} ());
197
198
199
200
mRootNode.setAttribute('visibility', 'visible');
201
},
202
203
update: function (dt) {
204
mRadar.update(dt);
205
mSpeedometer.update(dt);
206
mLevelIndicator.update(dt);
207
mProgressIndicator.update(dt);
208
mLifeCounter.update(dt);
209
mFrameRateIndicator.update(dt);
210
},
211
212
updateDOM: function () {
213
mRadar.updateDOM();
214
mSpeedometer.updateDOM();
215
mLevelIndicator.updateDOM();
216
mProgressIndicator.updateDOM();
217
mLifeCounter.updateDOM();
218
mFrameRateIndicator.updateDOM();
219
}
220
};
221
}());
222
223