private static bool PerformTrade(TradingOffer trade) { if (Game.current.inventory.HasStack(trade.Request)) { Game.current.inventory.SubtractStack(trade.Request); Game.current.inventory.AddStack(trade.Offer); return(true); } else { return(false); } }
/// <summary> /// Offers the trade. Success delegate is called before the enumerator finishes but after /// the trade is performed. /// </summary> /// <returns>The trade.</returns> /// <param name="trade">Trade.</param> /// <param name="success">Success.</param> public static IEnumerator OfferTrade(TradingOffer trade, System.Action <TradingResult> success) { var offerCount = trade.Offer.Count; var offerName = trade.Offer.ItemType.Name; var requestCount = trade.Request.Count; var requestName = trade.Request.ItemType.Name; string tradeText = string.Format("You are being offered {0} {1}{2} for {3} {4}{5}.", offerCount, offerName, offerCount > 1 ? "s" : "", requestCount, requestName, requestCount > 1 ? "s" : ""); return(OfferTrade(trade, tradeText, success)); }
/// <summary> /// Offers the trade, presenting the given text. Success delegate is called before the enumerator /// finishes but after the trade is performed. /// </summary> /// <returns>The trade.</returns> /// <param name="trade">Trade.</param> /// <param name="tradeText">Trade text.</param> /// <param name="success">Success.</param> public static IEnumerator OfferTrade(TradingOffer trade, string tradeText, System.Action <TradingResult> success) { var acceptOption = new DialogBox.Choice("Accept", () => { if (PerformTrade(trade)) { success(TradingResult.SUCCEED); PlayClip(instance.acceptSound); } else { success(TradingResult.FAILACCEPT); } }); var denyOption = new DialogBox.Choice("Deny", () => { success(TradingResult.FAILDENY); PlayClip(instance.declineSound); }); yield return(DialogSystem.Instance().DisplayMessageAndChoices(tradeText, acceptOption, denyOption)); }
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; } } }
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); }