forked from jtc42/monopong.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonopong.js
355 lines (287 loc) · 11.3 KB
/
monopong.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
import {
PlayArea
} from './objects/playarea.js'
import {
leftKeyID,
rightKeyID,
escKeyID,
difficulty
} from './logic/basics.js'
var VERSION = "delta"
//PWA STUFF
//Register service worker
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js')
.then(function (registration) {
console.log('Registration successful, scope is:', registration.scope);
})
.catch(function (error) {
console.log('Service worker registration failed, error:', error);
});
}
//SET UP CANVAS
var canvas = document.querySelector('canvas');
var ctx = canvas.getContext('2d');
//Store view sizes
var viewWidth = document.body.clientWidth;
var viewHeight = document.body.clientHeight;
// Set display size (css pixels).
canvas.style.width = viewWidth + "px";
canvas.style.height = viewHeight + "px";
// Set actual size in memory (scaled to account for extra pixel density).
var scale = window.devicePixelRatio;
canvas.width = viewWidth * scale;
canvas.height = viewHeight * scale;
// Normalize coordinate system to use css pixels.
ctx.scale(scale, scale);
const playareaMain = new PlayArea(viewWidth, viewHeight)
//Function to recalculate all dimensions
function resizeCanvas() {
//Pause game immediately
if (playareaMain.gameStarted && !playareaMain.gamePaused) {
playareaMain.pauseGame()
}
//Update view sizes
viewWidth = document.body.clientWidth;
viewHeight = document.body.clientHeight;
// Update display size (css pixels).
canvas.style.width = viewWidth + "px";
canvas.style.height = viewHeight + "px";
// Set actual size in memory (scaled to account for extra pixel density).
scale = window.devicePixelRatio;
canvas.width = viewWidth * scale;
canvas.height = viewHeight * scale;
// Normalize coordinate system to use css pixels.
ctx.scale(scale, scale);
// Canvas-scaled dimensions
playareaMain.calculateDims(viewWidth, viewHeight);
}
// Handle resize events
window.addEventListener('resize', resizeCanvas, false);
//HANDLE CONTEXT MENU OVERRIDE
if (document.addEventListener) {
document.addEventListener('contextmenu', function (e) {
console.log("You've tried to open context menu"); //here you draw your own menu
e.preventDefault();
e.preventDefault && e.preventDefault();
e.stopPropagation && e.stopPropagation();
e.cancelBubble = true;
e.returnValue = false;
}, false);
} else {
document.attachEvent('oncontextmenu', function () {
console.log("You've tried to open context menu the other way");
window.event.returnValue = false;
});
}
// HANDLE CONTROLS
// Key IDs
addEventListener("keydown", function (e) {
playareaMain.keysDown[e.keyCode] = true; //Add key to array
}, false);
addEventListener("keyup", function (e) {
delete playareaMain.keysDown[e.keyCode]; //Remove key from array
}, false);
// HANDLE TOUCH EVENTS
// Get the position of a touch relative to the canvas
function getTouchPos(canvasDom, touchEvent) {
var rect = canvasDom.getBoundingClientRect();
return {
x: touchEvent.touches[0].clientX - rect.left,
y: touchEvent.touches[0].clientY - rect.top
};
}
canvas.addEventListener("touchstart", function (e) {
mousePos = getTouchPos(canvas, e);
if (mousePos['x'] < x0) {
playareaMain.keysDown[leftKeyID] = true; //Add key to array (emulates a keyboard keypress)
} else {
playareaMain.keysDown[rightKeyID] = true; //Add key to array (emulates a keyboard keypress)
}
}, false);
canvas.addEventListener("touchend", function (e) {
if (mousePos['x'] < x0) {
delete playareaMain.keysDown[leftKeyID]; //Remove key from array (emulates a keyboard key release)
} else {
delete playareaMain.keysDown[rightKeyID]; //Remove key from array (emulates a keyboard key release)
}
}, false);
//FRAME RATE
var lastAnimationFrameTime = 0,
lastFpsUpdateTime = 0;
function calculateFps(now) {
var fps = 1000 / (now - lastAnimationFrameTime);
lastAnimationFrameTime = now;
if (now - lastFpsUpdateTime > 1000) {
lastFpsUpdateTime = now;
}
return Math.round(fps);
}
// Create objects for game
//ANIMATION SEQUENCE
function loop(now) {
//Run main loop
clear();
playareaMain.update(); //Update all positions
playareaMain.collisionHandler(); //Handle ball-batton collisions
draw(playareaMain); //Redraw in new positions
queue();
//Get FPS and speedScale
var fps = calculateFps(now);
var fpsScale = 60 / fps;
playareaMain.level = Math.round((playareaMain.hits + 5) / 10);
//Set speedScale by FPS and level increments
playareaMain.speedScale = fpsScale * difficulty(playareaMain.level, 1.4, 0.3);
//Check focus
if (playareaMain.gameStarted && !playareaMain.gamePaused && (!document.hasFocus() || escKeyID in playareaMain.keysDown)) { //If started, not paused, and (not in focus or escape pressed)
playareaMain.pauseGame();
}
}
//CLEAR CANVAS ON EVERY FRAME
function clear() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
function regularPolygon(ctx, x, y, radius, sides) {
if (sides < 3) return;
ctx.beginPath();
var a = ((Math.PI * 2)/sides);
ctx.translate(x,y);
ctx.moveTo(radius,0);
for (var i = 1; i < sides; i++) {
ctx.lineTo(radius*Math.cos(a*i),radius*Math.sin(a*i));
}
ctx.closePath();
ctx.translate(-x,-y);
}
function draw(playarea) { //DRAW FRAME
//Calculate text sizes
var fontTitle = "normal " + 0.20 * playarea.R + "px monospace";
var fontBig = "normal " + 0.15 * playarea.R + "px monospace";
var fontMedium = "normal " + 0.08 * playarea.R + "px monospace";
var fontSmall = "normal " + 0.062 * playarea.R + "px monospace";
var ringColour = '#00bca6';
//Recalculate ball size
playarea.ball.size = 0.032 * playarea.R;
//Set ring colour
if (playarea.gameOver && !playarea.startTimerActive) { //If gameOver and startTimer not started
ringColour = '#FF0000';
} else if (playarea.gamePaused && !playarea.pauseTimerActive) {
ringColour = '#FFFFFF';
} else if (playarea.startTimerActive || playarea.pauseTimerActive || (!playarea.gameOver && !playarea.gameStarted)) { //If any timer started, or not gameOver but game not started (ie first run)
ringColour = '#ff7700';
} else { //If game is running
if (playarea.level <= 5) {
ringColour = '#00bca6';
} else {
ringColour = '#505050'; //Make decoration gray after level 10
}
}
// Draw ring below level 6
if (playarea.level <= 5) {
ctx.beginPath();
ctx.arc(playarea.x0, playarea.y0, playarea.R, 0, 2 * Math.PI);
ctx.lineWidth = 0.01 * playarea.R;
ctx.strokeStyle = ringColour;
ctx.shadowBlur = 20;
ctx.shadowColor = ringColour;
ctx.stroke();
ctx.shadowBlur = 0;
}
//Draw batton decoration below level 11
if (playarea.level <= 10) {
ctx.beginPath();
ctx.arc(playarea.x0, playarea.y0, 1.012 * playarea.R, playarea.batton.angle - 1.5 * playarea.batton.size, playarea.batton.angle + 1.5 * playarea.batton.size);
ctx.lineWidth = 0.025 * playarea.R;
ctx.strokeStyle = ringColour;
ctx.stroke();
}
//Velocity indicator
if (playarea.gamePaused) {
ctx.globalAlpha = 0.3;
ctx.beginPath();
ctx.fillStyle = '#ffffff';
ctx.arc(playarea.ball.position.x - 3 * playarea.ball.velocity.x, playarea.ball.position.y - 3 * playarea.ball.velocity.y, playarea.ball.size, 0, 2 * Math.PI, false);
ctx.fill();
ctx.globalAlpha = 0.1;
ctx.beginPath();
ctx.fillStyle = '#ffffff';
ctx.arc(playarea.ball.position.x - 6 * playarea.ball.velocity.x, playarea.ball.position.y - 6 * playarea.ball.velocity.y, playarea.ball.size, 0, 2 * Math.PI, false);
ctx.fill();
ctx.globalAlpha = 1.0;
}
// Draw hits decoration
// regularPolygon(ctx, playarea.x0, playarea.y0, 1.2 * playarea.R, playarea.hits%10);
// ctx.strokeStyle = '#ccc';
// ctx.shadowBlur = 0;
// ctx.stroke();
//Ball
ctx.beginPath();
ctx.fillStyle = '#ffffff';
ctx.arc(playarea.ball.position.x, playarea.ball.position.y, playarea.ball.size, 0, 2 * Math.PI, false);
ctx.fill();
//Batton
ctx.beginPath();
ctx.arc(playarea.x0, playarea.y0, 1.012 * playarea.R, -playarea.batton.angle - 0.5 * playarea.batton.size, -playarea.batton.angle + 0.5 * playarea.batton.size);
ctx.lineWidth = 0.035 * playarea.R;
ctx.strokeStyle = '#ffffff';
ctx.stroke();
//Title
if (!playarea.gameStarted) { //If game hasn't started
ctx.fillStyle = "#ffffff";
if (!playarea.startTimerActive) { //If countdown hasn't started
ctx.textAlign = "center";
ctx.font = fontMedium;
ctx.fillText("TOUCH/ENTER TO START", playarea.x0, playarea.y0 + (0.18 * playarea.R));
if (!playarea.gameOver) {
ctx.font = fontSmall;
ctx.fillText("TOUCH LEFT/RIGHT OF DISPLAY", playarea.x0, playarea.y0 + (0.30 * playarea.R));
ctx.fillText("OR USE LEFT/RIGHT KEYS TO MOVE", playarea.x0, playarea.y0 + (0.38 * playarea.R));
ctx.font = fontTitle;
ctx.fillText("MONOPONG", playarea.x0, playarea.y0 - (0.28 * playarea.R));
ctx.font = fontMedium;
ctx.fillText(VERSION, playarea.x0, playarea.y0 - (0.14 * playarea.R));
}
} else {
ctx.font = fontTitle;
ctx.textAlign = "center";
ctx.fillText(playarea.timerValue, playarea.x0, playarea.y0 - (0.28 * playarea.R));
}
}
//Gameover screen
if (playarea.gameOver && !playarea.startTimerActive) {
ctx.font = fontBig;
ctx.fillText("GAME OVER", playarea.x0, playarea.y0 - (0.28 * playarea.R));
ctx.font = fontMedium;
ctx.fillText("SCORE: " + playarea.hits, playarea.x0, playarea.y0 - (0.14 * playarea.R));
}
//Pause screen
if (playarea.gamePaused) {
ctx.textAlign = "center";
ctx.font = fontMedium;
ctx.fillText("TOUCH/ENTER TO START", playarea.x0, playarea.y0 + (0.18 * playarea.R));
if (playarea.pauseTimerActive) { //If unpause timer has started
ctx.textAlign = "center";
ctx.font = fontTitle;
ctx.fillText(playarea.timerValue, playarea.x0, playarea.y0 - (0.28 * playarea.R));
} else { //If paused, and unpause timer not started
ctx.font = fontBig;
ctx.fillText("PAUSED", playarea.x0, playarea.y0 - (0.28 * playarea.R));
}
}
//Score
ctx.font = fontSmall;
ctx.fillStyle = "#ffffff";
ctx.textAlign = "left";
ctx.fillText("Level: " + playarea.level, (0.18 * playarea.R), (0.18 * playarea.R));
ctx.fillText("Hits: " + playarea.hits, (0.18 * playarea.R), (0.30 * playarea.R));
ctx.fillText("Highscore: " + playarea.topScore, (0.18 * playarea.R), (0.42 * playarea.R));
// Debug
// ctx.fillText("Batton: " + playarea.batton.angle, (0.18 * playarea.R), (0.54 * playarea.R));
// ctx.fillText("Ball: " + playarea.ball.positionAngle + ", " + playarea.ball.positionRadius, (0.18 * playarea.R), (0.66 * playarea.R));
}
function queue() { //GET NEW FRAME
window.requestAnimationFrame(loop);
}
window.onload = function () {
loop(); //Run animation loop
}