Skip to content

Commit

Permalink
fix fast finish
Browse files Browse the repository at this point in the history
  • Loading branch information
gtanczyk committed Oct 26, 2024
1 parent 870227e commit 30f6f75
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
3 changes: 2 additions & 1 deletion games/masterplan/src/screens/battle/model/simulate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Unit } from '../../designer/designer-types';
import { MIN_TICK } from '../consts';
import { GameWorld } from '../game/game-world';
import { TerrainData } from '../game/terrain/terrain-generator';
import { initCurrentState } from '../states';
Expand Down Expand Up @@ -28,7 +29,7 @@ export function simulate(plan: Unit[], counterPlan: Unit[], terrainData: Terrain
createMasterPlan(world, -1, '#00ff00', counterPlan);

for (let i = 0; i < 60 * 1000; i += 10) {
world.update(10);
world.update(MIN_TICK);
if (i % 1000 === 0 && [0, 1].includes(world.getBalance())) {
break;
}
Expand Down
18 changes: 8 additions & 10 deletions games/masterplan/src/screens/battle/states/state-game-battle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ import {
} from '../events';
import { renderGame } from '../game/game-render';
import { VMath } from '../util/vmath';
import { LAYER_DEFAULT, EDGE_RADIUS } from '../consts';
import { LAYER_DEFAULT, EDGE_RADIUS, MIN_TICK } from '../consts';
import { dispatchCustomEvent } from '../../../../../nukes/src/events';
import { stateInit } from '../states';
import { Unit } from '../../designer/designer-types';
import { TerrainData } from '../game/terrain/terrain-generator';
import { GameWorldRender } from '../game/game-world-render';
import { aa } from '../util/arcade-audio';

export function createMasterPlan(world: GameWorld, direction: 1 | -1, color: string, definitions: Unit[]) {
const angle = (Math.PI / 2) * direction;
Expand All @@ -49,9 +50,10 @@ export function stateGameBattleInit(definitions: Unit[], definitionsEnemy: Unit[
createMasterPlan(world, -1, '#00ff00', definitionsEnemy);

const HUD = new GameHUD(world);

HUD.setNames('Player', 'Computer');

aa.setEnabled(true);

return function GameBattleInitHandler(eventType: number) {
renderGame(world, worldRender);
HUD.render(world);
Expand Down Expand Up @@ -87,19 +89,15 @@ function stateGameBattle(world: GameWorld, worldRender: GameWorldRender, HUD: Ga
}

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) {
aa.setEnabled(false);
while (world.getTime() <= 60000) {
// Max 60 seconds as per the original time limit
world.update(UPDATE_STEP);
simulationTime += UPDATE_STEP;
world.update(MIN_TICK);

const balance = world.getBalance();

// Check win conditions
if (balance === 0 || balance === 1 || simulationTime >= 60000) {
if (balance === 0 || balance === 1 || world.getTime() >= 60000) {
// Render final state
renderGame(world, worldRender);
HUD.render(world);
Expand Down
7 changes: 6 additions & 1 deletion games/masterplan/src/screens/battle/util/arcade-audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { jsfxr } from '../lib/jsfxr';
const isBrowser = typeof window !== 'undefined';
export class ArcadeAudio {
constructor() {
this.enabled = true;
this.sounds = {};
if (isBrowser) {
this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
Expand Down Expand Up @@ -34,7 +35,7 @@ export class ArcadeAudio {
}

play(key) {
if (!isBrowser) {
if (!isBrowser || !this.enabled) {
return;
}

Expand Down Expand Up @@ -67,6 +68,10 @@ export class ArcadeAudio {
}
return bytes.buffer;
}

setEnabled(enabled) {
this.enabled = enabled;
}
}

export const aa = new ArcadeAudio();
Expand Down

0 comments on commit 30f6f75

Please sign in to comment.