Add repair command to repair using inventory item

This commit is contained in:
Marc Di Luzio 2020-06-27 01:35:09 +01:00
parent 7957454ec1
commit 693b8a54f1
7 changed files with 97 additions and 5 deletions

View file

@ -1,9 +1,14 @@
package game
const (
// Moves the rover in the chosen bearing
CommandMove = "move"
// Will attempt to stash the object at the current location
CommandStash = "stash"
// Will attempt to repair the rover with an inventory object
CommandRepair = "repair"
)
// Command represends a single command to execute

View file

@ -363,11 +363,12 @@ func (w *World) Enqueue(rover string, commands ...Command) error {
// First validate the commands
for _, c := range commands {
switch c.Command {
case "move":
case CommandMove:
if _, err := bearing.FromString(c.Bearing); err != nil {
return fmt.Errorf("unknown bearing: %s", c.Bearing)
}
case "stash":
case CommandStash:
case CommandRepair:
// Nothing to verify
default:
return fmt.Errorf("unknown command: %s", c.Command)
@ -446,6 +447,17 @@ func (w *World) ExecuteCommand(c *Command, rover string) (err error) {
return err
}
case CommandRepair:
if r, err := w.GetRover(rover); err != nil {
return err
} else {
// Consume an inventory item to repair
if len(r.Inventory) > 0 {
r.Inventory = r.Inventory[:len(r.Inventory)-1]
r.Integrity = r.Integrity + 1
w.Rovers[rover] = r
}
}
default:
return fmt.Errorf("unknown command: %s", c.Command)
}

View file

@ -161,5 +161,74 @@ func TestWorld_RoverStash(t *testing.T) {
inv, err := world.RoverInventory(a)
assert.NoError(t, err, "Failed to get inventory")
assert.Equal(t, objects.SmallRock, inv[0])
}
func TestWorld_RoverDamage(t *testing.T) {
world := NewWorld(2, 2)
a, err := world.SpawnRover()
assert.NoError(t, err)
pos := vector.Vector{
X: 0.0,
Y: 0.0,
}
err = world.WarpRover(a, pos)
assert.NoError(t, err, "Failed to set position for rover")
info, err := world.GetRover(a)
assert.NoError(t, err, "couldn't get rover info")
err = world.Atlas.SetTile(vector.Vector{X: 0.0, Y: 1.0}, objects.LargeRock)
assert.NoError(t, err, "Failed to set tile to rock")
vec, err := world.MoveRover(a, bearing.North)
assert.NoError(t, err, "Failed to move rover")
assert.Equal(t, pos, vec, "Rover managed to move into large rock")
newinfo, err := world.GetRover(a)
assert.NoError(t, err, "couldn't get rover info")
assert.Equal(t, info.Integrity-1, newinfo.Integrity, "rover should have lost integrity")
}
func TestWorld_RoverRepair(t *testing.T) {
world := NewWorld(2, 2)
a, err := world.SpawnRover()
assert.NoError(t, err)
pos := vector.Vector{
X: 0.0,
Y: 0.0,
}
err = world.WarpRover(a, pos)
assert.NoError(t, err, "Failed to set position for rover")
originalInfo, err := world.GetRover(a)
assert.NoError(t, err, "couldn't get rover info")
err = world.Atlas.SetTile(pos, objects.SmallRock)
assert.NoError(t, err, "Failed to set tile to rock")
o, err := world.RoverStash(a)
assert.NoError(t, err, "Failed to stash")
assert.Equal(t, objects.SmallRock, o, "Failed to get correct object")
err = world.Atlas.SetTile(vector.Vector{X: 0.0, Y: 1.0}, objects.LargeRock)
assert.NoError(t, err, "Failed to set tile to rock")
vec, err := world.MoveRover(a, bearing.North)
assert.NoError(t, err, "Failed to move rover")
assert.Equal(t, pos, vec, "Rover managed to move into large rock")
newinfo, err := world.GetRover(a)
assert.NoError(t, err, "couldn't get rover info")
assert.Equal(t, originalInfo.Integrity-1, newinfo.Integrity, "rover should have lost integrity")
err = world.ExecuteCommand(&Command{Command: CommandRepair}, a)
assert.NoError(t, err, "Failed to repair rover")
newinfo, err = world.GetRover(a)
assert.NoError(t, err, "couldn't get rover info")
assert.Equal(t, originalInfo.Integrity, newinfo.Integrity, "rover should have gained integrity")
}