public void SetObstacle_OutOfMaterix() { AStarWithOutClosedList aStar = new AStarWithOutClosedList(10, 10); aStar.SetObstacle(new Vector2(-1, 3)); aStar.SetObstacle(new Vector2(1, -3)); aStar.SetObstacle(new Vector2(5, 10)); aStar.SetObstacle(new Vector2(25, 3)); foreach (AStarWithOutClosedListNode node in aStar.matrix) { Assert.IsTrue(node.canThrough); } }
public void RemoveObstacle_OutOfMaterix() { Vector2 obstaclePosition = new Vector2(5, 3); AStarWithOutClosedList aStar = new AStarWithOutClosedList(10, 10); aStar.SetObstacle(obstaclePosition); aStar.RemoveObstacle(new Vector2(100, 5)); aStar.RemoveObstacle(new Vector2(5, 100)); aStar.RemoveObstacle(new Vector2(-10, 5)); aStar.RemoveObstacle(new Vector2(5, -10)); for (int x = 0; x < aStar.matrix.GetLength(0); x++) { for (int y = 0; y < aStar.matrix.GetLength(1); y++) { if (x != (int)obstaclePosition.x || y != (int)obstaclePosition.y) { Assert.IsTrue(aStar.matrix[x, y].canThrough); } else { Assert.IsFalse(aStar.matrix[x, y].canThrough); } } } }
public void RemoveObstacle_Normal() { Vector2 obstaclePosition = new Vector2(5, 3); AStarWithOutClosedList aStar = new AStarWithOutClosedList(10, 10); aStar.SetObstacle(obstaclePosition); aStar.RemoveObstacle(obstaclePosition); foreach (AStarWithOutClosedListNode node in aStar.matrix) { Assert.IsTrue(node.canThrough); } }
public void Pathfind_EndIsObstacle() { AStarWithOutClosedList aStar = new AStarWithOutClosedList(7, 7); Vector2 startPosition = new Vector2(0, 0); Vector2 endPosition = new Vector2(6, 6); aStar.SetObstacle(new Vector2(6, 6)); aStar.FindPath(startPosition, endPosition); foreach (AStarWithOutClosedListNode node in aStar.matrix) { Assert.AreEqual(null, node.nextNode); } }
void MouseMiddleDown() { Vector2 position = GetMousePosition(); AStarWithOutClosedListNode node = _aStar.GetNode(position); if (node == null) { return; } if (node.canThrough) { _aStar.SetObstacle(position); } else { _aStar.RemoveObstacle(position); } DoFindPath(); }
public void Pathfind_CantFind() { AStarWithOutClosedList aStar = new AStarWithOutClosedList(7, 7); Vector2 startPosition = new Vector2(0, 0); Vector2 endPosition = new Vector2(6, 6); aStar.SetObstacle(new Vector2(0, 1)); aStar.SetObstacle(new Vector2(1, 1)); aStar.SetObstacle(new Vector2(2, 1)); aStar.SetObstacle(new Vector2(4, 1)); aStar.SetObstacle(new Vector2(4, 2)); aStar.SetObstacle(new Vector2(4, 3)); aStar.SetObstacle(new Vector2(5, 3)); aStar.SetObstacle(new Vector2(6, 3)); aStar.SetObstacle(new Vector2(1, 4)); aStar.SetObstacle(new Vector2(2, 4)); aStar.SetObstacle(new Vector2(3, 4)); aStar.SetObstacle(new Vector2(2, 5)); aStar.SetObstacle(new Vector2(0, 6)); aStar.SetObstacle(new Vector2(1, 6)); aStar.FindPath(startPosition, endPosition); /* * N N 0 0 0 0 E * * ↓ ← N 0 0 0 0 * * ↓ N N N 0 0 0 * * → → → ↓ N N N * * → → → ↓ N ↓ ← * * N N N ↓ N ↓ ← * * S ← ← ← ← ← ← */ AStarWithOutClosedListNode[,] matrix = aStar.matrix; AStarWithOutClosedListNode[,] nextNodes = new AStarWithOutClosedListNode[7, 7] { { null, matrix[0, 0], matrix[1, 0], matrix[2, 0], matrix[3, 0], matrix[4, 0], matrix[5, 0] }, { null, null, null, matrix[3, 0], null, matrix[5, 0], matrix[5, 1] }, { matrix[1, 2], matrix[2, 2], matrix[3, 2], matrix[3, 1], null, matrix[5, 1], matrix[5, 2] }, { matrix[1, 3], matrix[2, 3], matrix[3, 3], matrix[3, 2], null, null, null }, { matrix[0, 3], null, null, null, null, null, null }, { matrix[0, 4], matrix[0, 5], null, null, null, null, null }, { null, null, null, null, null, null, null } }; for (int x = 0; x < 7; x++) { for (int y = 0; y < 7; y++) { Assert.AreEqual(nextNodes[y, x], aStar.matrix[x, y].nextNode); //横y竖x,何等反人类,而且竟然严格符合了数组下标顺序:前面是第一层,后面是第二层 } } }