reafactoring

This commit is contained in:
LuckyLaszlo
2022-11-17 12:22:57 +01:00
parent 6fb1470dbb
commit 023b5ed485
5 changed files with 61 additions and 52 deletions

View File

@@ -155,3 +155,18 @@ class Line extends Rectangle {
}
export {Rectangle, MovingRectangle, Player, 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;
} */

View File

@@ -1,4 +1,15 @@
export const CanvasWidth = 1500;
export const CanvasWidth = 1500;
export const CanvasRatio = 1.66666;
/* ratio 5/3 (1.66) */
export const CanvasRatio = 1.66666;
export const w = CanvasWidth;
export const h = CanvasWidth / CanvasRatio;
export const w_mid = Math.floor(w/2);
export const h_mid = Math.floor(h/2);
export const pw = Math.floor(w/50);
export const ph = pw*5;
export const ballSize = pw;
export const wallSize = Math.floor(w/100);
export const playerSpeed = Math.floor(w/1.5); // pixel per second
export const ballSpeed = Math.floor(w/1.5); // pixel per second

View File

@@ -11,32 +11,20 @@ export let ball: Ball;
function initGame(ctx: CanvasRenderingContext2D)
{
// Const (could maybe be in constants.ts ?)
const w = c.CanvasWidth;
const h = c.CanvasWidth / c.CanvasRatio;
const w_mid = Math.floor(w/2);
const h_mid = Math.floor(h/2);
const pw = Math.floor(w/50);
const ph = pw*5;
const ballSize = pw;
const wallSize = Math.floor(w/100);
const playerSpeed = Math.floor(w/1.5); // pixel per second
const ballSpeed = Math.floor(w/1.5); // pixel per second
let pos = new VectorInteger;
// Component
pos.assign(0, 0);
wall_top = new Rectangle(ctx, pos, "grey", w, wallSize);
pos.assign(0, h-wallSize);
wall_bottom = new Rectangle(ctx, pos, "grey", w, wallSize);
wall_top = new Rectangle(ctx, pos, "grey", c.w, c.wallSize);
pos.assign(0, c.h-c.wallSize);
wall_bottom = new Rectangle(ctx, pos, "grey", c.w, c.wallSize);
pos.assign(0+pw, h_mid-ph/2);
player1 = new Player(ctx, pos, "white", pw, ph, playerSpeed);
pos.assign(w-pw-pw, h_mid-ph/2);
player2 = new Player(ctx, pos, "white", pw, ph, playerSpeed);
pos.assign(0+c.pw, c.h_mid-c.ph/2);
player1 = new Player(ctx, pos, "white", c.pw, c.ph, c.playerSpeed);
pos.assign(c.w-c.pw-c.pw, c.h_mid-c.ph/2);
player2 = new Player(ctx, pos, "white", c.pw, c.ph, c.playerSpeed);
pos.assign(w_mid-ballSize/2, h_mid-ballSize/2);
ball = new Ball(ctx, pos, "white", ballSize, ballSpeed);
pos.assign(c.w_mid-c.ballSize/2, c.h_mid-c.ballSize/2);
ball = new Ball(ctx, pos, "white", c.ballSize, c.ballSpeed);
ball.dir.assign(-0.8, +0.2);
}