Example #1
0
        /// <summary>
        /// Adds a successor to a list if it is not impassible or the parent node
        /// </summary>
        /// <param name="successorsRef">List of successors</param>
        /// <param name="AX">x-coordinate</param>
        /// <param name="AY">y-coordinate</param>
        private void AddSuccessor(ArrayList successorsRef, int AX, int AY)
        {
            int CurrentCost = 1;

            if (AX < 0 || AX >= sizes.x)
            {
                return;
            }

            if (AY < 0 || AY >= sizes.y)
            {
                return;
            }

            Vector2Int point = new Vector2Int(AX, AY);

            if (walls.Contains(point))
            {
                return;
            }

            if (CurrentCost == -1)
            {
                return;
            }
            AStarNode2D NewNode = new AStarNode2D(this, this.goalNode, this.cost + CurrentCost, AX, AY);

            if (NewNode.IsSameState(this.parent))
            {
                return;
            }
            successorsRef.Add(NewNode);
        }
Example #2
0
        public Queue <Vector2Int> FindPath(Vector2Int from, Vector2Int to, IWorldInstance worldRef)
        {
            IWorldInstance world = worldRef;

            HashSet <Vector2Int> walls = world.Walls;

            AStar pathfinder = new AStar();

            AStarNode.sizes = new Vector2Int(world.Tiles.GetLength(0), world.Tiles.GetLength(1));
            AStarNode.walls = walls;
            AStarNode goalNode  = new AStarNode2D(null, null, 0, to.x, to.y);
            AStarNode startNode = new AStarNode2D(null, goalNode, 0, from.x, from.y);

            pathfinder.FindPath(startNode, goalNode);

            Queue <Vector2Int> path = new Queue <Vector2Int>();

            for (int i = 0; i < pathfinder.solution.Count; i++)
            {
                AStarNode2D node = (AStarNode2D)pathfinder.solution[i];
                path.Enqueue(new Vector2Int(node.x, node.y));
            }

            return(path);
        }