From 924e7d57c208d9a70e83628ecebfdf8fb733ef3b Mon Sep 17 00:00:00 2001 From: sigmasternchen Date: Tue, 29 Oct 2024 22:34:33 +0100 Subject: [PATCH] add standby ball handling --- html/static/styles.css | 14 ++++++++------ src/game/Ball.ts | 31 +++++++++++++++++++++++++++++++ src/game/Game.ts | 10 +++++++++- src/game/Paddle.ts | 20 ++++++++++++++++++-- src/game/geometry.ts | 6 ++++++ 5 files changed, 72 insertions(+), 9 deletions(-) create mode 100644 src/game/Ball.ts diff --git a/html/static/styles.css b/html/static/styles.css index 5221209..3977dfb 100644 --- a/html/static/styles.css +++ b/html/static/styles.css @@ -1,9 +1,6 @@ - #game { - width: calc((88px + 1px) * 11 + 1px); - height: 500px; - background-color: grey; position: relative; + border: solid 1px black; } .banner { @@ -14,11 +11,16 @@ .paddle { position: absolute; - bottom: 20px; - height: 10px; transform: translate(-50%, 0); background-color: #333; border-top-left-radius: 50%; border-top-right-radius: 50%; pointer-events: none; +} + +.ball { + position: absolute; + transform: translate(-50%, -50%); + background-color: black; + border-radius: 50%; } \ No newline at end of file diff --git a/src/game/Ball.ts b/src/game/Ball.ts new file mode 100644 index 0000000..e42cc43 --- /dev/null +++ b/src/game/Ball.ts @@ -0,0 +1,31 @@ +import {Position} from "./Position"; +import {ballSize} from "./geometry"; + +export class Ball { + private _position: Position; + private readonly element: HTMLElement; + + constructor() { + this.position = new Position(0, 0); + + this.element = document.createElement("div"); + this.element.classList.add("ball"); + this.element.style.width = ballSize + "px"; + this.element.style.height = ballSize + "px"; + + this.redraw(); + } + + set position(position: Position) { + this._position = position; + } + + public redraw(): void { + this.element.style.left = this._position.x + "px"; + this.element.style.top = this._position.y + "px"; + } + + public setup(gameElement: HTMLElement): void { + gameElement.appendChild(this.element); + } +} \ No newline at end of file diff --git a/src/game/Game.ts b/src/game/Game.ts index d054f12..f58d654 100644 --- a/src/game/Game.ts +++ b/src/game/Game.ts @@ -1,16 +1,23 @@ import {Banner, makeBanners} from "./Banner"; import {Position} from "./Position"; import {Paddle} from "./Paddle"; +import {Ball} from "./Ball"; +import {fieldHeight, fieldWidth} from "./geometry"; export class Game { private readonly root: HTMLElement; private banners: Banner[]; private paddle: Paddle; + private balls: Ball[]; constructor(root: HTMLElement) { this.root = root; + this.root.style.height = fieldHeight + "px"; + this.root.style.width = fieldWidth + "px"; + this.banners = makeBanners(11 * 6, i => new Position((i % 11) * (88 + 1), (0 | (i / 11)) * (31 + 1))); - this.paddle = new Paddle(); + this.paddle = new Paddle(new Ball()); + this.balls = []; } public load(): Promise { @@ -20,5 +27,6 @@ export class Game { public setup(): void { this.banners.forEach(banner => banner.setup(this.root)); this.paddle.setup(this.root); + this.balls.forEach(ball => ball.setup(this.root)); } } \ No newline at end of file diff --git a/src/game/Paddle.ts b/src/game/Paddle.ts index 9e15695..6a47a38 100644 --- a/src/game/Paddle.ts +++ b/src/game/Paddle.ts @@ -1,16 +1,23 @@ -import {fieldWidth} from "./geometry"; +import {ballSize, fieldWidth, paddleHeight, paddleY} from "./geometry"; +import {Ball} from "./Ball"; +import {Position} from "./Position"; export class Paddle { private position: number; private size: number; private readonly element: HTMLElement; - constructor() { + private ball: Ball|null; + + constructor(ball: Ball|null) { this.position = fieldWidth / 2; this.size = 60; + this.ball = ball; this.element = document.createElement("div"); this.element.classList.add("paddle"); + this.element.style.top = paddleY + "px"; + this.element.style.height = paddleHeight + "px"; this.redraw(); } @@ -18,6 +25,11 @@ export class Paddle { private redraw() { this.element.style.left = this.position + "px"; this.element.style.width = this.size + "px"; + + if (this.ball) { + this.ball.position = new Position(this.position, paddleY - ballSize / 2); + this.ball.redraw(); + } } private mouseHandler(event: MouseEvent) { @@ -34,5 +46,9 @@ export class Paddle { gameElement.addEventListener("mousemove", this.mouseHandler.bind(this)); gameElement.addEventListener("mouseenter", this.mouseHandler.bind(this)); gameElement.addEventListener("mouseleave", this.mouseHandler.bind(this)); + + if (this.ball) { + this.ball.setup(gameElement); + } } } \ No newline at end of file diff --git a/src/game/geometry.ts b/src/game/geometry.ts index 9db270e..ff82fbe 100644 --- a/src/game/geometry.ts +++ b/src/game/geometry.ts @@ -1,2 +1,8 @@ export const fieldWidth = (88 + 1) * 11 + 1; +export const fieldHeight = 500; + +export const paddleY = fieldHeight - 20; +export const paddleHeight = 10; + +export const ballSize = 15; \ No newline at end of file