An automap
Open the live demo · Read the source · View on GitHub
A brush level already has a map baked into it for the monsters: the
navigation grid. Automap reuses it for the player, keeping track of which
cells the player has walked near instead of showing the whole level from the
start.
Step 1: A grid to remember #
Automap wraps a NavGrid and keeps two bitsets over it: which cells are
floor the player has seen, and which are walls beside that floor.
// Two rooms and a corridor between them.
_grid = NavGrid.bake(<Brush>[
Brush(centre: Vector3(5, 0, 5), size: Vector3(10, 1, 10)),
Brush(centre: Vector3(13, 0, 5), size: Vector3(6, 1, 2)),
Brush(centre: Vector3(21, 0, 5), size: Vector3(10, 1, 10)),
], cellSize: 0.5);
_map = Automap(_grid, revealRadius: 3.0);
Step 2: Walk near some cells #
reveal floods outward from a point, along cells an agent could actually
step between, up to revealRadius metres. It does not radiate through
walls: a room on the other side of one stays hidden until the player is near
enough on this side of the doorway.
void _walk() {
_map.reveal(Vector3(1.0, 0.0, 1.0));
_map.reveal(Vector3(3.0, 0.0, 1.0));
}
Step 3: Read what has been seen #
isFloor and isRevealed answer per cell, which is what a screen draws from.
final nearWhereWalked = _map.isFloor(_grid.cellAtPoint(2.0, 1.0));
final farAcrossTheFloor = _map.isRevealed(_grid.cellAtPoint(9.0, 9.0));
A cell near where the player walked reads as floor. A cell far across the same open room, well outside the reveal radius, is not on the map at all yet.
Step 4: Watch the map fill in #
The level on this page is two rooms and a corridor, and the orange dot walks
between them. Each frame it calls reveal at its own position, and the map
learns a little more: floor light, walls dark, the rest blank. The second room
stays blank until the dot has actually gone through the doorway. Forget the
map starts it again from nothing.
// Every frame the player stands somewhere, and the map learns of it.
_map.reveal(_walker);