messages draw on canvas

+ bugfix: vector.assign() Uncaught TypeError
This commit is contained in:
LuckyLaszlo
2022-11-28 04:40:03 +01:00
parent 7f248b5449
commit 5325c8b9ee
7 changed files with 48 additions and 27 deletions

View File

@@ -27,6 +27,7 @@ class GameComponentsClient extends GameComponentsExtensionForClient {
midLine: Line; midLine: Line;
scoreLeft: TextNumericValue; scoreLeft: TextNumericValue;
scoreRight: TextNumericValue; scoreRight: TextNumericValue;
text1: TextElem;
w_grid_mid: RectangleClient; w_grid_mid: RectangleClient;
w_grid_u1: RectangleClient; w_grid_u1: RectangleClient;
@@ -46,6 +47,10 @@ class GameComponentsClient extends GameComponentsExtensionForClient {
this.scoreLeft.value = 0; this.scoreLeft.value = 0;
this.scoreRight.value = 0; this.scoreRight.value = 0;
// Text
pos.assign(0, c.h_mid);
this.text1 = new TextElem(pos, Math.floor(c.w/8), ctx, "white");
// Dotted Midline // Dotted Midline
pos.assign(c.w_mid-c.midLineSize/2, 0+c.wallSize); pos.assign(c.w_mid-c.midLineSize/2, 0+c.wallSize);
this.midLine = new Line(pos, c.midLineSize, c.h-c.wallSize*2, ctx, "white", 15); this.midLine = new Line(pos, c.midLineSize, c.h-c.wallSize*2, ctx, "white", 15);

View File

@@ -13,7 +13,8 @@ class TextElem implements Component {
constructor(pos: VectorInteger, size: number, constructor(pos: VectorInteger, size: number,
ctx: CanvasRenderingContext2D, color: string, font: string = "Bit5x3") ctx: CanvasRenderingContext2D, color: string, font: string = "Bit5x3")
{ {
this.pos = Object.assign({}, pos); // this.pos = Object.assign({}, pos); // create bug, Uncaught TypeError: X is not a function
this.pos = new VectorInteger(pos.x, pos.y);
this.size = size; this.size = size;
this.ctx = ctx; this.ctx = ctx;
this.color = color; this.color = color;

View File

@@ -12,6 +12,8 @@ function drawLoop()
drawStatic(); drawStatic();
gc.text1.update();
drawDynamic(); drawDynamic();
} }

View File

@@ -19,20 +19,34 @@ export const gc = new GameComponentsClient(pong.ctx);
function matchmaking() function matchmaking()
{ {
console.log("Searching an opponent..."); // PLACEHOLDER, todo draw console.log("Searching an opponent...");
gc.text1.clear();
gc.text1.pos.assign(c.w/5, c.h_mid);
gc.text1.text = "Searching...";
gc.text1.update();
} }
function matchmakingComplete() function matchmakingComplete()
{ {
console.log("Match Found !"); // PLACEHOLDER, TODO draw on canvas console.log("Match Found !");
gc.text1.clear();
gc.text1.pos.assign(c.w/8, c.h_mid);
gc.text1.text = "Match Found !";
gc.text1.update();
} }
function startGame() { function startGame() {
countdown(c.matchStartDelay/1000, resumeGame); gc.text1.pos.assign(c.w_mid, c.h_mid+c.h/4);
countdown(c.matchStartDelay/1000, (count: number) => {
gc.text1.clear();
gc.text1.text = `${count}`;
gc.text1.update();
}, resumeGame);
} }
function resumeGame() function resumeGame()
{ {
gc.text1.text = "";
window.addEventListener('keydown', function (e) { window.addEventListener('keydown', function (e) {
pong.addKey(e.key); pong.addKey(e.key);
}); });

View File

@@ -1,14 +1,17 @@
export * from "../shared_js/utils.js" export * from "../shared_js/utils.js"
function countdown(count: number, callback?: () => void) function countdown(count: number, callback?: (count: number) => void, endCallback?: () => void)
{ {
console.log("countdown ", count); // PLACEHOLDER, TODO draw on canvas console.log("countdown ", count);
if (count > 0) { if (count > 0) {
setTimeout(countdown, 1000, --count, callback); if (callback) {
callback(count);
} }
else if (callback) { setTimeout(countdown, 1000, --count, callback, endCallback);
callback(); }
else if (endCallback) {
endCallback();
} }
} }

View File

@@ -84,17 +84,11 @@ function gameUpdate(data: ev.EventGameUpdate)
gc.playerLeft.pos.y = data.playerLeft.y; gc.playerLeft.pos.y = data.playerLeft.y;
gc.playerRight.pos.y = data.playerRight.y; gc.playerRight.pos.y = data.playerRight.y;
gc.ball.pos.x = data.ball.x;
gc.ball.pos.y = data.ball.y; gc.ball.pos.assign(data.ball.x, data.ball.y);
gc.ball.dir.assign(data.ball.dirX, data.ball.dirY);
gc.ball.speed = data.ball.speed; gc.ball.speed = data.ball.speed;
gc.ball.dir.x = data.ball.dirX;
gc.ball.dir.y = data.ball.dirY;
// ERROR: Uncaught TypeError: gc.ball.pos.assign is not a function
// Why ?
// gc.ball.pos.assign( data.ball.x, data.ball.y );
repeatInput(data.lastInputId); // server reconciliation repeatInput(data.lastInputId); // server reconciliation
} }
@@ -113,11 +107,15 @@ function scoreUpdate(data: ev.EventScoreUpdate)
function matchEnd(data: ev.EventMatchEnd) function matchEnd(data: ev.EventMatchEnd)
{ {
console.log("matchEnd");
if (data.winner === clientInfo.side) { if (data.winner === clientInfo.side) {
alert("WIN"); // placeholder TODO draw gc.text1.pos.assign(c.w*0.415, c.h_mid);
gc.text1.text = "WIN";
} }
else { else {
alert("LOSE"); // placeholder TODO draw gc.text1.pos.assign(c.w*0.383, c.h_mid);
gc.text1.text = "LOSE";
} }
// matchEnded = true;
} }
// export let matchEnded = false;

View File

@@ -7,7 +7,7 @@ class Rectangle implements Component {
width: number; width: number;
height: number; height: number;
constructor(pos: VectorInteger, width: number, height: number) { constructor(pos: VectorInteger, width: number, height: number) {
this.pos = Object.assign({}, pos); this.pos = new VectorInteger(pos.x, pos.y);
this.width = width; this.width = width;
this.height = height; this.height = height;
} }
@@ -49,12 +49,10 @@ class MovingRectangle extends Rectangle implements Moving {
this.pos.y += Math.floor(this.dir.y * this.speed * delta); this.pos.y += Math.floor(this.dir.y * this.speed * delta);
} }
moveAndCollide(delta: number, colliderArr: Rectangle[]) { moveAndCollide(delta: number, colliderArr: Rectangle[]) {
let oldPos = Object.assign({}, this.pos); let oldPos = new VectorInteger(this.pos.x, this.pos.y);
this.move(delta); this.move(delta);
if (colliderArr.some(this.collision, this)) if (colliderArr.some(this.collision, this)) {
{ this.pos = oldPos;
this.pos.x = oldPos.x;
this.pos.y = oldPos.y;
} }
} }
} }