private void ReviewDirectionFaced() { var patrol = this._tank.DetermineDirection as PatrolPerimeter; if (patrol == null || patrol.IsDetached) { this._desiredRotation = 0m; return; } if (!this._tank.CurrentMovement.IsMoving) { return; } var tileMovingTowards = TilePos.TilePosFromPosition(this._tank.CurrentMovement.MovingTowards); using (var directionList = PatrolPerimeter.GetPreferredDirections(this._tank.CurrentMovement.Direction, patrol.CurrentAttachmentToWall).GetEnumerator()) { directionList.MoveNext(); var preferredDirection = directionList.Current; var tileAroundCorner = tileMovingTowards.GetPositionAfterOneMove(preferredDirection); if (CouldMoveTo(tileAroundCorner)) { this._desiredRotation = RotationForDirection[preferredDirection]; return; } directionList.MoveNext(); var currentDirection = directionList.Current; directionList.MoveNext(); var directionAwayFromWall = directionList.Current; var tileAwayFromWall = this._tank.TilePosition.GetPositionAfterOneMove(directionAwayFromWall); if (CouldMoveTo(tileAwayFromWall)) { this._desiredRotation = RotationForDirection[directionAwayFromWall]; return; } directionList.MoveNext(); var backwards = directionList.Current; var tileAhead = tileMovingTowards.GetPositionAfterOneMove(currentDirection); if (CouldMoveTo(tileAhead)) { this._desiredRotation = RotationForDirection[currentDirection]; return; } this._desiredRotation = RotationForDirection[backwards]; } }
public void TestPatrolPerimeterDirection() { Assert.IsTrue(PatrolPerimeter.GetPreferredDirections(Direction.Left, PatrolPerimeter.AttachmentToWall.FollowWallOnLeft).SequenceEqual(new[] { Direction.Down, Direction.Left, Direction.Up, Direction.Right })); Assert.IsTrue(PatrolPerimeter.GetPreferredDirections(Direction.Up, PatrolPerimeter.AttachmentToWall.FollowWallOnLeft).SequenceEqual(new[] { Direction.Left, Direction.Up, Direction.Right, Direction.Down })); Assert.IsTrue(PatrolPerimeter.GetPreferredDirections(Direction.Right, PatrolPerimeter.AttachmentToWall.FollowWallOnLeft).SequenceEqual(new[] { Direction.Up, Direction.Right, Direction.Down, Direction.Left })); Assert.IsTrue(PatrolPerimeter.GetPreferredDirections(Direction.Down, PatrolPerimeter.AttachmentToWall.FollowWallOnLeft).SequenceEqual(new[] { Direction.Right, Direction.Down, Direction.Left, Direction.Up })); Assert.IsTrue(PatrolPerimeter.GetPreferredDirections(Direction.Left, PatrolPerimeter.AttachmentToWall.FollowWallOnRight).SequenceEqual(new [] { Direction.Up, Direction.Left, Direction.Down, Direction.Right })); Assert.IsTrue(PatrolPerimeter.GetPreferredDirections(Direction.Up, PatrolPerimeter.AttachmentToWall.FollowWallOnRight).SequenceEqual(new[] { Direction.Right, Direction.Up, Direction.Left, Direction.Down })); Assert.IsTrue(PatrolPerimeter.GetPreferredDirections(Direction.Right, PatrolPerimeter.AttachmentToWall.FollowWallOnRight).SequenceEqual(new[] { Direction.Down, Direction.Right, Direction.Up, Direction.Left })); Assert.IsTrue(PatrolPerimeter.GetPreferredDirections(Direction.Down, PatrolPerimeter.AttachmentToWall.FollowWallOnRight).SequenceEqual(new[] { Direction.Left, Direction.Down, Direction.Right, Direction.Up })); }