Skip to content

Commit

Permalink
Cleanly separate out resbar-only resources in the types
Browse files Browse the repository at this point in the history
  • Loading branch information
jcreedcmu committed Feb 17, 2024
1 parent ea3648f commit cec3c2b
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 21 deletions.
Binary file modified public/assets/toolbar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions src/core/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { LandingMoveId } from './landing-resolve';
import { LandingResult, ProperLandingResult } from './landing-result';
import { SelectionState } from './selection';
import { InventoryItems, Location, MouseState, MoveMobile, SceneState } from './state';
import { Resource, Tool } from './tools';
import { ResbarResource, Tool } from './tools';

export type GameLowAction =
| { t: 'zoom', center: Point, amount: number }
Expand Down Expand Up @@ -47,7 +47,7 @@ export type GameLowAction =
| { t: 'restoreMobiles', ids: string[] } // put held mobiles back in cache
| { t: 'popCacheUpdateQueue', n: number }
| { t: 'addMob' }
| { t: 'startDragResource', wp: WidgetPoint, res: Resource, res_ix: number } // XXX: rename to startDragResbar or something
| { t: 'startDragResource', wp: WidgetPoint, res: ResbarResource, res_ix: number } // XXX: rename to startDragResbar or something
| { t: 'landResults', lrms: { lr: ProperLandingResult, move: LandingMoveId }[] }
;

Expand Down
4 changes: 2 additions & 2 deletions src/core/landing-resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { tryKillTileOfStateLoc } from "./kill-helpers";
import { LandingResult, ProperLandingResult } from "./landing-result";
import { CoreState, MoveMobile } from "./state";
import { addResourceMobile, mobileAtPoint, putMobileInWorld, putMobileNowhere, removeMobile } from "./tile-helpers";
import { Resource, fillWaterIntent } from "./tools";
import { ResbarResource, fillWaterIntent } from "./tools";

// A thing that can be moved onto something else
export type MoveSourceId =
| { t: 'mobile', id: string }
| { t: 'freshResource', res: Resource }
| { t: 'freshResource', res: ResbarResource }
;

export type LandingMoveId = { src: MoveSourceId, p_in_world_int: Point };
Expand Down
9 changes: 3 additions & 6 deletions src/core/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Grid, LocatedWord, mkGridOf } from './grid';
import { Overlay, mkOverlay } from './layer';
import { MobState } from './mobs';
import { SelectionOperation, SelectionState } from './selection';
import { Resource, Tool } from './tools';
import { ResbarResource, Tool, Resource } from './tools';
import { WinState } from './winState';

export type Scoring = {
Expand Down Expand Up @@ -48,7 +48,7 @@ export type MouseState =
t: 'drag_resource',
p_in_canvas: Point,
orig_p_in_canvas: Point,
res: Resource,
res: ResbarResource,
res_ix: number,
}
;
Expand Down Expand Up @@ -159,10 +159,7 @@ export type InventoryItems = {
times: number,
};

export type ResourceItems = {
wood: number,
axe: number,
};
export type ResourceItems = Record<ResbarResource, number>;

// state such that, if it updates, should induce redraw of Canvas2d content
export type SlowState = {
Expand Down
15 changes: 9 additions & 6 deletions src/core/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ const tools = [

export type Tool = (typeof tools)[number];

const resources = [
const resbarResources = [
'wood',
'axe',
'planks',
] as const;

export type Resource = (typeof resources)[number];
export type ResbarResource = (typeof resbarResources)[number];
export type WorldOnlyResource =
| 'planks'
;
export type Resource = ResbarResource | WorldOnlyResource;

export function toolOfIndex(index: number): Tool | undefined {
return tools[index];
Expand Down Expand Up @@ -78,11 +81,11 @@ export function getCurrentTools(state: CoreState): Tool[] {
return tools;
}

export function getCurrentResources(state: CoreState): Resource[] {
export function getCurrentResources(state: CoreState): ResbarResource[] {
if (state.slowState.winState.t == 'lost') {
return [];
}
const resources: Resource[] = [];
const resources: ResbarResource[] = [];
if (state.slowState.resource.wood > 0) {
resources.push('wood');
}
Expand All @@ -96,7 +99,7 @@ export function rectOfTool(tool: Tool): Rect {
return spriteRectOfPos(spriteLocOfTool(tool));
}

export function largeRectOf(tool: Tool | Resource): Rect {
export function largeRectOf(tool: Tool | ResbarResource): Rect {
return largeSpriteRectOfPos(largeSpriteLoc(tool));
}

Expand Down
5 changes: 2 additions & 3 deletions src/ui/sprite-sheet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Bonus } from "../core/bonus";
import { ChunkValue } from "../core/chunk";
import { MobState } from "../core/mobs";
import { LARGE_SPRITE_PIXEL_WIDTH, Resource, SPRITE_PIXEL_WIDTH, Tool } from "../core/tools";
import { LARGE_SPRITE_PIXEL_WIDTH, ResbarResource, SPRITE_PIXEL_WIDTH, Tool, Resource } from "../core/tools";
import { Buffer, buffer, fillRect } from "../util/dutil";
import { scale } from "../util/se2";
import { apply_to_rect } from "../util/se2-extra";
Expand Down Expand Up @@ -83,7 +83,7 @@ export function spriteLocOfRes(res: Resource): Point {
}
}

export function largeSpriteLoc(tool: Tool | Resource): Point {
export function largeSpriteLoc(tool: Tool | ResbarResource): Point {
switch (tool) {
case 'pointer': return { y: 0, x: 0 };
case 'hand': return { y: 0, x: 1 };
Expand All @@ -95,7 +95,6 @@ export function largeSpriteLoc(tool: Tool | Resource): Point {
case 'time': return { y: 0, x: 7 };
case 'wood': return { y: 0, x: 8 };
case 'axe': return { y: 0, x: 9 };
case 'planks': return { y: 0, x: 0 }; // XXX - this is invalid
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/ui/widget-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CoreState, HAND_TILE_LIMIT, Location } from "../core/state";
import { Resource, Tool, getCurrentResources, getCurrentTools } from "../core/tools";
import { ResbarResource, Tool, getCurrentResources, getCurrentTools } from "../core/tools";
import { SE2, apply, inverse } from "../util/se2";
import { Point, Rect } from "../util/types";
import { lerp, mapval, pixelSnapRect, pointInRect } from "../util/util";
Expand Down Expand Up @@ -161,7 +161,7 @@ export type DragWidgetPoint =
export type WidgetPoint =
| DragWidgetPoint
| { t: 'toolbar', p_in_local: Point, p_in_canvas: Point, local_from_canvas: SE2, tool: Tool }
| { t: 'resbar', p_in_local: Point, p_in_canvas: Point, local_from_canvas: SE2, res: Resource }
| { t: 'resbar', p_in_local: Point, p_in_canvas: Point, local_from_canvas: SE2, res: ResbarResource }
| { t: 'pauseButton', p_in_canvas: Point }
| { t: 'nowhere', p_in_canvas: Point } // outside canvas bounds
;
Expand Down

0 comments on commit cec3c2b

Please sign in to comment.