Exemple #1
0
    /// <summary> Shows the building menu (see <see cref="buildingMenuPrefab"/>) at the same
    /// screen position as the parameter LymphNode <paramref name="lymphNode"/>. Called by a
    /// LymphNode object when it is clicked without a tower on it. </summary>
    internal GameObject ShowBuildingMenu(Transform lymphNode)
    {
        GameObject buildingMenu = Instantiate(buildingMenuPrefab, new Vector3(0f, 0f, -1.2f), activeGameUIPanel.rotation);

        buildingMenu.transform.SetParent(activeGameUIPanel, false);

        /////////////////////////////////////

        // gather the cost data

        // todo: the building menu should dim towers that are unavailable for whatever reason

        ShopMenu shopMenu = buildingMenu.GetComponent <ShopMenu>();
        Shop     shop     = Shop.instance;

        if (shopMenu != null)
        {
            int cost1 = -1;
            int cost2 = -1;
            int cost3 = -1;
            int cost4 = -1;
            if (shop.tower1 != null)
            {
                cost1 = shop.tower1.GetBaseLevelCost();
            }
            if (shop.tower2 != null)
            {
                cost2 = shop.tower2.GetBaseLevelCost();
            }
            if (shop.tower3 != null)
            {
                cost3 = shop.tower3.GetBaseLevelCost();
            }
            if (shop.tower4 != null)
            {
                cost4 = shop.tower4.GetBaseLevelCost();
            }
            shopMenu.SetCostTower1(cost1);
            shopMenu.SetCostTower2(cost2);
            shopMenu.SetCostTower3(cost3);
            shopMenu.SetCostTower4(cost4);
        }

        /////////////////////////////////////

        // UI elements and other scene objects use different coordinate systems;
        // in order to position the menu where the lymph node is (on the screen)...
        // ...we have to do some conversions between World and Viewport
        RectTransform buildingMenuRT       = buildingMenu.GetComponent <RectTransform>();
        RectTransform canvasRT             = activeGameUIPanel.parent.GetComponent <RectTransform>();                                           // this assumes that this panel is the child of the top-level UI panel
        Vector2       viewportPosition     = Camera.main.WorldToViewportPoint(lymphNode.position);
        Vector2       uiOffset             = new Vector2((float)canvasRT.sizeDelta.x / 2f, (float)canvasRT.sizeDelta.y / 2f);                   // screen offset for the canvas
        Vector2       proportionalPosition = new Vector2(viewportPosition.x * canvasRT.sizeDelta.x, viewportPosition.y * canvasRT.sizeDelta.y); // position on the canvas

        // set the position and remove the screen offset
        buildingMenuRT.localPosition = proportionalPosition - uiOffset;

        return(buildingMenu);
    }