Skip to content

Commit

Permalink
Fog-of-war finished (fix #76)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcreedcmu committed Feb 18, 2024
1 parent 128e503 commit 463b053
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
Binary file modified public/assets/sprites.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions src/core/layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,9 @@ export function mkOverlayFrom(points: Point[]): Overlay<boolean> {
});
return layer;
}

export function combineOverlay<T>(layer1: Overlay<T>, layer2: Overlay<T>): Overlay<T> {
return {
cells: { ...layer1.cells, ...layer2.cells }
}
}
45 changes: 41 additions & 4 deletions src/core/state-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import { produce } from "../util/produce";
import * as se1 from '../util/se1';
import { SE2, apply, compose, inverse } from '../util/se2';
import { Point } from "../util/types";
import { vadd, vequal, vm } from "../util/vutil";
import { vadd, vequal, vm, vsnorm } from "../util/vutil";
import { Animation, mkPointDecayAnimation, mkScoreAnimation, mkWinAnimation } from './animations';
import { getAssets } from "./assets";
import { ScoringBonus, adjacentScoringOfBonus, isBlocking, isBlockingPoint, overlapScoringOfBonus, resolveScoring } from "./bonus";
import { getBonusFromLayer, updateBonusLayer } from "./bonus-helpers";
import { BIT_CONNECTED } from "./chunk";
import { BIT_CONNECTED, BIT_VISIBLE } from "./chunk";
import { PANIC_INTERVAL_MS, PanicData, PauseData, WORD_BONUS_INTERVAL_MS, now_in_game } from "./clock";
import { DrawForce, getLetterSample } from "./distribution";
import { checkConnected, checkGridWords, gridKeys, mkGridOfMainTiles } from "./grid";
import { resolveLandResult } from "./landing-resolve";
import { LandingResult, landMoveOnState } from "./landing-result";
import { mkOverlayFrom, overlayAny, overlayPoints, setOverlay } from "./layer";
import { Overlay, combineOverlay, getOverlay, mkOverlay, mkOverlayFrom, overlayAny, overlayPoints, setOverlay } from "./layer";
import { addRandomMob, collidesWithMob } from "./mobs";
import { PROGRESS_ANIMATION_POINTS, getHighWaterMark, getScore, setHighWaterMark } from "./scoring";
import { CacheUpdate, CoreState, GameState, HAND_TILE_LIMIT, Location, MainLoc, MobileEntity, MouseState, MoveMobileNoId, RenderableMobile, Scoring, Tile, TileEntity, WordBonusState } from "./state";
Expand All @@ -25,6 +25,8 @@ import { ensureTileId } from "./tile-id-helpers";
import { getCurrentTool } from "./tools";
import { WIN_SCORE, canWinFromState, shouldStartPanicBar } from "./winState";

const PLACED_MOBILE_SEEN_CELLS_RADIUS = 2.5;

export function addWorldTiles(state: CoreState, tiles: Tile[]): CoreState {
return produce(state, s => {
tiles.forEach(tile => {
Expand Down Expand Up @@ -147,6 +149,36 @@ export function freshPanic(state: CoreState): PanicData {
return { currentTime_in_game, lastClear_in_game: currentTime_in_game - debug_offset };
}

function updateFogOfWar(state: CoreState): CoreState {
const rad = PLACED_MOBILE_SEEN_CELLS_RADIUS;
const irad = Math.ceil(rad);
const updates: Point[] = [];
const recentlySeen: Overlay<boolean> = mkOverlay();
get_mobiles(state).forEach(({ loc }) => {
if (loc.t == 'world') {
const { p_in_world_int: center } = loc;
for (let x = -irad; x <= irad; x++) {
for (let y = -irad; y <= irad; y++) {
const off = { x, y };
const p_in_world_int = vadd(center, off);
if (vsnorm(off) <= rad * rad && !getOverlay(state.seen_cells, p_in_world_int) && !getOverlay(recentlySeen, p_in_world_int)) {
setOverlay(recentlySeen, p_in_world_int, true);
updates.push(p_in_world_int);
}
}
}
}
});

const cacheUpdates: CacheUpdate[] = updates.map(p => ({ p_in_world_int: p, chunkUpdate: { t: 'setBit', bit: BIT_VISIBLE } }));
const combined = combineOverlay(state.seen_cells, recentlySeen);
return produce(state, s => {
s._cacheUpdateQueue.push(...cacheUpdates);
s.seen_cells = combined;
});
}


export function checkValid(state: CoreState): CoreState {
const tiles = get_main_tiles(state);
const grid = mkGridOfMainTiles(tiles);
Expand Down Expand Up @@ -188,7 +220,7 @@ export function checkValid(state: CoreState): CoreState {
chunkUpdate: { t: 'setBit', bit: BIT_CONNECTED }
}));

return produce(state, s => {
state = produce(state, s => {
s.panic = panic;
s.slowState.invalidWords = invalidWords;
s.connectedSet = connectedSet;
Expand All @@ -201,6 +233,11 @@ export function checkValid(state: CoreState): CoreState {
if (getCurrentTool(state) != 'dynamite')
s.slowState.currentTool = 'pointer';
});

if (allValid)
state = updateFogOfWar(state);

return state;
}


Expand Down
1 change: 1 addition & 0 deletions src/ui/instructions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ function exampleState(): GameState {
_cacheUpdateQueue: [],
animations: [],
mobile_entities: {},
seen_cells: mkOverlay(),
connectedSet: mkGridOf([]),
energies: {
byLetter: [
Expand Down

0 comments on commit 463b053

Please sign in to comment.