Example #1
0
    public static float RegionBlockDistance(TileManager.Map.RegionBlock startRegionBlock, TileManager.Map.RegionBlock endRegionBlock, bool careAboutWalkability, bool walkable, bool allowEndTileNonWalkable)
    {
        if (!allowEndTileNonWalkable && (careAboutWalkability && (startRegionBlock.tileType.walkable != endRegionBlock.tileType.walkable || startRegionBlock.tiles[0].region != endRegionBlock.tiles[0].region)))
        {
            return(Mathf.Infinity);
        }

        PathfindingRegionBlock        currentRegionBlock = new PathfindingRegionBlock(startRegionBlock, null, 0);
        List <PathfindingRegionBlock> frontier           = new List <PathfindingRegionBlock>()
        {
            currentRegionBlock
        };
        List <TileManager.Map.RegionBlock> checkedBlocks = new List <TileManager.Map.RegionBlock>()
        {
            startRegionBlock
        };

        while (frontier.Count > 0)
        {
            currentRegionBlock = frontier[0];
            if (currentRegionBlock.regionBlock == endRegionBlock)
            {
                float distance = 0;
                while (currentRegionBlock.cameFrom != null)
                {
                    distance          += Vector2.Distance(currentRegionBlock.regionBlock.averagePosition, currentRegionBlock.cameFrom.regionBlock.averagePosition);
                    currentRegionBlock = currentRegionBlock.cameFrom;
                }
                return(distance);
            }
            frontier.RemoveAt(0);
            foreach (TileManager.Map.RegionBlock regionBlock in currentRegionBlock.regionBlock.horizontalSurroundingRegionBlocks)
            {
                if ((allowEndTileNonWalkable && regionBlock == endRegionBlock || (!careAboutWalkability || regionBlock.tileType.walkable == walkable)) && !checkedBlocks.Contains(regionBlock))
                {
                    PathfindingRegionBlock pRegionBlock = new PathfindingRegionBlock(regionBlock, currentRegionBlock, Vector2.Distance(regionBlock.averagePosition, endRegionBlock.averagePosition));
                    frontier.Add(pRegionBlock);
                    checkedBlocks.Add(regionBlock);
                }
            }
            frontier = frontier.OrderBy(regionBlock => regionBlock.cost).ToList();
        }
        return(Mathf.Infinity);
    }
Example #2
0
 public PathfindingRegionBlock(TileManager.Map.RegionBlock regionBlock, PathfindingRegionBlock cameFrom, float cost)
 {
     this.regionBlock = regionBlock;
     this.cameFrom    = cameFrom;
     this.cost        = cost;
 }