pong: make other bot idle when not busy
This commit is contained in:
parent
16242e95ca
commit
be4eb906c5
25
src/pong.ts
25
src/pong.ts
@ -122,8 +122,13 @@ class PongCore {
|
||||
|
||||
// Simulate game logic
|
||||
simulate() {
|
||||
this.botInput(this.left);
|
||||
this.botInput(this.right);
|
||||
if (this.ball.dx > 0) {
|
||||
this.botInput(this.left);
|
||||
this.botIdle(this.right);
|
||||
} else {
|
||||
this.botInput(this.right);
|
||||
this.botIdle(this.left);
|
||||
}
|
||||
|
||||
// move paddles by their velocity
|
||||
this.left.geometry.y += this.left.dy;
|
||||
@ -246,6 +251,22 @@ class PongCore {
|
||||
if (ballRegion > paddleRegion) player.dy = player.speed;
|
||||
if (ballRegion === paddleRegion) player.dy = 0;
|
||||
}
|
||||
|
||||
// Randomly move the bot paddle when ball not towards it
|
||||
botIdle(player: Paddle) {
|
||||
let direction: number = Math.floor(Math.random() * 3);
|
||||
switch (direction) {
|
||||
case 0:
|
||||
player.dy = 0;
|
||||
break;
|
||||
case 1:
|
||||
player.dy = -player.speed;
|
||||
break;
|
||||
case 2:
|
||||
player.dy = player.speed;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// listen to keyboard events to move the paddles
|
||||
|
Loading…
Reference in New Issue
Block a user