Add basic champion elements (#26)

This commit is contained in:
lucas authored and GitHub committed 2026-01-05 19:21:16 +01:00
1 parent ebd85d4c05
commit f3143c7b88
5 files changed
+177 -2

No files matched your search

+126
View File
@@ -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,
})
]
});
+1 -1
View File
@@ -1,7 +1,7 @@
import { Actor, Color, Engine, Graphic, Rectangle } from "excalibur"; import { Actor, Color, Engine, Graphic, Rectangle } from "excalibur";
import { groundHeight } from "../utils/physicsConstant"; import { groundHeight } from "../utils/physicsConstant";
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!;
+27 -1
View File
@@ -1,11 +1,14 @@
import { Connection } from "../Connection"; import { Connection } from "../Connection";
import { GameStateScene } from "./GameStateScene"; import { GameStateScene } from "./GameStateScene";
import { Ground } from "../GameElements/Ground"; import { Ground, groundColor } from "../GameElements/Ground";
import { defaultTableConfiguration, Table } from "../GameElements/Table"; import { defaultTableConfiguration, Table } from "../GameElements/Table";
import { Cup, defaultCupConfiguration } from "../GameElements/Cups"; import { Cup, defaultCupConfiguration } from "../GameElements/Cups";
import { Color, vec } from "excalibur"; import { Color, vec } from "excalibur";
import { Ball, defaultBallConfiguration } from "../GameElements/Ball"; import { Ball, defaultBallConfiguration } from "../GameElements/Ball";
import { groundHeight } from "../utils/physicsConstant"; 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 { export class GameScene extends GameStateScene {
private _ground: Ground = null!; private _ground: Ground = null!;
@@ -13,7 +16,10 @@ export class GameScene extends GameStateScene {
private _ball: Ball = null!; private _ball: Ball = null!;
private _cups: Cup[] = []; private _cups: Cup[] = [];
private _champions: Champion[] = [];
// eslint-disable-next-line @typescript-eslint/no-unused-vars
constructor(private _connection: Connection) { constructor(private _connection: Connection) {
super(); super();
} }
@@ -45,7 +51,27 @@ export class GameScene extends GameStateScene {
this._cups.push(cup); 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._ball = new Ball({}, defaultBallConfiguration);
this.add(this._ball); this.add(this._ball);
} }
} }
function randomNonGroundColor(): Color {
let color;
do {
color = randomColor();
} while (colorDistance(color, groundColor) < 100);
return color;
}
+5
View File
@@ -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);
}
+18
View File
@@ -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);
}