/// <summary>
 /// This method creates the list of all the AStar pathnodes.
 /// </summary>
 public static void GeneratePathfindingOverlay()
 {
     foreach (Vector3 graphNode in aStar.GraphNodes)
     {
         Vector3Int graphNodePosOnGrid = grid.WorldToCell(graphNode);
         RangeOverlayTiles.Add(graphNodePosOnGrid);
     }
 }
    /// <summary>
    /// This method draws an overlay tile on the Unit's tile position, this is meant for spells that can be casted on the caster.
    /// </summary>
    /// <param name="selectedTilePos"></param>
    private static void DrawSelfOverlay(Vector3Int selectedTilePos)
    {
        if (floorsTilemap.HasTile(selectedTilePos))
        {
            RangeOverlayTiles.Add(selectedTilePos);
        }

        rangeOverlayTilemap.SetTile(RangeOverlayTiles[0], confirmedTile);
    }
    /// <summary>
    /// This method draws a square overlay.
    /// </summary>
    /// <param name="selectedTilePos"></param>
    /// <param name="range"></param>
    private static void DrawSquareOverlay(Vector3Int selectedTilePos, int range)
    {
        for (int x = 0; x <= range; x++)
        {
            for (int y = 0; y <= range; y++)
            {
                Vector3Int tile = new Vector3Int(selectedTilePos.x + x, selectedTilePos.y + y, 0);

                if (floorsTilemap.HasTile(tile))
                {
                    RangeOverlayTiles.Add(tile);
                }
            }
        }

        for (int x = 0; x <= range; x++)
        {
            for (int y = 0; y <= range; y++)
            {
                Vector3Int tile = new Vector3Int(selectedTilePos.x + x, selectedTilePos.y - y, 0);

                if (floorsTilemap.HasTile(tile))
                {
                    RangeOverlayTiles.Add(tile);
                }
            }
        }

        for (int x = 0; x <= range; x++)
        {
            for (int y = 0; y <= range; y++)
            {
                Vector3Int tile = new Vector3Int(selectedTilePos.x - x, selectedTilePos.y + y, 0);

                if (floorsTilemap.HasTile(tile))
                {
                    RangeOverlayTiles.Add(tile);
                }
            }
        }

        for (int x = 0; x <= range; x++)
        {
            for (int y = 0; y <= range; y++)
            {
                Vector3Int tile = new Vector3Int(selectedTilePos.x - x, selectedTilePos.y - y, 0);

                if (floorsTilemap.HasTile(tile))
                {
                    RangeOverlayTiles.Add(tile);
                }
            }
        }
    }
    public static void ClearTiles()
    {
        foreach (Vector3Int t in PathNodesOverlayTiles)
        {
            pathfindingOverlayTilemap.SetTile(t, null);
        }

        foreach (Vector3Int t in RangeOverlayTiles)
        {
            rangeOverlayTilemap.SetTile(t, null);
        }

        cursorOverlayTilemap.SetTile(InputManager.Instance.previousTileMousePos, null);

        RangeOverlayTiles.Clear();
        PathNodesOverlayTiles.Clear();
    }
    /// <summary>
    /// This function actually set the proper tile colors and visually show the tile on the map.
    /// </summary>
    /// <param name="currentMousePosition"></param>
    private static void DrawRangeOverlay(Vector3Int currentMousePosition)
    {
        foreach (Vector3Int t in RangeOverlayTiles)
        {
            cursorOverlayTilemap.SetTile(t, null);

            if (GetUnitAtTile(t) != null && overlayProperties.canUseAbility)
            {
                if (GetUnitAtTile(t) is AIEnemy)
                {
                    rangeOverlayTilemap.SetTile(t, confirmedTile);
                }
                else
                {
                    if (overlayProperties.canCastOnFriendlies)
                    {
                        if (overlayProperties.canDamageFriendlies)
                        {
                            rangeOverlayTilemap.SetTile(t, warningTile);
                        }
                        else
                        {
                            rangeOverlayTilemap.SetTile(t, confirmedTile);
                        }
                    }
                }
            }
            else
            {
                rangeOverlayTilemap.SetTile(t, deniedTile);
            }
        }

        if (!realtimeOverlay.isEnabled && aStar.selected == null)
        {
            rangeOverlayTilemap.SetTile(currentMousePosition, null);
            cursorOverlayTilemap.SetTile(currentMousePosition, deniedTile);
            RangeOverlayTiles.Remove(currentMousePosition);
        }
    }
    /// <summary>
    /// This method is going to draw a "diamond" shape overlay.
    /// </summary>
    /// <param name="selectedTilePos"></param>
    /// <param name="range"></param>
    private static void DrawDiamondOverlay(Vector3Int selectedTilePos, int range)
    {
        if (range > 0)
        {
            for (int x = 0; x <= range; x++)
            {
                for (int y = 0; y <= range - x; y++)
                {
                    Vector3Int tile = new Vector3Int(selectedTilePos.x + x, selectedTilePos.y + y, 0);

                    if (floorsTilemap.HasTile(tile) && !RangeOverlayTiles.Contains(tile))
                    {
                        RangeOverlayTiles.Add(tile);
                    }
                }
            }

            for (int x = 0; x <= range; x++)
            {
                for (int y = 0; y <= range - x; y++)
                {
                    Vector3Int tile = new Vector3Int(selectedTilePos.x + x, selectedTilePos.y - y, 0);

                    if (floorsTilemap.HasTile(tile) && !RangeOverlayTiles.Contains(tile))
                    {
                        RangeOverlayTiles.Add(tile);
                    }
                }
            }

            for (int x = 0; x <= range; x++)
            {
                for (int y = 0; y <= range - x; y++)
                {
                    Vector3Int tile = new Vector3Int(selectedTilePos.x - x, selectedTilePos.y + y, 0);

                    if (floorsTilemap.HasTile(tile) && !RangeOverlayTiles.Contains(tile))
                    {
                        RangeOverlayTiles.Add(tile);
                    }
                }
            }

            for (int x = 0; x <= range; x++)
            {
                for (int y = 0; y <= range - x; y++)
                {
                    Vector3Int tile = new Vector3Int(selectedTilePos.x - x, selectedTilePos.y - y, 0);

                    if (floorsTilemap.HasTile(tile) && !RangeOverlayTiles.Contains(tile))
                    {
                        RangeOverlayTiles.Add(tile);
                    }
                }
            }
        }
        else
        {
            Vector3Int tile = new Vector3Int(selectedTilePos.x, selectedTilePos.y, 0);

            if (floorsTilemap.HasTile(tile))
            {
                RangeOverlayTiles.Add(tile);
            }
        }
    }
    /// <summary>
    /// This method is going to draw a "+" overlay according to a certain range.
    /// </summary>
    /// <param name="selectedTilePos"></param>
    /// <param name="range"></param>
    private static void DrawCrossOverlay(Vector3Int selectedTilePos, int range)
    {
        for (int i = 0; i <= range; i++)
        {
            Vector3Int tile = new Vector3Int(selectedTilePos.x + i, selectedTilePos.y, 0);

            if (!RangeOverlayTiles.Contains(tile))
            {
                if (floorsTilemap.HasTile(tile) && !CheckIfWall(tile) || !floorsTilemap.HasTile(tile) && CheckIfPit(tile) || !floorsTilemap.HasTile(tile) && CheckIfVoid(tile))
                {
                    RangeOverlayTiles.Add(tile);
                }
                else
                {
                    break;
                }
            }
        }

        for (int i = 0; i <= range; i++)
        {
            Vector3Int tile = new Vector3Int(selectedTilePos.x, selectedTilePos.y + i, 0);

            if (!RangeOverlayTiles.Contains(tile))
            {
                if (floorsTilemap.HasTile(tile) && !CheckIfWall(tile) || !floorsTilemap.HasTile(tile) && CheckIfPit(tile) || !floorsTilemap.HasTile(tile) && CheckIfVoid(tile))
                {
                    RangeOverlayTiles.Add(tile);
                }
                else
                {
                    break;
                }
            }
        }

        for (int i = 0; i <= range; i++)
        {
            Vector3Int tile = new Vector3Int(selectedTilePos.x - i, selectedTilePos.y, 0);

            if (!RangeOverlayTiles.Contains(tile))
            {
                if (floorsTilemap.HasTile(tile) && !CheckIfWall(tile) || !floorsTilemap.HasTile(tile) && CheckIfPit(tile) || !floorsTilemap.HasTile(tile) && CheckIfVoid(tile))
                {
                    RangeOverlayTiles.Add(tile);
                }
                else
                {
                    break;
                }
            }
        }

        for (int i = 0; i <= range; i++)
        {
            Vector3Int tile = new Vector3Int(selectedTilePos.x, selectedTilePos.y - i, 0);

            if (!RangeOverlayTiles.Contains(tile))
            {
                if (floorsTilemap.HasTile(tile) && !CheckIfWall(tile) || !floorsTilemap.HasTile(tile) && CheckIfPit(tile) || !floorsTilemap.HasTile(tile) && CheckIfVoid(tile))
                {
                    RangeOverlayTiles.Add(tile);
                }
                else
                {
                    break;
                }
            }
        }
    }