diff --git a/src/core/intent.ts b/src/core/intent.ts index 92f1e42..a23750d 100644 --- a/src/core/intent.ts +++ b/src/core/intent.ts @@ -6,8 +6,8 @@ import { tryKillTileOfState } from './kill-helpers'; import { Tool, bombIntent, copyIntent, dynamiteIntent } from './tools'; import { SelectionOperation, selectionOperationOfMods } from './selection'; import { vacuous_down, deselect } from './reduce'; -import { drawSpecificOfState, withCoreState } from './state-helpers'; -import { tileAtPoint } from './tile-helpers'; +import { checkValid, drawSpecific, withCoreState } from './state-helpers'; +import { addHandTile, tileAtPoint } from './tile-helpers'; export type KillIntent = | { t: 'kill', radius: number, cost: number } @@ -58,7 +58,7 @@ export function getIntentOfMouseDown(tool: Tool, wp: WidgetPoint, button: number export function reduceIntent(state: GameState, intent: Intent, wp: WidgetPoint): GameState { switch (intent.t) { - case 'dragTile': + case 'dragTile': { if (wp.t != 'world' && wp.t != 'hand') return vacuous_down(state, wp); const p_in_world_int = vm(wp.p_in_local, Math.floor); let state2 = state; @@ -78,6 +78,7 @@ export function reduceIntent(state: GameState, intent: Intent, wp: WidgetPoint): flipped: false, }; }); + } case 'exchangeTiles': { // FIXME: only works for world tiles right now if (wp.t != 'world') return vacuous_down(state, wp); @@ -119,11 +120,25 @@ export function reduceIntent(state: GameState, intent: Intent, wp: WidgetPoint): const hoverTile = tileAtPoint(state.coreState, wp.p_in_local); if (hoverTile == undefined) return state; - const newCs = drawSpecificOfState(state.coreState, hoverTile.letter); - if (newCs == state.coreState) return state; - return withCoreState(state, cs => produce(newCs, s => { + const res = drawSpecific(state.coreState, hoverTile.letter); + if (res == undefined) + return state; + res.cs = produce(res.cs, s => { + s.selected = undefined; s.inventory.copies--; - })); + }); + return produce(state, s => { + s.coreState = checkValid(res.cs); + s.mouseState = { + t: 'drag_tile', + orig_loc: { t: 'hand', p_in_hand_int: { x: 0, y: 0 } }, // XXX: Seems like this orig_loc being wrong is harmless? + id: res.newId, + orig_p_in_canvas: wp.p_in_canvas, + p_in_canvas: wp.p_in_canvas, + flipped: false, + }; + }); + } else { return state; diff --git a/src/core/state-helpers.ts b/src/core/state-helpers.ts index eb2a424..5d91c3e 100644 --- a/src/core/state-helpers.ts +++ b/src/core/state-helpers.ts @@ -65,13 +65,17 @@ export function drawOfState(state: CoreState, drawForce?: DrawForce): CoreState })); } -export function drawSpecificOfState(state: CoreState, letter: string): CoreState { +// doesn't call checkValid! +export function drawSpecific(state: CoreState, letter: string): { cs: CoreState, newId: string } | undefined { const handLength = get_hand_tiles(state).length; if (handLength >= HAND_TILE_LIMIT) - return state; - return checkValid(produce(state, s => { - addHandTile(s, ensureTileId({ letter, p_in_world_int: { x: 0, y: handLength } })); - })); + return undefined; + const tile = ensureTileId({ letter, p_in_world_int: { x: 0, y: handLength } }); + return { + cs: produce(state, s => { + addHandTile(s, tile); + }), newId: tile.id + }; } const directions: Point[] = [[1, 0], [-1, 0], [0, 1], [0, -1]].map(([x, y]) => ({ x, y }));