La référence API provient des commentaires de documentation Go du code source et est rédigée en anglais.
package fog
import "github.com/nsgo-dev/game/pkg/fog"Vue d’ensemble
Package fog tracks fog of war for strategy games: which cells of an orthogonal, isometric, staggered or hexagonal tile grid each team has explored and which it can see right now.
A Fog holds a grid of opaque cells (walls, cliffs, forests) and a set of vision sources (units and buildings), each with a team, a cell and a radius in cells. Update recomputes every changed team's visible cells by symmetric shadowcasting from its sources, limited to each source's radius, and adds them to the cells the team has explored, which stay explored when the sources move away. A cell is therefore Unseen, Explored or Visible to each team.
Draw paints the fog over a tilemap in world coordinates under a tilemap.Camera aligned to the map's orientation (rectangles for orthogonal grids, diamonds for isometric and staggered grids, hexagons for hexagonal grids), and a Fog marshals to and from versioned JSON text, so a save.Store slot keeps a team's explored map, the sources and the opaque cells through a save and load.
Index
- Constantes
- Variables
- type Fog
- func New(width, height int, tileWidth, tileHeight float32) *Fog
- func NewFromTilemap(m *tilemap.Tilemap, mask tilemap.TileFlags) *Fog
- func (f *Fog) AddSource(s Source) SourceID
- func (f *Fog) CellAt(p core.Point) (col, row int, ok bool)
- func (f *Fog) Draw(d *core.Drawing, team Team, cam *tilemap.Camera, style Style)
- func (f *Fog) Explore(team Team, col, row int)
- func (f *Fog) ExploreAll(team Team)
- func (f *Fog) ExploreRect(team Team, col, row, w, h int)
- func (f *Fog) Explored(team Team, col, row int) bool
- func (f *Fog) ExploredCount(team Team) int
- func (f *Fog) Forget(team Team)
- func (f *Fog) Height() int
- func (f *Fog) InBounds(col, row int) bool
- func (f Fog) MarshalJSON() ([]byte, error)
- func (f *Fog) Move(id SourceID, col, row int) bool
- func (f *Fog) MoveTo(id SourceID, p core.Point) bool
- func (f *Fog) Opaque(col, row int) bool
- func (f *Fog) RemoveSource(id SourceID) bool
- func (f *Fog) Revision(team Team) uint64
- func (f *Fog) SetOpaque(col, row int, opaque bool)
- func (f *Fog) SetSource(id SourceID, s Source) bool
- func (f *Fog) Source(id SourceID) (Source, bool)
- func (f *Fog) Sources() []SourceID
- func (f *Fog) State(team Team, col, row int) State
- func (f *Fog) Teams() []Team
- func (f *Fog) UnmarshalJSON(data []byte) error
- func (f *Fog) Update() bool
- func (f *Fog) Visible(team Team, col, row int) bool
- func (f *Fog) VisibleAt(team Team, p core.Point) bool
- func (f *Fog) Width() int
- type Source
- type SourceID
- type State
- type Style
- type Team
Constantes
const FormatVersion = 1FormatVersion is the snapshot schema version MarshalJSON writes and the newest UnmarshalJSON reads.
const MaxCells = 1 << 24MaxCells is the largest number of cells (width times height) a Fog holds.
Variables
var (
// ErrInvalidSnapshot reports a fog snapshot that is malformed, has unknown fields, or disagrees with its own grid size.
ErrInvalidSnapshot = errors.New("fog: invalid snapshot")
// ErrFutureVersion reports a fog snapshot written by a newer schema version than this package reads.
ErrFutureVersion = errors.New("fog: snapshot version newer than supported")
)Types
type Fog struct {
// TileWidth and TileHeight are a cell's size in world points, used by Draw and CellAt.
TileWidth, TileHeight float32
// Origin is the world-space position of cell (0, 0)'s origin, copied from Tilemap.Origin by NewFromTilemap.
Origin core.Point
// Orientation selects how cells are arranged for visibility, CellAt and Draw.
// The zero value (tilemap.OrientationOrthogonal) preserves orthogonal behavior.
Orientation tilemap.Orientation
// StaggerAxis selects which axis is staggered for staggered isometric and hexagonal grids.
StaggerAxis tilemap.StaggerAxis
// StaggerIndex selects whether odd or even rows or columns are shifted for staggered and hexagonal grids.
StaggerIndex tilemap.StaggerIndex
// HexSideLength is the length of a hex's flat side along its staggered axis; 0 defaults to half TileWidth (StaggerX) or half TileHeight (StaggerY), matching tilemap.Tilemap.
HexSideLength float32
// contains filtered or unexported fields
}Fog is the fog of war of one map for every team: opaque cells, vision sources, and each team's visible and explored cells.
A Fog is not safe for concurrent use. Change sources and opaque cells during a fixed step, then call Update once before reading or drawing the fog.
func New(width, height int, tileWidth, tileHeight float32) *FogNew returns a Fog over a grid of width by height cells, each tileWidth by tileHeight world points, with no opaque cells, sources or teams.
New panics if width or height is negative, exactly one of them is zero, or the grid has more than MaxCells cells.
func NewFromTilemap(m *tilemap.Tilemap, mask tilemap.TileFlags) *FogNewFromTilemap returns a Fog matching m's grid size, tile size, world Origin and projection (Orientation, StaggerAxis, StaggerIndex, HexSideLength) in which every cell whose tilemap flags include any bit of mask is opaque; a zero mask makes no cell opaque.
func (f *Fog) AddSource(s Source) SourceIDAddSource adds a vision source and returns its ID; its team sees from it at the next Update.
func (f *Fog) CellAt(p core.Point) (col, row int, ok bool)CellAt returns the cell holding world point p, given TileWidth and TileHeight, and whether it lies in the grid.
func (f *Fog) Draw(d *core.Drawing, team Team, cam *tilemap.Camera, style Style)Draw paints team's fog over the grid in world coordinates, as of the last Update: one filled path of Style.Unseen over unseen cells and one of Style.Explored over explored cells. Visible cells stay uncovered.
Like tilemap.Tilemap.Draw, Draw expects d to already carry the camera's transform (cam.Apply(d)), and uses cam only to skip cells outside cam.VisibleBounds(); a nil cam draws the whole grid. Draw it after the tilemap and units so it covers them. Orthogonal grids emit horizontal runs of rectangles; isometric and staggered grids emit aligned tile diamonds; hexagonal grids emit aligned pointy-top or flat-top hexagons matching Tilemap's projection.
func (f *Fog) Explore(team Team, col, row int)Explore marks (col, row) explored for team without making it visible, such as for a scouting report or a revealed landmark. Cells outside the grid are ignored.
func (f *Fog) ExploreAll(team Team)ExploreAll marks the whole grid explored for team, such as for a map-reveal cheat or a replay viewer.
func (f *Fog) ExploreRect(team Team, col, row, w, h int)ExploreRect marks the cells from (col, row) spanning w columns and h rows explored for team, clipped to the grid.
func (f *Fog) Explored(team Team, col, row int) boolExplored reports whether team has ever seen (col, row), including cells it sees now.
func (f *Fog) ExploredCount(team Team) intExploredCount returns how many cells team has explored, such as for an exploration percentage.
func (f *Fog) Forget(team Team)Forget clears every cell team has explored except those it sees now, such as for a "re-fog" effect.
func (f *Fog) InBounds(col, row int) boolInBounds reports whether (col, row) is a cell of the grid.
func (f Fog) MarshalJSON() ([]byte, error)MarshalJSON writes the fog as versioned JSON text: the grid size, tile size, opaque cells, sources and each team's explored cells, in a fixed order with sources by ID and teams in increasing order, so the same fog always writes the same bytes.
Visible cells are not written; UnmarshalJSON recomputes them from the sources and opaque cells, so a Fog passed to save.Store.Save and back through save.Store.Load sees and has explored exactly what it did.
MarshalJSON has a value receiver so a Fog held by value in a struct that is saved by value still writes its snapshot rather than its exported fields alone.
func (f *Fog) Move(id SourceID, col, row int) boolMove moves the source with id to (col, row) and reports whether it exists.
func (f *Fog) MoveTo(id SourceID, p core.Point) boolMoveTo moves the source with id to the cell holding world point p (see CellAt) and reports whether it exists.
func (f *Fog) Opaque(col, row int) boolOpaque reports whether (col, row) blocks sight; cells outside the grid report true.
func (f *Fog) RemoveSource(id SourceID) boolRemoveSource removes the source with id, such as when a unit dies, and reports whether it existed. Its team stops seeing from it at the next Update, but keeps what it explored.
func (f *Fog) Revision(team Team) uint64Revision returns a number that grows whenever team's visible or explored cells change, and that every team gets afresh when the fog is loaded, so a minimap or cached fog texture can redraw only when it differs. Revisions come from one counter shared by every Fog in the program, so a Fog loaded in place of another, even through save.Store.Load, never repeats a revision a cache has already seen. A team with nothing reports 0.
func (f *Fog) SetOpaque(col, row int, opaque bool)SetOpaque makes (col, row) block sight or not, such as when a door opens or a forest burns, and marks every team for the next Update. Cells outside the grid are ignored.
An opaque cell is still seen itself when a source has sight of it, so walls show at the edge of vision.
func (f *Fog) SetSource(id SourceID, s Source) boolSetSource replaces the source with id, such as when a unit moves, changes sides or gains sight range, and reports whether it exists. A change marks the old and new teams for the next Update; setting the same values does nothing.
func (f *Fog) Source(id SourceID) (Source, bool)Source returns the source with id and whether it exists.
func (f *Fog) Sources() []SourceIDSources returns the IDs of every source in increasing order.
func (f *Fog) State(team Team, col, row int) StateState returns what team knows about (col, row) as of the last Update; cells outside the grid and teams with nothing are Unseen.
func (f *Fog) Teams() []TeamTeams returns every team that has had a source or explored cells, in increasing order.
func (f *Fog) UnmarshalJSON(data []byte) errorUnmarshalJSON replaces the fog with one read from JSON text written by MarshalJSON, and recomputes every team's visible cells from the sources.
It refuses unknown fields, a missing or newer version (ErrFutureVersion), a grid larger than MaxCells or with exactly one zero size, rows that disagree with the grid size, sources or teams out of order or repeated, a source ID above nextSource, and a source whose team has no explored grid. On error the fog is unchanged.
func (f *Fog) Update() boolUpdate recomputes the visible cells of every team whose sources or opaque cells changed since the last Update, adds them to the team's explored cells, and reports whether any team's visible or explored cells changed.
func (f *Fog) Visible(team Team, col, row int) boolVisible reports whether team can see (col, row) as of the last Update, such as to show or hide an enemy unit standing there.
func (f *Fog) VisibleAt(team Team, p core.Point) boolVisibleAt reports whether team can see the cell holding world point p as of the last Update.
type Source struct {
// Team is the side the source reveals cells for.
Team Team
// Col and Row are the cell the source stands on; a source outside the grid sees nothing.
Col, Row int
// Radius is how far the source sees, in cells: a cell dx columns and dy rows away is in range when dx*dx+dy*dy <= Radius*Radius+Radius, which rounds the disc. 0 sees only the source's own cell, and a negative radius behaves as 0.
Radius int
}Source is something that gives its team vision: a unit, a building or a ward.
type SourceID uint32SourceID identifies a vision source added with Fog.AddSource; IDs start at 1 and are never reused by one Fog.
type State uint8State is what a team knows about one cell.
const (
// Unseen marks a cell the team has never seen.
Unseen State = iota
// Explored marks a cell the team has seen before but cannot see now.
Explored
// Visible marks a cell one of the team's sources can see now.
Visible
)func (s State) String() stringString returns "unseen", "explored" or "visible".
type Style struct {
// Unseen fills cells the team has never seen; a zero alpha leaves them uncovered.
Unseen core.Color
// Explored fills cells the team has seen before but cannot see now; a zero alpha leaves them uncovered.
Explored core.Color
}Style is the paint Draw lays over cells a team cannot see now.
func DefaultStyle() StyleDefaultStyle returns opaque black over unseen cells and half-transparent black over explored ones, so remembered terrain shows dimmed.