ball speed up on player collision
This commit is contained in:
47
src/pong.ts
47
src/pong.ts
@@ -33,7 +33,7 @@ function startGame()
|
||||
{
|
||||
pong = new gameArea();
|
||||
|
||||
// Const
|
||||
// Const
|
||||
const w = pong.canvas.width;
|
||||
const h = pong.canvas.height;
|
||||
const w_mid = Math.floor(w/2);
|
||||
@@ -56,20 +56,14 @@ function startGame()
|
||||
wall_bottom = new Rectangle(pos, "grey", w, wallSize);
|
||||
|
||||
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);
|
||||
player2 = new Player(pos, "white", pw, ph);
|
||||
player1.speed = playerSpeed;
|
||||
player2.speed = playerSpeed;
|
||||
player2 = new Player(pos, "white", pw, ph, playerSpeed);
|
||||
|
||||
pos.assign(w_mid-ballSize/2, h_mid-ballSize/2);
|
||||
ball = new Ball(pos, "white", ballSize);
|
||||
ball.speed = ballSpeed;
|
||||
ball.dir.x = -0.8;
|
||||
ball.dir.y = +0.2;
|
||||
ball = new Ball(pos, "white", ballSize, ballSpeed);
|
||||
ball.dir.assign(-0.8, +0.2);
|
||||
|
||||
// w = w/8
|
||||
// h = w/12
|
||||
pos.assign(w_mid-scoreSize*1.6, scoreSize*1.5);
|
||||
score1 = new TextNumericValue(pos, "white", scoreSize);
|
||||
pos.assign(w_mid+scoreSize*1.1, scoreSize*1.5);
|
||||
@@ -94,9 +88,9 @@ function startGame()
|
||||
pos.assign(w-w/4, 0);
|
||||
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 ?)
|
||||
drawInit();
|
||||
|
||||
pong.start();
|
||||
}
|
||||
|
||||
@@ -160,13 +154,13 @@ function gameLoop()
|
||||
ballInPlay = false;
|
||||
score1.clear();
|
||||
++score1.value;
|
||||
setTimeout(newRound, 1000);
|
||||
setTimeout(newRound, 1500);
|
||||
}
|
||||
else if (ball.pos.x < 0 - ball.width) {
|
||||
ballInPlay = false;
|
||||
score2.clear();
|
||||
++score2.value;
|
||||
setTimeout(newRound, 1000);
|
||||
setTimeout(newRound, 1500);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,9 +184,10 @@ function newRound()
|
||||
return;
|
||||
}
|
||||
}
|
||||
ballInPlay = true;
|
||||
ball.pos.x = pong.canvas.width/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) {
|
||||
@@ -390,13 +385,16 @@ interface Moving {
|
||||
|
||||
class MovingRectangle extends Rectangle implements Moving {
|
||||
dir: Vector = new Vector(0,0);
|
||||
speed: number = 1;
|
||||
constructor(pos: VectorInteger, color: string, width: number, height: number) {
|
||||
speed: number;
|
||||
readonly baseSpeed: number;
|
||||
constructor(pos: VectorInteger, color: string, width: number, height: number, baseSpeed: number) {
|
||||
super(pos, color, width, height);
|
||||
this.baseSpeed = baseSpeed;
|
||||
this.speed = baseSpeed;
|
||||
}
|
||||
move() {
|
||||
this.pos.x += this.dir.x * this.speed;
|
||||
this.pos.y += this.dir.y * this.speed;
|
||||
move() { // Math.floor WIP until VectorInteger debug
|
||||
this.pos.x += Math.floor(this.dir.x * this.speed);
|
||||
this.pos.y += Math.floor(this.dir.y * this.speed);
|
||||
}
|
||||
moveAndCollide(colliderArr: Rectangle[]) {
|
||||
let oldPos = Object.assign({}, this.pos);
|
||||
@@ -416,15 +414,15 @@ class MovingRectangle extends Rectangle implements Moving {
|
||||
|
||||
|
||||
class Player extends MovingRectangle {
|
||||
constructor(pos: VectorInteger, color: string, width: number, height: number) {
|
||||
super(pos, color, width, height);
|
||||
constructor(pos: VectorInteger, color: string, width: number, height: number, baseSpeed: number) {
|
||||
super(pos, color, width, height, baseSpeed);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class Ball extends MovingRectangle {
|
||||
constructor(pos: VectorInteger, color: string, size: number) {
|
||||
super(pos, color, size, size);
|
||||
constructor(pos: VectorInteger, color: string, size: number, baseSpeed: number) {
|
||||
super(pos, color, size, size, baseSpeed);
|
||||
}
|
||||
bounce(collider?: Rectangle) {
|
||||
/* Could be more generic, but testing only player is enough,
|
||||
@@ -453,6 +451,7 @@ class Ball extends MovingRectangle {
|
||||
}
|
||||
private _bouncePlayer(collider: Player) { // WIP
|
||||
// Bounce for Player need to be more complexe than this
|
||||
this.speed += this.baseSpeed/20;
|
||||
ball.dir.x = ball.dir.x * -1;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user