//Event handler when the user's next turn is started
        private void OnUserTurnStart()
        {
            EntityUsing.TurnStartEvent -= OnUserTurnStart;

            Debug.Log($"Using {ItemChosen.Name} for {EntityUsing.Name}, which was received via Mystery!");

            //Clear the menu stack as the action will be selected automatically
            EntityUsing.BManager.battleUIManager.ClearMenuStack();

            //Immediately start using the item
            ItemAction itemChosenAction = ItemChosen.GetActionAssociated(User);

            //Special case: if the item chosen is a Mystery, initialize it
            //We can't do this earlier in Initialize, otherwise we run into an infinite loop
            if (itemChosenAction is MysteryAction)
            {
                itemChosenAction.Initialize();
            }

            //1. Find out if this item targets enemies or allies
            //2. If it targets allies, only use it on the entity using it
            //3. If it targets enemies, use it on the first enemy if it targets only one, otherwise use it on all enemies
            List <BattleEntity> entitiesAffected = new List <BattleEntity>();

            itemChosenAction.GetEntitiesMoveAffects(entitiesAffected);
            if (itemChosenAction.MoveProperties.SelectionType == Enumerations.EntitySelectionType.Single ||
                itemChosenAction.MoveProperties.SelectionType == Enumerations.EntitySelectionType.First)
            {
                //If this selects the first or a single entity and it's an ally, make sure it always targets the entity using the Mystery
                //Examples include a Mushroom or Honey Syrup
                if (UtilityGlobals.MoveAffectionTypesHasFlag(itemChosenAction.MoveProperties.MoveAffectionType,
                                                             MoveAffectionTypes.Self | MoveAffectionTypes.Ally))
                {
                    entitiesAffected.Clear();
                    entitiesAffected.Add(EntityUsing);
                }

                //If it affects a first or single entity and we have more in the list, remove all but the first one
                //If this is a single target damaging item that targets enemies, it'll choose the first enemy (Ex. Egg Bomb)
                if (entitiesAffected.Count > 1)
                {
                    entitiesAffected.RemoveRange(1, entitiesAffected.Count - 1);
                }
            }

            //Start the second half of the sequence
            EntityUsing.StartAction(itemChosenAction, true, entitiesAffected.ToArray());

            EntityUsing = null;
            ItemChosen  = null;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates an ItemSubMenu.
        /// </summary>
        /// <param name="dipTurnCount">The number of item turns. This is used for Double/Triple Dip.</param>
        /// <param name="fpCost">The amount of FP it costs to use an item. This is used for Double/Triple Dip.</param>
        /// <param name="isRootMenu">Tells if the ItemSubMenu is the root menu. This is only true if Double/Triple Dip is used.</param>
        public ItemSubMenu(BattleEntity user, int dipTurnCount, int fpCost, bool isRootMenu = false) : base(user)
        {
            Name     = "Items";
            Position = new Vector2(230, 150);

            DipTurnCount = dipTurnCount;
            FPCost       = fpCost;
            IsRootMenu   = isRootMenu;

            Item[] usableItems = Inventory.Instance.FindItems(Item.ItemCategories.Standard,
                                                              Item.ItemTypes.Healing | Item.ItemTypes.Damage | Item.ItemTypes.Status | Item.ItemTypes.Revival);
            for (int i = 0; i < usableItems.Length; i++)
            {
                //This cast fails if the Item doesn't derive from BattleItem
                //This can also happen if an Item that can't be used in battle had its ItemType set to the wrong value
                BattleItem item = (BattleItem)usableItems[i];

                //Set item properties
                ItemAction newItemAction = item.GetActionAssociated(User);
                newItemAction.SetDipFPCost(FPCost);
                //Set the item turn count
                if (dipTurnCount > 1)
                {
                    newItemAction.SetOnItemUsed(SetEntityDipTurnCount);
                }

                BattleActions.Add(newItemAction);
            }

            if (BattleActions.Count == 0)
            {
                //Add the No Items action, which, when selected, brings up a battle message saying "You can't select that!"
                //and brings you back to the menu. This happens even with Double Dip and Triple Dip, essentially forcing you
                //to stop using items.

                MessageAction noItems = new MessageAction(User, "No Items", null, "You have no items.",
                                                          (int)BattleGlobals.BattleEventPriorities.Message, "You can't select that!");
                BattleActions.Add(noItems);
            }

            //Initialize here if this is the root menu, as it won't be initialized like it normally is
            if (IsRootMenu == true)
            {
                MoveCategory = Enumerations.MoveCategories.Item;
                Initialize();
            }
        }