Skip to content

Commit

Permalink
Can move logs around
Browse files Browse the repository at this point in the history
  • Loading branch information
jcreedcmu committed Feb 10, 2024
1 parent 3b2e176 commit 92bddf2
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 29 deletions.
22 changes: 12 additions & 10 deletions src/core/intent.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { WidgetPoint } from '../ui/widget-helpers';
import { produce } from '../util/produce';
import { Point } from '../util/types';
import { vint, vm } from '../util/vutil';
import { vm } from '../util/vutil';
import { Bonus } from './bonus';
import { tryKillTileOfState } from './kill-helpers';
import { vacuous_down } from './low-actions';
import { SelectionOperation, deselect, selectionOperationOfMods } from './selection';
import { CacheUpdate, GameState } from './state';
import { checkValid, drawSpecific, withCoreState } from './state-helpers';
import { CellContents, getMobileLoc, tileAtPoint } from './tile-helpers';
import { CellContents, getMobileLoc, mobileAtPoint } from './tile-helpers';
import { Tool, bombIntent, copyIntent, dynamiteIntent } from './tools';

export type KillIntent =
Expand All @@ -28,7 +28,7 @@ export type Intent =
;

function dynamiteableCell(cell: CellContents): boolean {
return cell.t == 'tile' || (cell.t == 'bonus' && killableBonus(dynamiteIntent, cell.bonus));
return cell.t == 'mobile' || (cell.t == 'bonus' && killableBonus(dynamiteIntent, cell.bonus));
}

