tile

package module
v0.0.0-...-981580c Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2023 License: MIT Imports: 10 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Direction

type Direction byte

Diretion represents a direction

const (
	North Direction = iota
	NorthEast
	East
	SouthEast
	South
	SouthWest
	West
	NorthWest
)

Various directions

func (Direction) String

func (v Direction) String() string

String returns a string representation of a direction

type Grid

type Grid[T comparable] struct {
	Size Point // The map size
	// contains filtered or unexported fields
}

Grid represents a 2D tile map. Internally, a map is composed of 3x3 pages.

func NewGrid

func NewGrid(width, height int16) *Grid[string]

NewGrid returns a new map of the specified size. The width and height must be both multiples of 3.

func NewGridOf

func NewGridOf[T comparable](width, height int16) *Grid[T]

NewGridOf returns a new map of the specified size. The width and height must be both multiples of 3.

func ReadFile

func ReadFile[T comparable](filename string) (grid *Grid[T], err error)

Restore restores the grid from the specified file. The grid must be written using the corresponding WriteFile() method.

func ReadFrom

func ReadFrom[T comparable](src io.Reader) (grid *Grid[T], err error)

ReadFrom reads the grid from the reader.

func (*Grid[T]) Around

func (m *Grid[T]) Around(from Point, distance uint32, costOf costFn, fn func(Point, Tile[T]))

Around performs a breadth first search around a point.

func (*Grid[T]) At

func (m *Grid[T]) At(x, y int16) (Tile[T], bool)

At returns the tile at a specified position

func (*Grid[T]) Each

func (m *Grid[T]) Each(fn func(Point, Tile[T]))

Each iterates over all of the tiles in the map.

func (*Grid[T]) MaskAt

func (m *Grid[T]) MaskAt(x, y int16, tile, mask Value)

MaskAt atomically updates the bits of tile at a specific coordinate. The bits are specified by the mask. The bits that need to be updated should be flipped on in the mask.

func (*Grid[T]) MergeAt

func (m *Grid[T]) MergeAt(x, y int16, merge func(Value) Value)

Merge atomically merges the tile by applying a merging function at a specific coordinate.

func (*Grid[T]) Neighbors

func (m *Grid[T]) Neighbors(x, y int16, fn func(Point, Tile[T]))

Neighbors iterates over the direct neighbouring tiles

func (*Grid[T]) Path

func (m *Grid[T]) Path(from, to Point, costOf costFn) ([]Point, int, bool)

Path calculates a short path and the distance between the two locations

func (*Grid[T]) View

func (m *Grid[T]) View(rect Rect, fn func(Point, Tile[T])) *View[T]

View creates a new view of the map.

func (*Grid[T]) Within

func (m *Grid[T]) Within(nw, se Point, fn func(Point, Tile[T]))

Within selects the tiles within a specifid bounding box which is specified by north-west and south-east coordinates.

func (*Grid[T]) WriteAt

func (m *Grid[T]) WriteAt(x, y int16, tile Value)

WriteAt updates the entire tile value at a specific coordinate

func (*Grid[T]) WriteFile

func (m *Grid[T]) WriteFile(filename string) error

WriteFile writes the grid into a flate-compressed binary file.

func (*Grid[T]) WriteTo

func (m *Grid[T]) WriteTo(dst io.Writer) (n int64, err error)

WriteTo writes the grid to a specific writer.

type Point

type Point struct {
	X int16 // X coordinate
	Y int16 // Y coordinate
}

Point represents a 2D coordinate.

func At

func At(x, y int16) Point

At creates a new point at a specified x,y coordinate.

func (Point) Add

func (p Point) Add(p2 Point) Point

Add adds two points together.

func (Point) DistanceTo

func (p Point) DistanceTo(other Point) uint32

DistanceTo calculates manhattan distance to the other point

func (Point) Divide

func (p Point) Divide(p2 Point) Point

Divide divides the first point by the second.

func (Point) DivideScalar

func (p Point) DivideScalar(s int16) Point

DivideScalar divides the given point by the scalar.

func (Point) Equal

func (p Point) Equal(other Point) bool

Equal compares two points and returns true if they are equal.

func (Point) Integer

func (p Point) Integer() uint32

Integer returns a packed 32-bit integer representation of a point.

func (Point) Move

func (p Point) Move(direction Direction) Point

Move moves a point by one in the specified direction.

func (Point) MoveBy

func (p Point) MoveBy(direction Direction, n int16) Point

MoveBy moves a point by n in the specified direction.

func (Point) Multiply

func (p Point) Multiply(p2 Point) Point

Multiply multiplies two points together.

func (Point) MultiplyScalar

func (p Point) MultiplyScalar(s int16) Point

MultiplyScalar multiplies the given point by the scalar.

func (Point) String

func (p Point) String() string

String returns string representation of a point.

func (Point) Subtract

func (p Point) Subtract(p2 Point) Point

Subtract subtracts the second point from the first.

func (Point) Within

func (p Point) Within(nw, se Point) bool

Within checks if the point is within the specified bounding box.

func (Point) WithinRect

func (p Point) WithinRect(box Rect) bool

