Skip to content

Commit

Permalink
Drag with copy tool drags freshly created tile (fix #87)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcreedcmu committed Nov 23, 2023
1 parent 3a41ed8 commit b36a0dc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
29 changes: 22 additions & 7 deletions src/core/intent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 9 additions & 5 deletions src/core/state-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }));
Expand Down

0 comments on commit b36a0dc

Please sign in to comment.