diff --git a/src/frontend/src/GameElements/Champion.ts b/src/frontend/src/GameElements/Champion.ts new file mode 100644 index 0000000..6d67cf9 --- /dev/null +++ b/src/frontend/src/GameElements/Champion.ts @@ -0,0 +1,126 @@ +import { Actor, ActorArgs, Color, Engine, Graphic, GraphicsGroup, Line, Polygon,vec } from "excalibur"; +import { pixelsPerMeter } from "../utils/physicsConstant"; +import { jitterVector, randomInteger } from "../utils/randomUtils"; + +const jiggleTime = 500; + +export class Champion extends Actor { + private _graphic: Graphic = null!; + + 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 + } + + } +} + +// eslint-disable-next-line @typescript-eslint/no-unused-vars +const _crossEyeGraphic = new GraphicsGroup({ + members: [ + new Line({ + start: vec(-5, -5), + end: vec(5, 5), + color: Color.Black, + thickness: 3, + }), + new Line({ + start: vec(5, -5), + end: vec(-5, 5), + color: Color.Black, + thickness: 3, + }) + ] + }); + +const smileEyeGraphic = new GraphicsGroup({ + members: [ + new Line({ + start: vec(-5, 5), + end: vec(5, -5), + color: Color.Black, + thickness: 3, + }), + new Line({ + start: vec(5, -5), + end: vec(10, 5), + color: Color.Black, + thickness: 3, + }) + ] + }); \ No newline at end of file diff --git a/src/frontend/src/GameElements/Ground.ts b/src/frontend/src/GameElements/Ground.ts index e7f058e..092a035 100644 --- a/src/frontend/src/GameElements/Ground.ts +++ b/src/frontend/src/GameElements/Ground.ts @@ -1,7 +1,7 @@ import { Actor, Color, Engine, Graphic, Rectangle } from "excalibur"; import { groundHeight } from "../utils/physicsConstant"; -const groundColor = Color.Gray; +export const groundColor = Color.Gray; export class Ground extends Actor { private _graphic: Graphic = null!; diff --git a/src/frontend/src/Scenes/GameScene.ts b/src/frontend/src/Scenes/GameScene.ts index 773c322..3f1c4c7 100644 --- a/src/frontend/src/Scenes/GameScene.ts +++ b/src/frontend/src/Scenes/GameScene.ts @@ -1,11 +1,14 @@ import { Connection } from "../Connection"; import { GameStateScene } from "./GameStateScene"; -import { Ground } from "../GameElements/Ground"; +import { Ground, groundColor } 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"; +import { Champion } from "../GameElements/Champion"; +import { randomColor } from "../utils/randomUtils"; +import { colorDistance } from "../utils/colorUtils"; export class GameScene extends GameStateScene { private _ground: Ground = null!; @@ -13,7 +16,10 @@ export class GameScene extends GameStateScene { private _ball: Ball = null!; private _cups: Cup[] = []; + private _champions: Champion[] = []; + + // eslint-disable-next-line @typescript-eslint/no-unused-vars constructor(private _connection: Connection) { super(); } @@ -45,7 +51,27 @@ export class GameScene extends GameStateScene { this._cups.push(cup); }); + const championPositions = [ + ...[50, 150, 250].map(v => -v - tableHalfWidth), + ...[50, 150, 250].map(v => v + tableHalfWidth) + ]; + championPositions.forEach(offsetX => { + + const champion = new Champion({color: randomNonGroundColor(), pos: vec(offsetX, groundHeight)}); + this.add(champion); + this._champions.push(champion); + }); + this._ball = new Ball({}, defaultBallConfiguration); this.add(this._ball); } } + +function randomNonGroundColor(): Color { + let color; + do { + color = randomColor(); + } while (colorDistance(color, groundColor) < 100); + + return color; +} \ No newline at end of file diff --git a/src/frontend/src/utils/colorUtils.ts b/src/frontend/src/utils/colorUtils.ts new file mode 100644 index 0000000..b6eaabb --- /dev/null +++ b/src/frontend/src/utils/colorUtils.ts @@ -0,0 +1,5 @@ +import { Color } from "excalibur"; + +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); +} \ No newline at end of file diff --git a/src/frontend/src/utils/randomUtils.ts b/src/frontend/src/utils/randomUtils.ts new file mode 100644 index 0000000..9729601 --- /dev/null +++ b/src/frontend/src/utils/randomUtils.ts @@ -0,0 +1,18 @@ +import { Color, Random, vec, Vector } from "excalibur"; + +const rand = new Random(); + +export function randomColor(): Color { + 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] */ +export function jitterVector(v: Vector, magnitude: number): Vector { + const x = v.x + rand.next() * magnitude - magnitude / 2; + const y = v.y + rand.next() * magnitude - magnitude / 2; + return vec(x, y); +} + +export function randomInteger(min: number, max:number): number { + return rand.integer(min, max -1); +} \ No newline at end of file