-
Notifications
You must be signed in to change notification settings - Fork 0
/
cannon.js
379 lines (334 loc) · 12.6 KB
/
cannon.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import { drawCircle, drawRandomColoredCircle, drawRectangle, drawCanvasFrame, randomInt } from "./common.js";
let canvas = document.getElementById("mainCanvas");
let canvasHeight = canvas.height;
let canvasWidth = canvas.width;
let canvasContext = canvas.getContext("2d");
// The score.
let scoreCard = document.getElementById("scoreCard");
let gameScore = 0;
// The encouragement.
let encouragement = document.getElementById("encouragement");
setInterval(() => {
let belt = int2KarateBelt(gameScore);
if (belt !== "black") {
encouragement.innerText = `Point-and-Click. You can do it, ${belt} belt!`;
} else {
encouragement.innerText = `Well done, ${belt} belt! You've done it!`;
}
}, 2000);
// Some settings around projectiles
const projectileSpeed = 50;
const projectileSize = 5;
// Will projectiles bounce?
const projectilesWillBounce = false;
// General Constants for Gameplay
const cursorSize = 15;
// Keep this even because I don't feel like programming around
// floors and other dumb numeric problems, OK? I'm hateful, and _will_
// enforce this
const cannonSideLength = 32;
if (cannonSideLength % 2 !== 0) {
throw new Error("Cannon side length should be even in parity!");
}
class MouseFollowingInput {
constructor() {
this.x;
this.y;
this.register();
}
register() {
// Adding listeners for the mouse
// Binding
// Here, when we add the event listener, and when updateMouseState is called
// as a callback, _inside of updateMouseState_ 'this' is NOT the class. It's
// the _canvas_, which means if I try to set this.x and this.y, that's meaning-
// less. So, I need to be able to pass the class in through to the update
// function. I can do this by setting a new variable, _cls, here, since in the
// context I'm in right now, 'this' IS the class.
// Now, on the other side, the first argument to the function being passed to bind
// will be the class, and the second argument being passed will be the event.
let _cls = this;
canvas.addEventListener("mousemove", this.updateMouseState.bind(null, _cls));
}
updateMouseState(cls, mouseEvent) {
cls.x = mouseEvent.offsetX;
cls.y = mouseEvent.offsetY;
}
get coordinates() {
return {
x: this.x,
y: this.y,
};
}
}
class CannonData {
// The cannon winds up being _centered_ on (x,y)
constructor(x, y) {
this.x = x;
this.y = y;
this.angle = 90;
}
get getBarrelAngle() {
return this.angle;
}
set setBarrelAngle(a) {
this.angle = a;
}
get coordinates() {
return {
x: this.x,
y: this.y,
};
}
set coordinates(coordinates) {
this.x = coordinates.x;
this.y = coordinates.y;
}
}
class Projectile {
constructor(x, y, angle, speed) {
this.x = x;
this.y = y;
this.angle = angle;
this.speed = speed;
this.hasBounced = false;
this.queueDeletion = false;
this.setInMotion();
}
setInMotion() {
// I'm lifting some of this logic from what I've learned in the
// bubbles game I made.
setInterval(() => {
// If we've hit the right or left side, we should reverse our slope. This will
// cause us to 'bounce off' the side, and start moving in a different direction.
// We only get to bounce off once. After that, we need to destroy ourselves.
if (this.x >= canvasWidth || this.x <= 0) {
if (projectilesWillBounce) {
if (this.hasBounced === false) {
this.speed = -this.speed;
this.hasBounced = true;
} else {
this.queueDeletion = true;
}
} else {
this.queueDeletion = true;
}
}
// If we've hit the top or bottom, we should reverse the sign of our y value. This
// will cause us to start moving in the opposite direction with respect to the y
// axis, which is just a bounce off the top or bottom.
// We only get to bounce off once. After that, we need to destroy ourselves.
if (this.y >= canvasHeight || this.y <= 0) {
if (projectilesWillBounce) {
if (this.hasBounced === false) {
this.speed = -this.speed;
this.hasBounced = true;
} else {
this.queueDeletion = true;
}
} else {
this.queueDeletion = true;
}
}
// The next position is based on a simple vector decomposition.
// Here, the magnitude is the speed, and then we decompose the
// vector angle into its consituent parts.
this.x += this.speed * Math.cos((this.angle * Math.PI) / 180);
this.y += this.speed * Math.sin((this.angle * Math.PI) / 180);
}, 50);
}
}
class RandomPathBubble {
// Lifted from Character and Enemy in bubbles.js
constructor(x, y, r, speed) {
this.x = x;
this.y = y;
this.r = r; // temporary
this.speed = speed;
this.queueDeletion = false;
this.setInMotion();
}
setInMotion() {
let slope = randomInt(-10, 10);
let ySign = 1;
// For whatever reason, when I try to use setInterval with this.moveBy,
// 'this' winds up being null in the caller, which I don't really understand
// Anyway, this is how we update random movement for the enemies. They just
// float around.
setInterval(() => {
// If we've hit the right or left side, we should reverse our slope. This will
// cause us to 'bounce off' the side, and start moving in a different direction.
if (this.x >= canvasWidth || this.x <= 0) {
slope = -slope;
}
// If we've hit the top or bottom, we should reverse the sign of our y value. This
// will cause us to start moving in the opposite direction with respect to the y
// axis, which is just a bounce off the top or bottom.
if (this.y >= canvasHeight || this.y <= 0) {
ySign = -ySign;
}
// Finally, given our computed slope and y sign, whatever they are, update our
// location.
this.x += slope;
this.y += 1 * ySign;
// I've set the interval here to be rather swift so that we pre-empt the browser
// framerate. It's very smooth this way.
}, 50);
}
}
let playerInput = new MouseFollowingInput();
// Add a cannon right in the middle of the canvas
let cannon1 = new CannonData(canvasWidth / 2, canvasHeight / 2);
// We can divine where the circle belongs based on
// the current location of the player's cursor
const drawCursor = (canvasContext, input) => {
let x = input.coordinates.x;
let y = input.coordinates.y;
drawCircle(canvasContext, x, y, cursorSize);
};
const int2KarateBelt = (integer) => {
if (integer > 700) {
return "black";
} else if (integer > 450) {
return "sienna";
} else if (integer > 250) {
return "green";
} else if (integer > 100) {
return "blue";
} else if (integer > 50) {
return "orange";
} else {
return "yellow";
}
};
const drawCannon = (canvasContext, cannonData) => {
let cannonBaseX = cannonData.coordinates.x;
let cannonBaseY = cannonData.coordinates.y;
// We're centering the cannon's base around the (x,y)
// coordinate pair given. To do so, calculate the top left
// and bottom right corners of the square of side length
// cannonSideLength centered at the (x,y) coordinate pair in
// cannonData.
let cannonBaseStart = {
x: cannonBaseX - cannonSideLength / 2,
y: cannonBaseY + cannonSideLength / 2,
};
let cannonBaseFinish = {
x: cannonBaseX + cannonSideLength / 2,
y: cannonBaseY - cannonSideLength / 2,
};
drawRectangle(canvasContext, cannonBaseStart, cannonBaseFinish);
// Rotating a rectangle that acts as the cannon's barrel.
canvasContext.save();
// Draw a rectangle to start with
// canvasContext.fillStyle = "blue";
// canvasContext.fillRect(cannonBaseX, cannonBaseY, cannonSideLength, Math.floor(cannonSideLength / 8));
// Move the transformation matrix? to the center of the cannon
canvasContext.translate(cannonBaseX, cannonBaseY);
// Rotation by the current angle of the cannon barrel according to the cannon data.
// Since we have translated over to the base of the cannon, the angle we need to pass
// in needs to be calculated based on a translated reference frame. That's fine, we
// simply subtract the location of the cannon's base from the player input location.
let referenceFrameTransformedX = playerInput.coordinates.x - cannonBaseX;
let referenceFrameTransformedY = playerInput.coordinates.y - cannonBaseY;
// Computing the angle that the cannon should have based on the
// current input value. Uses arctangent calculation using opposite
// & adjacent to save calculating a hypotenuse. Output is apparently
// already in radians, so no need to convert when using rotate().
// We can just pass in to atan2 since we have already accounted for the difference in
// the reference frame. This is the arctangent of the triangle with opp-hyp intersection
// located at (referenceFrameTransformedX, referenceFrameTransformedY)
let cannonRadians = Math.atan2(referenceFrameTransformedY, referenceFrameTransformedX);
// Reset the state of the cannon to have the new angle set. In hindsight, demanding degrees
// was probably a really bad idea. Maybe we'll fix that. Either way, we need to expose this
// so that things other than this transformation know what angle the cannon is pointing to.
let cannonDegrees = (cannonRadians * 180) / Math.PI;
cannonData.angle = cannonDegrees;
canvasContext.rotate(cannonRadians);
// Draw the rectangle!
canvasContext.fillStyle = int2KarateBelt(gameScore);
// The rectangle is at (0, -1) because the current reference frame is at the location
// where we put the cannon, and because the rectangle looks a little better if we
// translate it upward by 1.
canvasContext.fillRect(0, -1, cannonSideLength, Math.floor(cannonSideLength / 8));
// Move the transformation matrix? Back exactly to where it came from
canvasContext.restore();
};
const onScreenProjectiles = new Array();
const fireCannon = (cannonData, mouseClickEvent) => {
const x = cannonData.coordinates.x;
const y = cannonData.coordinates.y;
const angle = cannonData.angle;
let firedProjectile = new Projectile(x, y, angle, projectileSpeed);
onScreenProjectiles.push(firedProjectile);
};
// The objects in the array need to have a 'queueDeletion' property
const garbageCollectObjects = (arrayOfObjects) => {
arrayOfObjects.forEach((deletableObject) => {
if (deletableObject.queueDeletion === true) {
const deletableObjectIndex = arrayOfObjects.indexOf(deletableObject);
arrayOfObjects.splice(deletableObjectIndex, 1);
}
});
};
let maxOnScreenBubbles = 15;
let onScreenBubbles = new Array();
const bubbleGenerator = () => {
let enemySpawnX = randomInt(0, canvasWidth);
let enemySpawnY = randomInt(0, canvasHeight);
let enemySize = randomInt(30, 75);
let enemySpeed = randomInt(1, 6);
return new RandomPathBubble(enemySpawnX, enemySpawnY, enemySize, enemySpeed);
};
const randomlyGenerateBubbles = (chance) => {
// Chance: an integer in [1, 100]. Specifying 10 means there's
// a 10% chance to spawn a bubble.
if (randomInt(1, 100) > 100 - chance && onScreenBubbles.length < maxOnScreenBubbles) {
onScreenBubbles.push(bubbleGenerator());
}
};
// For computing the distance between two points
// A and B, each with a component x and a component y
const D = (A, B) => {
return Math.sqrt(Math.pow(B.y - A.y, 2) + Math.pow(B.x - A.x, 2));
};
const computeCollisions = (projectiles, bubbles) => {
projectiles.forEach((projectile) => {
bubbles.forEach((bubble) => {
if (D(projectile, bubble) <= bubble.r) {
projectile.queueDeletion = true;
bubble.queueDeletion = true;
gameScore += 1;
}
});
});
};
canvas.addEventListener("click", fireCannon.bind(null, cannon1));
const update = () => {
// clear the canvas
canvasContext.clearRect(0, 0, canvasWidth, canvasHeight);
// draw the frame around the canvas
drawCanvasFrame(canvasContext);
// draw the player's cursor
drawCursor(canvasContext, playerInput);
drawCannon(canvasContext, cannon1);
onScreenProjectiles.forEach((element) => {
drawCircle(canvasContext, element.x, element.y, projectileSize);
});
randomlyGenerateBubbles(5);
onScreenBubbles.forEach((bubble) => {
drawRandomColoredCircle(canvasContext, bubble.x, bubble.y, bubble.r);
});
computeCollisions(onScreenProjectiles, onScreenBubbles);
garbageCollectObjects(onScreenProjectiles);
garbageCollectObjects(onScreenBubbles);
scoreCard.innerText = `${gameScore}`;
};
(() => {
let animationFrameRequestToken;
const main = (hiResTimeStamp) => {
animationFrameRequestToken = window.requestAnimationFrame(main);
update();
};
main();
})();