-
Notifications
You must be signed in to change notification settings - Fork 21
/
GameCore.js
363 lines (310 loc) · 8.89 KB
/
GameCore.js
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
/**
* The GameCore class provides the base to build games on.
* @name GameCore
* @constructor GameCore
* @example
* var myGame = new GameCore({
* canvasId: 'myCanvas',
* update: function(millis){
* // do updating of game state
* },
* draw: function(context){
* // do drawing of the game
* }
* });
*
* //start the game
* myGame.run();
*/
const InputManager = require('./InputManager');
const ResourceManager = require('./ResourceManager');
class GameCore {
constructor(options = {}){
/**
* Whether or not the game should be running its loop
* @type {Boolean}
* @memberOf GameCore#
* @default
*/
this.isRunning = false;
/**
* The id of the canvas element to use render the game on
* @type {String}
* @memberOf GameCore#
* @default
*/
this.canvasId = null;
/**
* Max number of milliseconds between updates. (in case user switches tabs and requestAnimationFrame pauses)
* @type {Number}
* @memberOf GameCore#
* @default
*/
this.maxStep = 40;
/**
* The type of context to request from the canvas. 2d or 3d
* @type {String}
* @memberOf GameCore#
* @default
*/
this.contextType = '2d';
/**
* The height of the Game and canvas
* @type {Number}
* @memberOf GameCore#
* @default
*/
this.height = 0;
/**
* The width of the Game and canvas
* @type {Number}
* @memberOf GameCore#
* @default
*/
this.width = 0;
/**
* The ResourceManager to be used for game
* @type {ResourceManager}
* @memberOf GameCore#
* @default
*/
this.resourceManager = null;
/**
* The InputManager to be used for game
* @type {InputManager}
* @memberOf GameCore#
* @default
*/
this.inputManager = null;
/**
* The style to be used for the foreground while game resources are loading
* @type {String}
* @memberOf GameCore#
* @default
*/
this.loadingForeground = '#00F';
/**
* The style to be used for the background while game resources are loading
* @type {String}
* @memberOf GameCore#
* @default
*/
this.loadingBackground = '#FFF';
/**
* The ID of a DOM element that contains the game's canvas
* @type {String}
* @memberOf GameCore#
* @default
*/
this.gameAreaId = null;
/**
* The percentage (0 to 1.0) of the height and width the canvas should use to fill in its container DOM element
* @type {Number}
* @memberOf GameCore#
* @default
*/
this.canvasPercentage = 0;
Object.assign(this, options);
if(!this.resourceManager){
this.resourceManager = new ResourceManager();
}
}
/**
* Sets the height on your GameCore instance and on your canvas reference
* @function
* @memberOf GameCore#
* @param {Number} newHeight The new height desired
*/
setHeight(newHeight){
this.height = newHeight;
this.canvas.height = newHeight;
}
/**
* Sets the width on your GameCore instance and on your canvas reference
* @function
* @memberOf GameCore#
* @param {Number} newWidth The new width desired
*/
setWidth(newWidth){
this.width = newWidth;
this.canvas.width = newWidth;
}
/**
* Signals the game loop that it's time to quit
* @function
* @memberOf GameCore#
*/
stop() {
this.isRunning = false;
}
/**
* Launches the game.
* @function
* @memberOf GameCore#
*/
run() {
if(!this.isRunning){
this.init();
this.loadResources(this.resourceManager);
this.initInput(this.inputManager);
this.launchLoop();
}
}
/**
* Can be overidden in GameCore subclasses to load images and sounds
* @function
* @memberOf GameCore#
* @param {ResourceManager} resourceManager
*/
loadResources(resourceManager){
}
/**
* Sets the screen mode and initiates and objects.
* @function
* @memberOf GameCore#
*/
init() {
if(!this.canvas){
this.canvas = document.getElementById(this.canvasId);
}
if(!this.canvas){
alert('Sorry, your browser does not support canvas. I recommend any browser but Internet Explorer');
return;
}
if(!this.context){
this.context = this.canvas.getContext(this.contextType);
}
if(!this.context){
alert('Sorry, your browser does not support a ' + this.contextType + ' drawing surface on canvas. I recommend any browser but Internet Explorer');
return;
}
this.setHeight(this.height || this.canvas.height);
this.setWidth(this.width || this.canvas.width);
if(!this.inputManager){
//handle resizing if gameArea and canvasPercentage are specified
if(this.gameAreaId && this.canvasPercentage){
this.inputManager = new InputManager({
canvas: this.canvas,
gameArea: document.getElementById(this.gameAreaId),
canvasPercentage: this.canvasPercentage
});
}else{
this.inputManager = new InputManager({
canvas: this.canvas
});
}
}
this.inputManager.resize();
this.isRunning = true;
}
/**
* Can be overidden in the subclasses to map user input to actions
* @function
* @memberOf GameCore#
* @param {InputManager} inputManager
*/
initInput(inputManager) {
}
/**
* Can be overidden in the subclasses to deal with user input before updating the game state
* @function
* @memberOf GameCore#
* @param {InputManager} inputManager
* @param {Number} elapsedTime Elapsed time in milliseconds
*/
handleInput(inputManager,elapsedTime) {
}
/**
* Runs through the game loop until stop() is called.
* @function
* @memberOf GameCore#
*/
gameLoop() {
this.currTime = new Date().getTime();
this.elapsedTime = Math.min(this.currTime - this.prevTime, this.maxStep);
this.prevTime = this.currTime;
//it's using a resource manager, but resources haven't finished
if(this.resourceManager && !this.resourceManager.resourcesReady()){
this.updateLoadingScreen(this.elapsedTime);
this.drawLoadingScreen(this.context);
} else {
this.handleInput(this.inputManager,this.elapsedTime);
if(!this.paused){
// update
this.update(this.elapsedTime);
}
// draw the screen
this.context.save();
this.draw(this.context);
this.context.restore();
}
}
/**
* Launches the game loop.
* @function
* @memberOf GameCore#
*/
launchLoop(){
this.elapsedTime = 0;
var startTime = Date.now();
this.currTime = startTime;
this.prevTime = startTime;
//need to keep the context defined here so the game loop has access to it
this.loopRunner = this.loopRunner.bind(this);
window.requestAnimationFrame(this.loopRunner);
}
loopRunner(){
this.gameLoop();
window.requestAnimationFrame(this.loopRunner);
}
/**
* Should be overridden to update the state of the game/animation based on the amount of elapsed time that has passed.
* @function
* @memberOf GameCore#
* @param {Number} elapsedTime Elapsed time in milliseconds
*/
update(elapsedTime) {
}
/**
* Can be overridden to update the state of the game/animation while a custom loading screen is displayed.
* @function
* @memberOf GameCore#
* @param {Number} elapsedTime Elapsed time in milliseconds
*/
updateLoadingScreen(elapsedTime) {
}
/**
* Draws to the screen. Subclasses or instances must override this method to paint items to the screen.
* @function
* @memberOf GameCore#
* @param {Context} context An HTML5 canvas drawing context.
*/
draw(context){
if(this.contextType === '2d'){
context.font = "14px sans-serif";
context.fillText("This game does not have its own draw function!", 10, 50);
}
}
/**
* Draws the progress of the resource manger to the screen while loading.
* Subclasses or instances may override for custom loading animations.
* @function
* @memberOf GameCore#
* @param {Context} context An HTML5 canvas drawing context.
*/
drawLoadingScreen(context){
if(this.resourceManager && (this.contextType === '2d')){
context.fillStyle = this.loadingBackground;
context.fillRect(0,0, this.width,this.height);
context.fillStyle = this.loadingForeground;
context.strokeStyle = this.loadingForeground;
var textPxSize = Math.floor(this.height/12);
context.font = "bold " + textPxSize + "px sans-serif";
context.fillText("Loading... " + this.resourceManager.getPercentComplete() + "%", this.width * 0.1, this.height * 0.55);
context.strokeRect(this.width * 0.1, this.height * 0.7, this.width * 0.8, this.height * 0.1);
context.fillRect(this.width * 0.1, this.height * 0.7, (this.width * 0.8) * this.resourceManager.getPercentComplete()/100, this.height * 0.1);
context.lineWidth = 4;
}
}
}
module.exports = GameCore;