-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
431 lines (365 loc) · 11.5 KB
/
sketch.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
const Engine = Matter.Engine;
const World = Matter.World;
const Bodies = Matter.Bodies;
const Body = Matter.Body;
const Events = Matter.Events;
let engine;
let world;
const exaustClouds = 25;
let car;
let computerCar;
let balls = [];
let walls = [];
let bumpers = [];
let pockets = [];
let visibleWallOffset;
let ballComputerIsFocusedOn;
let computersBallType = 'solid';
let gameStarted = false;
let replayButton;
let countdownMode = true;
let countdownText;
let eightBallEarthquake = false;
function resetGame() {
replayButton.hide();
// remove previous balls and setup new balls
balls.forEach(b => {
World.remove(world, b.body);
})
balls = [];
setupRackOfBalls();
updateCountdownOverlay();
// remove previous cars and setup new cars
World.remove(world, car.body);
World.remove(world, computerCar.body);
car = new Car(isPlayer = true)
computerCar = new Car(isPlayer = false);
computerCar.accelerating(true);
}
function setup() {
const h = min(window.innerHeight, window.innerWidth / 2);
// keep table dimensions nice
const w = min(window.innerWidth, h * 2);
createCanvas(w, h);
visibleWallOffset = width/32;
engine = Engine.create();
world = engine.world;
// disable matter.js gravity (top-down game)
engine.world.gravity.y = 0;
//collision detection (pockets should make balls disappear)
Events.on(engine, 'collisionStart', collision);
addWalls();
addBumpers();
addPockets();
car = new Car(isPlayer = true)
// computer always accelerates (TODO: AI)
computerCar = new Car(isPlayer = false);
computerCar.accelerating(true);
replayButton = createButton("Play Again");
replayButton.addClass('replay-button');
replayButton.hide();
replayButton.mousePressed(resetGame);
const playButton = select('#play');
const startScreen = select('#start-screen');
playButton.mouseClicked(function() {
startScreen.hide();
setupRackOfBalls();
updateCountdownOverlay();
gameStarted = true;
});
}
function collision(event) {
const pairs = event.pairs;
for (let i = 0; i < pairs.length; i++) {
const labelA = pairs[i].bodyA.label;
const labelB = pairs[i].bodyB.label;
if (labelA === 'pocket' && labelB === 'ball') {
removeBall(pairs[i].bodyB);
} else if (labelA === 'ball' && labelB === 'pocket') {
removeBall(pairs[i].bodyA);
} else if (labelA === 'eightBall' && labelB === 'car') {
eightBallHit(pairs[i].bodyB);
} else if (labelA === 'car' && labelB === 'eightBall') {
eightBallHit(pairs[i].bodyA);
} else if ((labelA === 'ball' || labelA === 'eightBall') &&
labelB === 'wall') {
// matterjs BUG: ball has gone through bumper
resetBallPosition(pairs[i].bodyA);
} else if ((labelB === 'ball' || labelB === 'eightBall') &&
labelA === 'wall') {
// matterjs BUG: ball has gone through bumper
resetBallPosition(pairs[i].bodyB);
}
}
}
function resetBallPosition(ballBody) {
const ballId = ballBody.id;
const matches = balls.filter(b => b.body.id === ballId);
const ball = matches.length > 0 ? matches[0] : null;
if (ball) {
setBallBackInBounds(ball);
}
}
function setBallBackInBounds(ball) {
const visibleWallOffset = width/32;
const bumperThickness = width/108;
const edgeOffset = visibleWallOffset + bumperThickness;
const xPos = ball.body.position.x;
const yPos = ball.body.position.y;
let x = xPos, y = yPos;
if (xPos < edgeOffset) {
x = edgeOffset + 1;
}
if (xPos > width - edgeOffset) {
x = width - edgeOffset - 1
}
if (yPos < edgeOffset) {
y = edgeOffset + 1
}
if (yPos > height - edgeOffset) {
y = height - edgeOffset - 1
}
Body.setPosition(ball.body, { x, y });
}
function eightBallHit(carBody) {
const playerId = car.body.id;
const computerId = computerCar.body.id;
if (playerId === carBody.id) {
car.eightBallCollision();
} else if (computerId === carBody.id) {
computerCar.eightBallCollision();
}
eightBallEarthquake = true;
setTimeout(function() { eightBallEarthquake = false }, 1000);
}
function updateCountdownOverlay(msgIdx = 0) {
let msgs = ['3', '2', '1', 'GO!'];
countdownText = msgs[msgIdx];
countdownMode = true;
if (msgIdx === msgs.length) {
countdownMode = false;
} else {
setTimeout(function() {
updateCountdownOverlay(msgIdx + 1)
}, 1000);
}
}
function drawCountdownOverlay() {
push();
// draw grayed out background
fill(0, 105);
rect(0, 0, width, height);
// text settings
textAlign(CENTER, CENTER);
const size = width > 600 ? 256 : 128;
textSize(size);
fill(255);
noStroke();
// countdownText is a global variable (can be '3', '2', '1', or 'GO!')
text(countdownText, width/2, height/2);
pop();
}
function removeBall(body) {
const bodyId = body.id;
World.remove(world, body);
for (let i = balls.length - 1; i >= 0; i--) {
if (bodyId == balls[i].id) {
balls.splice(i, 1);
}
}
}
// helper method to shuffle ball order
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
function getRandomizedBallOrderArray() {
let solidBalls = [1, 2, 3, 4, 5, 6, 7];
let stripedBalls = [9, 10, 11, 12, 13, 14, 15];
// eight ball in the back middle. everything else is random.
let nonEightBalls = solidBalls.concat(stripedBalls);
shuffleArray(nonEightBalls);
let firstTwelve = nonEightBalls.slice(0, 12);
let firstThirteen = firstTwelve.concat(8);
let lastTwo = nonEightBalls.slice(12, 14);
const randomizedBallOrderArray = firstThirteen.concat(lastTwo);
return randomizedBallOrderArray;
}
function setupRackOfBalls() {
const centerX = 0.68 * width;
const centerY = 0.5 * height;
const ballDiameter = width/32;
const ballNums = getRandomizedBallOrderArray();
let row = 1;
let y = centerY;
let x = centerX + ballDiameter;
balls.push(new Ball(ballNums[0], x, y));
for (let i = 1; i <= 14; i++) {
if ([1, 3, 6, 10].indexOf(i) > -1) {
row++;
x += ballDiameter;
y = centerY - ((row - 1) * ballDiameter * 0.5)
} else {
y += ballDiameter;
}
balls.push(new Ball(ballNums[i], x, y));
}
}
function keyReleased() {
if (keyCode == UP_ARROW || keyCode == DOWN_ARROW) {
car.accelerating(false)
} else if (keyCode == RIGHT_ARROW || keyCode == LEFT_ARROW) {
car.rotate(0)
}
}
function keyPressed() {
if (keyCode === RIGHT_ARROW) {
car.rotate(PI/72)
} else if (keyCode === LEFT_ARROW) {
car.rotate(-PI/72)
} else if (keyCode === UP_ARROW) {
car.accelerationDirection = 'forwards';
car.accelerating(true)
} else if (keyCode === DOWN_ARROW) {
car.accelerationDirection = 'backwards';
car.isBoosting = false;
car.accelerating(true)
} else if (keyCode === 32) {
car.boost();
car.accelerationDirection = 'forwards';
car.accelerating(true);
} else if (keyCode === 80) {
gameStarted = !gameStarted;
}
}
function draw() {
if (eightBallEarthquake) {
translate(random(-10, 10), random(-10, 10))
}
if (gameStarted) {
drawGame();
if (countdownMode) { drawCountdownOverlay() };
let gameOver = isGameOver();
if (!gameOver) { updateGame() };
}
}
function updateGame() {
Engine.update(engine);
if (!countdownMode) {
car.update();
computerCar.update();
};
balls.forEach(b => b.update())
closestBall = findClosestBall(computersBallType);
if (closestBall) {
computerCar.pointTowardsBall(closestBall);
}
computerCar.checkIfNeedToGoInReverse();
}
function drawGame() {
drawPoolTable()
car.render()
balls.length > 0 ? balls.forEach(b => b.render()) : setupRackOfBalls();
computerCar.render();
}
function isGameOver() {
let gameOver = false;
const stripes = balls.filter(b => b.ballType() === 'stripe').length;
const solids = balls.filter(b => b.ballType() === 'solid').length;
if (stripes === 0) {
gameOver = true;
this.gameOver(winner = "stripe");
} else if (solids === 0) {
gameOver = true;
this.gameOver(winner = "solid");
}
return gameOver;
}
function gameOver(winner) {
replayButton.show();
const winningPlayer = computersBallType === winner ? 'Computer' : 'Player';
push();
fill(0, 105);
rect(0, 0, width, height);
textAlign(CENTER, CENTER);
const size = width > 600 ? 32 : 24;
textSize(size);
fill(255);
noStroke();
text(`All ${winner}s sunk! ${winningPlayer} wins!`, width/2, height/4);
pop();
}
function findClosestBall(ballType) {
let closestDistance = Number.MAX_SAFE_INTEGER;
let closestBall = null;
const firstBall = ballType === 'solid' ? 1 : 9;
const lastBall = ballType === 'solid' ? 7 : 15;
let currentBall; let currentDistance;
// array indexing so subtract one
for (let i = firstBall - 1; i <= lastBall - 1; i++) {
currentBallArray = balls.filter(b => b.number === i);
if (currentBallArray.length > 0) {
currentBall = currentBallArray[0];
currentDistance = distanceBetween(computerCar, currentBall);
if (currentDistance < closestDistance) {
closestDistance = currentDistance;
closestBall = currentBall;
}
}
}
return closestBall;
}
function distanceBetween(object1, object2) {
const a2 = Math.pow(object1.position.x - object2.position.x, 2);
const b2 = Math.pow(object1.position.y - object2.position.y, 2);
return a2 + b2;
}
function drawPoolTable() {
background(COLORS.blueGreen);
walls.forEach(w => w.render());
bumpers.forEach(b => b.render());
pockets.forEach(p => p.render());
}
function addWalls() {
const wallThickness = 500;
const wt2 = wallThickness/2;
bottomWall = new Wall(width/2, height + wt2 - visibleWallOffset, width, wallThickness, 0);
topWall = new Wall(width/2, -wt2 + visibleWallOffset, width, wallThickness, 0);
leftWall = new Wall(-wt2 + visibleWallOffset, height/2, height, wallThickness, PI/2);
rightWall = new Wall(width + wt2 - visibleWallOffset, height/2, height, wallThickness, PI/2);
walls.push(topWall); walls.push(bottomWall);
walls.push(leftWall); walls.push(rightWall);
}
function addBumpers() {
const bumperThickness = width/108;
const adjustedWidth = width - visibleWallOffset*2;
bottomLeftBumper = new Bumper(adjustedWidth/4 + visibleWallOffset, height - visibleWallOffset, adjustedWidth/2, bumperThickness, 0);
bottomRightBumper = new Bumper(3*adjustedWidth/4 + visibleWallOffset, height - visibleWallOffset, adjustedWidth/2, bumperThickness, 0);
topLeftBumper = new Bumper(adjustedWidth/4 + visibleWallOffset, visibleWallOffset, adjustedWidth/2, bumperThickness, -PI);
topRightBumper = new Bumper(3*adjustedWidth/4 + visibleWallOffset, visibleWallOffset, adjustedWidth/2, bumperThickness, -PI);
leftBumper = new Bumper(visibleWallOffset, height/2, height - visibleWallOffset*2, bumperThickness, PI/2);
rightBumper = new Bumper(width - visibleWallOffset, height/2, height - visibleWallOffset*2, bumperThickness, -PI/2);
bumpers.push(topLeftBumper); bumpers.push(topRightBumper);
bumpers.push(leftBumper); bumpers.push(rightBumper);
bumpers.push(bottomLeftBumper); bumpers.push(bottomRightBumper);
}
function addPockets() {
const radius = width/24,
topY = visibleWallOffset,
bottomY = height - visibleWallOffset,
leftX = visibleWallOffset,
middleX = width/2,
rightX = width - visibleWallOffset;
[leftX, middleX, rightX].forEach((x) => {
[topY, bottomY].forEach((y) => {
if (x === middleX) {
pockets.push(new Pocket(x, y, radius, isMiddle = true));
} else {
pockets.push(new Pocket(x, y, radius, isMiddle = false));
}
})
})
}