Пример #1
0
        public static bool IsLineWalkable(this List <Node> line, IMoveable <Node> moveable)
        {
            if (line.IsLineInterrupted())
            {
                return(false);
            }
            int lineCount = line.Count;

            for (int i = 0; i < lineCount; i++)
            {
                if (!moveable.Walkable(line[i]))
                {
                    return(false);
                }
            }
            return(true);
        }
Пример #2
0
        /// <summary>
        /// Get a path from combatant position to target position, based off of what the combatant can traverse
        /// </summary>
        /// <param name="combatant"></param>
        /// <param name="to"></param>
        /// <param name="path">Out path</param>
        public void GetPath(IMoveable <Node> moveable, Vector2Int to, List <Node> path)
        {
            // fromNode = start position, toNode = end position
            //Debug.Log(moveable.Node);
            Node fromNode = grid[moveable.Node.Position.x, moveable.Node.Position.y],
                 toNode   = grid[to.x, to.y],
                 current  = null,
                 child;
            Vector2Int gridSize, center,
                       convertedPosition;

            // Reset the lists
            open.Clear();
            closed.Clear();

            // Reset the root parent
            fromNode.Cost   = 0;
            fromNode.Parent = null;

            open.Add(fromNode);

            // While there are paths that haven't been discovered
            while (open.Count > 0)
            {
                // Get best path from open and remove it, while adding it to closed
                current = open.Get();
                closed.Add(current);

                // If path has been found
                if (current.Position == to)
                {
                    break;
                }

                // Foreach moveset check if there are available moves
                foreach (Move move in moveable.MoveSet.moves)
                {
                    gridSize = new Vector2Int(move.footPrint.GetLength(0), move.footPrint.GetLength(1));
                    center   = move.Center();

                    foreach (Vector2Int vec in move.movePositions)
                    {
                        convertedPosition = new Vector2Int(current.Position.x + vec.x - center.x, current.Position.y + vec.y - center.y);

                        if (grid.IsOutOfBounds(convertedPosition.x, convertedPosition.y))
                        {
                            continue;
                        }

                        child = grid[convertedPosition.x, convertedPosition.y];

                        // If this is not walkable in grid and this is not the goal
                        if (!moveable.Walkable(child) && child.Position != to)
                        {
                            continue;
                        }

                        // If this has to be visible
                        if (move.directConnectionRequired)
                        {
                            if (!moveable.Walkable(current.Position, child.Position))
                            {
                                continue;
                            }
                        }

                        // If this has been investigated already
                        if (closed.Contains(child))
                        {
                            continue;
                        }

                        // If this is being investigated but the one being investigated is better
                        if (open.Contains(child))
                        {
                            if (child.Cost <= current.Value)
                            {
                                continue;
                            }
                        }

                        // Add child to open with correct information
                        child.Parent = current;
                        child.Cost   = current.Value;
                        child.Move   = move;
                        open.Add(child);
                    }
                }
            }

            // If no path has been found
            if (current.Position != to)
            {
                return;
            }

            path.Add(current);
            // Get complete path from current node
            while (current.Parent != null)
            {
                path.Add(current);
                current = current.Parent;
            }
        }