Skip to content

Commit

Permalink
fast finish
Browse files Browse the repository at this point in the history
  • Loading branch information
gtanczyk committed Oct 25, 2024
1 parent 4ae345c commit b93e9d1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
6 changes: 6 additions & 0 deletions games/masterplan/src/screens/battle/battle-screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
EVENT_BATTLE_PAUSE,
EVENT_BATTLE_RESUME,
EVENT_BATTLE_STOP,
EVENT_BATTLE_FAST_FINISH,
} from './events';
import { useRafLoop } from 'react-use';
import { BattleControls } from './battle-controls';
Expand Down Expand Up @@ -107,6 +108,10 @@ export function BattleScreen({
onBattleEnd();
};

const handleFastFinish = () => {
setTimeout(() => updateState(EVENT_BATTLE_FAST_FINISH));
};

return (
<>
<BattleStyles />
Expand All @@ -126,6 +131,7 @@ export function BattleScreen({
<div id="battle-controls">
<button onClick={handlePauseResume}>{isPaused ? 'Resume' : 'Pause'}</button>
<button onClick={handleStop}>Exit</button>
<button onClick={handleFastFinish}>Fast Finish</button>
</div>
</div>
</div>
Expand Down
3 changes: 3 additions & 0 deletions games/masterplan/src/screens/battle/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ export const EVENT_DAMAGE_ARROW = eventCounter++;
export const EVENT_BATTLE_PAUSE = eventCounter++;
export const EVENT_BATTLE_RESUME = eventCounter++;
export const EVENT_BATTLE_STOP = eventCounter++;

// fast finish
export const EVENT_BATTLE_FAST_FINISH = eventCounter++;
27 changes: 27 additions & 0 deletions games/masterplan/src/screens/battle/states/state-game-battle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
EVENT_BATTLE_PAUSE,
EVENT_BATTLE_RESUME,
EVENT_BATTLE_STOP,
EVENT_BATTLE_FAST_FINISH,
} from '../events';
import { renderGame } from '../game/game-render';
import { VMath } from '../util/vmath';
Expand Down Expand Up @@ -85,6 +86,32 @@ function stateGameBattle(world: GameWorld, worldRender: GameWorldRender, HUD: Ga
renderGame(world, worldRender);
}

if (eventType === EVENT_BATTLE_FAST_FINISH) {
// Run simulation until completion without rendering
const UPDATE_STEP = 16; // 60fps equivalent step
let simulationTime = world.getTime();

while (simulationTime <= 60000) {
// Max 60 seconds as per the original time limit
world.update(UPDATE_STEP);
simulationTime += UPDATE_STEP;

const balance = world.getBalance();

// Check win conditions
if (balance === 0 || balance === 1 || simulationTime >= 60000) {
// Render final state
renderGame(world, worldRender);
HUD.render(world);

// Transition to end state
return stateGameBattleEnd(world, worldRender, HUD);
}
}

return stateGameBattleEnd(world, worldRender, HUD);
}

if (eventType === EVENT_INTERVAL_100MS) {
HUD.render(world);
}
Expand Down

0 comments on commit b93e9d1

Please sign in to comment.