ball speed up on player collision

This commit is contained in:
LuckyLaszlo
2022-10-23 03:23:25 +02:00
parent d76d6def47
commit e56c8e0b67
2 changed files with 24 additions and 27 deletions

View File

@@ -1,6 +1,4 @@
- ball speed up
- on newRound()
- speed reset
- ball direction based on player hit location - ball direction based on player hit location
--- ---

View File

@@ -56,20 +56,14 @@ function startGame()
wall_bottom = new Rectangle(pos, "grey", w, wallSize); wall_bottom = new Rectangle(pos, "grey", w, wallSize);
pos.assign(0+pw, h_mid-ph/2); pos.assign(0+pw, h_mid-ph/2);
player1 = new Player(pos, "white", pw, ph); player1 = new Player(pos, "white", pw, ph, playerSpeed);
pos.assign(w-pw-pw, h_mid-ph/2); pos.assign(w-pw-pw, h_mid-ph/2);
player2 = new Player(pos, "white", pw, ph); player2 = new Player(pos, "white", pw, ph, playerSpeed);
player1.speed = playerSpeed;
player2.speed = playerSpeed;
pos.assign(w_mid-ballSize/2, h_mid-ballSize/2); pos.assign(w_mid-ballSize/2, h_mid-ballSize/2);
ball = new Ball(pos, "white", ballSize); ball = new Ball(pos, "white", ballSize, ballSpeed);
ball.speed = ballSpeed; ball.dir.assign(-0.8, +0.2);
ball.dir.x = -0.8;
ball.dir.y = +0.2;
// w = w/8
// h = w/12
pos.assign(w_mid-scoreSize*1.6, scoreSize*1.5); pos.assign(w_mid-scoreSize*1.6, scoreSize*1.5);
score1 = new TextNumericValue(pos, "white", scoreSize); score1 = new TextNumericValue(pos, "white", scoreSize);
pos.assign(w_mid+scoreSize*1.1, scoreSize*1.5); pos.assign(w_mid+scoreSize*1.1, scoreSize*1.5);
@@ -94,9 +88,9 @@ function startGame()
pos.assign(w-w/4, 0); pos.assign(w-w/4, 0);
h_grid_d1 = new Rectangle(pos, "darkgreen", gridSize, h); h_grid_d1 = new Rectangle(pos, "darkgreen", gridSize, h);
// Start
score1.update(); // first Text draw init the custom font (graphic leftover ortherwise) (a better solution ?) score1.update(); // first Text draw init the custom font (graphic leftover ortherwise) (a better solution ?)
drawInit(); drawInit();
pong.start(); pong.start();
} }
@@ -160,13 +154,13 @@ function gameLoop()
ballInPlay = false; ballInPlay = false;
score1.clear(); score1.clear();
++score1.value; ++score1.value;
setTimeout(newRound, 1000); setTimeout(newRound, 1500);
} }
else if (ball.pos.x < 0 - ball.width) { else if (ball.pos.x < 0 - ball.width) {
ballInPlay = false; ballInPlay = false;
score2.clear(); score2.clear();
++score2.value; ++score2.value;
setTimeout(newRound, 1000); setTimeout(newRound, 1500);
} }
} }
@@ -190,9 +184,10 @@ function newRound()
return; return;
} }
} }
ballInPlay = true;
ball.pos.x = pong.canvas.width/2; ball.pos.x = pong.canvas.width/2;
ball.pos.y = pong.canvas.height/4 + Math.floor(random() * pong.canvas.height/2); ball.pos.y = pong.canvas.height/4 + Math.floor(random() * pong.canvas.height/2);
ball.speed = ball.baseSpeed;
ballInPlay = true;
} }
function random(min: number = 0, max: number = 1) { function random(min: number = 0, max: number = 1) {
@@ -390,13 +385,16 @@ interface Moving {
class MovingRectangle extends Rectangle implements Moving { class MovingRectangle extends Rectangle implements Moving {
dir: Vector = new Vector(0,0); dir: Vector = new Vector(0,0);
speed: number = 1; speed: number;
constructor(pos: VectorInteger, color: string, width: number, height: number) { readonly baseSpeed: number;
constructor(pos: VectorInteger, color: string, width: number, height: number, baseSpeed: number) {
super(pos, color, width, height); super(pos, color, width, height);
this.baseSpeed = baseSpeed;
this.speed = baseSpeed;
} }
move() { move() { // Math.floor WIP until VectorInteger debug
this.pos.x += this.dir.x * this.speed; this.pos.x += Math.floor(this.dir.x * this.speed);
this.pos.y += this.dir.y * this.speed; this.pos.y += Math.floor(this.dir.y * this.speed);
} }
moveAndCollide(colliderArr: Rectangle[]) { moveAndCollide(colliderArr: Rectangle[]) {
let oldPos = Object.assign({}, this.pos); let oldPos = Object.assign({}, this.pos);
@@ -416,15 +414,15 @@ class MovingRectangle extends Rectangle implements Moving {
class Player extends MovingRectangle { class Player extends MovingRectangle {
constructor(pos: VectorInteger, color: string, width: number, height: number) { constructor(pos: VectorInteger, color: string, width: number, height: number, baseSpeed: number) {
super(pos, color, width, height); super(pos, color, width, height, baseSpeed);
} }
} }
class Ball extends MovingRectangle { class Ball extends MovingRectangle {
constructor(pos: VectorInteger, color: string, size: number) { constructor(pos: VectorInteger, color: string, size: number, baseSpeed: number) {
super(pos, color, size, size); super(pos, color, size, size, baseSpeed);
} }
bounce(collider?: Rectangle) { bounce(collider?: Rectangle) {
/* Could be more generic, but testing only player is enough, /* Could be more generic, but testing only player is enough,
@@ -453,6 +451,7 @@ class Ball extends MovingRectangle {
} }
private _bouncePlayer(collider: Player) { // WIP private _bouncePlayer(collider: Player) { // WIP
// Bounce for Player need to be more complexe than this // Bounce for Player need to be more complexe than this
this.speed += this.baseSpeed/20;
ball.dir.x = ball.dir.x * -1; ball.dir.x = ball.dir.x * -1;
} }
} }