-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicGameLoop__NonStopArcade.js
More file actions
351 lines (292 loc) · 8.95 KB
/
BasicGameLoop__NonStopArcade.js
File metadata and controls
351 lines (292 loc) · 8.95 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
//
// This version of code adapted by Eric Marceau, Ottawa, Canada
// Intent of the code layout and comments is
// - for reference documentation
// - educational purposes as template example
//
// Date: 2025-03-15
//
//###################################################################################
//
// PSEUDO-CODE START
//
// Simplified pseudo-code for logic segmentation within JavaScript game loop
//
//###################################################################################
/*
function masterGameLoop(){
// 1. Handle Input
processInput() ;
// 2. Update Game State
changeState() ;
// 3. Render Graphics
render() ;
// 4. Repeat the loop
//window.requestAnimationFrame( masterGameLoop ) ; // Original
requestAnimationFrame( masterGameLoop ) ; // Iteration 1
}
// 0. Initiate game loop
//window.requestAnimationFrame( masterGameLoop ) ; // Original
//requestAnimationFrame( masterGameLoop ) ; // Iteration 1
masterGameLoop() ; // Iteration 2
// REF: https://dev.to/chintanonweb/canvas-and-game-loops-explained-a-one-byte-overview-for-game-development-5epg
*/
//###################################################################################
//
// PSEUDO-CODE END
//
//###################################################################################
//###################################################################################
//
// Adaptation of Basic Game Loop provided in the following article:
//
// REF: https://www.sitepoint.com/quick-tip-game-loop-in-javascript/
//
// KEY DIFFERENCES:
// - code formatting for legibility
// - replacing references to event.keyCode with event.key
// - introduction of "maxspeed" variable
// - adding inline comments about purpose of code segments
//
//###################################################################################
//
// Define JavaScript canvas object that will be target for all graphics display
//
var canvas = document.getElementById("myGameCanvas") ;
var ctx = canvas.getContext("2d") ;
//
// Set display size parameters outside of function for global accessibility
//
var width ;
var height ;
//
// Define logic to adapt canvas "viewport" range to user's resizing of window
//
function reSize() {
width = window.innerWidth ;
height = window.innerHeight ;
canvas.width = width ;
canvas.height = height ;
}
//
// Initialize parameters related to canvas object for sizing to window
//
reSize() ;
//
// Define event watch for window resizing
//
window.addEventListener('resize', reSize);
//
// Object definition to store state properties for Asteroids-like rocket ship
//
var state = {
position: {
x: (width / 2),
y: (height / 2)
},
movement: {
x: 0,
y: 0
},
rotation: 0,
keyPress: {
left: false,
right: false,
up: false,
down: false
}
}
//
// This code segment is of UNKNOWN USEFULNESS.
// If is retained here for reference purposes only.
//
// Not sure how these labels/values relate to those which are which are referenced in
// the later code and are functional.
//
// URL: https://codeincomplete.com/articles/javascript-game-foundations-player-input/
//
// N.B.: NOT using keyCode because it apparently varies among devices/manufacturers
/*
var KEY = {
BACKSPACE: 8,
TAB: 9,
RETURN: 13,
ESC: 27,
SPACE: 32,
PAGEUP: 33,
PAGEDOWN: 34,
END: 35,
HOME: 36,
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40,
INSERT: 45,
DELETE: 46,
ZERO: 48, ONE: 49, TWO: 50, THREE: 51, FOUR: 52, FIVE: 53, SIX: 54, SEVEN: 55, EIGHT: 56, NINE: 57,
A: 65, B: 66, C: 67, D: 68, E: 69, F: 70, G: 71, H: 72, I: 73, J: 74, K: 75, L: 76, M: 77,
N: 78, O: 79, P: 80, Q: 81, R: 82, S: 83, T: 84, U: 85, V: 86, W: 87, X: 88, Y: 89, Z: 90,
TILDA: 192
};
*/
//
// Functions for detecting keyboard input and handling/tracking state of keys pressed by user
//
function keydown(event) {
var key = event.key ;
state.keyPress[key] = true ;
}
function keyup(event) {
var key = event.key ;
state.keyPress[key] = false ;
}
//
// Define event watch for when keys are pressed or not so as to know
// when, or not, to respond to that key's programmed function
//
window.addEventListener("keydown", keydown, false) ;
window.addEventListener("keyup", keyup, false) ;
//
// IMPORTANT: Key label strings are CASE SENSITIVE
//
// REF: List of documented event key value "labels"
// URL: https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_key_values#navigation_keys
//
// Modifying state for change in orientation
//
function updateRotation(p) {
if (state.keyPress.ArrowLeft) {
// Left: event.key is "ArrowLeft", keyCode is 37
state.rotation -= p * 5 ;
}
else if (state.keyPress.ArrowRight) {
// Right: event.key is "ArrowRight", keyCode is 39
state.rotation += p * 5 ;
}
}
//
// Modifying state for change in direction of movement based on orientation
//
function updateMovement(p) {
// Mapping rotation to [x,y] vector components
var accelerationVector = {
x: p * 0.2 * Math.cos((state.rotation-90) * (Math.PI/180)),
y: p * 0.2 * Math.sin((state.rotation-90) * (Math.PI/180))
}
if (state.keyPress.ArrowUp) {
// Up: event.key is "ArrowUp", keyCode is 38
state.movement.x += accelerationVector.x ;
state.movement.y += accelerationVector.y ;
}
else if (state.keyPress.ArrowDown) {
// Down: event.key is "ArrowDown", keyCode is 40
state.movement.x -= accelerationVector.x ;
state.movement.y -= accelerationVector.y ;
}
// Restrict movement to a pre-set maximum value
const maxspeed = 40 ;
if ( state.movement.x > maxspeed ) {
state.movement.x = maxspeed ;
}
else if (state.movement.x < -maxspeed ) {
state.movement.x = -maxspeed ;
}
if ( state.movement.y > maxspeed ) {
state.movement.y = maxspeed ;
}
else if (state.movement.y < -maxspeed ) {
state.movement.y = -maxspeed ;
}
}
//
// Modifying state for position change within display (a.k.a movement)
// If motion carries toward one edge, it re-defines as moving away,
// on same direction vector, away from the opposite edge of the canvas.
//
function updatePosition(p) {
state.position.x += state.movement.x ;
state.position.y += state.movement.y ;
// Detect boundaries
if ( state.position.x > width) {
state.position.x -= width ;
}
else if (state.position.x < 0 ) {
state.position.x += width ;
}
if ( state.position.y > height) {
state.position.y -= height ;
}
else if (state.position.y < 0 ) {
state.position.y += height ;
}
}
//
// Updating the complete game state as input for the next display refresh
//
function update(progress) {
// Update the state of the world for the elapsed time since last render
var p = progress / 16 ;
updateRotation(p) ; // Evaluate state for current ship orientation
updateMovement(p) ; // Evaluate state for current ship dynamics/momentum
updatePosition(p) ; // Evaluate state for resulting ship position
}
//
// Re-drawing the graphics displayed representing the game's latest state image/frame
//
function draw() {
// Draw the state of the world
ctx.clearRect(0, 0, width, height) ;
//
// Save current canvas state
//
ctx.save() ;
ctx.translate(state.position.x, state.position.y) ;
ctx.rotate((Math.PI/180) * state.rotation) ;
//
// Define style applied for all strokes (lines, circles, etc.)
//
ctx.strokeStyle = 'white' ;
ctx.lineWidth = 2 ;
//
// IMPORTANT:
// Use of beginPath() at every Frame will "flush" contents
// of canvas object, to prevent endless reserving of memory
// URL: https://stackoverflow.com/a/23858808
//
ctx.beginPath() ; // NB: required for delimiting start of object to which the "stroke()" action will apply
//
// Draw the outline representing the ship that is to be controlled by the key-press signals
//
ctx.moveTo( 0, 0) ; // [y,x]
ctx.lineTo( 10, 10) ;
ctx.lineTo( 0, -20) ;
ctx.lineTo(-10, 10) ;
ctx.lineTo( 0, 0) ;
ctx.closePath() ; // Directive to join current position on "path" with start of "path"
ctx.stroke() ; // Apply "style" to latest constructed path element
ctx.restore() ; // Directive that "pops" background construct of display contents onto canvas for display
}
//
// Game Loop
//
function loop(timestamp) {
var progress = timestamp - lastRender ; // Calculate elapsed "real-time" used to
// re-define the evolved game state
update(progress) ; // Re-assess components of game state based on elapsed "real-time"
draw() ; // Re-draw/Display graphics to refelct the new state of the game
lastRender = timestamp ; // Keep track of when last state UPDATE was performed
//
// Correct form for repetitively calling game loop for iterations
//
window.requestAnimationFrame(loop) ; // ??? is "timestamp" variable implicit ???
}
//
// Initialize timestamp reference
//
var lastRender = 0 ;
//
// REF: Method for starting the actual "Game Loop" per Mozilla
// URL: https://developer.mozilla.org/en-US/docs/Games/Anatomy
//
//window.requestAnimationFrame(loop) ; // Original form
loop() ; // Iteration 1