grid

package
v0.0.1 Latest Latest
Warning

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

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

Documentation

Overview

Package grid is a 2D tile map for walkability and world↔cell conversion.

The grid is shared map state — not ECS entities. Pathing and movement sample it; towers mark cells blocked when placed or sold.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Grid

type Grid struct {
	Width  int
	Height int
	// contains filtered or unexported fields
}

Grid is a rectangular tile map. Cells are indexed row-major: y*Width + x.

func New

func New(width, height int) (*Grid, error)

New creates a Width×Height grid. Every cell starts walkable.

func (*Grid) CellCenter

func (g *Grid) CellCenter(cx, cy int, tileSize float64) (wx, wy float64)

CellCenter returns the world-space center of cell (cx, cy).

func (*Grid) EachCell

func (g *Grid) EachCell(r Rect, fn func(x, y int))

EachCell calls fn for every cell in r that is in bounds.

func (*Grid) FillWalkable

func (g *Grid) FillWalkable(walkable bool)

FillWalkable sets every cell to the same walkability.

func (*Grid) InBounds

func (g *Grid) InBounds(x, y int) bool

InBounds reports whether (x, y) lies inside the grid.

func (*Grid) Index

func (g *Grid) Index(x, y int) int

Index returns the dense index for (x, y). Panics if out of bounds.

func (*Grid) Neighbors4

func (g *Grid) Neighbors4(x, y int, dst [][2]int) [][2]int

Neighbors4 appends the four cardinal neighbors of (x, y) that are in bounds. Order: N, E, S, W.

func (*Grid) RectClear

func (g *Grid) RectClear(r Rect) bool

RectClear reports whether every cell of r is currently walkable. Out-of-bounds rects are not clear.

func (*Grid) RectInBounds

func (g *Grid) RectInBounds(r Rect) bool

RectInBounds reports whether every cell of r lies on the grid.

func (*Grid) SetBlocked

func (g *Grid) SetBlocked(x, y int, blocked bool)

SetBlocked marks (x, y) non-walkable when blocked is true.

func (*Grid) SetRectBlocked

func (g *Grid) SetRectBlocked(r Rect, blocked bool)

SetRectBlocked marks every cell in r blocked or clear. Out-of-bounds cells are skipped.

func (*Grid) SetWalkable

func (g *Grid) SetWalkable(x, y int, walkable bool)

SetWalkable sets whether (x, y) may be entered. Out-of-bounds is a no-op.

func (*Grid) Walkable

func (g *Grid) Walkable(x, y int) bool

Walkable reports whether units may enter (x, y). Out-of-bounds cells are never walkable.

func (*Grid) WorldToCell

func (g *Grid) WorldToCell(wx, wy, tileSize float64) (cx, cy int)

WorldToCell converts a world position to a cell using floor division.

type Rect

type Rect struct {
	X, Y int
	W, H int
}

Rect is an axis-aligned footprint in cell coordinates (top-left + size). Buildings larger than 1×1 use this to claim walkability.

func NewRect

func NewRect(x, y, w, h int) (Rect, error)

NewRect returns a footprint. w and h must be positive.

func (Rect) Contains

func (r Rect) Contains(x, y int) bool

Contains reports whether cell (x, y) lies inside r.

func (Rect) MaxX

func (r Rect) MaxX() int

MaxX returns the exclusive right edge (X + W).

func (Rect) MaxY

func (r Rect) MaxY() int

MaxY returns the exclusive bottom edge (Y + H).

Jump to

Keyboard shortcuts

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