minors GameComponents changes

This commit is contained in:
LuckyLaszlo
2022-12-04 19:05:21 +01:00
parent 59c7ae2a16
commit 7355a70821
3 changed files with 27 additions and 32 deletions

View File

@@ -5,7 +5,7 @@ import { Vector, VectorInteger } from "../../shared_js/class/Vector.js";
import { TextElem, TextNumericValue } from "./Text.js";
import { RectangleClient, MovingRectangleClient, RacketClient, BallClient, Line } from "./RectangleClient.js";
import { GameComponents } from "../../shared_js/class/GameComponents.js";
import { assertMovingRectangle } from "../utils.js";
import { MovingRectangle } from "../../shared_js/class/Rectangle.js";
class GameComponentsExtensionForClient extends GameComponents {
wallTop: RectangleClient | MovingRectangleClient;
@@ -18,19 +18,20 @@ class GameComponentsExtensionForClient extends GameComponents {
super(options);
// Rackets
const basePL = this.playerLeft;
const basePR = this.playerRight;
this.playerLeft = new RacketClient(
this.playerLeft.pos, this.playerLeft.width, this.playerLeft.height, this.playerLeft.baseSpeed,
ctx, "white");
basePL.pos, basePL.width, basePL.height, basePL.baseSpeed,
ctx, "white");
this.playerRight = new RacketClient(
this.playerRight.pos, this.playerRight.width, this.playerRight.height, this.playerRight.baseSpeed,
ctx, "white");
basePR.pos, basePR.width, basePR.height, basePR.baseSpeed,
ctx, "white");
// Balls
const newBallsArr: BallClient[] = [];
this.ballsArr.forEach((ball) => {
newBallsArr.push(new BallClient(
ball.pos, ball.width, ball.baseSpeed, ball.speedIncrease,
ctx, "white")
newBallsArr.push(new BallClient(ball.pos, ball.width, ball.baseSpeed, ball.speedIncrease,
ctx, "white")
);
});
this.ballsArr = newBallsArr;
@@ -38,32 +39,25 @@ class GameComponentsExtensionForClient extends GameComponents {
// Walls
if (options & en.MatchOptions.movingWalls)
{
const dir = new Vector;
const baseWT = <MovingRectangle>this.wallTop;
const baseWB = <MovingRectangle>this.wallBottom;
assertMovingRectangle(this.wallTop);
dir.assign(this.wallTop.dir.x, this.wallTop.dir.y);
this.wallTop = new MovingRectangleClient(
this.wallTop.pos, this.wallTop.width, this.wallTop.height,
this.wallTop.baseSpeed,
ctx, "grey");
(<MovingRectangleClient>this.wallTop).dir.assign(dir.x, dir.y);
assertMovingRectangle(this.wallBottom);
dir.assign(this.wallBottom.dir.x, this.wallBottom.dir.y);
this.wallBottom = new MovingRectangleClient(
this.wallBottom.pos, this.wallBottom.width, this.wallBottom.height,
this.wallBottom.baseSpeed,
ctx, "grey");
(<MovingRectangleClient>this.wallBottom).dir.assign(dir.x, dir.y);
this.wallTop = new MovingRectangleClient(baseWT.pos, baseWT.width, baseWT.height, baseWT.baseSpeed,
ctx, "grey");
(<MovingRectangleClient>this.wallTop).dir.assign(baseWT.dir.x, baseWT.dir.y);
this.wallBottom = new MovingRectangleClient(baseWB.pos, baseWB.width, baseWB.height, baseWB.baseSpeed,
ctx, "grey");
(<MovingRectangleClient>this.wallBottom).dir.assign(baseWB.dir.x, baseWB.dir.y);
}
else
{
this.wallTop = new RectangleClient(
this.wallTop.pos, this.wallTop.width, this.wallTop.height,
ctx, "grey");
this.wallBottom = new RectangleClient(
this.wallBottom.pos, this.wallBottom.width, this.wallBottom.height,
ctx, "grey");
const baseWT = this.wallTop;
const baseWB = this.wallBottom;
this.wallTop = new RectangleClient(baseWT.pos, baseWT.width, baseWT.height,
ctx, "grey");
this.wallBottom = new RectangleClient(baseWB.pos, baseWB.width, baseWB.height,
ctx, "grey");
}
}
}

View File

@@ -3,7 +3,7 @@ import * as c from "../constants.js"
import * as en from "../../shared_js/enums.js"
import { VectorInteger } from "./Vector.js";
import { Rectangle, MovingRectangle, Racket, Ball } from "./Rectangle.js";
import { clamp, random } from "../utils.js";
import { random } from "../utils.js";
class GameComponents {
wallTop: Rectangle | MovingRectangle;
@@ -36,7 +36,7 @@ class GameComponents {
ball.dir.x *= -1;
}
ball.dir.y = clamp(random(), 0, 0.2);
ball.dir.y = random(0, 0.2);
if (random() > 0.5) {
ball.dir.y *= -1;
}

View File

@@ -18,6 +18,7 @@ function clamp(n: number, min: number, max: number) : number
return (n);
}
// Typescript hack, unused
function assertMovingRectangle(value: unknown): asserts value is MovingRectangle {
// if (value !== MovingRectangle) throw new Error("Not a MovingRectangle");
return;