Add whole body sprites with idle and shoot poses (#34)
This commit is contained in:
1 parent
c0886a7daf
commit
db3781c11d
19 files changed
+221
-179
No files matched your search
Binary file not shown.
|
After Width: | Height: | Size: 493 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 971 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 1.1 MiB |
Binary file not shown.
|
After Width: | Height: | Size: 1.0 MiB |
@@ -10,7 +10,7 @@ import {
|
|||||||
vec,
|
vec,
|
||||||
Vector,
|
Vector,
|
||||||
} from "excalibur";
|
} from "excalibur";
|
||||||
import { gravitationalConstant } from "../utils/physicsConstant";
|
import { gravitationalConstant } from "../configs/physicsConstant";
|
||||||
import { Parabola as ParabolaJSON } from "../jsonTypes";
|
import { Parabola as ParabolaJSON } from "../jsonTypes";
|
||||||
import { BallClock } from "../Timers/BallClock";
|
import { BallClock } from "../Timers/BallClock";
|
||||||
import { BaseActor } from "./BaseActor";
|
import { BaseActor } from "./BaseActor";
|
||||||
|
|||||||
@@ -1,144 +1,77 @@
|
|||||||
import {
|
import { ActorArgs, Engine, Sprite, vec } from "excalibur";
|
||||||
Actor,
|
import { pixelsPerMeter } from "../configs/physicsConstant";
|
||||||
ActorArgs,
|
import { randomInteger } from "../utils/randomUtils";
|
||||||
Color,
|
import { getImage } from "../imageLoader";
|
||||||
Engine,
|
import { Clock } from "../Timers/Clock";
|
||||||
Graphic,
|
import { BaseActor } from "./BaseActor";
|
||||||
GraphicsGroup,
|
import { ChampionName, champions } from "./champions";
|
||||||
Line,
|
|
||||||
Polygon,
|
|
||||||
vec,
|
|
||||||
} from "excalibur";
|
|
||||||
import { pixelsPerMeter } from "../utils/physicsConstant";
|
|
||||||
import { jitterVector, randomInteger } from "../utils/randomUtils";
|
|
||||||
|
|
||||||
const jiggleTime = 500;
|
type Facing = "left" | "right";
|
||||||
|
|
||||||
export class Champion extends Actor {
|
export class Champion extends BaseActor {
|
||||||
private _graphic: Graphic = null!;
|
facing: Facing;
|
||||||
|
championName: ChampionName;
|
||||||
|
|
||||||
private _jiggleTimer = randomInteger(0, 1000);
|
constructor(actorArgs: ActorArgs, facing: Facing, name: ChampionName) {
|
||||||
|
|
||||||
constructor(actorArgs: ActorArgs) {
|
|
||||||
super(actorArgs);
|
super(actorArgs);
|
||||||
|
this.facing = facing;
|
||||||
|
this.championName = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 height = 1.4 * pixelsPerMeter;
|
const championConfig = champions[this.championName];
|
||||||
const width = 0.4 * pixelsPerMeter;
|
|
||||||
const waistHeight = 0.3 * pixelsPerMeter;
|
|
||||||
const jitterMag = 0.1 * pixelsPerMeter;
|
|
||||||
const headSize = 0.5 * pixelsPerMeter;
|
|
||||||
|
|
||||||
const bodyPoints = [
|
const height = championConfig.height * pixelsPerMeter;
|
||||||
jitterVector(vec(-width / 2, waistHeight), jitterMag),
|
const width = 0.8 * pixelsPerMeter;
|
||||||
jitterVector(vec(-width / 2, height), jitterMag),
|
|
||||||
jitterVector(vec(width / 2, height), jitterMag),
|
|
||||||
jitterVector(vec(width / 2, waistHeight), jitterMag),
|
|
||||||
];
|
|
||||||
|
|
||||||
const bodyGraphic = new Polygon({
|
this.anchor = vec(0.5, 0.95);
|
||||||
points: bodyPoints,
|
const flipHorizontal = this.facing === "right";
|
||||||
color: this.color,
|
|
||||||
});
|
|
||||||
|
|
||||||
const leftLeg = new Line({
|
const idleImage = getImage(`${this.championName}_idle`);
|
||||||
start: bodyPoints[0],
|
const shootImage = getImage(`${this.championName}_shoot`);
|
||||||
end: vec(bodyPoints[0].x, 0),
|
|
||||||
thickness: 5,
|
|
||||||
color: Color.Black,
|
|
||||||
});
|
|
||||||
|
|
||||||
const rightLeg = new Line({
|
const idleGraphic = new Sprite({
|
||||||
start: bodyPoints[3],
|
image: idleImage,
|
||||||
end: vec(bodyPoints[3].x, 0),
|
flipHorizontal,
|
||||||
thickness: 5,
|
destSize: {
|
||||||
color: Color.Black,
|
width: width,
|
||||||
});
|
height: width + height,
|
||||||
|
|
||||||
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);
|
const shootGraphic = new Sprite({
|
||||||
|
image: shootImage,
|
||||||
|
flipHorizontal,
|
||||||
|
destSize: {
|
||||||
|
width: width,
|
||||||
|
height: width + height,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
this.rotation = (Math.PI / 180) * 5;
|
this.graphics.add("idle", idleGraphic);
|
||||||
|
this.graphics.add("shoot", shootGraphic);
|
||||||
|
this.setPose("idle");
|
||||||
|
|
||||||
|
this.rotation = (Math.PI / 180) * 1;
|
||||||
|
|
||||||
|
this._setupJiggle();
|
||||||
}
|
}
|
||||||
|
|
||||||
update(_engine: Engine, elapsed: number): void {
|
private _setupJiggle(): void {
|
||||||
this._jiggle(elapsed);
|
const jiggleClock = new Clock();
|
||||||
}
|
jiggleClock.start(randomInteger(0, 1000));
|
||||||
|
|
||||||
private _jiggle(elapsed: number): void {
|
// Repeat by starting when stopping with a new random duration.
|
||||||
this._jiggleTimer += elapsed;
|
jiggleClock.on("stop", () => {
|
||||||
if (this._jiggleTimer > jiggleTime) {
|
|
||||||
this._jiggleTimer -= jiggleTime;
|
|
||||||
this.rotation = -this.rotation;
|
this.rotation = -this.rotation;
|
||||||
|
jiggleClock.start(randomInteger(500, 800));
|
||||||
|
});
|
||||||
|
|
||||||
|
this.addClock(jiggleClock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setPose(pose: "idle" | "shoot"): void {
|
||||||
|
this.graphics.use(pose);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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,
|
|
||||||
}),
|
|
||||||
],
|
|
||||||
});
|
|
||||||
@@ -9,7 +9,7 @@ import {
|
|||||||
Polygon,
|
Polygon,
|
||||||
vec,
|
vec,
|
||||||
} from "excalibur";
|
} from "excalibur";
|
||||||
import { pixelsPerMeter } from "../utils/physicsConstant";
|
import { pixelsPerMeter } from "../configs/physicsConstant";
|
||||||
|
|
||||||
export const defaultCupConfiguration = {
|
export const defaultCupConfiguration = {
|
||||||
cupColor: Color.Red,
|
cupColor: Color.Red,
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Actor, Color, Engine, Graphic, Rectangle } from "excalibur";
|
import { Actor, Color, Engine, Graphic, Rectangle } from "excalibur";
|
||||||
import { groundHeight } from "../utils/physicsConstant";
|
import { groundHeight } from "../configs/sceneConfiguration";
|
||||||
|
|
||||||
export const groundColor = Color.Gray;
|
export const groundColor = Color.Gray;
|
||||||
|
|
||||||
|
|||||||
@@ -7,16 +7,6 @@ import {
|
|||||||
Polygon,
|
Polygon,
|
||||||
vec,
|
vec,
|
||||||
} from "excalibur";
|
} from "excalibur";
|
||||||
import { pixelsPerMeter } from "../utils/physicsConstant";
|
|
||||||
|
|
||||||
export const defaultTableConfiguration = {
|
|
||||||
color: Color.Brown,
|
|
||||||
width: 2.44 * pixelsPerMeter,
|
|
||||||
height: 0.7 * pixelsPerMeter,
|
|
||||||
thickness: 0.15 * pixelsPerMeter,
|
|
||||||
legOffset: 0.15 * pixelsPerMeter,
|
|
||||||
legThickness: 0.15 * pixelsPerMeter,
|
|
||||||
};
|
|
||||||
|
|
||||||
export interface TableConfiguration {
|
export interface TableConfiguration {
|
||||||
readonly color: Color;
|
readonly color: Color;
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
export type ChampionName = "lucas" | "berset";
|
||||||
|
|
||||||
|
export interface ChampionConfig {
|
||||||
|
name: ChampionName;
|
||||||
|
height: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
const lucas: ChampionConfig = {
|
||||||
|
name: "lucas",
|
||||||
|
height: 1.4,
|
||||||
|
};
|
||||||
|
|
||||||
|
const berset: ChampionConfig = {
|
||||||
|
name: "berset",
|
||||||
|
height: 1.5,
|
||||||
|
};
|
||||||
|
|
||||||
|
export const champions: Record<ChampionName, ChampionConfig> = {
|
||||||
|
lucas,
|
||||||
|
berset,
|
||||||
|
};
|
||||||
@@ -9,7 +9,7 @@ export class BaseScene extends Scene {
|
|||||||
this._clocks.forEach((clock) => clock.update(elapsed));
|
this._clocks.forEach((clock) => clock.update(elapsed));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected addClock(timer: Clock): void {
|
addClock(timer: Clock): void {
|
||||||
this._clocks.push(timer);
|
this._clocks.push(timer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,22 +1,25 @@
|
|||||||
import { Connection, MatchMessageEvent } from "../Connection";
|
|
||||||
import { Ground, groundColor } from "../GameElements/Ground";
|
|
||||||
import {
|
|
||||||
defaultTableConfiguration,
|
|
||||||
Table as TableElement,
|
|
||||||
} from "../GameElements/Table";
|
|
||||||
import { Cup, defaultCupConfiguration } from "../GameElements/Cups";
|
|
||||||
import { Color, vec } from "excalibur";
|
import { Color, vec } from "excalibur";
|
||||||
|
import { Connection, MatchMessageEvent } from "../Connection";
|
||||||
import { Ball, defaultBallConfiguration, Parabola } from "../GameElements/Ball";
|
import { Ball, defaultBallConfiguration, Parabola } from "../GameElements/Ball";
|
||||||
import { groundHeight } from "../utils/physicsConstant";
|
|
||||||
import { Champion } from "../GameElements/Champion";
|
import { Champion } from "../GameElements/Champion";
|
||||||
import { randomColor } from "../utils/randomUtils";
|
import { Cup, defaultCupConfiguration } from "../GameElements/Cups";
|
||||||
import { colorDistance } from "../utils/colorUtils";
|
import { Ground, groundColor } from "../GameElements/Ground";
|
||||||
|
import { Table as TableElement } from "../GameElements/Table";
|
||||||
import { MatchState } from "../MatchState";
|
import { MatchState } from "../MatchState";
|
||||||
import { Action, Table } from "../jsonTypes";
|
|
||||||
import { assertNotNull } from "../utils/assertUtils";
|
|
||||||
import { Clock } from "../Timers/Clock";
|
import { Clock } from "../Timers/Clock";
|
||||||
|
import {
|
||||||
|
championPositions,
|
||||||
|
groundHeight,
|
||||||
|
tableConfiguration,
|
||||||
|
} from "../configs/sceneConfiguration";
|
||||||
|
import { Action, Table } from "../jsonTypes";
|
||||||
|
import { assertNotNull, assertNull } from "../utils/assertUtils";
|
||||||
|
import { colorDistance } from "../utils/colorUtils";
|
||||||
|
import { randomColor, randomInteger } from "../utils/randomUtils";
|
||||||
import { BaseScene } from "./BaseScene";
|
import { BaseScene } from "./BaseScene";
|
||||||
|
|
||||||
|
const speedUpFactor = 3;
|
||||||
|
|
||||||
export class MatchScene extends BaseScene {
|
export class MatchScene extends BaseScene {
|
||||||
private _ground: Ground = null!;
|
private _ground: Ground = null!;
|
||||||
private _table: TableElement = null!;
|
private _table: TableElement = null!;
|
||||||
@@ -27,6 +30,8 @@ export class MatchScene extends BaseScene {
|
|||||||
|
|
||||||
private _matchClock = new Clock();
|
private _matchClock = new Clock();
|
||||||
|
|
||||||
|
private _playingHistoryPromise?: Promise<void>;
|
||||||
|
|
||||||
constructor(private _connection: Connection) {
|
constructor(private _connection: Connection) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
@@ -34,11 +39,11 @@ export class MatchScene extends BaseScene {
|
|||||||
|
|
||||||
this._connection.on("match-message", (e: MatchMessageEvent) => {
|
this._connection.on("match-message", (e: MatchMessageEvent) => {
|
||||||
if (!this.isInitialized) {
|
if (!this.isInitialized) {
|
||||||
this.once("initialize", () => this._playFroMatchState(e.matchState));
|
this.once("initialize", () => this._playFromMatchState(e.matchState));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this._playFroMatchState(e.matchState);
|
this._playFromMatchState(e.matchState);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,7 +56,7 @@ export class MatchScene extends BaseScene {
|
|||||||
|
|
||||||
this._table = new TableElement(
|
this._table = new TableElement(
|
||||||
{ pos: vec(0, groundHeight) },
|
{ pos: vec(0, groundHeight) },
|
||||||
defaultTableConfiguration,
|
tableConfiguration,
|
||||||
);
|
);
|
||||||
this.add(this._table);
|
this.add(this._table);
|
||||||
|
|
||||||
@@ -75,15 +80,16 @@ export class MatchScene extends BaseScene {
|
|||||||
this._cups.push(cup);
|
this._cups.push(cup);
|
||||||
});
|
});
|
||||||
|
|
||||||
const championPositions = [
|
championPositions.forEach((offsetX, index) => {
|
||||||
...[50, 150, 250].map((v) => -v - tableHalfWidth),
|
const facing = index < 3 ? "right" : "left";
|
||||||
...[50, 150, 250].map((v) => v + tableHalfWidth),
|
const champion = new Champion(
|
||||||
];
|
{
|
||||||
championPositions.forEach((offsetX) => {
|
|
||||||
const champion = new Champion({
|
|
||||||
color: randomNonGroundColor(),
|
color: randomNonGroundColor(),
|
||||||
pos: vec(offsetX, groundHeight),
|
pos: vec(offsetX, groundHeight),
|
||||||
});
|
},
|
||||||
|
facing,
|
||||||
|
randomInteger(0, 2) === 0 ? "berset" : "lucas",
|
||||||
|
);
|
||||||
this.add(champion);
|
this.add(champion);
|
||||||
this._champions.push(champion);
|
this._champions.push(champion);
|
||||||
});
|
});
|
||||||
@@ -91,19 +97,47 @@ export class MatchScene extends BaseScene {
|
|||||||
this._ball = new Ball({}, defaultBallConfiguration);
|
this._ball = new Ball({}, defaultBallConfiguration);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async _playFroMatchState(state: MatchState): Promise<void> {
|
private async _playFromMatchState(state: MatchState): Promise<void> {
|
||||||
|
const promise = this._playingHistoryPromise;
|
||||||
|
|
||||||
|
if (promise != null) {
|
||||||
|
const previousMatchClockSpeed = this._matchClock.speedFactor;
|
||||||
|
const previousBallSpeed = this._ball.clock.speedFactor;
|
||||||
|
this._matchClock.speedFactor *= speedUpFactor;
|
||||||
|
this._ball.clock.speedFactor *= speedUpFactor;
|
||||||
|
|
||||||
|
await promise;
|
||||||
|
|
||||||
|
this._matchClock.speedFactor = previousMatchClockSpeed;
|
||||||
|
this._ball.clock.speedFactor = previousBallSpeed;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertNull(this._playingHistoryPromise);
|
||||||
|
|
||||||
|
let resolver: () => void;
|
||||||
|
this._playingHistoryPromise = new Promise(
|
||||||
|
(resolve) => (resolver = resolve),
|
||||||
|
);
|
||||||
|
|
||||||
for (const actionOrTable of state.matchStateRaw.history) {
|
for (const actionOrTable of state.matchStateRaw.history) {
|
||||||
if ("cupsA" in actionOrTable) {
|
if ("cupsA" in actionOrTable) {
|
||||||
this._updateFromTableState(actionOrTable);
|
this._updateFromTableState(actionOrTable);
|
||||||
await this._matchClock.startAndWait(2000);
|
await this._matchClock.startAndWait(2000);
|
||||||
} else {
|
} else {
|
||||||
this.updateActorVisibility(this._ball, true);
|
this.updateActorVisibility(this._ball, true);
|
||||||
|
const currentChampion = this._getCurrentChampion(actionOrTable.turn);
|
||||||
|
currentChampion.setPose("shoot");
|
||||||
this._updateFromActionState(actionOrTable);
|
this._updateFromActionState(actionOrTable);
|
||||||
await this._ball.clock.whenEnd();
|
await this._ball.clock.whenEnd();
|
||||||
|
currentChampion.setPose("idle");
|
||||||
|
|
||||||
await this._matchClock.startAndWait(1000);
|
await this._matchClock.startAndWait(1000);
|
||||||
this.updateActorVisibility(this._ball, false);
|
this.updateActorVisibility(this._ball, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this._playingHistoryPromise = undefined;
|
||||||
|
resolver!();
|
||||||
}
|
}
|
||||||
|
|
||||||
private _updateFromTableState(table: Table): void {
|
private _updateFromTableState(table: Table): void {
|
||||||
@@ -119,6 +153,10 @@ export class MatchScene extends BaseScene {
|
|||||||
const parabola = Parabola.fromJSON(action.parabola);
|
const parabola = Parabola.fromJSON(action.parabola);
|
||||||
this._ball.startParabolaAnimation(parabola);
|
this._ball.startParabolaAnimation(parabola);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _getCurrentChampion(turn: "a" | "b"): Champion {
|
||||||
|
return turn === "a" ? this._champions[0] : this._champions[3];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function randomNonGroundColor(): Color {
|
function randomNonGroundColor(): Color {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { Ball, Parabola } from "../GameElements/Ball";
|
import { Ball, Parabola } from "../GameElements/Ball";
|
||||||
import { assertNotNull } from "../utils/assertUtils";
|
import { assertNotNull } from "../utils/assertUtils";
|
||||||
import { groundHeight } from "../utils/physicsConstant";
|
import { groundHeight } from "../configs/sceneConfiguration";
|
||||||
import { Clock } from "./Clock";
|
import { Clock } from "./Clock";
|
||||||
|
|
||||||
export class BallClock extends Clock {
|
export class BallClock extends Clock {
|
||||||
@@ -24,8 +24,8 @@ export class BallClock extends Clock {
|
|||||||
|
|
||||||
assertNotNull(this._parabola);
|
assertNotNull(this._parabola);
|
||||||
|
|
||||||
assertNotNull(this._elapsedTime);
|
assertNotNull(this._elapsedTimeMS);
|
||||||
const newPosition = this._parabola.position(this._elapsedTime);
|
const newPosition = this._parabola.position(this._elapsedTimeMS);
|
||||||
|
|
||||||
// Collide with ground
|
// Collide with ground
|
||||||
const yMin = groundHeight - this._ball.configuration.radius;
|
const yMin = groundHeight - this._ball.configuration.radius;
|
||||||
|
|||||||
@@ -5,18 +5,19 @@ export const TimerEvents = {
|
|||||||
start: Event,
|
start: Event,
|
||||||
stop: Event,
|
stop: Event,
|
||||||
end: Event,
|
end: Event,
|
||||||
reset: Event,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export class Clock extends Evented<typeof TimerEvents> {
|
export class Clock extends Evented<typeof TimerEvents> {
|
||||||
private _running: boolean = false;
|
private _running: boolean = false;
|
||||||
protected _elapsedTime: number | null = null;
|
protected _elapsedTimeMS: number | null = null;
|
||||||
|
|
||||||
private _duration?: number;
|
private _durationMS?: number;
|
||||||
|
|
||||||
|
speedFactor = 1;
|
||||||
|
|
||||||
constructor(duration?: number) {
|
constructor(duration?: number) {
|
||||||
super();
|
super();
|
||||||
this._duration = duration;
|
this._durationMS = duration;
|
||||||
}
|
}
|
||||||
|
|
||||||
get running(): boolean {
|
get running(): boolean {
|
||||||
@@ -26,7 +27,7 @@ export class Clock extends Evented<typeof TimerEvents> {
|
|||||||
start(duration?: number): void {
|
start(duration?: number): void {
|
||||||
this.reset();
|
this.reset();
|
||||||
if (duration != null) {
|
if (duration != null) {
|
||||||
this._duration = duration;
|
this._durationMS = duration;
|
||||||
}
|
}
|
||||||
this._running = true;
|
this._running = true;
|
||||||
this.emit("start", {});
|
this.emit("start", {});
|
||||||
@@ -34,14 +35,13 @@ export class Clock extends Evented<typeof TimerEvents> {
|
|||||||
|
|
||||||
stop(): void {
|
stop(): void {
|
||||||
this._running = false;
|
this._running = false;
|
||||||
this._elapsedTime = null;
|
this._elapsedTimeMS = null;
|
||||||
this.emit("stop", {});
|
this.emit("stop", {});
|
||||||
}
|
}
|
||||||
|
|
||||||
reset(): void {
|
reset(): void {
|
||||||
this._running = false;
|
this._running = false;
|
||||||
this._elapsedTime = null;
|
this._elapsedTimeMS = null;
|
||||||
this.emit("reset", {});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async whenEnd(): Promise<void> {
|
async whenEnd(): Promise<void> {
|
||||||
@@ -58,17 +58,17 @@ export class Clock extends Evented<typeof TimerEvents> {
|
|||||||
|
|
||||||
update(elapsed: number): void {
|
update(elapsed: number): void {
|
||||||
if (!this.running) {
|
if (!this.running) {
|
||||||
assertNull(this._elapsedTime);
|
assertNull(this._elapsedTimeMS);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this._elapsedTime == null) {
|
if (this._elapsedTimeMS == null) {
|
||||||
this._elapsedTime = 0;
|
this._elapsedTimeMS = 0;
|
||||||
}
|
}
|
||||||
this._elapsedTime += elapsed;
|
this._elapsedTimeMS += elapsed * this.speedFactor;
|
||||||
|
|
||||||
if (this._duration != null) {
|
if (this._durationMS != null) {
|
||||||
if (this._elapsedTime > this._duration) {
|
if (this._elapsedTimeMS > this._durationMS) {
|
||||||
this.stop();
|
this.stop();
|
||||||
this.emit("end", {});
|
this.emit("end", {});
|
||||||
}
|
}
|
||||||
|
|||||||
-1
@@ -1,3 +1,2 @@
|
|||||||
export const pixelsPerMeter = 100;
|
export const pixelsPerMeter = 100;
|
||||||
export const gravitationalConstant = 9.81 * pixelsPerMeter;
|
export const gravitationalConstant = 9.81 * pixelsPerMeter;
|
||||||
export const groundHeight = 0;
|
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
import { Color } from "excalibur";
|
||||||
|
import { pixelsPerMeter } from "./physicsConstant";
|
||||||
|
|
||||||
|
export const tableConfiguration = {
|
||||||
|
color: Color.Brown,
|
||||||
|
width: 2.44 * pixelsPerMeter,
|
||||||
|
height: 0.7 * pixelsPerMeter,
|
||||||
|
thickness: 0.15 * pixelsPerMeter,
|
||||||
|
legOffset: 0.15 * pixelsPerMeter,
|
||||||
|
legThickness: 0.15 * pixelsPerMeter,
|
||||||
|
};
|
||||||
|
|
||||||
|
export const groundHeight = 0;
|
||||||
|
|
||||||
|
export const championDistanceFromTable = 0.5 * pixelsPerMeter;
|
||||||
|
export const championGap = 0.8 * pixelsPerMeter;
|
||||||
|
|
||||||
|
const championDistances = [
|
||||||
|
championDistanceFromTable,
|
||||||
|
championDistanceFromTable + championGap,
|
||||||
|
championDistanceFromTable + 2 * championGap,
|
||||||
|
];
|
||||||
|
|
||||||
|
export const championPositions = [
|
||||||
|
...championDistances.map((v) => -v - tableConfiguration.width / 2),
|
||||||
|
...championDistances.map((v) => v + tableConfiguration.width / 2),
|
||||||
|
];
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
import { ImageSource } from "excalibur";
|
||||||
|
import { assertNotNull } from "./utils/assertUtils";
|
||||||
|
|
||||||
|
const imageStrings = [
|
||||||
|
"lucas_idle.png",
|
||||||
|
"lucas_shoot.png",
|
||||||
|
"berset_idle.png",
|
||||||
|
"berset_shoot.png",
|
||||||
|
];
|
||||||
|
|
||||||
|
export const images = new Map<string, ImageSource>(
|
||||||
|
imageStrings.map((fileName) => [
|
||||||
|
fileName.replace(/\.[^/.]+$/, ""),
|
||||||
|
new ImageSource(`/assets/${fileName}`),
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
|
||||||
|
export function getImage(key: string): ImageSource {
|
||||||
|
const image = images.get(key);
|
||||||
|
assertNotNull(image);
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function loadImages(): Promise<void> {
|
||||||
|
await Promise.all([...images.values()].map((img) => img.load()));
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ import {
|
|||||||
mockLobbyMessage,
|
mockLobbyMessage,
|
||||||
MockSocket,
|
MockSocket,
|
||||||
} from "./sockets/MockSocket";
|
} from "./sockets/MockSocket";
|
||||||
|
import { loadImages } from "./imageLoader";
|
||||||
|
|
||||||
const urlParams = new URLSearchParams(window.location.search);
|
const urlParams = new URLSearchParams(window.location.search);
|
||||||
const mocked = urlParams.get("mock");
|
const mocked = urlParams.get("mock");
|
||||||
@@ -27,9 +28,11 @@ export const maxPlayers = 2;
|
|||||||
scenes: { lobby: lobbyScene, match: matchScene },
|
scenes: { lobby: lobbyScene, match: matchScene },
|
||||||
backgroundColor: ex.Color.fromHex("23213b"),
|
backgroundColor: ex.Color.fromHex("23213b"),
|
||||||
});
|
});
|
||||||
|
|
||||||
game.toggleDebug();
|
game.toggleDebug();
|
||||||
|
|
||||||
await game.start();
|
await game.start();
|
||||||
|
await loadImages();
|
||||||
await game.goToScene("lobby");
|
await game.goToScene("lobby");
|
||||||
|
|
||||||
connection.on("lobby-message", () => game.goToScene("lobby"));
|
connection.on("lobby-message", () => game.goToScene("lobby"));
|
||||||
|
|||||||
@@ -22,7 +22,12 @@ export class MockSocket
|
|||||||
|
|
||||||
send(message: string): void {
|
send(message: string): void {
|
||||||
if (message.includes("join")) {
|
if (message.includes("join")) {
|
||||||
this.sendToClient(JSON.stringify({ player: { id: 0, name: "Dubbel" } }));
|
this.sendToClient(
|
||||||
|
JSON.stringify({
|
||||||
|
messageType: "joinResponse",
|
||||||
|
messageContent: { client: { id: 0, name: "Dubbel" } },
|
||||||
|
}),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -47,8 +52,8 @@ export function mockLobbyMessage(): string {
|
|||||||
|
|
||||||
export function mockGameMessage(): string {
|
export function mockGameMessage(): string {
|
||||||
const matchUpdate = {
|
const matchUpdate = {
|
||||||
state: {
|
messageType: "matchState",
|
||||||
stateType: "match",
|
messsageContent: {
|
||||||
history: [
|
history: [
|
||||||
{
|
{
|
||||||
cupsA: [true, true, true],
|
cupsA: [true, true, true],
|
||||||
|
|||||||
Reference in new issue
Block a user