arena

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 11, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package arena is the shared playfield for maze-style tower defense.

It owns the grid, flow field, spawn/exit cells, and build↔wave phase. Building *types*, combat, and economy stay in game code; this package only manages footprints on walkability and when pathing is rebuilt.

Typical loop (Matrix Defense / Desktop TD style):

a.BeginBuild()
a.Place(footprint)   // or Unplace after a sell — marks dirty
a.BeginWave()        // rebuilds field if needed; requires open path
move.FollowFlow(...) // during wave
a.BeginBuild()       // after wave cleared

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotBuildPhase is returned when Place/Unplace runs outside build.
	ErrNotBuildPhase = errors.New("arena: not in build phase")
	// ErrNotWavePhase is returned when wave-only ops run outside a wave.
	ErrNotWavePhase = errors.New("arena: not in wave phase")
	// ErrFootprintBlocked is returned when a footprint overlaps occupied cells.
	ErrFootprintBlocked = errors.New("arena: footprint not clear")
	// ErrFootprintOOB is returned when a footprint leaves the playable grid.
	ErrFootprintOOB = errors.New("arena: footprint out of bounds")
	// ErrPathBlocked is returned when a place/start would seal spawn→exit.
	ErrPathBlocked = errors.New("arena: placement blocks all paths to exit")
	// ErrNoSpawns is returned when the arena has no spawn cells.
	ErrNoSpawns = errors.New("arena: no spawn cells")
)

Functions

func DefaultTopBottom

func DefaultTopBottom(width, height int) (spawns, exits []pathing.Cell, err error)

DefaultTopBottom returns spawns along the full top row and exits along the full bottom row — the classic “enter top, escape bottom” layout.

func VerticalPlayfield added in v0.0.2

func VerticalPlayfield(width, height, spawnRows int) (spawns, exits []pathing.Cell, err error)

VerticalPlayfield is the Matrix Defense–style map: a vertical rectangle, a horizontal spawn strip of spawnRows at the top, and a full-width exit strip on the bottom row.

Players typically build horizontal walls left→right then right→left, leaving a one-cell gap on alternating ends so creeps snake downward.

Types

type Arena

type Arena struct {
	Grid   *grid.Grid
	Field  *pathing.Field
	Phase  Phase
	Config Config
	// contains filtered or unexported fields
}

Arena is the maze rectangle + phase machine.

func New

func New(cfg Config) (*Arena, error)

New creates an arena in PhaseBuild with an initial flow field.

func (*Arena) BeginBuild

func (a *Arena) BeginBuild()

BeginBuild enters (or returns to) build phase. Safe to call at wave end.

func (*Arena) BeginWave

func (a *Arena) BeginWave() error

BeginWave locks footprint edits, rebuilds pathing if dirty, and starts the wave. Fails if RequirePath and any spawn cannot reach an exit.

func (*Arena) CanPlace

func (a *Arena) CanPlace(fp grid.Rect) error

CanPlace reports whether footprint may be blocked in the current build.

func (*Arena) Dirty

func (a *Arena) Dirty() bool

Dirty reports whether the flow field may be stale.

func (*Arena) MarkDirty

func (a *Arena) MarkDirty()

MarkDirty notes that walkability changed outside Place/Pickup.

func (*Arena) Place

func (a *Arena) Place(fp grid.Rect) error

Place blocks footprint cells. Build phase only.

func (*Arena) SpawnsReachable

func (a *Arena) SpawnsReachable() bool

SpawnsReachable reports whether every configured spawn can reach an exit on the *current* grid (cheap BFS; does not require a fresh Field).

func (*Arena) SyncPathing

func (a *Arena) SyncPathing() error

SyncPathing rebuilds the flow field when dirty.

func (*Arena) TileSize

func (a *Arena) TileSize() float64

TileSize returns the configured world units per cell.

func (*Arena) Unplace

func (a *Arena) Unplace(fp grid.Rect) error

Unplace clears footprint cells (e.g. after the game sells a building). Build phase only. Game code owns economy and which entity occupied the cells.

type Config

type Config struct {
	Width, Height int
	TileSize      float64
	// Spawns are entry cells (typically a full top strip).
	Spawns []pathing.Cell
	// Exits are creep goals (typically the full bottom row).
	Exits []pathing.Cell
	// RequirePath rejects placements (and BeginWave) that seal every
	// spawn from every exit — Desktop TD / fair-maze style.
	// If false, players may wall off; creeps on sealed tiles simply stall.
	RequirePath bool
	// LivePathing rebuilds the flow field after every Place/Unplace in
	// build phase so UI can preview the maze path. If false, pathing
	// rebuilds on BeginWave (and SyncPathing) only.
	LivePathing bool
}

Config seeds a new Arena.

type Phase

type Phase int

Phase is the high-level play cadence.

const (
	// PhaseBuild: place/pickup/move buildings; creeps are not marching.
	PhaseBuild Phase = iota
	// PhaseWave: flow field is live; footprint edits are locked.
	PhaseWave
)

func (Phase) String

func (p Phase) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL