authoritative server OK

+ TODO actual matchmaking
This commit is contained in:
LuckyLaszlo
2022-11-21 19:46:25 +01:00
parent 48665cfe30
commit add08c216f
20 changed files with 485 additions and 240 deletions

View File

@@ -3,28 +3,14 @@ import {Vector, VectorInteger} from "./Vector.js";
import {Component, Moving} from "./interface.js";
class Rectangle implements Component {
ctx: CanvasRenderingContext2D;
pos: VectorInteger;
color: string;
width: number;
height: number;
constructor(ctx: CanvasRenderingContext2D, pos: VectorInteger, color: string, width: number, height: number) {
this.ctx = ctx;
constructor(pos: VectorInteger, width: number, height: number) {
this.pos = Object.assign({}, pos);
this.color = color;
this.width = width;
this.height = height;
}
update() {
this.ctx.fillStyle = this.color;
this.ctx.fillRect(this.pos.x, this.pos.y, this.width, this.height);
}
clear(pos?: VectorInteger) {
if (pos)
this.ctx.clearRect(pos.x, pos.y, this.width, this.height);
else
this.ctx.clearRect(this.pos.x, this.pos.y, this.width, this.height);
}
collision(collider: Rectangle): boolean { // Collision WIP. To redo
var myleft = this.pos.x;
var myright = this.pos.x + (this.width);
@@ -49,8 +35,8 @@ class MovingRectangle extends Rectangle implements Moving {
dir: Vector = new Vector(0,0);
speed: number;
readonly baseSpeed: number;
constructor(ctx: CanvasRenderingContext2D, pos: VectorInteger, color: string, width: number, height: number, baseSpeed: number) {
super(ctx, pos, color, width, height);
constructor(pos: VectorInteger, width: number, height: number, baseSpeed: number) {
super(pos, width, height);
this.baseSpeed = baseSpeed;
this.speed = baseSpeed;
}
@@ -69,23 +55,18 @@ class MovingRectangle extends Rectangle implements Moving {
this.pos.x = oldPos.x;
this.pos.y = oldPos.y;
}
else
{
this.clear(oldPos);
this.update();
}
}
}
class Racket extends MovingRectangle {
constructor(ctx: CanvasRenderingContext2D, pos: VectorInteger, color: string, width: number, height: number, baseSpeed: number) {
super(ctx, pos, color, width, height, baseSpeed);
constructor(pos: VectorInteger, width: number, height: number, baseSpeed: number) {
super(pos, width, height, baseSpeed);
}
}
class Ball extends MovingRectangle {
constructor(ctx: CanvasRenderingContext2D, pos: VectorInteger, color: string, size: number, baseSpeed: number) {
super(ctx, pos, color, size, size, baseSpeed);
constructor(pos: VectorInteger, size: number, baseSpeed: number) {
super(pos, size, size, baseSpeed);
}
bounce(collider?: Rectangle) {
/* Could be more generic, but testing only Racket is enough,
@@ -98,7 +79,6 @@ class Ball extends MovingRectangle {
}
}
moveAndBounce(delta: number, colliderArr: Rectangle[]) {
let oldPos = Object.assign({}, this.pos);
this.move(delta);
let i = colliderArr.findIndex(this.collision, this);
if (i != -1)
@@ -106,8 +86,6 @@ class Ball extends MovingRectangle {
this.bounce(colliderArr[i]);
this.move(delta);
}
this.clear(oldPos);
this.update();
}
private _bounceWall() { // Should be enough for Wall
this.dir.y = this.dir.y * -1;
@@ -119,54 +97,4 @@ class Ball extends MovingRectangle {
}
}
class Line extends Rectangle {
gapeCount: number = 0;
segmentCount: number;
segmentWidth: number;
segmentHeight: number;
constructor(ctx: CanvasRenderingContext2D, pos: VectorInteger, color: string, width: number, height: number, gapeCount?: number) {
super(ctx, pos, color, width, height);
if (gapeCount)
this.gapeCount = gapeCount;
this.segmentCount = this.gapeCount * 2 + 1;
this.segmentWidth = this.width;
this.segmentHeight = this.height / this.segmentCount;
// for Horizontal Line
// this.segmentWidth = this.width / this.segmentCount;
// this.segmentHeight = this.height;
}
update() {
this.ctx.fillStyle = this.color;
let pos: VectorInteger = new VectorInteger;
let i = 0;
while (i < this.segmentCount)
{
// for Horizontal Line
// pos.y = this.pos.y;
// pos.x = this.pos.x + this.segmentWidth * i;
pos.x = this.pos.x;
pos.y = this.pos.y + this.segmentHeight * i;
this.ctx.fillRect(pos.x, pos.y, this.segmentWidth, this.segmentHeight);
i += 2;
}
}
}
export {Rectangle, MovingRectangle, Racket, Ball, Line}
// How to handle const export in initGame ?
// example for class Rectangle
/* constructor(ctx?: CanvasRenderingContext2D, pos?: VectorInteger, color?: string, width?: number, height?: number) {
if (ctx && pos && color && width && height)
this.init(ctx, pos, color, width, height);
}
// constructor() {}
init(ctx: CanvasRenderingContext2D, pos: VectorInteger, color: string, width: number, height: number) {
this.ctx = ctx;
this.pos = Object.assign({}, pos);
this.color = color;
this.width = width;
this.height = height;
} */
export {Rectangle, MovingRectangle, Racket, Ball}