-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplatformRight.js
40 lines (34 loc) · 999 Bytes
/
platformRight.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
game.platformRight = {
width: 40,
height: 164,
x: game.width - 40,
y: (game.height - 164) / 2,
velocity: 6,
vY: 0, // Y-axis movement velocity
ball: game.ball,
moveArtificialIntelligence() {
// this.vY = this.velocity
this.vY = Math.abs(this.ball.vY)
const diff = this.y - this.ball.y
if (diff < 0 && diff < -this.height * 0.5) { // move down
this.y = this.y + this.ball.vY
} else if (diff > 0 && diff > this.height * 0.5) { // move up
this.y = this.y - this.ball.vY
} else {
this.y = this.y // stay put
}
},
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
}
}
}