示例#1
0
    public void CraftTableBTN()
    {
        #region Find the recipe
        int    recipeIndex  = Craft.CheckIfCorrectRecipe(ItemsInCraftTable);
        Recipe targetRecipe = Craft.Recipes[recipeIndex];
        if (recipeIndex == -1)
        {
            RemoveItemFromInventory();
            GlobalEventText.AddMessage("Bad recipe!");
            return;
        }
        //Check for the random success
        bool success = true;
        if (targetRecipe.SuccessChance != 100)
        {
            int result = Random.Range(0, 100);
            if (result <= targetRecipe.SuccessChance)
            {
                success = true;
            }
            else
            {
                success = false;
                GlobalEventText.AddMessage("You failed...");
            }
        }
        if (!targetRecipe.isKnown && success)
        {
            targetRecipe.isKnown = true; //Set the selected recipe as "known"
            GlobalEventText.AddMessage(string.Format("Yay! You learned how to make {0}", targetRecipe.Name));
        }

        if (success)
        {
            #region RandomResult
            //Check if the result is random:
            if (targetRecipe.isRandomResult)
            {
                bool        isOK         = false;
                int         index        = 0;
                List <Item> tempItemList = new List <Item>(targetRecipe.RandomResults.Length);
                for (int i = 0; i < targetRecipe.RandomResults.Length; i++)
                {
                    tempItemList.Add(targetRecipe.RandomResults[i]);
                }
                for (int i = 0; i < targetRecipe.AmountOfRandomItemInResult; i++)
                {
                    isOK = false;
                    Item rndResult = null;
                    //Get the random result
                    do
                    {
                        index     = Random.Range(0, tempItemList.Count);
                        rndResult = (Item)tempItemList[index].Clone();
                        for (int j = 0; j < 3; j++)
                        {
                            if (ItemsInResult[j] == null)
                            {
                                isOK = true;
                            }
                            else if (ItemsInResult[j].Name != rndResult.Name)
                            {
                                isOK = true;
                            }
                        }
                    } while (!isOK);


                    if (ItemsInResult[i] != null) // Look if the item is different
                    {
                        if (ItemsInResult[i].Name == rndResult.Name)
                        {
                            ItemsInResult[i].Count += rndResult.Count;
                        }
                        else
                        {
                            Inventory.AddItemToInventory(ItemsInResult[i]);
                            ItemsInResult[i] = (Item)rndResult.Clone();
                        }
                    }
                    else
                    {
                        ItemsInResult[i] = (Item)rndResult.Clone();
                    }

                    //Update result sprite
                    _resultImages[i].gameObject.SetActive(true);
                    _resultImages[i].sprite = ItemsInResult[i].InventorySprite;

                    tempItemList.RemoveAt(index);
                }
            }
            #endregion
            else
            {
                //Get the results (put the results in the result panel)
                for (int i = 0; i < 3; i++)
                {
                    if (Craft.Recipes[recipeIndex].Results[i] != null)
                    {
                        //Compare the actual recipe to the old one:
                        if (_previousRecipe != null && Craft.Recipes[recipeIndex].Name == _previousRecipe.Name)
                        {
                            if (ItemsInResult[i] != null)
                            {
                                ItemsInResult[i].Count += Craft.Recipes[recipeIndex].Results[i].Count;
                            }
                        }
                        else
                        {
                            //Add the previous item to the inventory
                            if (ItemsInResult[i] != null)
                            {
                                Inventory.AddItemToInventory(ItemsInResult[i]);
                            }

                            ItemsInResult[i] = (Item)Craft.Recipes[recipeIndex].Results[i].Clone();

                            //Update result sprite
                            _resultImages[i].gameObject.SetActive(true);
                            _resultImages[i].sprite = ItemsInResult[i].InventorySprite;
                        }
                    }
                    else
                    {
                        ItemsInResult[i] = null;

                        //Update result sprite
                        _resultImages[i].gameObject.SetActive(false);
                        _resultImages[i].sprite = ResourcesManager.instance.EmptySprite;
                    }
                }
            }
            _previousRecipe = Craft.Recipes[recipeIndex];
        }
        else
        {
            _previousRecipe = null;
        }


        //Handle craft items:
        for (int i = 0; i < 9; i++)
        {
            if (Craft.Recipes[recipeIndex].Items[i] != null)                                                   //Check if not empty
            {
                Inventory.DecreaseCount(ItemsInCraftTable[i].Name, Craft.Recipes[recipeIndex].Items[i].Count); //Remove the recipe count of the item
                DecreaseCount(ItemsInCraftTable[i], Craft.Recipes[recipeIndex].Items[i].Count);
            }
        }
        #endregion

        //Update craftUI (keep the rest in the ui)
        RefreshUI();
        RefreshResults();
        RefreshRecipesList();
        //Update the inventory
        Inventory.RefreshUI();
    }