From 7355a70821c7ef1b8c51f781da394c913774cb24 Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Sun, 4 Dec 2022 19:05:21 +0100 Subject: [PATCH] minors GameComponents changes --- src/client/class/GameComponentsClient.ts | 54 +++++++++++------------- src/shared_js/class/GameComponents.ts | 4 +- src/shared_js/utils.ts | 1 + 3 files changed, 27 insertions(+), 32 deletions(-) diff --git a/src/client/class/GameComponentsClient.ts b/src/client/class/GameComponentsClient.ts index 00fce3c1..bf90f66f 100644 --- a/src/client/class/GameComponentsClient.ts +++ b/src/client/class/GameComponentsClient.ts @@ -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 = this.wallTop; + const baseWB = 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"); - (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"); - (this.wallBottom).dir.assign(dir.x, dir.y); + this.wallTop = new MovingRectangleClient(baseWT.pos, baseWT.width, baseWT.height, baseWT.baseSpeed, + ctx, "grey"); + (this.wallTop).dir.assign(baseWT.dir.x, baseWT.dir.y); + + this.wallBottom = new MovingRectangleClient(baseWB.pos, baseWB.width, baseWB.height, baseWB.baseSpeed, + ctx, "grey"); + (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"); } } } diff --git a/src/shared_js/class/GameComponents.ts b/src/shared_js/class/GameComponents.ts index 4994af89..10e60932 100644 --- a/src/shared_js/class/GameComponents.ts +++ b/src/shared_js/class/GameComponents.ts @@ -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; } diff --git a/src/shared_js/utils.ts b/src/shared_js/utils.ts index 35bb5af1..e8f7bca3 100644 --- a/src/shared_js/utils.ts +++ b/src/shared_js/utils.ts @@ -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;