// Selects a tile by:
    //  1) Placing a rune, if none exists
    //  2) Showing the slider and the 'remove rune' button
    void SelectTile(Vector2 tile)
    {
        selected = true;

        // First check if a rune exists at this tile
        bool runeExists = false;

        foreach (PlacedRune p in placedRuneList)
        {
            if (p.tile == tile)
            {
                selRune    = p;
                runeExists = true;
                break;
            }
        }
        // If not, place one first
        if (!runeExists)
        {
            // If out of runes to place, stop instead
            if (noneToPlace)
            {
                return;
            }
            selRune = PlaceRune(tile);
        }

        // Tell SliderChangeCheck() to ignore these changes
        resetingSlider = true;

        // Adjust the slider's config for this rune
        rotationSlider.minValue = selRune.min_rot;
        rotationSlider.maxValue = selRune.max_rot;
        rotationSlider.value    = selRune.curr_rot;

        // Put the rotation slider either above or below the rune
        float upOrDown = selRune.curr_rot % 360;

        upOrDown = (upOrDown > 180f) ? (360 - upOrDown) : upOrDown;
        upOrDown = (upOrDown < -180f) ? (360 + upOrDown) : upOrDown;

        Vector2 sliderPos = tile + (new Vector2(0f, -1f));

        if (Mathf.Abs(upOrDown) > 90)
        {
            sliderPos = tile + (new Vector2(0f, 1f));
        }

        // Move the slider and show to the player
        rotationSlider.gameObject.transform.position = sliderPos;
        rotationSlider.gameObject.SetActive(true);

        // Tell SliderChangeCheck() to start listening for changes again
        resetingSlider = false;

        // Move the delete button on top of the tile and show it
        deleteButton.gameObject.transform.position = tile;
        deleteButton.gameObject.SetActive(true);
    }
    // Turns rune placement mode on or off (true or false)
    public void InPlacementMode(bool update)
    {
        canPlace = update;

        if (update)
        {
            // Make sure the cursor is visible
            Cursor.visible = true;
            // Re-activate if issues are caused by mouse going off-screen:
            //Cursor.lockState = CursorLockMode.Confined;

            // Reset vars
            selected       = false;
            resetingSlider = false;
            lastClickTile  = new Vector2(0f, 0f);
            selRune        = new PlacedRune(null, new Vector2(0f, 0f), 0f, 0f, 0f, null);
            tilePos        = new Vector2(0f, 0f);
            hovering       = false;

            // Show all icons for each rune
            foreach (PlacedRune p in placedRuneList)
            {
                p.icon.SetActive(true);
            }

            // Show the rune selection UI
            rc.GetComponent <Canvas>().enabled = true;
        }
        else
        {
            // Turn off the highlight, slider, and button
            selectButton.SetActive(false);
            rotationSlider.gameObject.SetActive(false);
            deleteButton.gameObject.SetActive(false);

            // Hide all icons for each rune
            foreach (PlacedRune p in placedRuneList)
            {
                p.icon.SetActive(false);
            }

            // Hide the rune selection UI
            rc.GetComponent <Canvas>().enabled = false;
        }
    }
    // Invoked when the delete button is clicked
    public void DeleteRune()
    {
        // Delete the tile icon
        Destroy(selRune.icon);

        // Remove the currently selected rune from the list
        placedRuneList.Remove(selRune);

        // Let RuneCount know we deleted one
        rc.PickedUpRune(selRune.rune.type);

        // Call the delete function for the rune
        selRune.rune.DestroyRune();

        // Hide the slider and delete button
        deleteButton.gameObject.SetActive(false);
        rotationSlider.gameObject.SetActive(false);

        // Unselect this tile
        selRune       = new PlacedRune(null, new Vector2(0f, 0f), 0f, 0f, 0f, null);
        selected      = false;
        lastClickTile = new Vector2(0f, 0f);
    }
    // Places a rune at the clicked tile
    PlacedRune PlaceRune(Vector2 tile)
    {
        Vector2    runeBase = tile;
        GameObject prefab   = null;
        float      rot      = 0f;
        // The ray from the clicked tile to runeBase
        Vector2 point = new Vector2(0f, 0f);

        // Find which side to place the base of the rune
        Vector2 topTile    = new Vector2(tile.x, tile.y + 1f);
        Vector2 bottomTile = new Vector2(tile.x, tile.y - 1f);
        Vector2 leftTile   = new Vector2(tile.x - 1f, tile.y);
        Vector2 rightTile  = new Vector2(tile.x + 1f, tile.y);

        if (!env.OverlapPoint(topTile))
        {
            runeBase = topTile;
            rot      = 0f;
            point    = new Vector2(0f, 1f);
        }
        else if (!env.OverlapPoint(bottomTile))
        {
            runeBase = bottomTile;
            rot      = 180f;
        }
        else if (!env.OverlapPoint(leftTile))
        {
            runeBase = leftTile;
            rot      = 90f;
        }
        else if (!env.OverlapPoint(rightTile))
        {
            runeBase = rightTile;
            rot      = 270f;
        }


        // Get the prefab for the current rune type
        prefab = (GameObject)Resources.Load(runePrefabPath, typeof(GameObject));

        // Instantiate a new (inactive) rune from prefab
        GameObject newRune = Instantiate(prefab, runeBase, Quaternion.Euler(0f, 0f, rot), GameObject.FindWithTag("AllRunes").transform);

        // Configure its properties
        newRune.GetComponent <Rune>().rot = rot;
        // Activate to call its Start() function
        newRune.SetActive(true);

        // Determine the range of rotation to be stored in the struct, starting at the rune's curr rot

        // Check clockwise until encounters wall or comes full circle
        float min = rot;

        while (!env.OverlapPoint(tile + RotToVector(min - 1f)) && ((rot - min) < 360))
        {
            min -= 1;
        }
        // Check counter-clockwise until encounters wall or comes full circle
        float max = rot;

        while (!env.OverlapPoint(tile + RotToVector(max + 1f)) && ((max - rot) < 360))
        {
            max += 1;
        }

        // Make a new tile icon for this rune
        prefab = (GameObject)Resources.Load(iconPrefabPath, typeof(GameObject));
        GameObject newTile = Instantiate(prefab, tile, Quaternion.Euler(0f, 0f, 0f), transform.parent);

        // Put the rune, its position, the min-max rotations, curr rot, and tile into the array
        PlacedRune rinfo = new PlacedRune(newRune.GetComponent <Rune>(), tile, rot, min, max, newTile);

        placedRuneList.Add(rinfo);

        // Let RuneCount know we consumed one rune
        rc.PlacedARune();

        return(rinfo);
    }