find a way to sharing init
+ vector.normalized() + misc
This commit is contained in:
@@ -1,7 +1,4 @@
|
||||
/*
|
||||
No more shared. Dont know how to implemente this.
|
||||
For the moment, this code is only used by the server.
|
||||
*/
|
||||
|
||||
import * as c from "../constants.js"
|
||||
import { VectorInteger } from "./Vector.js";
|
||||
import { Rectangle, Racket, Ball } from "./Rectangle.js";
|
||||
@@ -22,13 +19,12 @@ class GameComponents {
|
||||
this.wallBottom = new Rectangle(pos, c.w, c.wallSize);
|
||||
|
||||
pos.assign(0+c.pw, c.h_mid-c.ph/2);
|
||||
this.playerLeft = new Racket(pos, c.pw, c.ph, c.playerSpeed);
|
||||
this.playerLeft = new Racket(pos, c.pw, c.ph, c.racketSpeed);
|
||||
pos.assign(c.w-c.pw-c.pw, c.h_mid-c.ph/2);
|
||||
this.playerRight = new Racket(pos, c.pw, c.ph, c.playerSpeed);
|
||||
this.playerRight = new Racket(pos, c.pw, c.ph, c.racketSpeed);
|
||||
|
||||
// pos.assign(c.w_mid-c.ballSize/2, c.h_mid-c.ballSize/2); // center the ball
|
||||
pos.assign(-c.ballSize, -c.ballSize); // ball out =)
|
||||
this.ball = new Ball(pos, c.ballSize, c.ballSpeed);
|
||||
this.ball = new Ball(pos, c.ballSize, c.ballSpeed, c.ballSpeedIncrease);
|
||||
this.ball.dir.assign(-0.8, +0.2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,10 @@ class Vector {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
normalized() : Vector {
|
||||
const normalizationFactor = Math.abs(this.x) + Math.abs(this.y);
|
||||
return new Vector(this.x/normalizationFactor, this.y/normalizationFactor);
|
||||
}
|
||||
}
|
||||
|
||||
class VectorInteger extends Vector {
|
||||
|
||||
@@ -11,8 +11,9 @@ export const pw = Math.floor(w/60);
|
||||
export const ph = pw*6;
|
||||
export const ballSize = pw;
|
||||
export const wallSize = Math.floor(w/100);
|
||||
export const playerSpeed = Math.floor(w/1.5); // pixel per second
|
||||
export const racketSpeed = Math.floor(w/1.5); // pixel per second
|
||||
export const ballSpeed = Math.floor(w/2); // pixel per second
|
||||
export const ballSpeedIncrease = ballSpeed/20; // pixel per second
|
||||
|
||||
export const matchStartDelay = 3000; // millisecond
|
||||
export const newRoundDelay = 1500; // millisecond
|
||||
|
||||
Reference in New Issue
Block a user