Skip to content

Commit

Permalink
🕒 Add getPhaseDuration
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdodo committed Nov 26, 2024
1 parent 5c17b68 commit f6d7543
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
5 changes: 4 additions & 1 deletion core/utils/create-round-timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import { switchMap, distinctUntilKeyChanged, map } from "rxjs/operators";
import { timer } from "rxjs";
import type { Game } from "../types/game";
import type { MonoTypeOperatorFunction } from "rxjs";
import { getPhaseDuration } from "./get-phase-duration";

export function createRoundTimer(): MonoTypeOperatorFunction<Game> {
return (source) => {
return source.pipe(
distinctUntilKeyChanged("phase"),
switchMap((game) => timer(25000).pipe(map(() => game))),
switchMap((game) =>
timer(getPhaseDuration(game.phase)).pipe(map(() => game)),
),
);
};
}
12 changes: 12 additions & 0 deletions core/utils/get-phase-duration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Phase } from "../types/phase";

export function getPhaseDuration(phase: Phase): number {
switch (phase) {
case Phase.Planning:
return 5000;
case Phase.Combat:
return 30000;
default:
throw new Error(`Unknown phase: ${phase} !`);
}
}
7 changes: 5 additions & 2 deletions interface/utils/render-round-time-geometry.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { PlaneGeometry } from "three";
import type { Display } from "core/types/display";
import type { ThreeContext } from "../types/three-context";
import { getPhaseDuration } from "core/utils/get-phase-duration";

export function renderRoundTimeGeometry(
threeContext: ThreeContext,
display: Display,
): void {
const maxWidth = 0.1;

const phaseDuration = getPhaseDuration(display.phase);

const timeLeft = Math.max(
0,
new Date(display.phaseStartAt).getTime() + 30000 - Date.now(),
new Date(display.phaseStartAt).getTime() + phaseDuration - Date.now(),
);

const targetWidth = (Math.min(25000, timeLeft) / 25000) * maxWidth;
const targetWidth = (timeLeft / phaseDuration) * maxWidth;

if (threeContext.roundTimeGeometry.parameters.width !== targetWidth) {
threeContext.roundTimeGeometry = new PlaneGeometry(targetWidth, 0.004);
Expand Down
2 changes: 1 addition & 1 deletion sandbox/utils/display-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class DisplayFactory implements Subscribable<Display> {
phase: Phase.Combat,
money: 100,
levelUpCost: 40,
timeLeft: 15000,
phaseStartAt: new Date().toISOString(),
};

gui: GUI = new GUI();
Expand Down

0 comments on commit f6d7543

Please sign in to comment.