-
Notifications
You must be signed in to change notification settings - Fork 0
/
paddle.js
88 lines (73 loc) · 2.22 KB
/
paddle.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
// Tutorial Videos
// https://www.youtube.com/watch?v=gNA6HKRWAI0
// https://www.youtube.com/watch?v=zlKXZiWE1XI
function lineBounce(line, ball) {
let point1 = line[0]
let point2 = line[1]
// point1.debugDraw(ctx)
// point2.debugDraw(ctx)
let lineVector = point2.minus(point1)
let point1ToBall = ball.position.minus(point1)
let projection = point1ToBall.clampedProj(lineVector)
let displacement = point1ToBall.minus(projection)
let distance = displacement.norm()
let overlap = ball.radius - distance
if (distance <= ball.radius) {
ball.velocity.reflect(displacement)
displacement.setNorm(overlap)
ball.position.add(displacement)
}
lineVector.debugDraw(ctx, point1)
point1ToBall.debugDraw(ctx, point1, 1, false, 'gold')
projection.debugDraw(ctx, point1, 1, false, 'aqua')
displacement.debugDraw(ctx, point1.plus(projection), 1, false, 'tomato')
}
function createPaddle({ x, y0, width, height, upKey, downKey, minY, maxY, hitSide }) {
let y = y0
let velocity = 0
window.addEventListener('keydown', function (e) {
let key = e.key
if (key === upKey) {
velocity = -0.5
} else if (key === downKey) {
velocity = 0.5
}
})
window.addEventListener('keyup', function (e) {
let key = e.key
if (key === upKey || key === downKey) {
velocity = 0
}
})
function move(dt) {
y += velocity*dt
y = Math.min(y, maxY)
y = Math.max(y, minY)
}
function draw(ctx) {
ctx.fillStyle = 'white'
ctx.fillRect(x - width/2, y - height/2, width, height)
}
function getHitLine() {
if (hitSide === 'left') {
return [
V(x - width/2, y - height/2),
V(x - width/2, y + height/2)
]
} else if (hitSide === 'right') {
return [
V(x + width/2, y - height/2),
V(x + width/2, y + height/2)
]
}
}
function checkHit(ball) {
let hitLine = getHitLine()
lineBounce(hitLine, ball)
}
return {
move,
draw,
checkHit
}
}