예제 #1
0
    private void CompileCoordinates()
    {
        m_pathfindingInformation = new PathfindingInformation();

        foreach (Tile tile in m_currentTile.FindTilePath(new List <Tile>()))
        {
            if (tile.m_position != m_startTile.m_position)
            {
                m_pathfindingInformation.m_path.Add(tile.m_position * m_tileSize);
            }
        }

        //m_pathfindingInformation.m_path.Insert(0, m_endTile.m_position * m_tileSize);
    }
예제 #2
0
    private void Start()
    {
        m_pathfindingMap = new TileState[(int)m_mapSize.x, (int)m_mapSize.y];

        int priority = 0;

        foreach (Enemy enemy in FindObjectsOfType <Enemy>())
        {
            enemy.m_priority = priority;
            priority++;
        }

        foreach (Blocker blocker in FindObjectsOfType <Blocker>())
        {
            Vector2 pos = new Vector2(Mathf.RoundToInt(blocker.transform.position.x), Mathf.RoundToInt(blocker.transform.position.y)) / m_tileSize;
            UpdatePathfindingMap(pos, TileState.Closed, null);
        }

        m_pathfindingInformation = new PathfindingInformation();

        m_initialised = true;
    }
예제 #3
0
    //NOTE: pass in real-world positions into here- not array positions, we will convert from real-world to array in the function
    private IEnumerator FindPathCoroutine(Vector2 givenStartPosition, Vector2 givenEndPosition, TileState[,] givenPathfindingMap)
    {
        m_start = new Vector2(Mathf.RoundToInt(givenStartPosition.x / m_tileSize), Mathf.RoundToInt(givenStartPosition.y / m_tileSize));
        m_end   = new Vector2(Mathf.RoundToInt(givenEndPosition.x / m_tileSize), Mathf.RoundToInt(givenEndPosition.y / m_tileSize));

        m_visitedTiles = new int[givenPathfindingMap.GetLength(0), givenPathfindingMap.GetLength(1)];

        m_openTiles = new List <Tile>();

        m_startTile = new Tile();
        m_endTile   = new Tile();

        m_startTile.m_position = m_start;
        m_endTile.m_position   = m_end;

        m_currentTile = m_startTile;

        m_visitedTiles[(int)m_currentTile.m_position.x, (int)m_currentTile.m_position.y] = 1;

        m_iterationCount = 0;

        while (m_currentTile.m_position != m_endTile.m_position)
        {
            m_iterationCount++;
            m_cheapestTileCost = float.MaxValue;

            foreach (Tile tile in FindAdjacentTiles(m_currentTile, m_endTile, givenPathfindingMap))
            {
                bool tileAlreadyExists = false;

                for (int i = 0; i < m_openTiles.Count; i++)
                {
                    //if the tile already exists
                    if (m_openTiles[i].m_position == tile.m_position)
                    {
                        m_openTiles[i].m_pathCost = tile.m_pathCost;
                        tileAlreadyExists         = true;
                    }
                }

                if (!tileAlreadyExists)
                {
                    m_openTiles.Insert(0, tile); //put at front of list, as recently looked at adjacent tiles are likely to be most efficient to be looked at next
                }
            }

            foreach (Tile tile in m_openTiles)
            {
                if ((tile.m_pathCost + (int)tile.m_directDistance) < m_cheapestTileCost)
                {
                    m_cheapestTileCost = tile.m_pathCost + (int)tile.m_directDistance;
                    m_currentTile      = tile;
                }
            }
            m_visitedTiles[(int)m_currentTile.m_position.x, (int)m_currentTile.m_position.y] = 1;
            m_openTiles.Remove(m_currentTile);

            if (m_iterationCount > 100) //this prevents too much work being carried out in one frame, so the game won't freeze
            {
                m_iterationCount = 0;
                yield return(null);
            }

            if (m_openTiles.Count == 0) //explored all tiles, means we've tried to get to a blocked area
            {
                m_pathfindingInformation = new PathfindingInformation();
                m_pathfindingInformation.m_canReachTile = false;
                StopCoroutine("FindPathCoroutine");
            }
        }
    }