public void DecrementItemInInventory(string resourceName, int amount)
    {
        // Convert the name of the resource to 'ResourceTypes' enum, residing in Inventory
        Inventory.ResourceTypes resType = (Inventory.ResourceTypes)Enum.Parse(typeof(Inventory.ResourceTypes), resourceName.ToLower());

        // Call the related function residing in Inventory class to add the item corresponding to inputted arguments
        playerInventory.SubtractItemFromInventory(resType, amount);

        // Trigger an event called 'Resource amount changed': UIManager.cs is listening to update the ResourceGUI
        EventManager.TriggerEvent("Resource amount changed");
    }
    // Function to create individual resource GUI element
    //void CreateResourceGUI(Inventory.ResourceTypes type, int amount)
    //{
    //    // Instantiate the prefab
    //    GameObject newResource = Instantiate(resource);

    //    // Set its parent to this transform
    //    newResource.transform.SetParent(transform);

    //    // Change its name. Using enum to string would be a good idea here
    //    // We can then reverse it to get the 'Inventory.ResourceTypes' enum if need be
    //    newResource.name = type.ToString();

    //    // Depending on the resource type, put appropriate icon image, and corresponding amount
    //    switch (type)
    //    {
    //        case Inventory.ResourceTypes.log:
    //            newResource.GetComponentInChildren<RawImage>().texture = resourceIcons[0].texture;
    //            newResource.GetComponentInChildren<Text>().text = amount.ToString();
    //            break;
    //        case Inventory.ResourceTypes.rock:
    //            newResource.GetComponentInChildren<RawImage>().texture = resourceIcons[1].texture;
    //            newResource.GetComponentInChildren<Text>().text = amount.ToString();
    //            break;
    //        default:
    //            break;
    //    }

    //    // Add this newly created resource GUI element to resourceGUIContainer list
    //    // We can then iterate through this list to update the resource amounts
    //    resourceGUIContainer.Add(newResource);
    //}

    // Function for updating the resource GUI elements. This function will be called
    //  when 'Resource amount changed' event is triggered.
    void UpdateResourceGUI()
    {
        // Form a dictionary, whose key-value pairs are 'Inventory.ResourceTypes' and 'int'
        Dictionary <Inventory.ResourceTypes, int> inventoryContents = new Dictionary <Inventory.ResourceTypes, int>();

        // Populate the inventoryContents dictionary by calling the GetInventoryContent() method from PlayerInventory.cs
        inventoryContents = playerInventory.GetInventoryContent();

        //// Iterate through resourceGUIContainer list
        //foreach (GameObject resGUI in resourceGUIContainer)
        //{
        //    // Convert resourceGUI element's name to Inventory.ResourceTypes enum type
        //    Inventory.ResourceTypes type = (Inventory.ResourceTypes)Enum.Parse(typeof(Inventory.ResourceTypes), resGUI.name);

        //    // Update the resourceGUI element's amount
        //    resGUI.GetComponentInChildren<Text>().text = inventoryContents[type].ToString();
        //}

        foreach (GameObject resGUI in resourceGUIElements)
        {
            Inventory.ResourceTypes type = (Inventory.ResourceTypes)Enum.Parse(typeof(Inventory.ResourceTypes), "log");

            switch (resGUI.name)
            {
            case "LogGUI":
                // Convert resourceGUI element's name to Inventory.ResourceTypes enum type
                type = (Inventory.ResourceTypes)Enum.Parse(typeof(Inventory.ResourceTypes), "log");
                // Update the resourceGUI element's amount
                resGUI.GetComponentInChildren <Text>().text = inventoryContents[type].ToString();
                break;

            case "RockGUI":
                // Convert resourceGUI element's name to Inventory.ResourceTypes enum type
                type = (Inventory.ResourceTypes)Enum.Parse(typeof(Inventory.ResourceTypes), "rock");
                // Update the resourceGUI element's amount
                resGUI.GetComponentInChildren <Text>().text = inventoryContents[type].ToString();
                break;

            default:
                break;
            }
        }
    }
Beispiel #3
0
    bool CanBuildTower()
    {
        float distance = Vector3.Distance(player.transform.position, transform.position);

        // Check distance to player
        if (distance < minMaxDistance.x || distance > minMaxDistance.y)
        {
            return(false);
        }

        Collider[] perimeterObjects = Physics.OverlapSphere(transform.position, maxPerimeterRadius);

        // Check if there is any other tower or resources within a certain perimeter
        if (perimeterObjects.Length > 2)
        {
            return(false);
        }

        // Check if there is enough resource
        if (tag == "WoodGhost")
        {
            Inventory.ResourceTypes type = (Inventory.ResourceTypes)Enum.Parse(typeof(Inventory.ResourceTypes), "log");

            if (player.GetComponent <PlayerInventory>().GetInventory().GetInventoryContent()[type] < buildManager.towers[0].GetComponent <WoodTower>().woodCost)
            {
                return(false);
            }
        }
        else if (tag == "RockGhost")
        {
            Inventory.ResourceTypes type = (Inventory.ResourceTypes)Enum.Parse(typeof(Inventory.ResourceTypes), "rock");

            if (player.GetComponent <PlayerInventory>().GetInventory().GetInventoryContent()[type] < buildManager.towers[1].GetComponent <RockTower>().rockCost)
            {
                return(false);
            }
        }

        // If all the conditions are met, then signal the ability for tower to be built
        return(true);
    }