Skip to content

Commit

Permalink
Improve lost state (#32)
Browse files Browse the repository at this point in the history
Now you can at least pan around.
  • Loading branch information
jcreedcmu committed Nov 12, 2023
1 parent 496a2a0 commit 1ba45e2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
12 changes: 10 additions & 2 deletions src/core/reduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ function reduceMouseDownInWorld(state: GameState, wp: WidgetPoint & { t: 'world'
}

function reduceMouseDownInHand(state: GameState, wp: WidgetPoint & { t: 'hand' }, button: number, mods: Set<string>): GameState {
if (state.lost)
return vacuous_down(state, wp);

const p_in_hand_int = vm(wp.p_in_local, Math.floor);
const tiles = get_hand_tiles(state);
const tool = currentTool(state);
Expand Down Expand Up @@ -326,7 +329,10 @@ function reduceGameAction(state: GameState, action: GameAction): effectful.Resul
}));
}
case 'mouseDown': {
return gs(reduceMouseDown(state, getWidgetPoint(state, action.p), action.button, action.mods));
const wp = getWidgetPoint(state, action.p);
if (state.lost && wp.t == 'pauseButton')
return { state: { t: 'menu' }, effects: [] };
return gs(reduceMouseDown(state, wp, action.button, action.mods));
}
case 'mouseUp': return gs(resolveMouseup(state));
case 'mouseMove': return gs(produce(state, s => {
Expand All @@ -340,7 +346,9 @@ function reduceGameAction(state: GameState, action: GameAction): effectful.Resul
});
if (state.panic !== undefined) {
if (getPanicFraction(state.panic) > 1) {
return { state: { t: 'menu' }, effects: [] };
return gs(produce(state, s => {
s.lost = true;
}));
}
return gs(produce(state, s => { s.panic!.currentTime = now; }));
}
Expand Down
2 changes: 2 additions & 0 deletions src/core/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export type GameState = {
bonusOverlay: Overlay<Bonus>,
selected?: SelectionState,
score: number,
lost: boolean,
panic: PanicData | undefined,
paused: PauseData | undefined,
};
Expand Down Expand Up @@ -121,6 +122,7 @@ export function mkGameState(seed?: number): GameState {
bonusLayer: mkLayer(bonusGenerator),
bonusOverlay: mkOverlay<Bonus>(),
score: 0,
lost: false,
panic: undefined,
paused: undefined,
};
Expand Down
3 changes: 3 additions & 0 deletions src/core/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export function indexOfTool(tool: Tool): number {
}

export function currentTool(state: GameState): Tool {
if (state.lost) {
return 'hand';
}
// state should be responsible for maintaining the invariant that
// tool index is always valid. Maybe instead I should store Tool in
// state, not toolIndex, idk.
Expand Down
1 change: 1 addition & 0 deletions src/ui/instructions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ function exampleState(): GameState {
}
},
score: 7,
lost: false,
panic: { currentTime: Date.now(), lastClear: Date.now() - PANIC_INTERVAL_MS / 3 },
paused: undefined,
};
Expand Down
25 changes: 21 additions & 4 deletions src/ui/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,17 @@ export function rawPaint(ci: CanvasInfo, state: GameState) {
{ p: { x: 0, y: S * state.toolIndex }, sz: { x: S, y: S } }
);
fillRect(d, rect_in_canvas, 'rgba(255, 255, 0, 0.5)');
}

function drawPauseButton() {
d.textAlign = 'center';
d.textBaseline = 'middle';
fillText(d, "⏸", midpointOfRect(pause_button_bds_in_canvas), 'black', '48px sans-serif');
if (!state.lost) {
fillText(d, "⏸", midpointOfRect(pause_button_bds_in_canvas), 'black', '48px sans-serif');
}
else {
fillText(d, "⟳", midpointOfRect(pause_button_bds_in_canvas), 'black', '48px sans-serif');
}
}

function drawHand() {
Expand Down Expand Up @@ -280,11 +287,21 @@ export function rawPaint(ci: CanvasInfo, state: GameState) {
});
}

drawToolbar();
if (!state.lost)
drawToolbar();
else {
fillRect(d, toolbar_bds_in_canvas, backgroundGray);
}
drawPauseButton();
drawWorld();
drawHand();
drawOtherUi();
drawAnimations(Date.now());
if (!state.lost) {
drawOtherUi();
drawAnimations(Date.now());
}
else {
fillText(d, "You lost :(", midpointOfRect(canvas_bds_in_canvas), 'rgba(0,0,0,0.3)', '96px sans-serif');
}
}

export class RenderPane {
Expand Down

0 comments on commit 1ba45e2

Please sign in to comment.