Skip to content

Commit

Permalink
Move WinState into SlowState (fix #160)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcreedcmu committed Feb 5, 2024
1 parent edd9321 commit 814aa30
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/core/low-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function reduceMouseDownInWorld(state: GameState, wp: WidgetPoint & { t: 'world'
}

function reduceMouseDownInHand(state: GameState, wp: WidgetPoint & { t: 'hand' }, button: number, mods: Set<string>): GameLowAction {
if (state.coreState.winState.t == 'lost')
if (state.coreState.slowState.winState.t == 'lost')
return { t: 'vacuousDown', wp }

const index = wp.index;
Expand Down Expand Up @@ -292,7 +292,7 @@ export function getLowAction(state: GameState, action: GameAction): LowAction {
return { t: 'gameLowAction', action: { t: 'zoom', amount: action.delta, center: action.p } };
case 'mouseDown': {
const wp = getWidgetPoint(state.coreState, action.p);
if (wp.t == 'pauseButton' && shouldDisplayBackButton(state.coreState.winState))
if (wp.t == 'pauseButton' && shouldDisplayBackButton(state.coreState.slowState.winState))
return { t: 'returnToMenu' };
return gla(reduceMouseDown(state, wp, action.button, action.mods));
}
Expand Down Expand Up @@ -396,7 +396,7 @@ function resolveGameLowAction(state: GameState, action: GameLowAction): GameStat
if (cs.panic !== undefined) {
if (getPanicFraction(cs.panic, cs.game_from_clock) > 1) {
return produce(state, s => {
s.coreState.winState = { t: 'lost' };
s.coreState.slowState.winState = { t: 'lost' };
});
}

Expand Down
8 changes: 4 additions & 4 deletions src/core/state-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,14 @@ export function checkValid(state: CoreState): CoreState {
let panic = state.panic;
if (allValid)
panic = undefined;
else if (panic === undefined && shouldStartPanicBar(state.winState)) {
else if (panic === undefined && shouldStartPanicBar(state.slowState.winState)) {
panic = freshPanic(state);
}

let winState = state.winState;
let winState = state.slowState.winState;
let animations = state.animations;

if (getScore(state) >= WIN_SCORE && canWinFromState(state.winState)) {
if (getScore(state) >= WIN_SCORE && canWinFromState(state.slowState.winState)) {
winState = { t: 'won', winTime_in_game: now_in_game(state.game_from_clock) };
animations = [...animations, mkWinAnimation(state.game_from_clock)];
}
Expand All @@ -237,7 +237,7 @@ export function checkValid(state: CoreState): CoreState {
s.panic = panic;
s.slowState.invalidWords = invalidWords;
s.connectedSet = connectedSet;
s.winState = winState;
s.slowState.winState = winState;
s.animations = animations;
s._cacheUpdateQueue.push(...oldCacheUpdates);
s._cacheUpdateQueue.push(...newCacheUpdates);
Expand Down
4 changes: 2 additions & 2 deletions src/core/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export type SlowState = {
invalidWords: LocatedWord[],
renderToGl: boolean,
paused: PauseData | undefined,
winState: WinState,
}

export type CacheUpdate = {
Expand All @@ -147,7 +148,6 @@ export type CoreState = {
seed: number, // changes during game play, determines which letters are drawn
bonusOverlay: Overlay<Bonus>,
selected?: SelectionState,
winState: WinState,
panic: PanicData | undefined,
game_from_clock: SE1,
wordBonusState: WordBonusState,
Expand Down Expand Up @@ -192,6 +192,7 @@ export function mkGameState(seed: number, creative: boolean, bonusLayerSeed: num
invalidWords: [],
renderToGl: true,
paused: undefined,
winState: { t: creative ? 'creative' : 'playing' },
},
wordBonusState: {
shown: undefined,
Expand All @@ -211,7 +212,6 @@ export function mkGameState(seed: number, creative: boolean, bonusLayerSeed: num
connectedSet: mkGridOf([]),
energies: initialEnergies(),
seed,
winState: { t: creative ? 'creative' : 'playing' },
panic: undefined,
game_from_clock: se1.translate(-Date.now()),
bonusLayerSeed,
Expand Down
4 changes: 2 additions & 2 deletions src/core/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function indexOfTool(tool: Tool): number {
}

export function getCurrentTool(state: CoreState): Tool {
if (state.winState.t == 'lost') {
if (state.slowState.winState.t == 'lost') {
return 'hand';
}
return state.slowState.currentTool;
Expand All @@ -46,7 +46,7 @@ export const bombIntent: Intent & { t: 'bomb' } = { t: 'bomb' };
export const copyIntent: Intent & { t: 'copy' } = { t: 'copy' };

export function getCurrentTools(state: CoreState): Tool[] {
if (state.winState.t == 'lost') {
if (state.slowState.winState.t == 'lost') {
return [];
}
const tools: Tool[] = ['pointer', 'hand'];
Expand Down
2 changes: 1 addition & 1 deletion src/ui/gl-render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export function renderGlPane(ci: CanvasGlInfo, env: GlEnv, state: GameState): vo

// draw panic bar
if (!state.coreState.slowState.paused) {
if (state.coreState.winState.t != 'lost' && state.coreState.panic) {
if (state.coreState.slowState.winState.t != 'lost' && state.coreState.panic) {
const rr = renderPanicBar(state.coreState.panic, state.coreState.game_from_clock);
glFillRect(env, panic_bds_in_canvas, [0, 0, 0]);
glFillRect(env, rr.rect, rr.color);
Expand Down
2 changes: 1 addition & 1 deletion src/ui/instructions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ function exampleState(): GameState {
invalidWords: [],
renderToGl: true,
paused: undefined,
winState: { t: 'playing' },
},
wordBonusState: {
shown: undefined,
Expand Down Expand Up @@ -196,7 +197,6 @@ function exampleState(): GameState {

}
},
winState: { t: 'playing' },
panic: { currentTime_in_game: Date.now(), lastClear_in_game: Date.now() - PANIC_INTERVAL_MS / 3 },
mobsState: { mobs: [] },
},
Expand Down
12 changes: 6 additions & 6 deletions src/ui/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ export function rawPaint(ci: CanvasInfo, state: GameState, glEnabled: boolean) {
}

function drawPauseButton() {
if (shouldDisplayBackButton(cs.winState)) {
if (shouldDisplayBackButton(cs.slowState.winState)) {
d.textAlign = 'center';
d.textBaseline = 'middle';
fillText(d, '⟳', midpointOfRect(pause_button_bds_in_canvas), 'white', '48px sans-serif');
Expand All @@ -327,7 +327,7 @@ export function rawPaint(ci: CanvasInfo, state: GameState, glEnabled: boolean) {
function drawShuffleButton() {
d.textAlign = 'center';
d.textBaseline = 'middle';
if (cs.winState.t != 'lost') {
if (cs.slowState.winState.t != 'lost') {
fillText(d, '🔀', midpointOfRect(shuffle_button_bds_in_canvas), 'white', '36px sans-serif');
}
}
Expand Down Expand Up @@ -497,7 +497,7 @@ export function rawPaint(ci: CanvasInfo, state: GameState, glEnabled: boolean) {

clearRect(d, world_bds_in_canvas);

if (cs.winState.t != 'lost')
if (cs.slowState.winState.t != 'lost')
drawToolbar(d, cs);
else {
fillRect(d, toolbar_bds_in_canvas, backgroundGray);
Expand Down Expand Up @@ -526,7 +526,7 @@ export function rawPaint(ci: CanvasInfo, state: GameState, glEnabled: boolean) {
}
drawShuffleButton();
const mp = midpointOfRect(canvas_bds_in_canvas);
if (cs.winState.t == 'lost') {
if (cs.slowState.winState.t == 'lost') {
d.textAlign = 'center';
d.textBaseline = 'middle';
fillText(d, 'You lost :(', mp, 'rgba(0,0,0,0.3)', '96px sans-serif');
Expand All @@ -535,10 +535,10 @@ export function rawPaint(ci: CanvasInfo, state: GameState, glEnabled: boolean) {
drawOtherUi(glEnabled);
drawAnimations(now_in_game(cs.game_from_clock), glEnabled);
}
if (cs.winState.t == 'won') {
if (cs.slowState.winState.t == 'won') {
d.textAlign = 'center';
d.textBaseline = 'middle';
fillText(d, `Time: ${formatTime(cs.winState.winTime_in_game)}`, { x: mp.x, y: canvas_bds_in_canvas.sz.y - 12 }, 'black', 'bold 24px sans-serif');
fillText(d, `Time: ${formatTime(cs.slowState.winState.winTime_in_game)}`, { x: mp.x, y: canvas_bds_in_canvas.sz.y - 12 }, 'black', 'bold 24px sans-serif');
}
}

Expand Down

0 comments on commit 814aa30

Please sign in to comment.