geom

package
v0.0.0-...-47a16e8 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2023 License: GPL-3.0 Imports: 5 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FlatField = NewField(5, 8, 15)

FlatField is a default Field where the pixel-projection distance between hexes is equal (or close to) in all directions. The hexagons end up being 21 pixels wide by 15 pixels tall.

View Source
var FlatFieldDistance = FlatField.DistanceBetween(Key{}, Key{}.ToN())
View Source
var Opposite = map[DirectionType]DirectionType{
	S:  N,
	SW: NE,
	NW: SE,
	N:  S,
	NE: SW,
	SE: NW,
}

Opposite provides the 180 degree opposite to any cardinal DirectionType.

Functions

func DirectionTypeStrings

func DirectionTypeStrings() []string

DirectionTypeStrings returns a slice of all String values of the enum

func Distance

func Distance(h1, h2 *Hex) float64

Distance between two Hexagons.

func DistanceSquared

func DistanceSquared(h1, h2 *Hex) float64

DistanceSquared returns the distance between two Hexagons multiplied by itself. This is useful to use as a pathfinding heuristic when these values only need to be compared to other outputs of this function, and the cost of finding the square root adds nothing.

func Equal

func Equal(a, b *Key) bool

Equal determines if the M and N values of the passed pointers differ. If either value is nil, then it only returns true if the other is also nil. TODO: this may make more sense in the combat package as that is its only usage.

func RelativeDirectionStrings

func RelativeDirectionStrings() []string

RelativeDirectionStrings returns a slice of all String values of the enum

Types

type DirectionType

type DirectionType int

DirectionType enumerates directions.

const (
	N DirectionType = iota
	NE
	SE
	S
	SW
	NW
)

DirectionTypes represent the 6 edges of a hexagon.

func Actualize

func Actualize(facing DirectionType, relative RelativeDirection) DirectionType

Actualize a relative direction and a concrete direction together to form a new concrete direction. N and Behind make S. S and Forward make S etc.

func DirectionTypeString

func DirectionTypeString(s string) (DirectionType, error)

DirectionTypeString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.

func DirectionTypeValues

func DirectionTypeValues() []DirectionType

DirectionTypeValues returns all values of the enum

func FindDirection

func FindDirection(origin, projection Key) DirectionType

FindDirection returns the direction one would have to travel in from origin to arrive at projection. Returned values are rounded when the precise value is not one of the 6 hexagonal directions.

func (DirectionType) IsADirectionType

func (i DirectionType) IsADirectionType() bool

IsADirectionType returns "true" if the value is listed in the enum definition. "false" otherwise

func (DirectionType) MarshalJSON

func (i DirectionType) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for DirectionType

func (DirectionType) String

func (i DirectionType) String() string

func (*DirectionType) UnmarshalJSON

func (i *DirectionType) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for DirectionType

type Field

type Field struct {
	// contains filtered or unexported fields
}

Field of hexagons. Provides information about world coordinates.

func NewField

func NewField(bodyWidth, wingWidth, height int) *Field

NewField creates an empty Field. The parameters configure the shape of the Hexagons in the Field.

func (*Field) At

func (f *Field) At(x, y float64) *Hex

At looks for a Hexagon that is located at the world coordinates x,y. Returns nil if there is no Hexagon for those coordinates.

func (*Field) Center

func (f *Field) Center() (float64, float64)

Center returns a world coordinate that half the loaded hexagons are above, half below, half to the left, and half to the right.

func (*Field) DistanceBetween

func (f *Field) DistanceBetween(a, b Key) float64

DistanceBetween calculates the distance between two keys in the field.

func (*Field) Get

func (f *Field) Get(k Key) *Hex

Get the Hexagon at Key. Returns nil if there is no Hexagon for that key.

func (Field) HexHeight

func (f Field) HexHeight() int

func (Field) HexWidth

func (f Field) HexWidth() int

func (*Field) Hexes

func (f *Field) Hexes() []*Hex

Hexes generates a slice of the Hexes of the Field.

func (*Field) Ktow

func (f *Field) Ktow(k Key) (float64, float64)

Ktow calculates the world coordinates that are at the center of a Key.

func (*Field) Load

func (f *Field) Load(keys []Key) error

Load a collection of Keys into the Field.

func (*Field) Wtok

func (f *Field) Wtok(x, y float64) Key

Wtok finds the Key that these coordinates are inside.

type Hex

type Hex struct {
	// contains filtered or unexported fields
}

Hex is a part of a Field.

func (Hex) Center

func (h Hex) Center() (float64, float64)

Center of the Hexagon in world coordinates.

func (Hex) Key

func (h Hex) Key() Key

Key of the Hexagon. Where it exists in the Field grid.

type Key

type Key struct {
	M, N int
}

Key is a way of referencing a Hexagon in a Field.

func (Key) Adjacent

func (k Key) Adjacent() map[DirectionType]Key

Adjacent calculates the neighbors of a Key and returns them keyed by direction.

func (Key) ExpandBy

func (k Key) ExpandBy(min, max int) []Key

ExpandBy determines the Keys that are between min and max away from the Key. The order is randomised.

func (Key) HexesFrom

func (k Key) HexesFrom(other Key) int

HexesFrom calculates how many Hexes away another Key is.

func (Key) Neighbors

func (k Key) Neighbors() map[Key]DirectionType

Neighbors calculates the neighbors of a Key and returns them keyed by their Keys.

func (Key) String

func (k Key) String() string

func (Key) ToDirection

func (k Key) ToDirection(dir DirectionType) Key

func (Key) ToN

func (k Key) ToN() Key

ToN returns the Key that is to the North of that Key.

func (Key) ToNE

func (k Key) ToNE() Key

ToNE returns the Key that is to the Northeast of that Key.

func (Key) ToNW

func (k Key) ToNW() Key

ToNW returns the Key that is to the Northwest of that Key.

func (Key) ToS

func (k Key) ToS() Key

ToS returns the Key that is to the South of that Key.

func (Key) ToSE

func (k Key) ToSE() Key

ToSE returns the Key that is to the Southeast of that Key.

func (Key) ToSW

func (k Key) ToSW() Key

ToSW returns the Key that is to the Southwest of that Key.

type RelativeDirection

type RelativeDirection int
const (
	Forward RelativeDirection = iota
	Behind
	ForwardLeft
	ForwardRight
	BackLeft
	BackRight
)

func RelativeDirectionString

func RelativeDirectionString(s string) (RelativeDirection, error)

RelativeDirectionString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.

func RelativeDirectionValues

func RelativeDirectionValues() []RelativeDirection

RelativeDirectionValues returns all values of the enum

func (RelativeDirection) IsARelativeDirection

func (i RelativeDirection) IsARelativeDirection() bool

IsARelativeDirection returns "true" if the value is listed in the enum definition. "false" otherwise

func (RelativeDirection) MarshalJSON

func (i RelativeDirection) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for RelativeDirection

func (RelativeDirection) String

func (i RelativeDirection) String() string

func (*RelativeDirection) UnmarshalJSON

func (i *RelativeDirection) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for RelativeDirection

Jump to

Keyboard shortcuts

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