chore(eslint): fixed linter issues
This commit is contained in:
parent
0727067b27
commit
ce0b299d4d
1 changed files with 57 additions and 31 deletions
|
|
@ -98,7 +98,6 @@ function makeAngle(i: number): [number, number, number, number] {
|
||||||
export class Pong {
|
export class Pong {
|
||||||
public gameUpdate: NodeJS.Timeout | null = null;
|
public gameUpdate: NodeJS.Timeout | null = null;
|
||||||
|
|
||||||
|
|
||||||
public static readonly BALL_START_ANGLES: number[] = [
|
public static readonly BALL_START_ANGLES: number[] = [
|
||||||
...makeAngle(4),
|
...makeAngle(4),
|
||||||
...makeAngle(6),
|
...makeAngle(6),
|
||||||
|
|
@ -119,7 +118,11 @@ export class Pong {
|
||||||
Pong.GAME_WIDTH - Pong.PADDLE_OFFSET - Paddle.DEFAULT_WIDTH,
|
Pong.GAME_WIDTH - Pong.PADDLE_OFFSET - Paddle.DEFAULT_WIDTH,
|
||||||
(Pong.GAME_HEIGHT - Paddle.DEFAULT_HEIGHT) / 2,
|
(Pong.GAME_HEIGHT - Paddle.DEFAULT_HEIGHT) / 2,
|
||||||
);
|
);
|
||||||
public ball: Ball = new Ball(Pong.GAME_WIDTH / 2, Pong.GAME_HEIGHT / 2, Pong.BALL_START_ANGLES[this.ballAngleIdx++]);
|
public ball: Ball = new Ball(
|
||||||
|
Pong.GAME_WIDTH / 2,
|
||||||
|
Pong.GAME_HEIGHT / 2,
|
||||||
|
Pong.BALL_START_ANGLES[this.ballAngleIdx++],
|
||||||
|
);
|
||||||
|
|
||||||
public score: [number, number] = [0, 0];
|
public score: [number, number] = [0, 0];
|
||||||
public local: boolean = false;
|
public local: boolean = false;
|
||||||
|
|
@ -133,25 +136,33 @@ export class Pong {
|
||||||
constructor(
|
constructor(
|
||||||
public userLeft: UserId,
|
public userLeft: UserId,
|
||||||
public userRight: UserId,
|
public userRight: UserId,
|
||||||
) {
|
) { }
|
||||||
}
|
|
||||||
|
|
||||||
public tick() {
|
public tick() {
|
||||||
if (this.paddleCollision(this.leftPaddle, 'left')) {
|
if (this.paddleCollision(this.leftPaddle, 'left')) {
|
||||||
this.ball.collided('left', {
|
this.ball.collided(
|
||||||
|
'left',
|
||||||
|
{
|
||||||
left: this.leftPaddle.x + this.leftPaddle.width,
|
left: this.leftPaddle.x + this.leftPaddle.width,
|
||||||
right: 0,
|
right: 0,
|
||||||
top: 0,
|
top: 0,
|
||||||
bottom: 0}, false
|
bottom: 0,
|
||||||
|
},
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (this.paddleCollision(this.rightPaddle, 'right')) {
|
if (this.paddleCollision(this.rightPaddle, 'right')) {
|
||||||
this.ball.collided('right', {
|
this.ball.collided(
|
||||||
|
'right',
|
||||||
|
{
|
||||||
right: this.rightPaddle.x,
|
right: this.rightPaddle.x,
|
||||||
left: 0,
|
left: 0,
|
||||||
top: 0,
|
top: 0,
|
||||||
bottom: 0}, false);
|
bottom: 0,
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const wallCollision = this.boxCollision();
|
const wallCollision = this.boxCollision();
|
||||||
|
|
@ -186,25 +197,32 @@ export class Pong {
|
||||||
}
|
}
|
||||||
|
|
||||||
private paddleCollision(paddle: Paddle, side: 'left' | 'right'): boolean {
|
private paddleCollision(paddle: Paddle, side: 'left' | 'right'): boolean {
|
||||||
if (Math.abs(this.ball.angle) > Math.PI / 2 && side !== 'left' ||
|
if (
|
||||||
Math.abs(this.ball.angle) < Math.PI / 2 && side !== 'right')
|
(Math.abs(this.ball.angle) > Math.PI / 2 && side !== 'left') ||
|
||||||
return (false)
|
(Math.abs(this.ball.angle) < Math.PI / 2 && side !== 'right')
|
||||||
|
) {return false;}
|
||||||
|
|
||||||
// now we check only if the ball is near enought in the y axis to permform the collision
|
// now we check only if the ball is near enought in the y axis to permform the collision
|
||||||
if (!(
|
if (
|
||||||
|
!(
|
||||||
// check if ball is bellow the top of the paddle
|
// check if ball is bellow the top of the paddle
|
||||||
|
(
|
||||||
paddle.y - this.ball.size < this.ball.y &&
|
paddle.y - this.ball.size < this.ball.y &&
|
||||||
// check if ball is above the bottom of the paddle
|
// check if ball is above the bottom of the paddle
|
||||||
this.ball.y < paddle.y + paddle.height + this.ball.size)) return false;
|
this.ball.y < paddle.y + paddle.height + this.ball.size
|
||||||
|
)
|
||||||
|
)
|
||||||
|
) {return false;}
|
||||||
|
|
||||||
// so we know that the y is close enougth to be a bit, so we check the X. are we closer than the ball size ? if yes -> hit
|
// so we know that the y is close enougth to be a bit, so we check the X. are we closer than the ball size ? if yes -> hit
|
||||||
if (
|
if (
|
||||||
// check if the paddle.x is at most ball.size away from the center of the ball => we have a hit houston
|
// check if the paddle.x is at most ball.size away from the center of the ball => we have a hit houston
|
||||||
Math.abs(
|
Math.abs(
|
||||||
paddle.x + paddle.width * (side === 'left' ? 1 : 0)
|
paddle.x +
|
||||||
- this.ball.x)
|
paddle.width * (side === 'left' ? 1 : 0) -
|
||||||
< this.ball.size
|
this.ball.x,
|
||||||
) return true;
|
) < this.ball.size
|
||||||
|
) {return true;}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -217,11 +235,19 @@ export class Pong {
|
||||||
public movePaddle(user: UserId | ('left' | 'right'), dir: 'up' | 'down') {
|
public movePaddle(user: UserId | ('left' | 'right'), dir: 'up' | 'down') {
|
||||||
let paddle: Paddle | null = null;
|
let paddle: Paddle | null = null;
|
||||||
if (this.local) {
|
if (this.local) {
|
||||||
if (user === 'left') { paddle = this.leftPaddle; }
|
if (user === 'left') {
|
||||||
else if (user === 'right') { paddle = this.rightPaddle; }
|
paddle = this.leftPaddle;
|
||||||
|
}
|
||||||
|
else if (user === 'right') {
|
||||||
|
paddle = this.rightPaddle;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (user === this.userLeft) {
|
||||||
|
paddle = this.leftPaddle;
|
||||||
|
}
|
||||||
|
else if (user === this.userRight) {
|
||||||
|
paddle = this.rightPaddle;
|
||||||
}
|
}
|
||||||
else if (user === this.userLeft) { paddle = this.leftPaddle; }
|
|
||||||
else if (user === this.userRight) { paddle = this.rightPaddle; }
|
|
||||||
if (paddle === null) return;
|
if (paddle === null) return;
|
||||||
paddle.move(dir);
|
paddle.move(dir);
|
||||||
paddle.clamp(0, Pong.GAME_HEIGHT);
|
paddle.clamp(0, Pong.GAME_HEIGHT);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue