minors GameComponents changes
This commit is contained in:
@@ -5,7 +5,7 @@ import { Vector, VectorInteger } from "../../shared_js/class/Vector.js";
|
|||||||
import { TextElem, TextNumericValue } from "./Text.js";
|
import { TextElem, TextNumericValue } from "./Text.js";
|
||||||
import { RectangleClient, MovingRectangleClient, RacketClient, BallClient, Line } from "./RectangleClient.js";
|
import { RectangleClient, MovingRectangleClient, RacketClient, BallClient, Line } from "./RectangleClient.js";
|
||||||
import { GameComponents } from "../../shared_js/class/GameComponents.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 {
|
class GameComponentsExtensionForClient extends GameComponents {
|
||||||
wallTop: RectangleClient | MovingRectangleClient;
|
wallTop: RectangleClient | MovingRectangleClient;
|
||||||
@@ -18,19 +18,20 @@ class GameComponentsExtensionForClient extends GameComponents {
|
|||||||
super(options);
|
super(options);
|
||||||
|
|
||||||
// Rackets
|
// Rackets
|
||||||
|
const basePL = this.playerLeft;
|
||||||
|
const basePR = this.playerRight;
|
||||||
this.playerLeft = new RacketClient(
|
this.playerLeft = new RacketClient(
|
||||||
this.playerLeft.pos, this.playerLeft.width, this.playerLeft.height, this.playerLeft.baseSpeed,
|
basePL.pos, basePL.width, basePL.height, basePL.baseSpeed,
|
||||||
ctx, "white");
|
ctx, "white");
|
||||||
this.playerRight = new RacketClient(
|
this.playerRight = new RacketClient(
|
||||||
this.playerRight.pos, this.playerRight.width, this.playerRight.height, this.playerRight.baseSpeed,
|
basePR.pos, basePR.width, basePR.height, basePR.baseSpeed,
|
||||||
ctx, "white");
|
ctx, "white");
|
||||||
|
|
||||||
// Balls
|
// Balls
|
||||||
const newBallsArr: BallClient[] = [];
|
const newBallsArr: BallClient[] = [];
|
||||||
this.ballsArr.forEach((ball) => {
|
this.ballsArr.forEach((ball) => {
|
||||||
newBallsArr.push(new BallClient(
|
newBallsArr.push(new BallClient(ball.pos, ball.width, ball.baseSpeed, ball.speedIncrease,
|
||||||
ball.pos, ball.width, ball.baseSpeed, ball.speedIncrease,
|
ctx, "white")
|
||||||
ctx, "white")
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
this.ballsArr = newBallsArr;
|
this.ballsArr = newBallsArr;
|
||||||
@@ -38,32 +39,25 @@ class GameComponentsExtensionForClient extends GameComponents {
|
|||||||
// Walls
|
// Walls
|
||||||
if (options & en.MatchOptions.movingWalls)
|
if (options & en.MatchOptions.movingWalls)
|
||||||
{
|
{
|
||||||
const dir = new Vector;
|
const baseWT = <MovingRectangle>this.wallTop;
|
||||||
|
const baseWB = <MovingRectangle>this.wallBottom;
|
||||||
|
|
||||||
assertMovingRectangle(this.wallTop);
|
this.wallTop = new MovingRectangleClient(baseWT.pos, baseWT.width, baseWT.height, baseWT.baseSpeed,
|
||||||
dir.assign(this.wallTop.dir.x, this.wallTop.dir.y);
|
ctx, "grey");
|
||||||
this.wallTop = new MovingRectangleClient(
|
(<MovingRectangleClient>this.wallTop).dir.assign(baseWT.dir.x, baseWT.dir.y);
|
||||||
this.wallTop.pos, this.wallTop.width, this.wallTop.height,
|
|
||||||
this.wallTop.baseSpeed,
|
this.wallBottom = new MovingRectangleClient(baseWB.pos, baseWB.width, baseWB.height, baseWB.baseSpeed,
|
||||||
ctx, "grey");
|
ctx, "grey");
|
||||||
(<MovingRectangleClient>this.wallTop).dir.assign(dir.x, dir.y);
|
(<MovingRectangleClient>this.wallBottom).dir.assign(baseWB.dir.x, baseWB.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);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this.wallTop = new RectangleClient(
|
const baseWT = this.wallTop;
|
||||||
this.wallTop.pos, this.wallTop.width, this.wallTop.height,
|
const baseWB = this.wallBottom;
|
||||||
ctx, "grey");
|
this.wallTop = new RectangleClient(baseWT.pos, baseWT.width, baseWT.height,
|
||||||
this.wallBottom = new RectangleClient(
|
ctx, "grey");
|
||||||
this.wallBottom.pos, this.wallBottom.width, this.wallBottom.height,
|
this.wallBottom = new RectangleClient(baseWB.pos, baseWB.width, baseWB.height,
|
||||||
ctx, "grey");
|
ctx, "grey");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import * as c from "../constants.js"
|
|||||||
import * as en from "../../shared_js/enums.js"
|
import * as en from "../../shared_js/enums.js"
|
||||||
import { VectorInteger } from "./Vector.js";
|
import { VectorInteger } from "./Vector.js";
|
||||||
import { Rectangle, MovingRectangle, Racket, Ball } from "./Rectangle.js";
|
import { Rectangle, MovingRectangle, Racket, Ball } from "./Rectangle.js";
|
||||||
import { clamp, random } from "../utils.js";
|
import { random } from "../utils.js";
|
||||||
|
|
||||||
class GameComponents {
|
class GameComponents {
|
||||||
wallTop: Rectangle | MovingRectangle;
|
wallTop: Rectangle | MovingRectangle;
|
||||||
@@ -36,7 +36,7 @@ class GameComponents {
|
|||||||
ball.dir.x *= -1;
|
ball.dir.x *= -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ball.dir.y = clamp(random(), 0, 0.2);
|
ball.dir.y = random(0, 0.2);
|
||||||
if (random() > 0.5) {
|
if (random() > 0.5) {
|
||||||
ball.dir.y *= -1;
|
ball.dir.y *= -1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ function clamp(n: number, min: number, max: number) : number
|
|||||||
return (n);
|
return (n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Typescript hack, unused
|
||||||
function assertMovingRectangle(value: unknown): asserts value is MovingRectangle {
|
function assertMovingRectangle(value: unknown): asserts value is MovingRectangle {
|
||||||
// if (value !== MovingRectangle) throw new Error("Not a MovingRectangle");
|
// if (value !== MovingRectangle) throw new Error("Not a MovingRectangle");
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user