Add command "charge" to charge up the rover's energy store

This commit is contained in:
Marc Di Luzio 2020-07-04 22:42:20 +01:00
parent 15c337c067
commit e875f82b13
4 changed files with 45 additions and 0 deletions

View file

@ -9,6 +9,9 @@ const (
// CommandRepair Will attempt to repair the rover with an inventory object // CommandRepair Will attempt to repair the rover with an inventory object
CommandRepair = "repair" CommandRepair = "repair"
// CommandCharge Will use one tick to charge the rover
CommandCharge = "charge"
) )
// Command represends a single command to execute // Command represends a single command to execute

View file

@ -32,3 +32,38 @@ func TestCommand_Move(t *testing.T) {
pos.Add(vector.Vector{X: 0.0, Y: 1}) pos.Add(vector.Vector{X: 0.0, Y: 1})
assert.Equal(t, pos, newPos, "Failed to correctly set position for rover") assert.Equal(t, pos, newPos, "Failed to correctly set position for rover")
} }
func TestCommand_Charge(t *testing.T) {
world := NewWorld(8)
a, err := world.SpawnRover()
assert.NoError(t, err)
pos := vector.Vector{
X: 1.0,
Y: 2.0,
}
err = world.WarpRover(a, pos)
assert.NoError(t, err, "Failed to set position for rover")
// Move to use up some charge
moveCommand := Command{Command: CommandMove, Bearing: "N"}
assert.NoError(t, world.Enqueue(a, moveCommand), "Failed to queue move command")
// Tick the world
world.EnqueueAllIncoming()
world.ExecuteCommandQueues()
rover, _ := world.GetRover(a)
assert.Equal(t, rover.MaximumCharge-1, rover.Charge)
chargeCommand := Command{Command: CommandCharge}
assert.NoError(t, world.Enqueue(a, chargeCommand), "Failed to queue charge command")
// Tick the world
world.EnqueueAllIncoming()
world.ExecuteCommandQueues()
rover, _ = world.GetRover(a)
assert.Equal(t, rover.MaximumCharge, rover.Charge)
}

View file

@ -371,6 +371,7 @@ func (w *World) Enqueue(rover string, commands ...Command) error {
} }
case CommandStash: case CommandStash:
case CommandRepair: case CommandRepair:
case CommandCharge:
// Nothing to verify // Nothing to verify
default: default:
return fmt.Errorf("unknown command: %s", c.Command) return fmt.Errorf("unknown command: %s", c.Command)
@ -458,6 +459,11 @@ func (w *World) ExecuteCommand(c *Command, rover string) (err error) {
r.Integrity = r.Integrity + 1 r.Integrity = r.Integrity + 1
w.Rovers[rover] = r w.Rovers[rover] = r
} }
case CommandCharge:
_, err := w.ChargeRover(rover)
if err != nil {
return err
}
default: default:
return fmt.Errorf("unknown command: %s", c.Command) return fmt.Errorf("unknown command: %s", c.Command)
} }

View file

@ -65,6 +65,7 @@ message Command {
// "move" - Move the rover in a direction, requires bearing // "move" - Move the rover in a direction, requires bearing
// "stash" - Stashes item at current location in rover inventory // "stash" - Stashes item at current location in rover inventory
// "repair" - Repairs the rover using an inventory object // "repair" - Repairs the rover using an inventory object
// "charge" - Waits a tick to add more charge to the rover
string command = 1; string command = 1;
// The bearing, example: NE // The bearing, example: NE