Пример #1
0
 public void BuildPathMarker(PathNode newPath)
 {
     ResetPathMarkers();
     m_currentPath = newPath;
     while (newPath != null)
     {
         SetTileColor(newPath.GetNodePosition(), Color.Blue);
         newPath = newPath.GetNextNode();
     }
 }
        /// <summary>
        /// This method attempts to plot a path between the startPosition and the endPosition.
        /// If a path is found,  the method returns true and the destPos contains the position
        /// of the first node in the path list.
        /// </summary>
        /// <param name="startPosition">X,Y coordinates of the start position in pixels.</param>
        /// <param name="endPosition">X,Y coordinates of the end position in pixels.</param>
        /// <param name="destPos">X,Y coordinates of the first path node position in pixels, if one is found.</param>
        /// <returns>True if a path is found.</returns>
        public bool FindPathToDestination(Vector2 startPosition, Vector2 endPosition, out Vector2 destPos)
        {
            Vector2 startIndex = Map.MapHandler.GetInstance().GetTileIndex(startPosition);
            Vector2 endIndex = Map.MapHandler.GetInstance().GetTileIndex(endPosition);

            Vector2 closestSoFar = endIndex;
            // Check if the destination is solid
            bool isDestSolid = MapHandler.GetInstance().IsTileSolid(endIndex);
            if (isDestSolid)
            {
                List<Vector2> adj = MapHandler.GetInstance().GetAdjacentsToTile(endIndex);
                if (adj != null && adj.Count>0)
                {
                    closestSoFar = adj[0];
                    foreach (Vector2 vec in adj)
                    {
                        if (!MapHandler.GetInstance().IsTileSolid(vec))
                        {
                            // Find the closest adjecent to the caller.
                            if (Vector2.Distance(closestSoFar, startIndex) > Vector2.Distance(vec, startIndex))
                            {
                                closestSoFar = vec;
                            }
                            //endIndex = vec;
                            //break;
                        }
                    }
                }
            }
            endIndex = closestSoFar;
            // Call Pathfinder find a path from the startIndex to the end index.
            m_pnCurrPath = m_pfMyPathFinder.FindPath(startIndex, endIndex);

            // If a path was found, then make the first node in the
            // path the first destination of this entity.
            if (m_pnCurrPath != null)
            {
                 destPos = Map.MapHandler.GetInstance().GetTilePosition(m_pnCurrPath.GetNodePosition());

                 return true;
                //base.MoveTo(targetPos);
            }
            destPos = Vector2.Zero;
            return false ;
        }