find a way to sharing init

+ vector.normalized()
+ misc
This commit is contained in:
LuckyLaszlo
2022-11-28 04:33:27 +01:00
parent 4aafbac1a5
commit 7f248b5449
10 changed files with 45 additions and 65 deletions

View File

@@ -66,8 +66,10 @@ class Racket extends MovingRectangle {
}
class Ball extends MovingRectangle {
constructor(pos: VectorInteger, size: number, baseSpeed: number) {
readonly speedIncrease: number;
constructor(pos: VectorInteger, size: number, baseSpeed: number, speedIncrease: number) {
super(pos, size, size, baseSpeed);
this.speedIncrease = speedIncrease;
}
bounce(collider?: Rectangle) {
this._bounceAlgo(collider);
@@ -98,7 +100,7 @@ class Ball extends MovingRectangle {
this._bounceRacketAlgo(racket);
}
protected _bounceRacketAlgo(racket: Racket) {
this.speed += this.baseSpeed/20;
this.speed += this.speedIncrease;
let x = this.dir.x * -1;
@@ -109,7 +111,7 @@ class Ball extends MovingRectangle {
const racketMid = racket.pos.y + racketHalf;
let impact = ballMid - racketMid;
const horizontalMargin = racketHalf * 0.10;
const horizontalMargin = racketHalf * 0.15;
if (impact < horizontalMargin && impact > -horizontalMargin) {
impact = 0;
}
@@ -122,12 +124,10 @@ class Ball extends MovingRectangle {
let y = impact / (racketHalf - horizontalMargin) * angleFactor;
this.dir.assign(x, y);
/*
// Normalize Vector (for consistency in speed independent of direction)
const normalizationFactor = Math.abs(y) + Math.abs(x);
this.dir.assign(x/normalizationFactor, y/normalizationFactor);
*/
this.dir.assign(x, y);
this.dir = this.dir.normalized();
// console.log(`x: ${this.dir.x}, y: ${this.dir.y}`);
}
}