-
Notifications
You must be signed in to change notification settings - Fork 2
/
PINBALL.html
586 lines (514 loc) · 14.9 KB
/
PINBALL.html
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>Pinball </title>
<style type="text/css">
@import url('https://fonts.googleapis.com/css?family=Hind');
*, *::before, *::after {
box-sizing: border-box;
}
html, body {
height: 100%;
overflow: hidden;
}
body {
display: flex;
justify-content: center;
align-items: center;
margin: 0;
color: #dee2e6;
background-color: #212529;
font-family: 'Hind', sans-serif;
text-transform: uppercase;
}
.container {
position: relative;
line-height: 0;
}
.score {
position: absolute;
top: 10px;
line-height: 1;
}
.score span {
font-size: 3.25rem;
}
.current-score {
left: 10px;
}
.high-score {
text-align: right;
right: 10px;
}
.trigger {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
position: absolute;
width: 80px;
height: 80px;
bottom: 10px;
border: 0;
border-radius: 50%;
color: inherit;
background-color: #e64980;
text-align: center;
line-height: 80px;
font-size: 1.25rem;
text-transform: inherit;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
outline: none;
}
.left-trigger {
left: 10px;
}
.right-trigger {
right: 60px;
}
canvas {
overflow: hidden;
border-radius: 5px;
box-shadow: 0 5px 25px rgba(0, 0, 0, 0.75);
}
/* responsive scaling, because pinball tables are tall */
@media (min-height: 0px) {
.container { transform: scale(0.25); }
}
@media (min-height: 400px) {
.container { transform: scale(0.5); }
}
@media (min-height: 600px) {
.container { transform: scale(0.75); }
}
@media (min-height: 800px) {
.container { transform: scale(1); }
}
</style>
</head>
<body>
<!-- partial:index.partial.html -->
<div class="container">
<div class="score current-score">
score<br><span></span>
</div>
<div class="score high-score">
high score<br><span></span>
</div>
<button class="trigger left-trigger">tap!</button>
<button class="trigger right-trigger">tap!</button>
</div>
<!-- partial -->
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>
<script src='https://cdn.rawgit.com/schteppe/poly-decomp.js/1ef946f1/build/decomp.min.js'></script>
<script src='https://cdn.rawgit.com/liabru/matter-js/0895d81f/build/matter.min.js'></script>
<script src='https://cdn.rawgit.com/liabru/matter-attractors/c470ed42/build/matter-attractors.min.js'></script><script src="./script.js"></script>
<script>
(() => {
// plugins
Matter.use(MatterAttractors);
// constants
const PATHS = {
DOME: '0 0 0 250 19 250 20 231.9 25.7 196.1 36.9 161.7 53.3 129.5 74.6 100.2 100.2 74.6 129.5 53.3 161.7 36.9 196.1 25.7 231.9 20 268.1 20 303.9 25.7 338.3 36.9 370.5 53.3 399.8 74.6 425.4 100.2 446.7 129.5 463.1 161.7 474.3 196.1 480 231.9 480 250 500 250 500 0 0 0',
DROP_LEFT: '0 0 20 0 70 100 20 150 0 150 0 0',
DROP_RIGHT: '50 0 68 0 68 150 50 150 0 100 50 0',
APRON_LEFT: '0 0 180 120 0 120 0 0',
APRON_RIGHT: '180 0 180 120 0 120 180 0'
};
const COLOR = {
BACKGROUND: '#212529',
OUTER: '#495057',
INNER: '#15aabf',
BUMPER: '#fab005',
BUMPER_LIT: '#fff3bf',
PADDLE: '#e64980',
PINBALL: '#dee2e6'
};
const GRAVITY = 0.75;
const WIREFRAMES = false;
const BUMPER_BOUNCE = 1.5;
const PADDLE_PULL = 0.002;
const MAX_VELOCITY = 50;
// score elements
let $currentScore = $('.current-score span');
let $highScore = $('.high-score span');
// shared variables
let currentScore, highScore;
let engine, world, render, pinball, stopperGroup;
let leftPaddle, leftUpStopper, leftDownStopper, isLeftPaddleUp;
let rightPaddle, rightUpStopper, rightDownStopper, isRightPaddleUp;
function load() {
init();
createStaticBodies();
createPaddles();
createPinball();
createEvents();
}
function init() {
// engine (shared)
engine = Matter.Engine.create();
// world (shared)
world = engine.world;
world.bounds = {
min: { x: 0, y: 0},
max: { x: 500, y: 800 }
};
world.gravity.y = GRAVITY; // simulate rolling on a slanted table
// render (shared)
render = Matter.Render.create({
element: $('.container')[0],
engine: engine,
options: {
width: world.bounds.max.x,
height: world.bounds.max.y,
wireframes: WIREFRAMES,
background: COLOR.BACKGROUND
}
});
Matter.Render.run(render);
// runner
let runner = Matter.Runner.create();
Matter.Runner.run(runner, engine);
// used for collision filtering on various bodies
stopperGroup = Matter.Body.nextGroup(true);
// starting values
currentScore = 0;
highScore = 0;
isLeftPaddleUp = false;
isRightPaddleUp = false;
}
function createStaticBodies() {
Matter.World.add(world, [
// table boundaries (top, bottom, left, right)
boundary(250, -30, 500, 100),
boundary(250, 830, 500, 100),
boundary(-30, 400, 100, 800),
boundary(530, 400, 100, 800),
// dome
path(239, 86, PATHS.DOME),
// pegs (left, mid, right)
wall(140, 140, 20, 40, COLOR.INNER),
wall(225, 140, 20, 40, COLOR.INNER),
wall(310, 140, 20, 40, COLOR.INNER),
// top bumpers (left, mid, right)
bumper(105, 250),
bumper(225, 250),
bumper(345, 250),
// bottom bumpers (left, right)
bumper(165, 340),
bumper(285, 340),
// shooter lane wall
wall(440, 520, 20, 560, COLOR.OUTER),
// drops (left, right)
path(25, 360, PATHS.DROP_LEFT),
path(425, 360, PATHS.DROP_RIGHT),
// slingshots (left, right)
wall(120, 510, 20, 120, COLOR.INNER),
wall(330, 510, 20, 120, COLOR.INNER),
// out lane walls (left, right)
wall(60, 529, 20, 160, COLOR.INNER),
wall(390, 529, 20, 160, COLOR.INNER),
// flipper walls (left, right);
wall(93, 624, 20, 98, COLOR.INNER, -0.96),
wall(357, 624, 20, 98, COLOR.INNER, 0.96),
// aprons (left, right)
path(79, 740, PATHS.APRON_LEFT),
path(371, 740, PATHS.APRON_RIGHT),
// reset zones (center, right)
reset(225, 50),
reset(465, 30)
]);
}
function createPaddles() {
// these bodies keep paddle swings contained, but allow the ball to pass through
leftUpStopper = stopper(160, 591, 'left', 'up');
leftDownStopper = stopper(140, 743, 'left', 'down');
rightUpStopper = stopper(290, 591, 'right', 'up');
rightDownStopper = stopper(310, 743, 'right', 'down');
Matter.World.add(world, [leftUpStopper, leftDownStopper, rightUpStopper, rightDownStopper]);
// this group lets paddle pieces overlap each other
let paddleGroup = Matter.Body.nextGroup(true);
// Left paddle mechanism
let paddleLeft = {};
paddleLeft.paddle = Matter.Bodies.trapezoid(170, 660, 20, 80, 0.33, {
label: 'paddleLeft',
angle: 1.57,
chamfer: {},
render: {
fillStyle: COLOR.PADDLE
}
});
paddleLeft.brick = Matter.Bodies.rectangle(172, 672, 40, 80, {
angle: 1.62,
chamfer: {},
render: {
visible: false
}
});
paddleLeft.comp = Matter.Body.create({
label: 'paddleLeftComp',
parts: [paddleLeft.paddle, paddleLeft.brick]
});
paddleLeft.hinge = Matter.Bodies.circle(142, 660, 5, {
isStatic: true,
render: {
visible: false
}
});
Object.values(paddleLeft).forEach((piece) => {
piece.collisionFilter.group = paddleGroup
});
paddleLeft.con = Matter.Constraint.create({
bodyA: paddleLeft.comp,
pointA: { x: -29.5, y: -8.5 },
bodyB: paddleLeft.hinge,
length: 0,
stiffness: 0
});
Matter.World.add(world, [paddleLeft.comp, paddleLeft.hinge, paddleLeft.con]);
Matter.Body.rotate(paddleLeft.comp, 0.57, { x: 142, y: 660 });
// right paddle mechanism
let paddleRight = {};
paddleRight.paddle = Matter.Bodies.trapezoid(280, 660, 20, 80, 0.33, {
label: 'paddleRight',
angle: -1.57,
chamfer: {},
render: {
fillStyle: COLOR.PADDLE
}
});
paddleRight.brick = Matter.Bodies.rectangle(278, 672, 40, 80, {
angle: -1.62,
chamfer: {},
render: {
visible: false
}
});
paddleRight.comp = Matter.Body.create({
label: 'paddleRightComp',
parts: [paddleRight.paddle, paddleRight.brick]
});
paddleRight.hinge = Matter.Bodies.circle(308, 660, 5, {
isStatic: true,
render: {
visible: false
}
});
Object.values(paddleRight).forEach((piece) => {
piece.collisionFilter.group = paddleGroup
});
paddleRight.con = Matter.Constraint.create({
bodyA: paddleRight.comp,
pointA: { x: 29.5, y: -8.5 },
bodyB: paddleRight.hinge,
length: 0,
stiffness: 0
});
Matter.World.add(world, [paddleRight.comp, paddleRight.hinge, paddleRight.con]);
Matter.Body.rotate(paddleRight.comp, -0.57, { x: 308, y: 660 });
}
function createPinball() {
// x/y are set to when pinball is launched
pinball = Matter.Bodies.circle(0, 0, 14, {
label: 'pinball',
collisionFilter: {
group: stopperGroup
},
render: {
fillStyle: COLOR.PINBALL
}
});
Matter.World.add(world, pinball);
launchPinball();
}
function createEvents() {
// events for when the pinball hits stuff
Matter.Events.on(engine, 'collisionStart', function(event) {
let pairs = event.pairs;
pairs.forEach(function(pair) {
if (pair.bodyB.label === 'pinball') {
switch (pair.bodyA.label) {
case 'reset':
launchPinball();
break;
case 'bumper':
pingBumper(pair.bodyA);
break;
}
}
});
});
// regulate pinball
Matter.Events.on(engine, 'beforeUpdate', function(event) {
// bumpers can quickly multiply velocity, so keep that in check
Matter.Body.setVelocity(pinball, {
x: Math.max(Math.min(pinball.velocity.x, MAX_VELOCITY), -MAX_VELOCITY),
y: Math.max(Math.min(pinball.velocity.y, MAX_VELOCITY), -MAX_VELOCITY),
});
// cheap way to keep ball from going back down the shooter lane
if (pinball.position.x > 450 && pinball.velocity.y > 0) {
Matter.Body.setVelocity(pinball, { x: 0, y: -10 });
}
});
// mouse drag (god mode for grabbing pinball)
Matter.World.add(world, Matter.MouseConstraint.create(engine, {
mouse: Matter.Mouse.create(render.canvas),
constraint: {
stiffness: 0.2,
render: {
visible: false
}
}
}));
// keyboard paddle events
$('body').on('keydown', function(e) {
if (e.which === 37) { // left arrow key
isLeftPaddleUp = true;
} else if (e.which === 39) { // right arrow key
isRightPaddleUp = true;
}
});
$('body').on('keyup', function(e) {
if (e.which === 37) { // left arrow key
isLeftPaddleUp = false;
} else if (e.which === 39) { // right arrow key
isRightPaddleUp = false;
}
});
// click/tap paddle events
$('.left-trigger')
.on('mousedown touchstart', function(e) {
isLeftPaddleUp = true;
})
.on('mouseup touchend', function(e) {
isLeftPaddleUp = false;
});
$('.right-trigger')
.on('mousedown touchstart', function(e) {
isRightPaddleUp = true;
})
.on('mouseup touchend', function(e) {
isRightPaddleUp = false;
});
}
function launchPinball() {
updateScore(0);
Matter.Body.setPosition(pinball, { x: 465, y: 765 });
Matter.Body.setVelocity(pinball, { x: 0, y: -25 + rand(-2, 2) });
Matter.Body.setAngularVelocity(pinball, 0);
}
function pingBumper(bumper) {
updateScore(currentScore + 10);
// flash color
bumper.render.fillStyle = COLOR.BUMPER_LIT;
setTimeout(function() {
bumper.render.fillStyle = COLOR.BUMPER;
}, 100);
}
function updateScore(newCurrentScore) {
currentScore = newCurrentScore;
$currentScore.text(currentScore);
highScore = Math.max(currentScore, highScore);
$highScore.text(highScore);
}
// matter.js has a built in random range function, but it is deterministic
function rand(min, max) {
return Math.random() * (max - min) + min;
}
// outer edges of pinball table
function boundary(x, y, width, height) {
return Matter.Bodies.rectangle(x, y, width, height, {
isStatic: true,
render: {
fillStyle: COLOR.OUTER
}
});
}
// wall segments
function wall(x, y, width, height, color, angle = 0) {
return Matter.Bodies.rectangle(x, y, width, height, {
angle: angle,
isStatic: true,
chamfer: { radius: 10 },
render: {
fillStyle: color
}
});
}
// bodies created from SVG paths
function path(x, y, path) {
let vertices = Matter.Vertices.fromPath(path);
return Matter.Bodies.fromVertices(x, y, vertices, {
isStatic: true,
render: {
fillStyle: COLOR.OUTER,
// add stroke and line width to fill in slight gaps between fragments
strokeStyle: COLOR.OUTER,
lineWidth: 1
}
});
}
// round bodies that repel pinball
function bumper(x, y) {
let bumper = Matter.Bodies.circle(x, y, 25, {
label: 'bumper',
isStatic: true,
render: {
fillStyle: COLOR.BUMPER
}
});
// for some reason, restitution is reset unless it's set after body creation
bumper.restitution = BUMPER_BOUNCE;
return bumper;
}
// invisible bodies to constrict paddles
function stopper(x, y, side, position) {
// determine which paddle composite to interact with
let attracteeLabel = (side === 'left') ? 'paddleLeftComp' : 'paddleRightComp';
return Matter.Bodies.circle(x, y, 40, {
isStatic: true,
render: {
visible: false,
},
collisionFilter: {
group: stopperGroup
},
plugin: {
attractors: [
// stopper is always a, other body is b
function(a, b) {
if (b.label === attracteeLabel) {
let isPaddleUp = (side === 'left') ? isLeftPaddleUp : isRightPaddleUp;
let isPullingUp = (position === 'up' && isPaddleUp);
let isPullingDown = (position === 'down' && !isPaddleUp);
if (isPullingUp || isPullingDown) {
return {
x: (a.position.x - b.position.x) * PADDLE_PULL,
y: (a.position.y - b.position.y) * PADDLE_PULL,
};
}
}
}
]
}
});
}
// contact with these bodies causes pinball to be relaunched
function reset(x, width) {
return Matter.Bodies.rectangle(x, 781, width, 2, {
label: 'reset',
isStatic: true,
render: {
fillStyle: '#fff'
}
});
}
window.addEventListener('load', load, false);
})();
</script>
</body>
</html>