diff --git a/pkg/game/command.go b/pkg/game/command.go index cde071e..caecaec 100644 --- a/pkg/game/command.go +++ b/pkg/game/command.go @@ -9,6 +9,9 @@ const ( // CommandRepair Will attempt to repair the rover with an inventory object CommandRepair = "repair" + + // CommandCharge Will use one tick to charge the rover + CommandCharge = "charge" ) // Command represends a single command to execute diff --git a/pkg/game/command_test.go b/pkg/game/command_test.go index 3356f2a..a809753 100644 --- a/pkg/game/command_test.go +++ b/pkg/game/command_test.go @@ -32,3 +32,38 @@ func TestCommand_Move(t *testing.T) { pos.Add(vector.Vector{X: 0.0, Y: 1}) 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) + +} diff --git a/pkg/game/world.go b/pkg/game/world.go index c5e9769..7e9147c 100644 --- a/pkg/game/world.go +++ b/pkg/game/world.go @@ -371,6 +371,7 @@ func (w *World) Enqueue(rover string, commands ...Command) error { } case CommandStash: case CommandRepair: + case CommandCharge: // Nothing to verify default: 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 w.Rovers[rover] = r } + case CommandCharge: + _, err := w.ChargeRover(rover) + if err != nil { + return err + } default: return fmt.Errorf("unknown command: %s", c.Command) } diff --git a/proto/rove/rove.proto b/proto/rove/rove.proto index 6c40234..35b658f 100644 --- a/proto/rove/rove.proto +++ b/proto/rove/rove.proto @@ -65,6 +65,7 @@ message Command { // "move" - Move the rover in a direction, requires bearing // "stash" - Stashes item at current location in rover inventory // "repair" - Repairs the rover using an inventory object + // "charge" - Waits a tick to add more charge to the rover string command = 1; // The bearing, example: NE