hermeticum/server/witch/direction.go

89 lines
1.6 KiB
Go
Raw Normal View History

2023-05-03 03:35:15 +00:00
package witch
2023-05-26 06:03:58 +00:00
import "fmt"
2023-05-03 03:35:15 +00:00
const (
dirEast = "_DIR_EAST"
dirWest = "_DIR_WEST"
dirNorth = "_DIR_NORTH"
dirSouth = "_DIR_SOUTH"
dirAbove = "_DIR_ABOVE"
dirBelow = "_DIR_BELOW"
)
type Direction struct {
raw string
}
2023-05-26 06:03:58 +00:00
func NewDirection(raw string) Direction {
return Direction{raw: raw}
}
2023-05-03 03:35:15 +00:00
func (d Direction) Reverse() Direction {
2023-05-10 02:31:02 +00:00
raw := ""
switch d.raw {
case dirAbove:
raw = dirBelow
case dirBelow:
raw = dirAbove
case dirEast:
raw = dirWest
case dirWest:
raw = dirEast
case dirNorth:
raw = dirSouth
case dirSouth:
raw = dirNorth
}
2023-05-26 06:03:58 +00:00
return NewDirection(raw)
2023-05-03 03:35:15 +00:00
}
2023-05-26 06:03:58 +00:00
// NormalizeDirection takes a direction someone might type like "up" or "north" and returns the correct Direction struct
func NormalizeDirection(humanDir string) (Direction, error) {
2023-05-10 02:31:02 +00:00
raw := ""
switch humanDir {
case "up":
case "above":
raw = dirAbove
case "down":
case "below":
raw = dirBelow
case "east":
raw = dirEast
case "west":
raw = dirWest
case "north":
raw = dirNorth
case "south":
raw = dirSouth
2023-05-26 06:03:58 +00:00
default:
return Direction{}, fmt.Errorf("did not understand direction '%s'", humanDir)
2023-05-10 02:31:02 +00:00
}
2023-05-26 06:03:58 +00:00
return NewDirection(raw), nil
2023-05-03 03:35:15 +00:00
}
// Human returns a string form of this direction like "above" or "north"
2023-05-10 02:31:02 +00:00
func (d Direction) Human() (humanDir string) {
switch d.raw {
case dirAbove:
humanDir = "above"
case dirBelow:
humanDir = "below"
case dirEast:
humanDir = "east"
case dirWest:
humanDir = "west"
case dirNorth:
humanDir = "north"
case dirSouth:
humanDir = "south"
}
return humanDir
2023-05-03 03:35:15 +00:00
}
2023-05-26 06:03:58 +00:00
func (d Direction) Equals(o Direction) bool {
return d.raw == o.raw
}