示例#1
0
 public void Craft()
 {
     if (recipeDisplayer.CraftRecipe())
     {
         NotificationSystem.ShowNotificationIfPossible("Crafted successfully!");
     }
     else
     {
         NotificationSystem.ShowNotificationIfPossible("Could not craft the item :c");
     }
 }
    private IEnumerator BeginAI()
    {
        while (true)
        {
            switch (Random.Range(0, 2))
            {
            case 0:
                // Offer a trade.
                var info = MetaInformation.Instance();

                if (info != null)
                {
                    int numMappings = info.GetNumberOfRegisteredItems();

                    if (numMappings != 0)
                    {
                        ItemType offType = info.GetItemTypeMappings().ElementAt(Random.Range(0, numMappings)).Value;
                        ItemType reqType = info.GetItemTypeMappings().ElementAt(Random.Range(0, numMappings)).Value;

                        int numOfferedItem = Random.Range(1, 10);
                        int numRequestItem = Random.Range(1, 10);

                        TradingOffer offer = TradingOffer.MakeOffer(
                            new ItemStack(offType, numOfferedItem),
                            new ItemStack(reqType, numRequestItem));

                        bool didSucceed = false;
                        yield return(TradingDialogUtility.OfferTrade(offer, (result) => {
                            didSucceed = result == TradingResult.SUCCEED;
                        }));


                        if (didSucceed)
                        {
                            NotificationSystem.ShowNotificationIfPossible("Trade succeeded.");
                        }
                        else
                        {
                            NotificationSystem.ShowNotificationIfPossible("Trade failed.");
                        }


                        break;
                    }
                    else
                    {
                        goto case 1;
                    }
                }
                else
                {
                    Debug.Log("No MetaInformation instance found. AI will not make a random trade offer.");
                    goto case 1;
                }


            case 1:
                // Move to a random piece of furniture in the shop.
                Shop shop = myShopMover.GetShop();

                Furniture target = shop.GetFurnitureAtIndex(Random.Range(0, shop.GetFurnitureAmount()));

                yield return(myShopMover.MoveToPosition(target.GetStandingPosition(), (succ) => {}));

                break;
            }
        }
    }
示例#3
0
    public void CombineItems()
    {
        CraftingSlot[] slots = GetComponentsInChildren <CraftingSlot> ();

        List <ItemType> items = new List <ItemType> ();

        foreach (var slot in slots)
        {
            var item = slot.GetItem();
            if (item != null)
            {
                items.Add(item);
                slot.Clear();
            }
        }

        if (items.Any((item) => !Game.current.inventory.HasItem(item)))
        {
            NotificationSystem.ShowNotificationIfPossible("You do not have enough of at least one of the items.");
        }
        else
        {
            ItemType bestCandidate = null;
            int      bestMatch     = 0;


            // try to find a matching crafting recipe by looping through all known items and their recipes
            foreach (var kv in MetaInformation.Instance().GetItemTypeMappings())
            {
                if (kv.Value.Recipe.CanBeCraftedGiven(items))
                {
                    int    matchStrength = 0;
                    bool[] matched       = new bool[items.Count];

                    foreach (var stack in kv.Value.Recipe.GetRequiredItems())
                    {
                        int numMatches = 0;
                        for (int i = 0; i < items.Count && numMatches < stack.Count; i++)
                        {
                            if (!matched [i] && stack.ItemTypeID == items [i].ItemTypeID)
                            {
                                matched [i] = true;
                                numMatches++;
                            }
                        }
                    }

                    matchStrength = matched.Select((b) => b ? 1 : 0).Sum();

                    if (matchStrength > bestMatch)
                    {
                        bestCandidate = kv.Value;
                        bestMatch     = matchStrength;
                    }
                }
            }

            if (bestCandidate == null)
            {
                NotificationSystem.ShowNotificationIfPossible("Didn't match any recipes.");

                PlaySound(craftingFailedSound);
            }
            else
            {
                bestCandidate.Recipe.UseIngredientsFrom(Game.current.inventory);
                Game.current.inventory.AddItem(bestCandidate);

                NotificationSystem.ShowNotificationIfPossible(string.Format("You made {0}", bestCandidate.Name));

                PlaySound(craftingSuccessfulSound);
            }

            RemakeInventory(Game.current.inventory);
        }
    }
    private IEnumerator BasicCustomerRoutine()
    {
        Game game = Game.current;

        if (game == null)
        {
            Debug.Log("Game.current is null; Customer won't do anything.");
            yield break;
        }


        // Walk to a random furniture.
        bool succeededMovingIntoShop = false;

        while (!succeededMovingIntoShop)
        {
            var allFurniture = game.furnitureInShop;

            if (allFurniture.Count > 0)
            {
                IntPair randomFurniturePosition = allFurniture [Random.Range(0, allFurniture.Count)].furnitureRef.GetStandingPosition();
                yield return(myShopMover.MoveToPosition(randomFurniturePosition, (suc) => succeededMovingIntoShop = suc));
            }
            else
            {
                yield return(myShopMover.MoveToPosition(new IntPair(0, 0)));

                succeededMovingIntoShop = true;
            }

            if (!succeededMovingIntoShop)
            {
                yield return(new WaitForSeconds(0.1f));                 // to prevent rapid looping
            }
        }

        // Pop up a trading dialog.
        var possibleOffers = character.possibleTradingOffers.Where((off) => Game.current.inventory.HasItem(off.Request.ItemType)).ToList();

        if (possibleOffers.Count <= 0)
        {
            Debug.Log("No offers possible.");
        }
        else
        {
            TradingOffer trade = possibleOffers [Random.Range(0, possibleOffers.Count)];

            string myTradingText;
            if (character.formattedTradingStrings.Length > 0)
            {
                var strs = character.formattedTradingStrings;
                var idx  = Random.Range(0, strs.Length);
                myTradingText = string.Format(strs [idx], trade.Offer.ItemType.Name, trade.Request.ItemType.Name,
                                              trade.Offer.Count, trade.Request.Count);
            }
            else
            {
                myTradingText = "UNIMPLEMENTED TRADING TEXT";
            }

            yield return(TradingDialogUtility.OfferTrade(trade, myTradingText, (result) => {
                switch (result)
                {
                case TradingResult.SUCCEED:
                    NotificationSystem.ShowNotificationIfPossible(string.Format("Trade succeeded! You got {0} {1}.", trade.Offer.Count, trade.Offer.ItemType.Name));
                    break;

                case TradingResult.FAILACCEPT:
                    NotificationSystem.ShowNotificationIfPossible(string.Format("Couldn't do the trade. You didn't have enough {0}", trade.Request.ItemType.Name));
                    break;

                case TradingResult.FAILDENY:
                    NotificationSystem.ShowNotificationIfPossible("Declined trade.");
                    break;
                }
            }));
        }


        // Return to start position.
        yield return(myShopMover.MoveToPosition(startPosition, (suc) => {}));

        // Despawn self!
        Destroy(gameObject);
    }