Example #1
0
        private List <RLBaseTile> GetNeighborTiles(RLBaseTile tile, List <Vector2Int> directions)
        {
            int x = tile.GetDisplayPosition().x;
            int y = tile.GetDisplayPosition().y;

            List <RLBaseTile> neighbors = new List <RLBaseTile>();

            foreach (Vector2Int direction in directions)
            {
                int nX = x + direction.x;
                int nY = y + direction.y;

                if (nX >= 0 && nX < Tiles.GetLength(0) && nY >= 0 && nY < Tiles.GetLength(1))
                {
                    neighbors.Add(Tiles[nX, nY]);
                }
            }

            return(neighbors);
        }
Example #2
0
 public virtual bool IsTargetInRange(RLBaseActor skillUser, RLBaseTile targetTile)
 {
     return(range <= 0 || Vector3.Distance(skillUser.GetTile().GetDisplayPosition(), targetTile.GetDisplayPosition()) <= range);
 }
Example #3
0
 private void DisplayTile(RLBaseTile tile)
 {
     environmentTilemap.SetTile(tile.GetDisplayPosition(), tileset[tile.GetDisplayType()]);
     memoryFogTilemap.SetTile(tile.GetDisplayPosition(), tile.IsCurrentlyVisibleToPlayer() ? null : tileset.memoryFogTile);
     fogOfWarTilemap.SetTile(tile.GetDisplayPosition(), tile.HasEverBeenSeenByPlayer() ? null : tileset.fogOfWarTile);
 }
Example #4
0
        private static List <RLBaseTile> AStarPathfind(DungeonFloor level, RLBaseTile start, RLBaseTile finish, CanMoveDelegate canMove, PathCostDelegate pathCost)
        {
            // A* Pathfinding
            Debug.Log("Connecting " + start.GetDisplayPosition() + " to " + finish.GetDisplayPosition() + " in " + level);
            List <RLBaseTile> closedSet = new List <RLBaseTile>();
            List <RLBaseTile> openSet   = new List <RLBaseTile>();

            openSet.Add(start);

            Dictionary <RLBaseTile, RLBaseTile> cameFrom = new Dictionary <RLBaseTile, RLBaseTile>();
            Dictionary <RLBaseTile, float>      gScore   = new Dictionary <RLBaseTile, float>();

            gScore[start] = 0;

            Dictionary <RLBaseTile, float> fScore = new Dictionary <RLBaseTile, float>();

            fScore[start] = Mathf.Abs(start.GetDisplayPosition().x - finish.GetDisplayPosition().x) + Mathf.Abs(start.GetDisplayPosition().y - finish.GetDisplayPosition().y);
            int length = 0;

            while (openSet.Count > 0)
            {
                length++;
                openSet.Sort((first, second) =>
                {
                    float firstScore;
                    float secondScore;
                    if (!fScore.TryGetValue(first, out firstScore))
                    {
                        firstScore = float.PositiveInfinity;
                    }
                    if (!fScore.TryGetValue(second, out secondScore))
                    {
                        secondScore = float.PositiveInfinity;
                    }
                    if (firstScore < secondScore)
                    {
                        return(-1);
                    }
                    else if (firstScore > secondScore)
                    {
                        return(1);
                    }
                    return(0);
                });
                RLBaseTile current = openSet[0];
                if (current == finish)
                {
                    // write and return path
                    List <RLBaseTile> hallTiles = ReconstructPath(current, cameFrom);
                    Debug.Log("Successful path from " + start.GetDisplayPosition() + " to " + finish.GetDisplayPosition());
                    return(hallTiles);
                }
                openSet.Remove(current);
                closedSet.Add(current);

                List <RLBaseTile> neighbors = level.GetOrthogonalNeighborTiles(current);
                neighbors.RemoveAll((tile) => closedSet.Contains(tile) || !canMove(current, tile));
                foreach (RLBaseTile neighbor in neighbors)
                {
                    if (!openSet.Contains(neighbor))
                    {
                        openSet.Add(neighbor);
                    }
                    float neighborGscore;
                    if (!gScore.TryGetValue(neighbor, out neighborGscore))
                    {
                        neighborGscore = float.PositiveInfinity;
                    }
                    // currently using 1 as dist_between
                    float tentativeGScore = gScore[current] + pathCost(current, neighbor);
                    if (tentativeGScore >= neighborGscore)
                    {
                        continue;
                    }
                    cameFrom[neighbor] = current;
                    gScore[neighbor]   = tentativeGScore;
                    fScore[neighbor]   = gScore[neighbor] + Mathf.Abs(neighbor.GetDisplayPosition().x - finish.GetDisplayPosition().x) + Mathf.Abs(neighbor.GetDisplayPosition().y - finish.GetDisplayPosition().y);
                }
            }
            Debug.Log("Connection failed.");
            return(new List <RLBaseTile>());
        }