Пример #1
0
    /* Crafting example code */
    public void OnInventoryCombineClick()
    {
        var s         = FirstInventoryItemsList.options[FirstInventoryItemsList.value].text;
        var itemFirst = GetItemByComboText(s);

        s = SecondInventoryItemsList.options[SecondInventoryItemsList.value].text;
        var itemSecond = GetItemByComboText(s);

        if (itemFirst == null || itemSecond == null)
        {
            return;
        }

        // First, we need to know if there are any combinations available with those two items
        var combinatoryResult = ZaraEngine.Inventory.InventoryItemsCombinatoryFactory.Instance.GetMatchedCombinations(itemFirst, itemSecond); // can be any number of items
        var combinationId     = Guid.Empty;

        Debug.Log($"{itemFirst.Name} + {itemSecond.Name} = {combinatoryResult.Count} items available to craft. Grabbing the first one (if any).");

        // For this demo, we will take first available combination. IRL, give user a choise
        if (combinatoryResult.Any())
        {
            combinationId = combinatoryResult.First().Id;
        }
        else
        {
            return;
        }

        // After choosing which combination to use, we need to check for resources availability for it
        var resourcesCheckResult = _inventory.CheckCombinationForResourcesAvailability(combinationId);

        Debug.Log($"{itemFirst.Name} + {itemSecond.Name} = {resourcesCheckResult.Result}");

        if (resourcesCheckResult.Result == InventoryCombinatoryResult.CombinatoryResult.Allowed)
        {
            // Actual crafting time
            var craftResult = _inventory.TryCombine(combinationId, checkOnly: false);

            Debug.Log($"Crafting result for {itemFirst.Name} + {itemSecond.Name} is {(craftResult.ResultedItem?.Name ?? "(nothing)")}");

            if (craftResult.Result == InventoryCombinatoryResult.CombinatoryResult.Allowed)
            {
                var newItem = craftResult.ResultedItem; // item already added to our inventory at this point

                RefreshConsumablesUICombo();
                RefreshToolsUICombo();
            }
        }
    }