Convert bearings to compass points and locations to int coords

This commit is contained in:
Marc Di Luzio 2020-06-05 16:37:52 +01:00
parent ae369715ec
commit be0f4f1aff
10 changed files with 187 additions and 35 deletions

View file

@ -70,6 +70,17 @@ const (
CommandMove = "move"
)
const (
BearingNorth = "North"
BearingNorthEast
BearingEast
BearingSouthEast
BearingSouth
BearingSouthWest
BearingWest
BearingNorthWest
)
// Command describes a single command to execute
// it contains the type, and then any members used for each command type
type Command struct {
@ -77,8 +88,8 @@ type Command struct {
Command string `json:"command"`
// Used for CommandMove
Bearing float64 `json:"bearing"` // The direction to move in degrees
Duration float64 `json:"duration"` // The duration of the move in seconds
Bearing string `json:"bearing"` // The direction to move on a compass in short (NW) or long (NorthWest) form
Duration int `json:"duration"` // The duration of the move in ticks
}
// ================

View file

@ -148,6 +148,22 @@ func HandleSpawn(s *Server, b io.ReadCloser, w io.Writer) error {
return nil
}
// ConvertCommands converts server commands to game commands
func (s *Server) ConvertCommands(commands []Command, inst uuid.UUID) ([]game.Command, error) {
var cmds []game.Command
for _, c := range commands {
switch c.Command {
case CommandMove:
if bearing, err := game.DirectionFromString(c.Bearing); err != nil {
return nil, err
} else {
cmds = append(cmds, s.world.CommandMove(inst, bearing, c.Duration))
}
}
}
return cmds, nil
}
// HandleSpawn will spawn the player entity for the associated account
func HandleCommands(s *Server, b io.ReadCloser, w io.Writer) error {
// Set up the response
@ -170,19 +186,13 @@ func HandleCommands(s *Server, b io.ReadCloser, w io.Writer) error {
} else if inst, err := s.accountant.GetRover(id); err != nil {
response.Error = fmt.Sprintf("Provided account has no rover: %s", err)
} else if cmds, err := s.ConvertCommands(data.Commands, inst); err != nil {
response.Error = fmt.Sprintf("Couldn't convert commands: %s", err)
} else {
// log the data sent
fmt.Printf("\tcommands data: %v\n", data)
// Iterate through the commands to generate all game commands
var cmds []game.Command
for _, c := range data.Commands {
switch c.Command {
case CommandMove:
cmds = append(cmds, s.world.CommandMove(inst, c.Bearing, c.Duration))
}
}
// Execute the commands
if err := s.world.Execute(cmds...); err != nil {
response.Error = fmt.Sprintf("Failed to execute commands: %s", err)

View file

@ -89,7 +89,7 @@ func TestHandleCommands(t *testing.T) {
Commands: []Command{
{
Command: CommandMove,
Bearing: 0.0,
Bearing: "N",
Duration: 1,
},
},