Add formatting to frontend (#27)

This commit is contained in:
lucas authored and GitHub committed 2026-01-05 19:25:41 +01:00
1 parent f3143c7b88
commit d4bb9b195f
16 files changed
+459 -372

No files matched your search

+1
View File
@@ -1 +1,2 @@
.DS_Store .DS_Store
.vscode
+5
View File
@@ -0,0 +1,5 @@
{
"singleQuote": false,
"semi": true,
"endOfLine": "auto"
}
+15 -4
View File
@@ -1,10 +1,21 @@
// @ts-check // @ts-check
import eslint from '@eslint/js'; import eslint from "@eslint/js";
import { defineConfig } from 'eslint/config'; import { defineConfig } from "eslint/config";
import tseslint from 'typescript-eslint'; import tseslint from "typescript-eslint";
import prettierConfig from "eslint-config-prettier";
import prettierPlugin from "eslint-plugin-prettier";
export default defineConfig( export default defineConfig(
eslint.configs.recommended, eslint.configs.recommended,
tseslint.configs.recommended, ...tseslint.configs.recommended,
// disables ESLint rules that conflict with Prettier (must be near the end)
prettierConfig,
{
plugins: { prettier: prettierPlugin },
rules: {
"prettier/prettier": "error",
},
},
); );
+5 -5
View File
@@ -1,13 +1,13 @@
<!doctype html> <!doctype html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="/style.css"> <link rel="stylesheet" href="/style.css" />
<title>Auto Beer Pong</title> <title>Auto Beer Pong</title>
</head> </head>
<body> <body>
<script type="module" src="/src/main.ts"></script> <script type="module" src="/src/main.ts"></script>
</body> </body>
</html> </html>
+6 -6
View File
@@ -13,9 +13,9 @@
"devDependencies": { "devDependencies": {
"@eslint/js": "^9.39.1", "@eslint/js": "^9.39.1",
"eslint": "^9.39.1", "eslint": "^9.39.1",
"eslint-config-prettier": "^10.1.2", "eslint-config-prettier": "^10.1.8",
"eslint-plugin-prettier": "^5.2.6", "eslint-plugin-prettier": "^5.5.4",
"prettier": "^3.5.3", "prettier": "^3.7.4",
"typescript": "~5.7.3", "typescript": "~5.7.3",
"typescript-eslint": "^8.48.0", "typescript-eslint": "^8.48.0",
"vite": "^6.2.0" "vite": "^6.2.0"
@@ -2135,9 +2135,9 @@
} }
}, },
"node_modules/prettier": { "node_modules/prettier": {
"version": "3.6.2", "version": "3.7.4",
"resolved": "https://registry.npmjs.org/prettier/-/prettier-3.6.2.tgz", "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.7.4.tgz",
"integrity": "sha512-I7AIg5boAr5R0FFtJ6rCfD+LFsWHp81dolrFD8S79U9tb8Az2nGrJncnMSnys+bpQJfRUzqs9hnA81OAA3hCuQ==", "integrity": "sha512-v6UNi1+3hSlVvv8fSaoUbggEM5VErKmmpGA7Pl3HF8V6uKY7rvClBOJlH6yNwQtfTueNkGVpOv/mtWL9L4bgRA==",
"dev": true, "dev": true,
"license": "MIT", "license": "MIT",
"bin": { "bin": {
+5 -3
View File
@@ -7,6 +7,8 @@
"start": "npm run dev", "start": "npm run dev",
"build": "npm run lint && tsc && vite build", "build": "npm run lint && tsc && vite build",
"lint": "eslint .", "lint": "eslint .",
"lint:fix": "eslint . --fix",
"format": "prettier . --write",
"dev": "vite" "dev": "vite"
}, },
"dependencies": { "dependencies": {
@@ -15,9 +17,9 @@
"devDependencies": { "devDependencies": {
"@eslint/js": "^9.39.1", "@eslint/js": "^9.39.1",
"eslint": "^9.39.1", "eslint": "^9.39.1",
"eslint-config-prettier": "^10.1.2", "eslint-config-prettier": "^10.1.8",
"eslint-plugin-prettier": "^5.2.6", "eslint-plugin-prettier": "^5.5.4",
"prettier": "^3.5.3", "prettier": "^3.7.4",
"typescript": "~5.7.3", "typescript": "~5.7.3",
"typescript-eslint": "^8.48.0", "typescript-eslint": "^8.48.0",
"vite": "^6.2.0" "vite": "^6.2.0"
+4 -4
View File
@@ -1,6 +1,6 @@
body { body {
margin: 0; margin: 0;
padding: 0; padding: 0;
color: rgba(255, 255, 255, 0.87); color: rgba(255, 255, 255, 0.87);
background-color: #000000; background-color: #000000;
} }
+111 -99
View File
@@ -1,138 +1,150 @@
import {
import { Actor, ActorArgs, Circle, Color, Engine, Graphic, GraphicsGroup, Random, vec, Vector } from "excalibur"; Actor,
ActorArgs,
Circle,
Color,
Engine,
Graphic,
GraphicsGroup,
Random,
vec,
Vector,
} from "excalibur";
import { assertNotNull, assertNull } from "../utils/assertUtils"; import { assertNotNull, assertNull } from "../utils/assertUtils";
import { gravitationalConstant, groundHeight } from "../utils/physicsConstant"; import { gravitationalConstant, groundHeight } from "../utils/physicsConstant";
export const defaultBallConfiguration: BallConfiguration = { export const defaultBallConfiguration: BallConfiguration = {
color: Color.Orange, color: Color.Orange,
radius: 5, radius: 5,
} };
export interface BallConfiguration { export interface BallConfiguration {
readonly color: Color; readonly color: Color;
readonly radius: number; readonly radius: number;
} }
const random = new Random(); const random = new Random();
export class Ball extends Actor { export class Ball extends Actor {
private _graphic: Graphic = null!; private _graphic: Graphic = null!;
private _animation: BallAnimation = new BallAnimation(this); private _animation: BallAnimation = new BallAnimation(this);
constructor(actorArgs: ActorArgs, public readonly configuration: BallConfiguration) { constructor(
super(actorArgs); actorArgs: ActorArgs,
} public readonly configuration: BallConfiguration,
) {
super(actorArgs);
}
onInitialize(engine: Engine): void {
const { configuration } = this;
const { radius } = configuration;
onInitialize(engine: Engine): void { const ballGraphic = new Circle({
const { configuration } = this; radius: radius,
const { radius } = configuration; color: configuration.color,
});
const ballGraphic = new Circle({ const specularGraphic = new Circle({
radius: radius, radius: radius / 5,
color: configuration.color, color: Color.White,
}); });
const specularGraphic = new Circle({ this._graphic = new GraphicsGroup({
radius: radius / 5, members: [
color: Color.White, { graphic: ballGraphic, offset: vec(0, 0) },
}); { graphic: specularGraphic, offset: vec(radius / 2, radius / 2) },
],
});
this._graphic = new GraphicsGroup({ this.graphics.add(this._graphic);
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;
const start = vec(-150, -200); engine.input.pointers.primary.on("down", () => {
this.pos = start; const randomVelocity = vec(
random.integer(100, 400),
random.integer(0, -400),
);
this.startParabolaAnimation(new Parabola(start, randomVelocity));
});
}
engine.input.pointers.primary.on("down", () => { public update(engine: Engine, elapsed: number): void {
const randomVelocity = vec(random.integer(100, 400), random.integer(0, -400)); super.update(engine, elapsed);
this.startParabolaAnimation(new Parabola(start, randomVelocity)); this._animation.update(elapsed);
}) }
}
startParabolaAnimation(parabola: Parabola): void {
public update(engine: Engine, elapsed: number): void { this._animation.start(parabola);
super.update(engine, elapsed); }
this._animation.update(elapsed);
}
startParabolaAnimation(parabola: Parabola): void {
this._animation.start(parabola);
}
} }
class BallAnimation { class BallAnimation {
private _running: boolean = false; private _running: boolean = false;
private _parabola: Parabola | null = null; private _parabola: Parabola | null = null;
private _elapsedTime: number | null = null; private _elapsedTime: number | null = null;
constructor(private _ball: Ball) { 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;
} }
get running(): boolean { assertNotNull(this._parabola);
return this._running;
if (this._elapsedTime == null) {
this._elapsedTime = 0;
} }
this._elapsedTime += elapsed;
start(parabola: Parabola): void { const newPosition = this._parabola.position(this._elapsedTime);
this.reset();
this._parabola = parabola;
this._running = true;
}
reset(): void { // Collide with ground
this._parabola = null; const yMin = groundHeight - this._ball.configuration.radius;
this._running = false; newPosition.y = Math.min(yMin, newPosition.y);
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;
}
this._ball.pos = newPosition;
}
} }
export class Parabola { export class Parabola {
constructor( constructor(
readonly startPos: Vector, readonly startPos: Vector,
readonly startVelocity: Vector readonly startVelocity: Vector,
) { } ) {}
position(time: number): Vector { position(time: number): Vector {
const timeSeconds = time / 1000; const timeSeconds = time / 1000;
const x = this.startPos.x + this.startVelocity.x * timeSeconds; const x = this.startPos.x + this.startVelocity.x * timeSeconds;
const y = this.startVelocity.y * timeSeconds + gravitationalConstant / 2 * timeSeconds ** 2 + this.startPos.y; const y =
this.startVelocity.y * timeSeconds +
return new Vector(x, y); (gravitationalConstant / 2) * timeSeconds ** 2 +
} this.startPos.y;
return new Vector(x, y);
}
} }
+129 -111
View File
@@ -1,126 +1,144 @@
import { Actor, ActorArgs, Color, Engine, Graphic, GraphicsGroup, Line, Polygon,vec } from "excalibur"; import {
Actor,
ActorArgs,
Color,
Engine,
Graphic,
GraphicsGroup,
Line,
Polygon,
vec,
} from "excalibur";
import { pixelsPerMeter } from "../utils/physicsConstant"; import { pixelsPerMeter } from "../utils/physicsConstant";
import { jitterVector, randomInteger } from "../utils/randomUtils"; import { jitterVector, randomInteger } from "../utils/randomUtils";
const jiggleTime = 500; const jiggleTime = 500;
export class Champion extends Actor { export class Champion extends Actor {
private _graphic: Graphic = null!; private _graphic: Graphic = null!;
private _jiggleTimer = randomInteger(0, 1000); private _jiggleTimer = randomInteger(0, 1000);
constructor(actorArgs: ActorArgs) {
super(actorArgs);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onInitialize(_engine: Engine): void {
const height = 1.4 * pixelsPerMeter;
const width = 0.4 * pixelsPerMeter;
const waistHeight = 0.3 * pixelsPerMeter;
const jitterMag = 0.1 * pixelsPerMeter;
const headSize = 0.5 * pixelsPerMeter;
const bodyPoints = [
jitterVector(vec(-width / 2, waistHeight), jitterMag),
jitterVector(vec(-width / 2, height), jitterMag),
jitterVector(vec(width / 2, height), jitterMag),
jitterVector(vec(width / 2, waistHeight), jitterMag),
];
const bodyGraphic = new Polygon({
points: bodyPoints,
color: this.color,
});
const leftLeg = new Line({
start: bodyPoints[0],
end: vec(bodyPoints[0].x, 0),
thickness: 5,
color: Color.Black,
});
const rightLeg = new Line({
start: bodyPoints[3],
end: vec(bodyPoints[3].x, 0),
thickness: 5,
color: Color.Black,
});
const headPoints = [
jitterVector(vec(-headSize / 2, 0), jitterMag),
jitterVector(vec(-headSize / 2, headSize), jitterMag),
jitterVector(vec(headSize / 2, headSize), jitterMag),
jitterVector(vec(headSize / 2, 0), jitterMag),
];
const headGraphic = new Polygon({
points: headPoints,
color: this.color,
});
this.anchor = vec(0.5, 1);
this._graphic = new GraphicsGroup({
members: [
{ graphic: leftLeg , offset: vec(0, -waistHeight)},
{ graphic: rightLeg , offset: vec(0, -waistHeight)},
{ graphic: bodyGraphic, offset: vec(-width / 2, -height) },
{ graphic: headGraphic, offset: vec(-headSize / 2, -height - headSize / 2) },
{ graphic: smileEyeGraphic.clone(), offset: vec(-headSize / 4, -height) },
{ graphic: smileEyeGraphic.clone(), offset: vec(headSize / 4, -height) },
],
useAnchor: false,
})
this.graphics.add(this._graphic);
this.rotation = Math.PI / 180 * 5;
}
update(_engine: Engine, elapsed: number): void {
this._jiggle(elapsed);
}
private _jiggle(elapsed: number): void {
this._jiggleTimer += elapsed;
if (this._jiggleTimer > jiggleTime) {
this._jiggleTimer -= jiggleTime;
this.rotation = -this.rotation
}
constructor(actorArgs: ActorArgs) {
super(actorArgs);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onInitialize(_engine: Engine): void {
const height = 1.4 * pixelsPerMeter;
const width = 0.4 * pixelsPerMeter;
const waistHeight = 0.3 * pixelsPerMeter;
const jitterMag = 0.1 * pixelsPerMeter;
const headSize = 0.5 * pixelsPerMeter;
const bodyPoints = [
jitterVector(vec(-width / 2, waistHeight), jitterMag),
jitterVector(vec(-width / 2, height), jitterMag),
jitterVector(vec(width / 2, height), jitterMag),
jitterVector(vec(width / 2, waistHeight), jitterMag),
];
const bodyGraphic = new Polygon({
points: bodyPoints,
color: this.color,
});
const leftLeg = new Line({
start: bodyPoints[0],
end: vec(bodyPoints[0].x, 0),
thickness: 5,
color: Color.Black,
});
const rightLeg = new Line({
start: bodyPoints[3],
end: vec(bodyPoints[3].x, 0),
thickness: 5,
color: Color.Black,
});
const headPoints = [
jitterVector(vec(-headSize / 2, 0), jitterMag),
jitterVector(vec(-headSize / 2, headSize), jitterMag),
jitterVector(vec(headSize / 2, headSize), jitterMag),
jitterVector(vec(headSize / 2, 0), jitterMag),
];
const headGraphic = new Polygon({
points: headPoints,
color: this.color,
});
this.anchor = vec(0.5, 1);
this._graphic = new GraphicsGroup({
members: [
{ graphic: leftLeg, offset: vec(0, -waistHeight) },
{ graphic: rightLeg, offset: vec(0, -waistHeight) },
{ graphic: bodyGraphic, offset: vec(-width / 2, -height) },
{
graphic: headGraphic,
offset: vec(-headSize / 2, -height - headSize / 2),
},
{
graphic: smileEyeGraphic.clone(),
offset: vec(-headSize / 4, -height),
},
{
graphic: smileEyeGraphic.clone(),
offset: vec(headSize / 4, -height),
},
],
useAnchor: false,
});
this.graphics.add(this._graphic);
this.rotation = (Math.PI / 180) * 5;
}
update(_engine: Engine, elapsed: number): void {
this._jiggle(elapsed);
}
private _jiggle(elapsed: number): void {
this._jiggleTimer += elapsed;
if (this._jiggleTimer > jiggleTime) {
this._jiggleTimer -= jiggleTime;
this.rotation = -this.rotation;
} }
}
} }
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
const _crossEyeGraphic = new GraphicsGroup({ const _crossEyeGraphic = new GraphicsGroup({
members: [ members: [
new Line({ new Line({
start: vec(-5, -5), start: vec(-5, -5),
end: vec(5, 5), end: vec(5, 5),
color: Color.Black, color: Color.Black,
thickness: 3, thickness: 3,
}), }),
new Line({ new Line({
start: vec(5, -5), start: vec(5, -5),
end: vec(-5, 5), end: vec(-5, 5),
color: Color.Black, color: Color.Black,
thickness: 3, thickness: 3,
}) }),
] ],
}); });
const smileEyeGraphic = new GraphicsGroup({ const smileEyeGraphic = new GraphicsGroup({
members: [ members: [
new Line({ new Line({
start: vec(-5, 5), start: vec(-5, 5),
end: vec(5, -5), end: vec(5, -5),
color: Color.Black, color: Color.Black,
thickness: 3, thickness: 3,
}), }),
new Line({ new Line({
start: vec(5, -5), start: vec(5, -5),
end: vec(10, 5), end: vec(10, 5),
color: Color.Black, color: Color.Black,
thickness: 3, thickness: 3,
}) }),
] ],
}); });
+62 -51
View File
@@ -1,67 +1,78 @@
import {
import { Actor, ActorArgs, Color, Engine, Graphic, GraphicsGroup, Line, Polygon,vec } from "excalibur"; Actor,
ActorArgs,
Color,
Engine,
Graphic,
GraphicsGroup,
Line,
Polygon,
vec,
} from "excalibur";
import { pixelsPerMeter } from "../utils/physicsConstant"; import { pixelsPerMeter } from "../utils/physicsConstant";
export const defaultCupConfiguration = { export const defaultCupConfiguration = {
cupColor: Color.Red, cupColor: Color.Red,
rimColor: Color.White, rimColor: Color.White,
widthRim: 0.18 * pixelsPerMeter, widthRim: 0.18 * pixelsPerMeter,
widthBottom: 0.12 * pixelsPerMeter, widthBottom: 0.12 * pixelsPerMeter,
height: 0.2 * pixelsPerMeter, height: 0.2 * pixelsPerMeter,
rimThickness: 0.02 * pixelsPerMeter, rimThickness: 0.02 * pixelsPerMeter,
} };
export interface CupConfiguration { export interface CupConfiguration {
readonly cupColor: Color; readonly cupColor: Color;
readonly rimColor: Color; readonly rimColor: Color;
readonly widthRim: number; readonly widthRim: number;
readonly widthBottom: number; readonly widthBottom: number;
readonly height: number; readonly height: number;
readonly rimThickness: number; readonly rimThickness: number;
} }
export class Cup extends Actor { export class Cup extends Actor {
private _graphic: Graphic = null!; private _graphic: Graphic = null!;
constructor(actorArgs: ActorArgs, public readonly configuration: CupConfiguration) { constructor(
super(actorArgs); actorArgs: ActorArgs,
} public readonly configuration: CupConfiguration,
) {
super(actorArgs);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onInitialize(_engine: Engine): void {
const { configuration } = this;
const { height, widthRim, widthBottom } = configuration;
const halfWidthBottom = widthBottom / 2;
const halfWidthRim = widthRim / 2;
// eslint-disable-next-line @typescript-eslint/no-unused-vars const points = [
onInitialize(_engine: Engine): void { vec(-halfWidthBottom, 0),
const { configuration } = this; vec(-halfWidthRim, -height),
const { height, widthRim, widthBottom, } = configuration; vec(halfWidthRim, -height),
const halfWidthBottom = widthBottom / 2; vec(halfWidthBottom, 0),
const halfWidthRim = widthRim / 2; ];
const points = [ const cupBody = new Polygon({
vec(-halfWidthBottom, 0), points,
vec(-halfWidthRim, -height), color: configuration.cupColor,
vec(halfWidthRim, -height), });
vec(halfWidthBottom, 0),
];
const cupBody = new Polygon({ const cupRim = new Line({
points, start: vec(-halfWidthRim - 1, 0),
color: configuration.cupColor, end: vec(halfWidthRim + 1, 0),
}); color: configuration.rimColor,
thickness: configuration.rimThickness,
});
const cupRim = new Line({ this._graphic = new GraphicsGroup({
start: vec(-halfWidthRim - 1, 0), members: [
end: vec(halfWidthRim + 1, 0), { graphic: cupBody, offset: vec(-halfWidthRim, -height) },
color: configuration.rimColor, { graphic: cupRim, offset: vec(0, -height) },
thickness: configuration.rimThickness, ],
}); useAnchor: false,
});
this._graphic = new GraphicsGroup({ this.graphics.add(this._graphic);
members: [ }
{ graphic: cupBody, offset: vec(-halfWidthRim, -height) },
{ graphic: cupRim, offset: vec(0, -height) },
],
useAnchor: false,
});
this.graphics.add(this._graphic);
}
} }
+14 -14
View File
@@ -4,22 +4,22 @@ import { groundHeight } from "../utils/physicsConstant";
export const groundColor = Color.Gray; export const groundColor = Color.Gray;
export class Ground extends Actor { export class Ground extends Actor {
private _graphic: Graphic = null!; private _graphic: Graphic = null!;
onInitialize(engine: Engine): void { onInitialize(engine: Engine): void {
const { width, height} = engine.screen; const { width, height } = engine.screen;
this._graphic = new Rectangle({ this._graphic = new Rectangle({
width, width,
height: height / 2, height: height / 2,
color: groundColor, color: groundColor,
}) });
this.graphics.add(this._graphic); this.graphics.add(this._graphic);
this.pos.setTo(0, groundHeight); this.pos.setTo(0, groundHeight);
this.anchor.setTo(0.5, 0); this.anchor.setTo(0.5, 0);
this._graphic.width = 10000; this._graphic.width = 10000;
this._graphic.height = 10000; this._graphic.height = 10000;
} }
} }
+55 -43
View File
@@ -1,59 +1,71 @@
import { Actor, ActorArgs, Color, Engine, Graphic, Polygon,vec } from "excalibur"; import {
Actor,
ActorArgs,
Color,
Engine,
Graphic,
Polygon,
vec,
} from "excalibur";
import { pixelsPerMeter } from "../utils/physicsConstant"; import { pixelsPerMeter } from "../utils/physicsConstant";
export const defaultTableConfiguration = { export const defaultTableConfiguration = {
color: Color.Brown, color: Color.Brown,
width: 2.44 * pixelsPerMeter, width: 2.44 * pixelsPerMeter,
height: 0.7 * pixelsPerMeter, height: 0.7 * pixelsPerMeter,
thickness: 0.15 * pixelsPerMeter, thickness: 0.15 * pixelsPerMeter,
legOffset: 0.15 * pixelsPerMeter, legOffset: 0.15 * pixelsPerMeter,
legThickness: 0.15 * pixelsPerMeter, legThickness: 0.15 * pixelsPerMeter,
} };
export interface TableConfiguration { export interface TableConfiguration {
readonly color: Color; readonly color: Color;
readonly width: number; readonly width: number;
readonly height: number; readonly height: number;
readonly thickness: number; readonly thickness: number;
readonly legOffset: number; readonly legOffset: number;
readonly legThickness: number; readonly legThickness: number;
} }
export class Table extends Actor { export class Table extends Actor {
private _graphic: Graphic = null!; private _graphic: Graphic = null!;
constructor(actorArgs: ActorArgs, public readonly configuration: TableConfiguration) { constructor(
super(actorArgs); actorArgs: ActorArgs,
} public readonly configuration: TableConfiguration,
) {
super(actorArgs);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
onInitialize(_engine: Engine): void { onInitialize(_engine: Engine): void {
const { width, height, thickness, legOffset, legThickness } = this.configuration; const { width, height, thickness, legOffset, legThickness } =
const halfWidth = width / 2; this.configuration;
const halfWidth = width / 2;
const points = [ const points = [
vec(halfWidth, -height), vec(halfWidth, -height),
vec(halfWidth, thickness - height), vec(halfWidth, thickness - height),
vec(halfWidth - legOffset, thickness - height), vec(halfWidth - legOffset, thickness - height),
vec(halfWidth - legOffset, 0), vec(halfWidth - legOffset, 0),
vec(halfWidth - legOffset - legThickness, 0), vec(halfWidth - legOffset - legThickness, 0),
vec(halfWidth - legOffset - legThickness, thickness - height), vec(halfWidth - legOffset - legThickness, thickness - height),
vec(-halfWidth + legOffset + legThickness, thickness - height), vec(-halfWidth + legOffset + legThickness, thickness - height),
vec(-halfWidth + legOffset + legThickness, 0), vec(-halfWidth + legOffset + legThickness, 0),
vec(-halfWidth + legOffset, 0), vec(-halfWidth + legOffset, 0),
vec(-halfWidth + legOffset, thickness - height), vec(-halfWidth + legOffset, thickness - height),
vec(-halfWidth, thickness - height), vec(-halfWidth, thickness - height),
vec(-halfWidth, -height), vec(-halfWidth, -height),
]; ];
this._graphic = new Polygon({ this._graphic = new Polygon({
points, points,
color: this.configuration.color, color: this.configuration.color,
}); });
this.graphics.add(this._graphic); this.graphics.add(this._graphic);
this.anchor.setTo(0.5, 1); this.anchor.setTo(0.5, 1);
} }
} }
+23 -16
View File
@@ -18,7 +18,6 @@ export class GameScene extends GameStateScene {
private _cups: Cup[] = []; private _cups: Cup[] = [];
private _champions: Champion[] = []; private _champions: Champion[] = [];
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
constructor(private _connection: Connection) { constructor(private _connection: Connection) {
super(); super();
@@ -31,33 +30,41 @@ export class GameScene extends GameStateScene {
this._ground = new Ground(); this._ground = new Ground();
this.add(this._ground); this.add(this._ground);
this._table = new Table({pos: vec(0, groundHeight)}, defaultTableConfiguration); this._table = new Table(
{ pos: vec(0, groundHeight) },
defaultTableConfiguration,
);
this.add(this._table); this.add(this._table);
const tableHalfWidth = this._table.configuration.width / 2; const tableHalfWidth = this._table.configuration.width / 2;
const tableHeight = this._table.configuration.height; const tableHeight = this._table.configuration.height;
const cupPositions = [ const cupPositions = [
...[10, 32, 54].map(v => v - tableHalfWidth), ...[10, 32, 54].map((v) => v - tableHalfWidth),
...[10, 32, 54].map(v => -v + tableHalfWidth), ...[10, 32, 54].map((v) => -v + tableHalfWidth),
]; ];
cupPositions.forEach(offsetX => { cupPositions.forEach((offsetX) => {
const cup = new Cup({ const cup = new Cup(
pos: vec(offsetX, groundHeight - tableHeight), {
color: Color.Red, pos: vec(offsetX, groundHeight - tableHeight),
}, defaultCupConfiguration) color: Color.Red,
},
defaultCupConfiguration,
);
this.add(cup); this.add(cup);
this._cups.push(cup); this._cups.push(cup);
}); });
const championPositions = [ const championPositions = [
...[50, 150, 250].map(v => -v - tableHalfWidth), ...[50, 150, 250].map((v) => -v - tableHalfWidth),
...[50, 150, 250].map(v => v + tableHalfWidth) ...[50, 150, 250].map((v) => v + tableHalfWidth),
]; ];
championPositions.forEach(offsetX => { championPositions.forEach((offsetX) => {
const champion = new Champion({
const champion = new Champion({color: randomNonGroundColor(), pos: vec(offsetX, groundHeight)}); color: randomNonGroundColor(),
pos: vec(offsetX, groundHeight),
});
this.add(champion); this.add(champion);
this._champions.push(champion); this._champions.push(champion);
}); });
+9 -9
View File
@@ -1,17 +1,17 @@
export function assert(condition: boolean, message?: string): void { export function assert(condition: boolean, message?: string): void {
if (!condition) { if (!condition) {
throw new Error(message || "Assertion failed"); throw new Error(message || "Assertion failed");
} }
} }
export function assertNull<T>(obj: T | null): asserts obj is null { export function assertNull<T>(obj: T | null): asserts obj is null {
if (obj != null) { if (obj != null) {
throw new Error("Assertion failed"); throw new Error("Assertion failed");
} }
} }
export function assertNotNull<T>(obj: T | null): asserts obj is T { export function assertNotNull<T>(obj: T | null): asserts obj is T {
if (obj == null) { if (obj == null) {
throw new Error("Assertion failed"); throw new Error("Assertion failed");
} }
} }
+5 -1
View File
@@ -1,5 +1,9 @@
import { Color } from "excalibur"; import { Color } from "excalibur";
export function colorDistance(color1: Color, color2: Color): number { export function colorDistance(color1: Color, color2: Color): number {
return Math.abs(color1.r - color2.r) + Math.abs(color1.g - color2.g) + Math.abs(color1.b - color2.b); return (
Math.abs(color1.r - color2.r) +
Math.abs(color1.g - color2.g) +
Math.abs(color1.b - color2.b)
);
} }
+10 -6
View File
@@ -3,16 +3,20 @@ import { Color, Random, vec, Vector } from "excalibur";
const rand = new Random(); const rand = new Random();
export function randomColor(): Color { export function randomColor(): Color {
return new Color(rand.integer(0, 255), rand.integer(0, 255), rand.integer(0, 255)); return new Color(
rand.integer(0, 255),
rand.integer(0, 255),
rand.integer(0, 255),
);
} }
/** Jitter given vector by at most the given magnitude. The random offset is [-magnitude / 2, magnitude / 2] */ /** Jitter given vector by at most the given magnitude. The random offset is [-magnitude / 2, magnitude / 2] */
export function jitterVector(v: Vector, magnitude: number): Vector { export function jitterVector(v: Vector, magnitude: number): Vector {
const x = v.x + rand.next() * magnitude - magnitude / 2; const x = v.x + rand.next() * magnitude - magnitude / 2;
const y = v.y + rand.next() * magnitude - magnitude / 2; const y = v.y + rand.next() * magnitude - magnitude / 2;
return vec(x, y); return vec(x, y);
} }
export function randomInteger(min: number, max:number): number { export function randomInteger(min: number, max: number): number {
return rand.integer(min, max -1); return rand.integer(min, max - 1);
} }