Copyright | Brent Yorgey |
---|---|
License | BSD-3-Clause |
Maintainer | byorgey@gmail.com |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
A world refers to the grid on which the game takes place, and the things in it (besides robots). A world has a base, immutable terrain layer, where each cell contains a terrain type, and a mutable entity layer, with at most one entity per cell.
A world is technically finite but practically infinite (worlds are indexed by 64-bit signed integers, so they correspond to a \( 2^{64} \times 2^{64} \) torus).
Synopsis
- newtype Coords = Coords {}
- locToCoords :: V2 Int64 -> Coords
- coordsToLoc :: Coords -> V2 Int64
- newtype WorldFun t e = WF {}
- worldFunFromArray :: Array (Int64, Int64) (t, Maybe e) -> (t, Maybe e) -> WorldFun t e
- data World t e
- loadCell :: IArray UArray t => Coords -> World t e -> World t e
- loadRegion :: forall t e. IArray UArray t => (Coords, Coords) -> World t e -> World t e
- newWorld :: WorldFun t e -> World t e
- emptyWorld :: t -> World t e
- lookupTerrain :: IArray UArray t => Coords -> World t e -> t
- lookupEntity :: Coords -> World t e -> Maybe e
- update :: Coords -> (Maybe e -> Maybe e) -> World t e -> World t e
- lookupTerrainM :: forall t e sig m. (Has (State (World t e)) sig m, IArray UArray t) => Coords -> m t
- lookupEntityM :: forall t e sig m. (Has (State (World t e)) sig m, IArray UArray t) => Coords -> m (Maybe e)
- updateM :: forall t e sig m. (Has (State (World t e)) sig m, IArray UArray t) => Coords -> (Maybe e -> Maybe e) -> m ()
World coordinates
World coordinates use (row,column) format, with the row increasing as we move down the screen. This format plays nicely with drawing the screen.
Instances
Worlds
A WorldFun t e
represents a 2D world with terrain of type t
(exactly one per cell) and entities of type e
(at most one per
cell).
worldFunFromArray :: Array (Int64, Int64) (t, Maybe e) -> (t, Maybe e) -> WorldFun t e Source #
Create a world function from a finite array of specified cells plus a single default cell to use everywhere else.
A World
consists of a WorldFun
that specifies the initial
world, a cache of loaded square tiles to make lookups faster, and
a map storing locations whose entities have changed from their
initial values.
Right now the World
simply holds on to all the tiles it has
ever loaded. Ideally it would use some kind of LRU caching
scheme to keep memory usage bounded, but it would be a bit
tricky, and in any case it's probably not going to matter much
for a while. Once tile loads can trigger robots to spawn, it
would also make for some difficult decisions in terms of how to
handle respawning.
Tile management
loadCell :: IArray UArray t => Coords -> World t e -> World t e Source #
Load the tile containing a specific cell.
loadRegion :: forall t e. IArray UArray t => (Coords, Coords) -> World t e -> World t e Source #
Load all the tiles which overlap the given rectangular region (specified as an upper-left and lower-right corner).
World functions
emptyWorld :: t -> World t e Source #
Create a new empty World
consisting of nothing but the given
terrain.
lookupTerrain :: IArray UArray t => Coords -> World t e -> t Source #
Look up the terrain value at certain coordinates: try looking it
up in the tile cache first, and fall back to running the WorldFun
otherwise.
This function does not ensure that the tile containing the
given coordinates is loaded. For that, see lookupTerrainM
.
lookupEntity :: Coords -> World t e -> Maybe e Source #
Look up the entity at certain coordinates: first, see if it is in
the map of locations with changed entities; then try looking it
up in the tile cache first; and finally fall back to running the
WorldFun
.
This function does not ensure that the tile containing the
given coordinates is loaded. For that, see lookupEntityM
.
Monadic variants
lookupTerrainM :: forall t e sig m. (Has (State (World t e)) sig m, IArray UArray t) => Coords -> m t Source #
A stateful variant of lookupTerrain
, which first loads the tile
containing the given coordinates if it is not already loaded,
then looks up the terrain value.