Skip to content

Commit

Permalink
Shortcut for grabbing top tile from hand (fix #19)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcreedcmu committed Nov 23, 2023
1 parent 948423a commit 3cb8b91
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/core/reduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { mkOverlayFrom } from './layer';
import { resolveSelection } from './selection';
import { tryReduceShortcut } from './shortcuts';
import { CoreState, GameState, HAND_TILE_LIMIT, Location, SceneState, mkGameSceneState } from './state';
import { MoveTile, addWorldTiles, bonusOfStatePoint, checkValid, drawOfState, filterExpiredAnimations, isCollision, isOccupied, isTilePinned, tileFall, unpauseState, withCoreState } from './state-helpers';
import { MoveTile, addWorldTiles, bonusOfStatePoint, checkValid, drawOfState, dropTopHandTile, filterExpiredAnimations, isCollision, isOccupied, isTilePinned, tileFall, unpauseState, withCoreState } from './state-helpers';
import { getTileId, get_hand_tiles, get_tiles, putTileInHand, putTileInWorld, putTilesInHand, removeAllTiles, setTileLoc, tileAtPoint } from "./tile-helpers";
import { bombIntent, dynamiteIntent, getCurrentTool, reduceToolSelect } from './tools';

Expand Down Expand Up @@ -266,6 +266,9 @@ function reduceGameAction(state: GameState, action: GameAction): effectful.Resul
if (action.code == 'k') {
return gs(withCoreState(state, cs => tryKillTileOfState(cs, getWidgetPoint(cs, state.mouseState.p_in_canvas), dynamiteIntent)));
}
if (action.code == 'a') {
return gs(dropTopHandTile(state));
}
if (action.code == 'S-d') {
return gs(withCoreState(state, cs => checkValid(produce(addWorldTiles(removeAllTiles(cs), debugTiles()), s => {
s.score = 1000;
Expand Down
19 changes: 18 additions & 1 deletion src/core/state-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { canvas_from_drag_tile } from "../ui/view-helpers";
import { getWidgetPoint } from "../ui/widget-helpers";
import { DEBUG, logger } from "../util/debug";
import { produce } from "../util/produce";
import * as se1 from '../util/se1';
Expand All @@ -13,7 +14,7 @@ import { DrawForce, getLetterSample } from "./distribution";
import { checkConnected, checkGridWords, mkGridOfMainTiles } from "./grid";
import { Layer, Overlay, getOverlayLayer, mkOverlayFrom, overlayAny, overlayPoints, setOverlay } from "./layer";
import { CoreState, GameState, HAND_TILE_LIMIT, Location, MouseState, Tile, TileEntity } from "./state";
import { addHandTile, addWorldTile, ensureTileId, get_hand_tiles, get_main_tiles, get_tiles } from "./tile-helpers";
import { addHandTile, addWorldTile, ensureTileId, get_hand_tiles, get_main_tiles, get_tiles, putTileInWorld, removeTile } from "./tile-helpers";

export function addWorldTiles(state: CoreState, tiles: Tile[]): CoreState {
return produce(state, s => {
Expand Down Expand Up @@ -169,3 +170,19 @@ export function withCoreState(state: GameState, k: (cs: CoreState) => CoreState)
s.coreState = ncs;
});
}

export function dropTopHandTile(state: GameState): GameState {
const cs = state.coreState;
const handTiles = get_hand_tiles(cs);
if (handTiles.length == 0) {
return state;
}
const tile = handTiles[0];
if (state.mouseState.t == 'up' && getWidgetPoint(cs, state.mouseState.p_in_canvas).t == 'world') {
const p_in_world_int = pointFall(cs, state.mouseState.p_in_canvas);
if (!isOccupied(cs, { id: tile.id, letter: tile.letter, p_in_world_int })) {
return withCoreState(state, cs => checkValid(putTileInWorld(cs, tile.id, p_in_world_int)));
}
}
return state;
}

0 comments on commit 3cb8b91

Please sign in to comment.