krampus19/soko/direction.go
Sander Schobers 93be82bdbd Moved level state to soko package.
Renamed entity type character and egg to player and target respectively.
2020-01-15 21:25:32 +01:00

51 lines
901 B
Go

package soko
import "opslag.de/schobers/geom"
type Direction int
const (
DirectionUp Direction = iota
DirectionRight
DirectionDown
DirectionLeft
)
var Directions = [4]Direction{DirectionUp, DirectionRight, DirectionDown, DirectionLeft}
func (d Direction) String() string {
switch d {
case DirectionUp:
return "up"
case DirectionRight:
return "right"
case DirectionDown:
return "down"
case DirectionLeft:
return "left"
}
return "invalid"
}
func (d Direction) ToPoint() geom.Point {
switch d {
case DirectionUp:
return geom.Pt(0, -1)
case DirectionRight:
return geom.Pt(1, 0)
case DirectionDown:
return geom.Pt(0, 1)
case DirectionLeft:
return geom.Pt(-1, 0)
}
return geom.Point{}
}
func Neighbours(p geom.Point) []geom.Point {
neighbours := make([]geom.Point, 4)
for i, dir := range Directions {
neighbours[i] = p.Add(dir.ToPoint())
}
return neighbours
}