Rename Direction -> Bearing
This commit is contained in:
parent
51fe918090
commit
4558e8a9b0
3 changed files with 25 additions and 25 deletions
|
@ -7,11 +7,11 @@ import (
|
||||||
"github.com/mdiluz/rove/pkg/vector"
|
"github.com/mdiluz/rove/pkg/vector"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Direction describes a compass direction
|
// Bearing describes a compass direction
|
||||||
type Direction int
|
type Bearing int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
North Direction = iota
|
North Bearing = iota
|
||||||
NorthEast
|
NorthEast
|
||||||
East
|
East
|
||||||
SouthEast
|
SouthEast
|
||||||
|
@ -21,14 +21,14 @@ const (
|
||||||
NorthWest
|
NorthWest
|
||||||
)
|
)
|
||||||
|
|
||||||
// DirectionString simply describes the strings associated with a direction
|
// bearingString simply describes the strings associated with a direction
|
||||||
type DirectionString struct {
|
type bearingString struct {
|
||||||
Long string
|
Long string
|
||||||
Short string
|
Short string
|
||||||
}
|
}
|
||||||
|
|
||||||
// DirectionStrings is the set of strings for each direction
|
// bearingStrings is the set of strings for each direction
|
||||||
var DirectionStrings = []DirectionString{
|
var bearingStrings = []bearingString{
|
||||||
{"North", "N"},
|
{"North", "N"},
|
||||||
{"NorthEast", "NE"},
|
{"NorthEast", "NE"},
|
||||||
{"East", "E"},
|
{"East", "E"},
|
||||||
|
@ -40,26 +40,26 @@ var DirectionStrings = []DirectionString{
|
||||||
}
|
}
|
||||||
|
|
||||||
// String converts a Direction to a String
|
// String converts a Direction to a String
|
||||||
func (d Direction) String() string {
|
func (d Bearing) String() string {
|
||||||
return DirectionStrings[d].Long
|
return bearingStrings[d].Long
|
||||||
}
|
}
|
||||||
|
|
||||||
// ShortString converts a Direction to a short string version
|
// ShortString converts a Direction to a short string version
|
||||||
func (d Direction) ShortString() string {
|
func (d Bearing) ShortString() string {
|
||||||
return DirectionStrings[d].Short
|
return bearingStrings[d].Short
|
||||||
}
|
}
|
||||||
|
|
||||||
// DirectionFromString gets the Direction from a string
|
// FromString gets the Direction from a string
|
||||||
func DirectionFromString(s string) (Direction, error) {
|
func FromString(s string) (Bearing, error) {
|
||||||
for i, d := range DirectionStrings {
|
for i, d := range bearingStrings {
|
||||||
if strings.ToLower(d.Long) == strings.ToLower(s) || strings.ToLower(d.Short) == strings.ToLower(s) {
|
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)
|
return -1, fmt.Errorf("Unknown direction: %s", s)
|
||||||
}
|
}
|
||||||
|
|
||||||
var DirectionVectors = []vector.Vector{
|
var bearingVectors = []vector.Vector{
|
||||||
{X: 0, Y: 1}, // N
|
{X: 0, Y: 1}, // N
|
||||||
{X: 1, Y: 1}, // NE
|
{X: 1, Y: 1}, // NE
|
||||||
{X: 1, Y: 0}, // E
|
{X: 1, Y: 0}, // E
|
||||||
|
@ -71,6 +71,6 @@ var DirectionVectors = []vector.Vector{
|
||||||
}
|
}
|
||||||
|
|
||||||
// Vector converts a Direction to a Vector
|
// Vector converts a Direction to a Vector
|
||||||
func (d Direction) Vector() vector.Vector {
|
func (d Bearing) Vector() vector.Vector {
|
||||||
return DirectionVectors[d]
|
return bearingVectors[d]
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,19 +14,19 @@ func TestDirection(t *testing.T) {
|
||||||
assert.Equal(t, "N", dir.ShortString())
|
assert.Equal(t, "N", dir.ShortString())
|
||||||
assert.Equal(t, vector.Vector{X: 0, Y: 1}, dir.Vector())
|
assert.Equal(t, vector.Vector{X: 0, Y: 1}, dir.Vector())
|
||||||
|
|
||||||
dir, err := DirectionFromString("N")
|
dir, err := FromString("N")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, North, dir)
|
assert.Equal(t, North, dir)
|
||||||
|
|
||||||
dir, err = DirectionFromString("n")
|
dir, err = FromString("n")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, North, dir)
|
assert.Equal(t, North, dir)
|
||||||
|
|
||||||
dir, err = DirectionFromString("north")
|
dir, err = FromString("north")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, North, dir)
|
assert.Equal(t, North, dir)
|
||||||
|
|
||||||
dir, err = DirectionFromString("NorthWest")
|
dir, err = FromString("NorthWest")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, NorthWest, dir)
|
assert.Equal(t, NorthWest, dir)
|
||||||
}
|
}
|
||||||
|
|
|
@ -179,7 +179,7 @@ func (w *World) WarpRover(id uuid.UUID, pos vector.Vector) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetPosition sets an rovers position
|
// 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()
|
w.worldMutex.Lock()
|
||||||
defer w.worldMutex.Unlock()
|
defer w.worldMutex.Unlock()
|
||||||
|
|
||||||
|
@ -278,7 +278,7 @@ func (w *World) Enqueue(rover uuid.UUID, commands ...Command) error {
|
||||||
for _, c := range commands {
|
for _, c := range commands {
|
||||||
switch c.Command {
|
switch c.Command {
|
||||||
case "move":
|
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)
|
return fmt.Errorf("unknown direction: %s", c.Bearing)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
@ -333,7 +333,7 @@ func (w *World) ExecuteCommand(c *Command, rover uuid.UUID) (finished bool, err
|
||||||
|
|
||||||
switch c.Command {
|
switch c.Command {
|
||||||
case "move":
|
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)
|
return true, fmt.Errorf("unknown direction in command %+v, skipping: %s\n", c, err)
|
||||||
|
|
||||||
} else if _, err := w.MoveRover(rover, dir); err != nil {
|
} else if _, err := w.MoveRover(rover, dir); err != nil {
|
||||||
|
|
Loading…
Add table
Reference in a new issue