export function killableBonus(intent: KillIntent, bonus: Bonus): boolean {
Expand All @@ -48,15 +48,15 @@ export function getIntentOfMouseDown(tool: Tool, wp: WidgetPoint, button: number

switch (tool) {
case 'pointer':
if (hoverCell.t == 'tile') {
const hoverTile = hoverCell.tile;
if (hoverCell.t == 'mobile') {
const hoverMobile = hoverCell.mobile;
if (pinned)
return { t: 'panWorld' };
if (mods.has('meta')) {
return { t: 'exchangeTiles', id: hoverTile.id };
return { t: 'exchangeTiles', id: hoverMobile.id };
}
else {
return { t: 'dragTile', id: hoverTile.id };
return { t: 'dragTile', id: hoverMobile.id };
}
}
return { t: 'startSelection', opn: selectionOperationOfMods(mods) };
Expand Down Expand Up @@ -158,10 +158,12 @@ export function reduceIntent(state: GameState, intent: Intent, wp: WidgetPoint):
});
case 'copy': {
if (wp.t == 'world') {
const hoverTile = tileAtPoint(state.coreState, wp.p_in_local);
if (hoverTile == undefined)
const hoverMobile = mobileAtPoint(state.coreState, wp.p_in_local);
if (hoverMobile == undefined)
return state;
const res = drawSpecific(state.coreState, hoverTile.letter);
if (hoverMobile.t != 'tile')
return state;
const res = drawSpecific(state.coreState, hoverMobile.letter);
if (res == undefined)
return state;
let newCs = produce(deselect(res.cs), s => {
Expand Down
12 changes: 6 additions & 6 deletions src/core/low-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import { reduceKey } from './reduceKey';
import { incrementScore, setScore } from './scoring';
import { deselect, resolveSelection, setSelected } from './selection';
import { CacheUpdate, CoreState, GameState, Location, MobsState, SceneState } from './state';
import { MoveMobile, addWorldTiles, checkValid, drawOfState, dropTopHandTile, filterExpiredAnimations, filterExpiredWordBonusState, isOccupied, isOccupiedForMobiles, isTilePinned, needsRefresh, pointFall, proposedHandDragOverLimit, tileFall, unpauseState, withCoreState } from './state-helpers';
import { addResourceMobile, cellAtPoint, getMobileId, getMobileLoc, getRenderableMobile, get_hand_tiles, get_mobiles, moveTiles, moveToHandLoc, putMobilesInWorld, putTileInHand, putTilesInHandFromNotHand, removeAllMobiles, tileAtPoint } from "./tile-helpers";
import { MoveMobile, addWorldTiles, checkValid, drawOfState, dropTopHandTile, filterExpiredAnimations, filterExpiredWordBonusState, isMobilePinned, isOccupied, isOccupiedForMobiles, needsRefresh, pointFall, proposedHandDragOverLimit, tileFall, unpauseState, withCoreState } from './state-helpers';
import { addResourceMobile, cellAtPoint, getMobileId, getMobileLoc, getRenderableMobile, get_hand_tiles, get_mobiles, mobileAtPoint, moveTiles, moveToHandLoc, putMobilesInWorld, putTileInHand, putTilesInHandFromNotHand, removeAllMobiles } from "./tile-helpers";
import { bombIntent, dynamiteIntent, fillWaterIntent, getCurrentTool, reduceToolSelect, toolPrecondition } from './tools';
import { shouldDisplayBackButton } from './winState';

Expand Down Expand Up @@ -51,7 +51,7 @@ function reduceMouseDownInWorld(state: GameState, wp: WidgetPoint & { t: 'world'
const p_in_world_int = vm(wp.p_in_local, Math.floor);
const hoverCell = cellAtPoint(state.coreState, p_in_world_int);
let pinned =
(hoverCell.t == 'tile' && hoverCell.tile.loc.t == 'world') ? isTilePinned(state.coreState, hoverCell.tile.id, hoverCell.tile.loc) : false;
(hoverCell.t == 'mobile' && hoverCell.mobile.loc.t == 'world') ? isMobilePinned(state.coreState, hoverCell.mobile.id, hoverCell.mobile.loc) : false;
const intent = getIntentOfMouseDown(getCurrentTool(state.coreState), wp, button, mods, hoverCell, pinned);
return { t: 'mouseDownIntent', intent, wp };
}
Expand Down Expand Up @@ -280,10 +280,10 @@ function resolveMouseupInner(state: GameState, p_in_canvas: Point): GameLowActio
return { t: 'none' };
const id0 = ms.id;
const loc0 = getMobileLoc(state.coreState, id0);
const tile = tileAtPoint(state.coreState, wp.p_in_local);
if (tile == undefined)
const mobile = mobileAtPoint(state.coreState, wp.p_in_local);
if (mobile == undefined)
return { t: 'none' };
const id1 = tile.id;
const id1 = mobile.id;
const loc1 = getMobileLoc(state.coreState, id1);
return {
t: 'multiple', actions: [
Expand Down
4 changes: 2 additions & 2 deletions src/core/state-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ export function checkValid(state: CoreState): CoreState {
}


export function isTilePinned(state: CoreState, tileId: string, loc: MainLoc): boolean {
if (state.selected && state.selected.selectedIds.includes(tileId)) {
export function isMobilePinned(state: CoreState, id: string, loc: MainLoc): boolean {
if (state.selected && state.selected.selectedIds.includes(id)) {
return overlayAny(state.selected.overlay, p => vequal(p, { x: 0, y: 0 }));
}
else {
Expand Down
18 changes: 9 additions & 9 deletions src/core/tile-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,23 +255,23 @@ export function isSelectedForDrag(state: GameState, tile: TileEntity): boolean {
}

export type CellContents =
| { t: 'tile', tile: TileEntity }
| { t: 'mobile', mobile: MobileEntity }
| { t: 'bonus', bonus: Bonus }
;

export function cellAtPoint(state: CoreState, p_in_world: Point): CellContents {
const tile = tileAtPoint(state, p_in_world);
if (tile !== undefined)
return { t: 'tile', tile };
const mobile = mobileAtPoint(state, p_in_world);
if (mobile !== undefined)
return { t: 'mobile', mobile };
return { t: 'bonus', bonus: getBonusFromLayer(state, p_in_world) };
}

export function tileAtPoint(state: CoreState, p_in_world: Point): TileEntity | undefined {
let hoverTile: TileEntity | undefined = undefined;
export function mobileAtPoint(state: CoreState, p_in_world: Point): MobileEntity | undefined {
let hoverMobile: MobileEntity | undefined = undefined;
const p_in_world_int = vm(p_in_world, Math.floor);
for (const tile of get_main_tiles(state)) {
if (vequal(p_in_world_int, tile.loc.p_in_world_int)) {
return tile;
for (const mobile of get_mobiles(state)) {
if (mobile.loc.t == 'world' && vequal(p_in_world_int, mobile.loc.p_in_world_int)) {
return mobile;
}
}
return undefined;
Expand Down
36 changes: 34 additions & 2 deletions src/ui/gl-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Point } from "../util/types";
import { pixelSnapRect, rectPts } from "../util/util";
import { vdiag } from "../util/vutil";
import { gl_from_canvas } from "./gl-helpers";
import { spriteLocOfRes } from "./sprite-sheet";
import { canvas_bds_in_canvas } from "./widget-helpers";

export const SPRITE_TEXTURE_UNIT = 0;
Expand Down Expand Up @@ -242,14 +243,45 @@ export function drawOneMobile(env: GlEnv, m: RenderableMobile, canvas_from_tile:
gl.uniform2f(u_canvasSize, devicePixelRatio * canvas_bds_in_canvas.sz.x, devicePixelRatio * canvas_bds_in_canvas.sz.y);

// The uniform from common.frag is called world_from_canvas, but this is a lie for this shader.
// We're actually supplying tile_from_canvas.
// We're actually supplying cell_from_canvas.
const u_world_from_canvas = gl.getUniformLocation(prog, "u_world_from_canvas");
gl.uniformMatrix3fv(u_world_from_canvas, false, asMatrix(inverse(compose(scale(vdiag(devicePixelRatio)), canvas_from_tile))));

gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
}
else {
throw new Error('Unimplemented'); // RESOURCE TODO
const { gl } = env;
const { prog, position } = env.spriteDrawer;
gl.useProgram(prog);

const chunk_rect_in_canvas = pixelSnapRect(apply_to_rect(canvas_from_tile, { p: vdiag(0), sz: { x: 1, y: 1 } }));
const chunk_rect_in_gl = apply_to_rect(gl_from_canvas, chunk_rect_in_canvas);

const [p1, p2] = rectPts(chunk_rect_in_gl);
bufferSetFloats(gl, position, [
p1.x, p2.y,
p2.x, p2.y,
p1.x, p1.y,
p2.x, p1.y,
]);

const spriteLoc = spriteLocOfRes(m.res);

const u_spriteLoc = gl.getUniformLocation(prog, 'u_spriteLoc');
gl.uniform2i(u_spriteLoc, spriteLoc.x, spriteLoc.y);

const u_spriteTexture = gl.getUniformLocation(prog, 'u_spriteTexture');
gl.uniform1i(u_spriteTexture, SPRITE_TEXTURE_UNIT);

const u_canvasSize = gl.getUniformLocation(prog, 'u_canvasSize');
gl.uniform2f(u_canvasSize, devicePixelRatio * canvas_bds_in_canvas.sz.x, devicePixelRatio * canvas_bds_in_canvas.sz.y);

// The uniform from common.frag is called world_from_canvas, but this is a lie for this shader.
// We're actually supplying cell_from_canvas.
const u_world_from_canvas = gl.getUniformLocation(prog, "u_world_from_canvas");
gl.uniformMatrix3fv(u_world_from_canvas, false, asMatrix(inverse(compose(scale(vdiag(devicePixelRatio)), canvas_from_tile))));

gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
}
}

Expand Down

0 comments on commit 92bddf2

Please sign in to comment.