-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplatformLeft.js
68 lines (59 loc) · 1.55 KB
/
platformLeft.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
game.platformLeft = {
width: 40,
height: 164,
x: 0,
y: (game.height - 164) / 2,
velocity: 6,
vY: 0, // Y-axis movement velocity
ball: game.ball,
startBall() {
// if a ball on the platform
if (this.ball) {
// then activate start() function
this.ball.start()
// and there is no ball on the platform now
this.ball = null
}
},
start(direction) {
if (direction === KEYS.UP) {
this.vY = -this.velocity
} else if (direction === KEYS.DOWN) {
this.vY = this.velocity
}
},
stop() {
this.vY = 0
},
move() {
if (this.vY) {
this.y += this.vY
// When a ball is on the platform, then they moving together.
if (this.ball) {
this.ball.y += this.vY
}
}
},
// Offset on the y-axis to obtain the correct bounce angle of the ball
getTouchOffset(collideCoordinate) {
// Get the down half part from the place of a collide
let diff = (this.y + this.height) - collideCoordinate
// Get the top half part from the place of a collide
let offset = this.height - diff
let result = 2 * offset / this.height
return result - 1
},
collideCanvasBounds() {
// Change of coordinates on next render
const y = this.y + this.vY
// Platform sides
const platformTopSide = y
const platformBottomSide = platformTopSide + this.height
// Canvas sides
const canvasTopSide = 0
const canvasBottomSide = game.height
if (platformTopSide <= canvasTopSide || platformBottomSide >= canvasBottomSide) {
this.vY = 0
}
}
}