-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGameEngine.js
More file actions
378 lines (327 loc) · 14.5 KB
/
GameEngine.js
File metadata and controls
378 lines (327 loc) · 14.5 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
376
377
378
"use strict";
/*
* The GameEngine is intended to contain logic relating to the shell of
* the game, such as: menus, configuration, keyboard, mouse, and browser
* interfaces.
*
* It initializes the game and passes all of the user's actions down to the
* PlayerEngine, which takes care of the rest.
*/
function GameEngine() {
var self = this;
//////////////////////////////
// PRIVATE MEMBER CONSTANTS //
//////////////////////////////
window.DIFFICULTY_EASY = 0;
window.DIFFICULTY_MEDIUM = 1;
window.DIFFICULTY_HARD = 2;
//////////////////////////////
// PRIVATE MEMBER VARIABLES //
//////////////////////////////
var playerEngine;
var isInMouseMode = false;
//////////////////////
// CONSTRUCTOR CODE //
//////////////////////
(function () {
//Override config constants
overrideConfigConstants()
.then(function () {
//Initialize canvas context
canvasContext = initializeAndGetCanvasContext();
attachMouseListeners();
//Get difficulty from user
getDifficultyFromUser()
.then(function (result) {
var difficulty = result;
var drawMinimap = difficulty <= DIFFICULTY_MEDIUM;
var drawPlayerOnMinimap = difficulty <= DIFFICULTY_EASY;
//Initialize game variables
isGameInProgress = true;
playerX = PLAYER_STARTING_X;
playerY = PLAYER_STARTING_Y;
playerA = PLAYER_STARTING_ANGLE_DEGREES * 2 * Math.PI / 360;
playerVerticalA = 0;
//Start engine
var raycastingEngine = new RaycastingEngine(drawMinimap, drawPlayerOnMinimap);
var tetrisEngine = new TetrisEngine();
playerEngine = new PlayerEngine(raycastingEngine, tetrisEngine);
//Initialize controls
if (isInMouseMode)
setupControlsForMouseMode();
else
setupControlsForKeyboardMode();
});
});
})();
/////////////////////
// PRIVATE METHODS //
/////////////////////
function overrideConfigConstants() {
var callback;
var callbackCalled = false;
//Notify parent window that we're ready to receive config values
parent.postMessage("ready", "*");
//Get config values if posted
window.addEventListener('message', function(event) {
if (event.data !== "ready") {
//Override
for (var key in event.data) {
if (event.data.hasOwnProperty(key)) {
window[key] = event.data[key];
}
}
callbackCalled = true;
callback();
}
});
//After a while, give up on config values and start the game anyways
setTimeout(function() {
if (!callbackCalled) {
callbackCalled = true;
callback();
}
}, CONFIG_VALUE_WAIT_TIME_MILLISECONDS);
//Return promise
return {then: function (promiseCallback) {
callback = promiseCallback;
}};
}
function getDifficultyFromUser() {
var callback;
var selectedDifficulty = DIFFICULTY_EASY;
//Draw prompt
drawDifficultyPrompt(selectedDifficulty);
//Handle user selection
document.onkeydown = function (keyboardEvent) {
switch (keyboardEvent.keyCode) {
case 38: //Up arrow
case 87: //W
selectedDifficulty--;
if (selectedDifficulty < DIFFICULTY_EASY)
selectedDifficulty += 1 + DIFFICULTY_HARD - DIFFICULTY_EASY;
drawDifficultyPrompt(selectedDifficulty);
break;
case 40: //Down arrow
case 83: //S
selectedDifficulty++;
if (selectedDifficulty > DIFFICULTY_HARD)
selectedDifficulty-= 1 + DIFFICULTY_HARD - DIFFICULTY_EASY;
drawDifficultyPrompt(selectedDifficulty);
break;
case 13: //Enter
callback(selectedDifficulty);
break;
}
}
//Return promise
return {then: function (promiseCallback) {
callback = promiseCallback;
}};
}
function drawDifficultyPrompt(selectedDifficulty) {
var selectionHeight = 120;
//Clear
canvasContext.clearRect(0, 0, canvas.width, canvas.height);
//Draw text
canvasContext.fillStyle = "black";
canvasContext.font = "bold 64px Arial";
canvasContext.fillText("Choose difficulty:", (VIEWPORT_WIDTH_PIXELS / 2) - 300, (VIEWPORT_HEIGHT_PIXELS / 2) -230);
canvasContext.fillText("Easy", (VIEWPORT_WIDTH_PIXELS / 2) - 150, (VIEWPORT_HEIGHT_PIXELS / 2) - 60 + DIFFICULTY_EASY * selectionHeight);
canvasContext.fillText("Medium", (VIEWPORT_WIDTH_PIXELS / 2) - 150, (VIEWPORT_HEIGHT_PIXELS / 2) - 60 + DIFFICULTY_MEDIUM * selectionHeight);
canvasContext.fillText("Hard", (VIEWPORT_WIDTH_PIXELS / 2) - 150, (VIEWPORT_HEIGHT_PIXELS / 2) - 60 + DIFFICULTY_HARD * selectionHeight);
//Draw selection
canvasContext.fillStyle = WALL_COLORS[WALL_TYPE_PIECE_ACTIVE - 1];
canvasContext.fillRect((VIEWPORT_WIDTH_PIXELS / 2) - 173, (VIEWPORT_HEIGHT_PIXELS / 2) -135 + selectedDifficulty * selectionHeight, 60, 20);
canvasContext.fillRect((VIEWPORT_WIDTH_PIXELS / 2) - 173, (VIEWPORT_HEIGHT_PIXELS / 2) -115 + selectedDifficulty * selectionHeight, 20, 20);
canvasContext.fillRect((VIEWPORT_WIDTH_PIXELS / 2) - 173, (VIEWPORT_HEIGHT_PIXELS / 2) -50 + selectedDifficulty * selectionHeight, 60, 20);
canvasContext.fillRect((VIEWPORT_WIDTH_PIXELS / 2) - 173, (VIEWPORT_HEIGHT_PIXELS / 2) -70 + selectedDifficulty * selectionHeight, 20, 20);
canvasContext.fillRect((VIEWPORT_WIDTH_PIXELS / 2) + 57, (VIEWPORT_HEIGHT_PIXELS / 2) -135 + selectedDifficulty * selectionHeight, 60, 20);
canvasContext.fillRect((VIEWPORT_WIDTH_PIXELS / 2) + 97, (VIEWPORT_HEIGHT_PIXELS / 2) -115 + selectedDifficulty * selectionHeight, 20, 20);
canvasContext.fillRect((VIEWPORT_WIDTH_PIXELS / 2) + 57, (VIEWPORT_HEIGHT_PIXELS / 2) -50 + selectedDifficulty * selectionHeight, 60, 20);
canvasContext.fillRect((VIEWPORT_WIDTH_PIXELS / 2) + 97, (VIEWPORT_HEIGHT_PIXELS / 2) -70 + selectedDifficulty * selectionHeight, 20, 20);
canvasContext.stroke();
}
function attachMouseListeners(){
var canvasElement = document.getElementById("canvas");
canvasElement.onclick = function () {
//Lock pointer and enter mouse mode
if ('pointerLockElement' in document || 'mozPointerLockElement' in document || 'webkitPointerLockElement' in document) {
canvasElement.requestPointerLock = canvasElement.requestPointerLock ||
canvasElement.mozRequestPointerLock ||
canvasElement.webkitRequestPointerLock;
canvasElement.requestPointerLock();
isInMouseMode = true;
if (isGameInProgress)
setupControlsForMouseMode();
//Allow unlocking out of mouse mode
document.onpointerlockchange = function (e) {
if (document.pointerLockElement !== canvasElement
&& document.mozPointerLockElement !== canvasElement
&& document.webkitPointerLockElement !== canvasElement) {
isInMouseMode = false;
if (isGameInProgress)
setupControlsForKeyboardMode();
}
}
document.onmozpointerlockchange = document.onpointerlockchange;
document.onwebkitpointerlockchange = document.onpointerlockchange;
}
}
}
function attachKeyboardListeners(isMouseMode) {
//Key down
document.onkeydown = function (keyboardEvent) {
if (isGameInProgress) {
switch (keyboardEvent.keyCode) {
case 87: //W
playerEngine.tryStartMovingForward();
break;
case 83: //S
playerEngine.tryStartMovingBackwards();
break;
case 65: //A
if (isMouseMode)
playerEngine.tryStartSidesteppingLeft();
else
playerEngine.tryStartTurningLeft();
break;
case 68: //D
if (isMouseMode)
playerEngine.tryStartSidesteppingRight();
else
playerEngine.tryStartTurningRight();
break;
case 81: //Q
if (!isMouseMode)
playerEngine.tryStartSidesteppingLeft();
break;
case 69: //E
if (!isMouseMode)
playerEngine.tryStartSidesteppingRight();
break;
case 37: //Right arrow
if (!isMouseMode) {
playerEngine.tryMovePieceRight();
}
break;
case 38: //Up arrow
if (!isMouseMode) {
playerEngine.tryRotatePieceClockwise();
}
break;
case 39: //Left arrow
if (!isMouseMode) {
playerEngine.tryMovePieceLeft();
}
break;
case 40: //Down arrow
if (!isMouseMode)
playerEngine.tryStartDroppingPiece();
break;
case 32: //Space
if (isMouseMode)
playerEngine.tryStartDroppingPiece();
}
}
}
//Key up
document.onkeyup = function (keyboardEvent) {
if (isGameInProgress) {
switch (keyboardEvent.keyCode) {
case 87: //W
playerEngine.tryStopMovingForward();
break;
case 83: //S
playerEngine.tryStopMovingBackwards();
break;
case 65: //A
if (isMouseMode)
playerEngine.tryStopSidesteppingLeft();
else
playerEngine.tryStopTurningLeft();
break;
case 68: //D
if (isMouseMode)
playerEngine.tryStopSidesteppingRight();
else
playerEngine.tryStopTurningRight();
break;
case 81: //Q
if (!isMouseMode)
playerEngine.tryStopSidesteppingLeft();
break;
case 69: //E
if (!isMouseMode)
playerEngine.tryStopSidesteppingRight();
break;
case 40: //Down arrow
if (!isMouseMode)
playerEngine.tryStopDroppingPiece();
break;
case 32: //Space
if (isMouseMode)
playerEngine.tryStopDroppingPiece();
}
}
}
}
function setupControlsForMouseMode() {
//Bind keyboard controls for mouse mode
attachKeyboardListeners(true);
//Mouse look
document.onmousemove = function (e) {
if (isGameInProgress) {
playerEngine.tryTurnLeft(MOUSE_SENSITIVITY * -e.movementX);
playerEngine.tryLookUp(MOUSE_SENSITIVITY * -e.movementY);
}
};
//Mouse click
document.onmousedown = function (e) {
if (isGameInProgress) {
switch (e.button) {
case 0: //Left click
if(e.ctrlKey)
playerEngine.tryRotatePieceCounterClockwise();
else
playerEngine.tryPullPiece();
break;
case 1: //Middle button
playerEngine.tryStartDroppingPiece();
break;
case 2: //Right click
if(e.ctrlKey)
playerEngine.tryRotatePieceClockwise();
else
playerEngine.tryPushPiece();
break;
}
}
}
//Mouse unclick
document.onmouseup = function (e) {
playerEngine.tryStopDroppingPiece();
};
//Disable browser context menu
document.oncontextmenu = function (e) {
e.preventDefault();
};
};
function setupControlsForKeyboardMode() {
//Bind keyboard for keyboard mode
attachKeyboardListeners(false);
//Unbind mouse listeners
document.onmousemove = null;
document.onmousedown = null;
document.onmouseup = null;
document.oncontextmenu = null;
}
function initializeAndGetCanvasContext() {
var canvas = document.getElementById("canvas");
canvas.setAttribute("width", VIEWPORT_WIDTH_PIXELS);
canvas.setAttribute("height", VIEWPORT_HEIGHT_PIXELS);
var context = canvas.getContext("2d");
context.lineWidth = COLUMN_WIDTH_PIXELS;
return context;
}
}