From fc54775df980be7be4d568da30743e5dffa9c2e9 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 7 Jun 2020 19:03:16 +0100 Subject: [PATCH] Stop movement into non-empty tiles --- pkg/game/world.go | 15 ++++++++++----- pkg/game/world_test.go | 9 +++++++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/pkg/game/world.go b/pkg/game/world.go index 1710a54..d0ccb5c 100644 --- a/pkg/game/world.go +++ b/pkg/game/world.go @@ -124,13 +124,18 @@ func (w *World) MoveRover(id uuid.UUID, bearing Direction) (RoverAttributes, err // Calculate the full movement based on the bearing move := bearing.Vector().Multiplied(distance) - // TODO: Verify there's nothing blocking this movement + // Try the new move position + newPos := i.Attributes.Pos.Added(move) - // Increment the position by the movement - i.Attributes.Pos.Add(move) + // Get the tile and verify it's empty + if tile, err := w.Atlas.GetTile(newPos); err != nil { + return i.Attributes, fmt.Errorf("couldn't get tile for new position") + } else if tile == TileEmpty { + // Perform the move + i.Attributes.Pos = newPos + w.Rovers[id] = i + } - // Set the rover values to the new ones - w.Rovers[id] = i return i.Attributes, nil } else { return RoverAttributes{}, fmt.Errorf("no rover matching id") diff --git a/pkg/game/world_test.go b/pkg/game/world_test.go index b33662d..cb88d21 100644 --- a/pkg/game/world_test.go +++ b/pkg/game/world_test.go @@ -60,8 +60,8 @@ func TestWorld_GetSetMovePosition(t *testing.T) { assert.NoError(t, err, "Failed to get rover attribs") pos := Vector{ - X: 1.0, - Y: 2.0, + X: 0.0, + Y: 0.0, } err = world.WarpRover(a, pos) @@ -77,6 +77,11 @@ func TestWorld_GetSetMovePosition(t *testing.T) { assert.NoError(t, err, "Failed to set position for rover") pos.Add(Vector{0, attribs.Speed * duration}) // We should have move one unit of the speed north assert.Equal(t, pos, newAttribs.Pos, "Failed to correctly move position for rover") + + // Place a tile in front of the rover + assert.NoError(t, world.Atlas.SetTile(Vector{0, 2}, TileWall)) + newAttribs, err = world.MoveRover(a, bearing) + assert.Equal(t, pos, newAttribs.Pos, "Failed to correctly not move position for rover into wall") } func TestWorld_RadarFromRover(t *testing.T) {