Add rover inventory capacity and test

This commit is contained in:
Marc Di Luzio 2020-07-04 12:01:35 +01:00
parent e6ff453ff1
commit 2eaed1447d
7 changed files with 104 additions and 47 deletions

View file

@ -19,6 +19,9 @@ type Rover struct {
// Inventory represents any items the rover is carrying
Inventory []objects.Object `json:"inventory"`
// Capacity is the maximum number of inventory items
Capacity int `json:"capacity"`
// Integrity represents current rover health
Integrity int `json:"integrity"`
}

View file

@ -77,6 +77,7 @@ func (w *World) SpawnRover() (string, error) {
rover := Rover{
Range: 4.0,
Integrity: 10,
Capacity: 10,
Name: uuid.New().String(),
}
@ -249,6 +250,11 @@ func (w *World) RoverStash(rover string) (objects.Type, error) {
return objects.None, fmt.Errorf("no rover matching id")
}
// Can't pick up when full
if len(r.Inventory) >= r.Capacity {
return objects.None, nil
}
_, obj := w.Atlas.QueryPosition(r.Pos)
if !obj.IsStashable() {
return objects.None, nil

View file

@ -129,18 +129,46 @@ func TestWorld_RoverStash(t *testing.T) {
// Set to a traversible tile
world.Atlas.SetTile(pos, atlas.TileRock)
rover, err := world.GetRover(a)
assert.NoError(t, err, "Failed to get rover")
for i := 0; i < rover.Capacity; i++ {
// Place an object
world.Atlas.SetObject(pos, objects.Object{Type: objects.SmallRock})
// Pick it up
o, err := world.RoverStash(a)
assert.NoError(t, err, "Failed to stash")
assert.Equal(t, objects.SmallRock, o, "Failed to get correct object")
// Check it's gone
_, obj := world.Atlas.QueryPosition(pos)
assert.Equal(t, objects.None, obj.Type, "Stash failed to remove object from atlas")
// Check we have it
inv, err := world.RoverInventory(a)
assert.NoError(t, err, "Failed to get inventory")
assert.Equal(t, i+1, len(inv))
assert.Equal(t, objects.Object{Type: objects.SmallRock}, inv[i])
}
// Place an object
world.Atlas.SetObject(pos, objects.Object{Type: objects.SmallRock})
// Try to pick it up
o, err := world.RoverStash(a)
assert.NoError(t, err, "Failed to stash")
assert.Equal(t, objects.SmallRock, o, "Failed to get correct object")
assert.Equal(t, objects.None, o, "Failed to get correct object")
// Check it's still there
_, obj := world.Atlas.QueryPosition(pos)
assert.Equal(t, objects.None, obj.Type, "Stash failed to remove object from atlas")
assert.Equal(t, objects.SmallRock, obj.Type, "Stash failed to remove object from atlas")
// Check we don't have it
inv, err := world.RoverInventory(a)
assert.NoError(t, err, "Failed to get inventory")
assert.Equal(t, objects.Object{Type: objects.SmallRock}, inv[0])
assert.Equal(t, rover.Capacity, len(inv))
}
func TestWorld_RoverDamage(t *testing.T) {