-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaddle.ts
More file actions
39 lines (31 loc) · 792 Bytes
/
Paddle.ts
File metadata and controls
39 lines (31 loc) · 792 Bytes
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
import Constants from './Constants';
class Paddle {
x: number;
y: number;
prevX: number;
prevY: number;
speedX: number;
speedY: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
this.prevX = x;
this.prevY = y;
this.speedX = 0;
this.speedY = 0;
}
setSpeedX(speedX: number) {
this.speedX = speedX;
}
setSpeedY(speedY: number) {
this.speedY = speedY;
}
calculate() {
this.prevX = this.x;
this.prevY = this.y;
this.x = Math.max(Math.min(this.x + this.speedX, Constants.CANVAS_WIDTH - Constants.PLAYER_SIZE_X), 0);
this.y = Math.max(Math.min(this.y + this.speedY, Constants.CANVAS_HEIGHT - Constants.PLAYER_SIZE_Y - 2 * Constants.BALL_RADIUS), 2 * Constants.BALL_RADIUS);
return { x: this.x, y: this.y };
}
}
export default Paddle;