// Update is called once per frame
    void Update()
    {
        if (GameStateManager.Instance.CurrState == GameStateManager.State.Plan)
        {
            RaycastHit hit;
            if (MouseRaycast("Foundation", out hit))
            {
                if (CurrBuilding != null)
                {
                    Tile hitTile = hit.transform.GetComponent <Tile>();
                    if (hitTile != null)
                    {
                        if (hitTile.OccupyingBuilding == null && !hitTile.EnvOccupied)
                        {
                            if (hitTile.Hovered == false)
                            {
                                TileManager.Instance.UnhoverAllTiles();
                                hitTile.SetHoverColor();
                                hitTile.Hovered = true;
                            }

                            CurrBuilding.TileUnder          = hitTile;
                            CurrBuilding.transform.position = hit.transform.position + CurrBuilding.GetComponent <Building>().SpawnHeight *CurrBuilding.transform.up;
                        }
                    }
                }
            }

            if ((Input.GetMouseButtonDown(1) || (Input.GetMouseButtonDown(0) && (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)))) && allowDeletingBuildings)
            {
                CancelBuilding();
            }
            else if (Input.GetMouseButtonDown(0))
            {
                if (MouseRaycast("Building", out hit))
                {
                    if (CurrBuilding != null)
                    {
                        if (hit.transform.GetComponent <Building>() == CurrBuilding)
                        {
                            TileManager.Instance.UnhoverAllTiles();

                            // Place current building and set Tile its hovering over as occupied
                            CurrBuilding.TileUnder.OccupyingBuilding = CurrBuilding.gameObject;
                            TileManager.Instance.SetTileOccupied(CurrBuilding.TileUnder);
                            CurrBuilding.PlaceBuilding();
                            CurrBuilding.transform.parent = CurrBuilding.TileUnder.transform;

                            // SFX
                            audioSource.PlayOneShot(PlaceBuilding);

                            CurrBuilding = null;
                            if (Input.GetKey(KeyCode.LeftShift))
                            {
                                MoneyManager.Instance.CopyItem();
                            }
                        }
                        else
                        {
                            /*
                             * TileManager.Instance.UnhoverAllTiles();
                             *
                             * // Place current building and set Tile its hovering over as occupied
                             * CurrBuilding.TileUnder.OccupyingBuilding = CurrBuilding.gameObject;
                             * TileManager.Instance.SetTileOccupied(CurrBuilding.TileUnder);
                             * CurrBuilding.PlaceBuilding();
                             *
                             *
                             * // Pickup other building we've selected
                             * CurrBuilding = hit.transform.GetComponent<Building>();
                             * CurrBuilding.TileUnder.OccupyingBuilding = null;
                             * CurrBuilding.TileUnder.SetHoverColor();
                             * TileManager.Instance.SetTileUnoccupied(CurrBuilding.TileUnder);
                             * CurrBuilding.PickUpBuilding();
                             *
                             * // SFX
                             * audioSource.PlayOneShot(PickupBuilding);
                             */
                        }
                    }
                    else if (allowPickingUpBuildings)
                    {
                        // Pickup building
                        CurrBuilding = hit.transform.GetComponent <Building>();
                        Wall wallComponent = CurrBuilding.GetComponent <Wall>();
                        while (wallComponent != null)
                        {
                            Tile tileAbove = wallComponent.TileAbove;
                            if (tileAbove.OccupyingBuilding != null)
                            {
                                Building nextBuilding = tileAbove.OccupyingBuilding.transform.GetComponent <Building>();
                                if (nextBuilding != null)
                                {
                                    CurrBuilding  = nextBuilding;
                                    wallComponent = CurrBuilding.GetComponent <Wall>();
                                }
                            }
                            else
                            {
                                wallComponent = null;
                            }
                        }
                        CurrBuilding.GetComponent <Building>().TileUnder.OccupyingBuilding = null;
                        TileManager.Instance.SetTileUnoccupied(CurrBuilding.TileUnder);
                        CurrBuilding.GetComponent <Building>().PickUpBuilding();

                        MoneyManager.Instance.UpdateLastItem(CurrBuilding.BuildingName);

                        // SFX
                        audioSource.PlayOneShot(PickupBuilding);
                    }
                }
            }

            // Handle additional options for specifc buildings bound to the a and d keys
            if (CurrBuilding != null && allowRotatingBuildings)
            {
                switch (CurrBuilding.GetComponent <Building>().BuildingName)
                {
                case WALL:
                    // Don't toggle wall height anymore
                    break;

                case ARROW:
                    if (Input.GetKeyDown(Toggle1))
                    {
                        CurrBuilding.transform.RotateAround(
                            CurrBuilding.transform.position,
                            CurrBuilding.transform.up,
                            45f);
                        CurrBuilding.GetComponent <AudioSource>().Play();
                    }
                    else if (Input.GetKeyDown(Toggle2))
                    {
                        CurrBuilding.transform.RotateAround(
                            CurrBuilding.transform.position,
                            CurrBuilding.transform.up,
                            -45f);
                        CurrBuilding.GetComponent <AudioSource>().Play();
                    }
                    break;

                case TRAMPOLINE:
                    // Do nothing
                    break;

                case HOLDING:
                    Holding currHolding = (Holding)CurrBuilding;
                    if (Input.GetKeyDown(Toggle1))
                    {
                        currHolding.IncrementThreshold();
                    }
                    else if (Input.GetKeyDown(Toggle2))
                    {
                        currHolding.DecrementThreshold();
                    }
                    break;
                }
            }

            if (CurrBuilding != null)
            {
                BuildingSelected = true;
            }
            else
            {
                BuildingSelected = false;
            }
        }
    }