WithinRect checks if the point is within the specified bounding box.

func (Point) WithinSize

func (p Point) WithinSize(size Point) bool

WithinSize checks if the point is within the specified bounding box which starts at 0,0 until the width/height provided.

type Rect

type Rect struct {
	Min Point // Top left point of the rectangle
	Max Point // Bottom right point of the rectangle
}

Rect represents a rectangle

func NewRect

func NewRect(left, top, right, bottom int16) Rect

NewRect creates a new rectangle left,top,right,bottom correspond to x1,y1,x2,y2

func (*Rect) Contains

func (r *Rect) Contains(p Point) bool

Contains returns whether a point is within the rectangle or not.

func (*Rect) Intersects

func (r *Rect) Intersects(box Rect) bool

Intersects returns whether a rectangle intersects with another rectangle or not.

func (*Rect) Size

func (r *Rect) Size() Point

Size returns the size of the rectangle

type Tile

type Tile[T comparable] struct {
	// contains filtered or unexported fields
}

Tile represents an iterator over all state objects at a particular location.

func (Tile[T]) Add

func (c Tile[T]) Add(v T)

Add adds object to the set

func (Tile[T]) ChangeDirection

func (c Tile[T]) ChangeDirection(d uint32, id uint32, data interface{})

func (Tile[T]) Count

func (c Tile[T]) Count() (count int)

Count returns number of objects at the current tile.

func (Tile[T]) Del

func (c Tile[T]) Del(v T)

Del removes the object from the set

func (Tile[T]) Despawn

func (c Tile[T]) Despawn(v T, id uint32, data interface{})

func (Tile[T]) Mask

func (c Tile[T]) Mask(tile, mask Value) Value

Mask updates the bits of tile. The bits are specified by the mask. The bits that need to be updated should be flipped on in the mask.

func (Tile[T]) Merge

func (c Tile[T]) Merge(merge func(Value) Value) Value

Merge atomically merges the tile by applying a merging function.

func (Tile[T]) MoveTo

func (c Tile[T]) MoveTo(cd Tile[T], v T, id uint32, data interface{})

Add adds object to the set

func (Tile[T]) Range

func (c Tile[T]) Range(fn func(T) error) error

Range iterates over all of the objects in the set

func (Tile[T]) SimpleDespawn

func (c Tile[T]) SimpleDespawn(v T)

func (Tile[T]) SimpleMoveTo

func (c Tile[T]) SimpleMoveTo(cd Tile[T], v T)

Add adds object to the set

func (Tile[T]) SimpleSpawn

func (c Tile[T]) SimpleSpawn(v T)

func (Tile[T]) Spawn

func (c Tile[T]) Spawn(v T, id uint32, data interface{})

func (Tile[T]) TriggerEvent

func (c Tile[T]) TriggerEvent(id uint32, data interface{})

func (Tile[T]) Value

func (c Tile[T]) Value() Value

Value reads the tile information

func (Tile[T]) Write

func (c Tile[T]) Write(tile Value)

Write updates the entire tile value.

type Update

type Update[T comparable] struct {
	Point       // The tile location
	Old   Value // Old tile value
	New   Value // New tile value
	Add   T     // An object was added to the tile
	Del   T     // An object was removed from the tile

	Dir       uint32
	Moved     T
	Spawned   T
	Despawned T
	Emmiter   uint32
	Data      interface{}
}

Update represents a tile update notification.

type Value

type Value = uint32

Value represents a packed tile information, it must fit on 4 bytes.

type View

type View[T comparable] struct {
	Grid  *Grid[T]       // The associated map
	Inbox chan Update[T] // The update inbox for the view
	// contains filtered or unexported fields
}

View represents a view which can monitor a collection of tiles.

func (*View[T]) At

func (v *View[T]) At(x, y int16) (Tile[T], bool)

At returns the tile at a specified position.

func (*View[T]) Close

func (v *View[T]) Close() error

Close closes the view and unsubscribes from everything.

func (*View[T]) Each

func (v *View[T]) Each(fn func(Point, Tile[T]))

Each iterates over all of the tiles in the view.

func (*View[T]) MergeAt

func (v *View[T]) MergeAt(x, y int16, tile, mask Value)

MergeAt updates the bits of tile at a specific coordinate. The bits are specified by the mask. The bits that need to be updated should be flipped on in the mask.

func (*View[T]) MoveAt

func (v *View[T]) MoveAt(nw Point, fnNew, fnOld func(Point, Tile[T]))

MoveAt moves the viewport to a specific coordinate.

func (*View[T]) MoveBy

func (v *View[T]) MoveBy(x, y int16, fnNew, fnOld func(Point, Tile[T]))

MoveBy moves the viewport towards a particular direction.

func (*View[T]) Resize

func (v *View[T]) Resize(box Rect, fnNew, fnOld func(Point, Tile[T]))

Resize resizes the viewport.

func (*View[T]) WriteAt

func (v *View[T]) WriteAt(x, y int16, tile Value)

WriteAt updates the entire tile at a specific coordinate.

Jump to

Keyboard shortcuts

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