//tested method]_[expected input]_[expected behavior] public void GetPath_BetweenTwoPoints_ShouldReturnCorrectPath(IntVector2 startPoint, IntVector2 endPoint, List <IntVector2> expectedPath) { // Arrange IGrid grid = CreateZigzagGrid(); IPathFinderController pathFinderController = new PathFinderController(grid); // Act var actualPath = pathFinderController.GetPath(startPoint, endPoint, null); // Assert Assert.True(expectedPath.SequenceEqual(actualPath)); }
public static IEnumerator Init(float pStepSize, Vector2 pTopLeft, Vector2 pBotRight) { //Debug.Log($"Init"); playerSize = Game.Instance.PlayerManager.PLAYER_SIZE; stepSize = pStepSize; grid = new List <List <Node> >(); Vector2 botLeft = new Vector2(pTopLeft.x, pBotRight.y); Vector2 topRight = new Vector2(pBotRight.x, pTopLeft.y); positionShift = -botLeft; yield return(new WaitForEndOfFrame()); //without this some MapObjects are ignored const int max_steps_per_frame = 100; int steps = 0; for (float x = botLeft.x; x < topRight.x; x += pStepSize) { grid.Add(new List <Node>()); for (float y = botLeft.y; y < topRight.y; y += pStepSize) { Vector2 nodePos = new Vector2(x, y); bool isWalkable = !PathFinderController.OverlapsWithMapObject(nodePos); if (debug_draw_grid) { Utils.DebugDrawCross(nodePos, isWalkable ? Color.green : Color.red, 1); } SVector2 nodePosScaled = GetScaledVector(nodePos); Node node = new Node(nodePosScaled, isWalkable); grid.Last().Add(node); //do only X steps per frame to avoid big lag steps++; if (steps % max_steps_per_frame == 0) { //Debug.Log($"Steps: {steps}"); yield return(new WaitForEndOfFrame()); } } } //astar = new Astar(grid); Debug_DrawGrid(); isInited = true; OnInited?.Invoke(); //Debug.Log($"Astar init"); }
/// <summary> /// Adds the pPoint to the pCollection if it does not overlap with /// any map object and is not in pIgnorePoints. /// Returns true if added. /// </summary> private static bool TryAddPointToCollection(Vector2 pCenter, List <Vector2> pIgnorePoints, List <Vector2> pCollection, Vector2 pPoint) { //note: IsReachable method does not work well when player is touching //a map object (always unreachable) //if(!Utils.ContainsPoint(pIgnorePoints, pPoint) && IsReachable(pCenter, pPoint)) if (!Utils.ContainsPoint(pIgnorePoints, pPoint) && !PathFinderController.OverlapsWithMapObject(pPoint)) { pCollection.Add(pPoint); return(true); } return(false); }
public void GetPath_BetweenTwoPoints_ShouldReturnDestinationPointBusy(IntVector2 startPoint, IntVector2 endPoint) { // Arrange bool destinationBusyCalled = false; IGrid grid = CreateWallGrid(); IPathFinderController pathFinderController = new PathFinderController(grid); pathFinderController.DestinationPointIsNotEmpty += v => destinationBusyCalled = true; // Act pathFinderController.GetPath(startPoint, endPoint, null); // Assert Assert.True(destinationBusyCalled); }
public void GetPath_BetweenTwoPoints_ShouldReturnNoWayToPoint(IntVector2 startPoint, IntVector2 endPoint) { // Arrange bool noWayCalled = false; IGrid grid = CreateWallGrid(); IPathFinderController pathFinderController = new PathFinderController(grid); pathFinderController.NoWayToDestinationPoint += v => noWayCalled = true; // Act pathFinderController.GetPath(startPoint, endPoint, null); // Assert Assert.True(noWayCalled); }
private PathFinderController aPathFinder; // This stores a reference to the PathFinderController script /** * We start we an awake function to initalize all variables that need initialized */ void Awake() { path = new List <Vector3>(); goalPos = new Vector3(); //If this is not done goalPos remains null goalFound = false; onNode = true; stop = false; elapsedTime = 0; reScanTime = 0; GameObject myObject = GameObject.FindGameObjectWithTag("PathFinder"); //Empty object where we store all nodes aPathFinder = myObject.GetComponent <PathFinderController>(); //a Reference to our pathfinding script }
public void GetPath_BetweenTwoPointsRepeatedly_ShouldReturnCorrectPath() { // Arrange int findPathCorrectXTimes = 0; int XTimes = 5; var expectedPath = PathFinderTestData.GetList0_0_5_5(); IGrid grid = CreateZigzagGrid(); IPathFinderController pathFinderController = new PathFinderController(grid); // Act for (int i = 0; i < XTimes; i++) { var actualPath = pathFinderController.GetPath(GetPoint(0, 0), GetPoint(5, 5), null); if (expectedPath.SequenceEqual(actualPath)) { findPathCorrectXTimes++; } } // Assert Assert.Equal(findPathCorrectXTimes, XTimes); }
/// <summary> /// Check if the last 4 nodes are in zig-zag shape. /// If so, try to replace one node with new one making the pPath more straight. /// X X - X /// | | /// X - X => X /// | | /// X X /// </summary> private static void TryStraightenThePath(List <Vector2> pPath, float pStepSize, bool pDebug) { if (pPath.Count < 4) { return; } if (!IsZigZag(pPath.GetRange(pPath.Count - 4, 4), pStepSize)) { return; } Vector2 p1 = pPath[pPath.Count - 4]; Vector2 p2 = pPath[pPath.Count - 3]; Vector2 p3 = pPath[pPath.Count - 2]; Vector2 p4 = pPath[pPath.Count - 1]; Utils.DebugDrawPath(new List <Vector2>() { p1, p2, p3, p4 }, Color.red); Vector2 newPoint = p1 + (p2 - p1) * 2; if (!PathFinderController.OverlapsWithMapObject(newPoint)) { pPath.RemoveAt(pPath.Count - 2); pPath.Insert(pPath.Count - 1, newPoint); return; } newPoint = p4 + (p3 - p4) * 2; if (!PathFinderController.OverlapsWithMapObject(newPoint)) { pPath.RemoveAt(pPath.Count - 3); pPath.Insert(pPath.Count - 2, newPoint); return; } }
private void Awake() { pathFinderController = GetComponent <PathFinderController>(); }