diff --git a/src/frontend/src/GameElements/Ball.ts b/src/frontend/src/GameElements/Ball.ts new file mode 100644 index 0000000..dbfb9e1 --- /dev/null +++ b/src/frontend/src/GameElements/Ball.ts @@ -0,0 +1,138 @@ + +import { Actor, ActorArgs, Circle, Color, Engine, Graphic, GraphicsGroup, Random, vec, Vector } from "excalibur"; +import { assertNotNull, assertNull } from "../utils/assertUtils"; +import { gravitationalConstant, groundHeight } from "../utils/physicsConstant"; + +export const defaultBallConfiguration: BallConfiguration = { + color: Color.Orange, + radius: 5, +} + +export interface BallConfiguration { + readonly color: Color; + readonly radius: number; +} + +const random = new Random(); + +export class Ball extends Actor { + private _graphic: Graphic = null!; + private _animation: BallAnimation = new BallAnimation(this); + + constructor(actorArgs: ActorArgs, public readonly configuration: BallConfiguration) { + super(actorArgs); + } + + + onInitialize(engine: Engine): void { + const { configuration } = this; + const { radius } = configuration; + + const ballGraphic = new Circle({ + radius: radius, + color: configuration.color, + }); + + const specularGraphic = new Circle({ + radius: radius / 5, + color: Color.White, + }); + + this._graphic = new GraphicsGroup({ + members: [ + { graphic: ballGraphic, offset: vec(0, 0)}, + { graphic:specularGraphic, offset: vec(radius / 2, radius / 2)} + ], + }) + + this.graphics.add(this._graphic); + + const start = vec(-150, -200); + this.pos = start; + + engine.input.pointers.primary.on("down", () => { + const randomVelocity = vec(random.integer(100, 400), random.integer(0, -400)); + this.startParabolaAnimation(new Parabola(start, randomVelocity)); + }) + } + + + public update(engine: Engine, elapsed: number): void { + super.update(engine, elapsed); + this._animation.update(elapsed); + } + + startParabolaAnimation(parabola: Parabola): void { + this._animation.start(parabola); + } +} + +class BallAnimation { + private _running: boolean = false; + private _parabola: Parabola | null = null; + private _elapsedTime: number | null = null; + + constructor(private _ball: Ball) { + + } + + get running(): boolean { + return this._running; + } + + start(parabola: Parabola): void { + this.reset(); + this._parabola = parabola; + this._running = true; + } + + reset(): void { + this._parabola = null; + this._running = false; + this._elapsedTime = null; + + // TODO: Reset ball position? + } + + update(elapsed: number): void { + if (!this.running) { + assertNull(this._parabola); + assertNull(this._elapsedTime); + return; + } + + assertNotNull(this._parabola); + + if (this._elapsedTime == null) { + this._elapsedTime = 0; + } + this._elapsedTime += elapsed; + + const newPosition = this._parabola.position(this._elapsedTime); + + // Collide with ground + const yMin = groundHeight - this._ball.configuration.radius; + newPosition.y = Math.min(yMin, newPosition.y); + + this._ball.pos = newPosition; + } + +} + +export class Parabola { + constructor( + readonly startPos: Vector, + readonly startVelocity: Vector + ) { } + + position(time: number): Vector { + const timeSeconds = time / 1000; + + const x = this.startPos.x + this.startVelocity.x * timeSeconds; + const y = this.startVelocity.y * timeSeconds + gravitationalConstant / 2 * timeSeconds ** 2 + this.startPos.y; + + return new Vector(x, y); + } + +} + diff --git a/src/frontend/src/GameElements/Cups.ts b/src/frontend/src/GameElements/Cups.ts index 423c5af..a730c6a 100644 --- a/src/frontend/src/GameElements/Cups.ts +++ b/src/frontend/src/GameElements/Cups.ts @@ -1,13 +1,14 @@ import { Actor, ActorArgs, Color, Engine, Graphic, GraphicsGroup, Line, Polygon,vec } from "excalibur"; +import { pixelsPerMeter } from "../utils/physicsConstant"; export const defaultCupConfiguration = { cupColor: Color.Red, rimColor: Color.White, - widthRim: 18, - widthBottom: 12, - height: 20, - rimThickness: 2, + widthRim: 0.18 * pixelsPerMeter, + widthBottom: 0.12 * pixelsPerMeter, + height: 0.2 * pixelsPerMeter, + rimThickness: 0.02 * pixelsPerMeter, } export interface CupConfiguration { diff --git a/src/frontend/src/GameElements/Ground.ts b/src/frontend/src/GameElements/Ground.ts index c805d67..e7f058e 100644 --- a/src/frontend/src/GameElements/Ground.ts +++ b/src/frontend/src/GameElements/Ground.ts @@ -1,4 +1,5 @@ import { Actor, Color, Engine, Graphic, Rectangle } from "excalibur"; +import { groundHeight } from "../utils/physicsConstant"; const groundColor = Color.Gray; @@ -15,7 +16,7 @@ export class Ground extends Actor { }) this.graphics.add(this._graphic); - this.pos.setTo(0, 0); + this.pos.setTo(0, groundHeight); this.anchor.setTo(0.5, 0); this._graphic.width = 10000; diff --git a/src/frontend/src/GameElements/Table.ts b/src/frontend/src/GameElements/Table.ts index 32273d1..f80404c 100644 --- a/src/frontend/src/GameElements/Table.ts +++ b/src/frontend/src/GameElements/Table.ts @@ -1,12 +1,13 @@ import { Actor, ActorArgs, Color, Engine, Graphic, Polygon,vec } from "excalibur"; +import { pixelsPerMeter } from "../utils/physicsConstant"; export const defaultTableConfiguration = { color: Color.Brown, - width: 300, - height: 60, - thickness: 10, - legOffset: 20, - legThickness: 10, + width: 2.44 * pixelsPerMeter, + height: 0.7 * pixelsPerMeter, + thickness: 0.15 * pixelsPerMeter, + legOffset: 0.15 * pixelsPerMeter, + legThickness: 0.15 * pixelsPerMeter, } export interface TableConfiguration { diff --git a/src/frontend/src/Scenes/GameScene.ts b/src/frontend/src/Scenes/GameScene.ts index 645409f..773c322 100644 --- a/src/frontend/src/Scenes/GameScene.ts +++ b/src/frontend/src/Scenes/GameScene.ts @@ -4,10 +4,13 @@ import { Ground } from "../GameElements/Ground"; import { defaultTableConfiguration, Table } from "../GameElements/Table"; import { Cup, defaultCupConfiguration } from "../GameElements/Cups"; import { Color, vec } from "excalibur"; +import { Ball, defaultBallConfiguration } from "../GameElements/Ball"; +import { groundHeight } from "../utils/physicsConstant"; export class GameScene extends GameStateScene { private _ground: Ground = null!; private _table: Table = null!; + private _ball: Ball = null!; private _cups: Cup[] = []; @@ -22,7 +25,7 @@ export class GameScene extends GameStateScene { this._ground = new Ground(); this.add(this._ground); - this._table = new Table({}, defaultTableConfiguration); + this._table = new Table({pos: vec(0, groundHeight)}, defaultTableConfiguration); this.add(this._table); const tableHalfWidth = this._table.configuration.width / 2; @@ -34,7 +37,7 @@ export class GameScene extends GameStateScene { ]; cupPositions.forEach(offsetX => { const cup = new Cup({ - pos: vec(offsetX, -tableHeight), + pos: vec(offsetX, groundHeight - tableHeight), color: Color.Red, }, defaultCupConfiguration) @@ -42,5 +45,7 @@ export class GameScene extends GameStateScene { this._cups.push(cup); }); + this._ball = new Ball({}, defaultBallConfiguration); + this.add(this._ball); } -} \ No newline at end of file +} diff --git a/src/frontend/src/utils/assertUtils.ts b/src/frontend/src/utils/assertUtils.ts new file mode 100644 index 0000000..1cbcbfd --- /dev/null +++ b/src/frontend/src/utils/assertUtils.ts @@ -0,0 +1,17 @@ +export function assert(condition: boolean, message?: string): void { + if (!condition) { + throw new Error(message || "Assertion failed"); + } +} + +export function assertNull(obj: T | null): asserts obj is null { + if (obj != null) { + throw new Error("Assertion failed"); + } +} + +export function assertNotNull(obj: T | null): asserts obj is T { + if (obj == null) { + throw new Error("Assertion failed"); + } +} \ No newline at end of file diff --git a/src/frontend/src/utils/physicsConstant.ts b/src/frontend/src/utils/physicsConstant.ts new file mode 100644 index 0000000..765fe90 --- /dev/null +++ b/src/frontend/src/utils/physicsConstant.ts @@ -0,0 +1,3 @@ +export const pixelsPerMeter = 100; +export const gravitationalConstant = 9.81 * pixelsPerMeter; +export const groundHeight = 0; \ No newline at end of file