Rename Direction -> Bearing

This commit is contained in:
Marc Di Luzio 2020-06-09 18:09:51 +01:00
parent 51fe918090
commit 4558e8a9b0
3 changed files with 25 additions and 25 deletions

View file

@ -7,11 +7,11 @@ import (
"github.com/mdiluz/rove/pkg/vector"
)
// Direction describes a compass direction
type Direction int
// Bearing describes a compass direction
type Bearing int
const (
North Direction = iota
North Bearing = iota
NorthEast
East
SouthEast
@ -21,14 +21,14 @@ const (
NorthWest
)
// DirectionString simply describes the strings associated with a direction
type DirectionString struct {
// bearingString simply describes the strings associated with a direction
type bearingString struct {
Long string
Short string
}
// DirectionStrings is the set of strings for each direction
var DirectionStrings = []DirectionString{
// bearingStrings is the set of strings for each direction
var bearingStrings = []bearingString{
{"North", "N"},
{"NorthEast", "NE"},
{"East", "E"},
@ -40,26 +40,26 @@ var DirectionStrings = []DirectionString{
}
// String converts a Direction to a String
func (d Direction) String() string {
return DirectionStrings[d].Long
func (d Bearing) String() string {
return bearingStrings[d].Long
}
// ShortString converts a Direction to a short string version
func (d Direction) ShortString() string {
return DirectionStrings[d].Short
func (d Bearing) ShortString() string {
return bearingStrings[d].Short
}
// DirectionFromString gets the Direction from a string
func DirectionFromString(s string) (Direction, error) {
for i, d := range DirectionStrings {
// FromString gets the Direction from a string
func FromString(s string) (Bearing, error) {
for i, d := range bearingStrings {
if strings.ToLower(d.Long) == strings.ToLower(s) || strings.ToLower(d.Short) == strings.ToLower(s) {
return Direction(i), nil
return Bearing(i), nil
}
}
return -1, fmt.Errorf("Unknown direction: %s", s)
}
var DirectionVectors = []vector.Vector{
var bearingVectors = []vector.Vector{
{X: 0, Y: 1}, // N
{X: 1, Y: 1}, // NE
{X: 1, Y: 0}, // E
@ -71,6 +71,6 @@ var DirectionVectors = []vector.Vector{
}
// Vector converts a Direction to a Vector
func (d Direction) Vector() vector.Vector {
return DirectionVectors[d]
func (d Bearing) Vector() vector.Vector {
return bearingVectors[d]
}

View file

@ -14,19 +14,19 @@ func TestDirection(t *testing.T) {
assert.Equal(t, "N", dir.ShortString())
assert.Equal(t, vector.Vector{X: 0, Y: 1}, dir.Vector())
dir, err := DirectionFromString("N")
dir, err := FromString("N")
assert.NoError(t, err)
assert.Equal(t, North, dir)
dir, err = DirectionFromString("n")
dir, err = FromString("n")
assert.NoError(t, err)
assert.Equal(t, North, dir)
dir, err = DirectionFromString("north")
dir, err = FromString("north")
assert.NoError(t, err)
assert.Equal(t, North, dir)
dir, err = DirectionFromString("NorthWest")
dir, err = FromString("NorthWest")
assert.NoError(t, err)
assert.Equal(t, NorthWest, dir)
}

View file

@ -179,7 +179,7 @@ func (w *World) WarpRover(id uuid.UUID, pos vector.Vector) error {
}
// SetPosition sets an rovers position
func (w *World) MoveRover(id uuid.UUID, bearing bearing.Direction) (RoverAttributes, error) {
func (w *World) MoveRover(id uuid.UUID, bearing bearing.Bearing) (RoverAttributes, error) {
w.worldMutex.Lock()
defer w.worldMutex.Unlock()
@ -278,7 +278,7 @@ func (w *World) Enqueue(rover uuid.UUID, commands ...Command) error {
for _, c := range commands {
switch c.Command {
case "move":
if _, err := bearing.DirectionFromString(c.Bearing); err != nil {
if _, err := bearing.FromString(c.Bearing); err != nil {
return fmt.Errorf("unknown direction: %s", c.Bearing)
}
default:
@ -333,7 +333,7 @@ func (w *World) ExecuteCommand(c *Command, rover uuid.UUID) (finished bool, err
switch c.Command {
case "move":
if dir, err := bearing.DirectionFromString(c.Bearing); err != nil {
if dir, err := bearing.FromString(c.Bearing); err != nil {
return true, fmt.Errorf("unknown direction in command %+v, skipping: %s\n", c, err)
} else if _, err := w.MoveRover(rover, dir); err != nil {