Skip to content

Commit

Permalink
Cleanup how timers work in GameMap. No need to call update.
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 0ab8f095609960e1e3b182583e3aa516f7b9ffdc
  • Loading branch information
cpojer committed Aug 2, 2024
1 parent c66be9c commit fe542eb
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 28 deletions.
12 changes: 3 additions & 9 deletions hera/GameMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1273,7 +1273,7 @@ export default class GameMap extends Component<Props, State> {
start: dateNow(),
timer: window.setTimeout(() => {
this._timers.delete(timerObject);
this._update(fn.call(null, this.state));
fn();
}, delay),
};
timers.add(timerObject);
Expand All @@ -1294,8 +1294,7 @@ export default class GameMap extends Component<Props, State> {
};

private _scheduleTimer = async (
// eslint-disable-next-line @typescript-eslint/ban-types
fn: Function,
fn: () => void,
delay: number,
): Promise<number> => {
const { replayState } = this.state;
Expand All @@ -1310,12 +1309,7 @@ export default class GameMap extends Component<Props, State> {

const timer = window.setTimeout(() => {
this._timers.delete(timerObject);
this._update({
...fn.call(null, this.state),
replayState: {
...this.state.replayState,
},
});
fn();
}, delay);
const timerObject = {
delay,
Expand Down
6 changes: 2 additions & 4 deletions hera/Types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ export type Props = Readonly<{

export type TimerState = Readonly<{
delay: number;
// eslint-disable-next-line @typescript-eslint/ban-types
fn: Function;
fn: () => void;
start: number;
timer: number;
}>;
Expand Down Expand Up @@ -152,8 +151,7 @@ export type StateLike = Partial<State>;

export type FactionNames = ReadonlyMap<PlayerID, string>;
export type TimerID = Promise<number>;
// eslint-disable-next-line @typescript-eslint/ban-types
export type TimerFunction = (fn: Function, delay: number) => TimerID;
export type TimerFunction = (fn: () => void, delay: number) => TimerID;
export type RequestFrameFunction = (fn: (timestamp: number) => void) => void;
export type ClearTimerFunction = (timer: number | TimerID) => void;
export type UpdateFunction = (
Expand Down
15 changes: 9 additions & 6 deletions hera/behavior/Base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,19 @@ export default class Base extends AbstractSelectBehavior {
}
}

private async showInfo(vector: Vector, state: State, actions: Actions) {
private async showInfo(
vector: Vector,
state: State,
{ scheduleTimer, update }: Actions,
) {
const { map, selectedPosition, vision } = state;
const unit = map.units.get(vector);
const info = unit?.info;
if (unit && !selectedPosition && vision.isVisible(map, vector)) {
const showAttackRadius = !unit.isCompleted() && info?.hasAttack();
this.infoTimer = await actions.scheduleTimer(
(state: State): StateLike | null => {
return {
this.infoTimer = await scheduleTimer(
() =>
update((state) => ({
namedPositions: [vector],
...(showAttackRadius && !state.radius
? {
Expand All @@ -72,8 +76,7 @@ export default class Base extends AbstractSelectBehavior {
},
}
: null),
};
},
})),
AnimationConfig.AnimationDuration * 2.5,
);
}
Expand Down
3 changes: 2 additions & 1 deletion hera/behavior/attack/clientAttackAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,15 @@ export default async function clientAttackAction(
});

return new Promise((resolve) =>
scheduleTimer(async (state: State) => {
scheduleTimer(async () => {
const response = await remoteAction;
if (isAttackAction(response.self?.actionResponse)) {
actionResponse = response.self.actionResponse;
newMap = applyActionResponse(previousMap, vision, actionResponse);
}
const newUnitA = isAttackAction(actionResponse) && actionResponse.unitA;
const directions = getAttackDirection(to, from);
let state = await update(null);
state = await attackActionAnimation(actions, state, {
attackStance: unitB.info.sprite.attackStance,
damage: unitA.health - ((newUnitA && newUnitA?.health) || 0),
Expand Down
2 changes: 1 addition & 1 deletion hera/card/BuildingCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export default memo(function BuildingCard({
position={vector}
requestFrame={requestAnimationFrame}
scheduleTimer={(fn, delay) =>
Promise.resolve(setTimeout(fn, delay))
Promise.resolve(setTimeout(fn, delay) as unknown as number)
}
size={TileSize}
/>
Expand Down
2 changes: 1 addition & 1 deletion hera/card/InlineTileList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export type SelectTileFn = (
const vision = new Vision(1);
const vector = vec(1, 1);
const scheduleTimer: TimerFunction = (fn, delay) =>
Promise.resolve(setTimeout(fn, delay));
Promise.resolve(setTimeout(fn, delay) as unknown as number);

export default function InlineTileList({
biome,
Expand Down
2 changes: 1 addition & 1 deletion hera/card/UnitCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ export default memo(function UnitCard({
position={defaultVector}
requestFrame={requestAnimationFrame}
scheduleTimer={(fn, delay) =>
Promise.resolve(setTimeout(fn, delay))
Promise.resolve(setTimeout(fn, delay) as unknown as number)
}
size={TileSize}
tile={previewMap.getTileInfo(defaultVector)}
Expand Down
2 changes: 1 addition & 1 deletion hera/lib/sleep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default async function sleep(
duration: 'short' | 'long',
) {
if (!Instant && AnimationDuration > 0) {
await new Promise((resolve) =>
await new Promise<void>((resolve) =>
scheduleTimer(
resolve,
AnimationDuration * (duration === 'short' ? 0.5 : 1.8),
Expand Down
5 changes: 1 addition & 4 deletions hera/ui/Notice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ export default function Notice(
transform: 'scale(0.9) translate3d(0, 100%, 0)',
}}
onAnimationComplete={() =>
scheduleTimer(
() => onComplete(),
animationConfig.AnimationDuration * 10,
)
scheduleTimer(onComplete, animationConfig.AnimationDuration * 10)
}
style={{
...(color
Expand Down

0 comments on commit fe542eb

Please sign in to comment.