//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; }
/// <summary> /// Adds the set of BattleEntities that this move affects into a supplied list. /// <para>This gets the entities based on the MoveAction's <see cref="MoveAffectionTypes"/>.</para> /// </summary> /// <param name="entityList">The list to add the BattleEntities this move affects into.</param> public void GetEntitiesMoveAffects(List <BattleEntity> entityList) { bool addedAllies = false; //Check for adding allies if (UtilityGlobals.MoveAffectionTypesHasFlag(MoveProperties.MoveAffectionType, MoveAffectionTypes.Ally) == true) { entityList.AddRange(User.BManager.GetEntities(User.EntityType, MoveProperties.HeightsAffected)); addedAllies = true; } //Check if the user of the move should be added if (UtilityGlobals.MoveAffectionTypesHasFlag(MoveProperties.MoveAffectionType, MoveAffectionTypes.Self) == true) { //If we didn't add allies, add the user of the move if (addedAllies == false) { entityList.Add(User); } } else { //Otherwise if we're not adding the user of the move and we did add allies, remove the user so we end up with only allies if (addedAllies == true) { entityList.Remove(User); } } //If this move targets other types of BattleEntities, add all of the ones it targets in order if (UtilityGlobals.MoveAffectionTypesHasFlag(MoveProperties.MoveAffectionType, MoveAffectionTypes.Other) == true) { if (MoveProperties.OtherEntTypes != null && MoveProperties.OtherEntTypes.Length > 0) { for (int i = 0; i < MoveProperties.OtherEntTypes.Length; i++) { EntityTypes otherType = MoveProperties.OtherEntTypes[i]; entityList.AddRange(User.BManager.GetEntities(otherType, MoveProperties.HeightsAffected)); } } else { Debug.LogWarning($"{Name} targets {nameof(MoveAffectionTypes.Other)}, but {nameof(MoveProperties.OtherEntTypes)} is null or empty."); } } //If this move has custom entity targeting logic, add the entities to the list if (UtilityGlobals.MoveAffectionTypesHasFlag(MoveProperties.MoveAffectionType, MoveAffectionTypes.Custom) == true) { GetCustomAffectedEntities(entityList); } //Filter out untargetable BattleEntities BattleManagerUtils.FilterEntitiesByTargetable(entityList); //If the BattleEntity has a custom targeting method and shouldn't be targeted, remove it //Otherwise, set to the true target in the event something is defending it for (int i = entityList.Count - 1; i >= 0; i--) { //Check for a custom targeting method bool?targetVal = entityList[i].EntityProperties.CustomTargeting?.Invoke(this); //If it returns false, remove the BattleEntity from the list if (targetVal == false) { entityList.RemoveAt(i); continue; } else { entityList[i] = entityList[i].GetTrueTarget(); } } }
/// <summary> /// Gets the set of BattleEntities that this move affects. /// <para>The default behavior is to get the entities based on the MoveAction's <see cref="MoveAffectionTypes"/>.</para> /// </summary> /// <returns>The BattleEntities the move affects based on its MoveAffectionType and the HeightStates it can target. /// If None, an empty array is returned.</returns> public BattleEntity[] GetEntitiesMoveAffects() { List <BattleEntity> entities = new List <BattleEntity>(); bool addedAllies = false; //Check for adding allies if (UtilityGlobals.MoveAffectionTypesHasFlag(MoveProperties.MoveAffectionType, MoveAffectionTypes.Ally) == true) { entities.AddRange(BattleManager.Instance.GetEntities(User.EntityType, MoveProperties.HeightsAffected)); addedAllies = true; } //Check if the user of the move should be added if (UtilityGlobals.MoveAffectionTypesHasFlag(MoveProperties.MoveAffectionType, MoveAffectionTypes.Self) == true) { //If we didn't add allies, add the user of the move if (addedAllies == false) { entities.Add(User); } } else { //Otherwise if we're not adding the user of the move and we did add allies, remove the user so we end up with only allies if (addedAllies == true) { entities.Remove(User); } } //If this move targets other types of BattleEntities, add all of the ones it targets in order if (UtilityGlobals.MoveAffectionTypesHasFlag(MoveProperties.MoveAffectionType, MoveAffectionTypes.Other) == true) { if (MoveProperties.OtherEntTypes != null && MoveProperties.OtherEntTypes.Length > 0) { for (int i = 0; i < MoveProperties.OtherEntTypes.Length; i++) { EntityTypes otherType = MoveProperties.OtherEntTypes[i]; entities.AddRange(BattleManager.Instance.GetEntities(otherType, MoveProperties.HeightsAffected)); } } else { Debug.LogWarning($"{Name} targets {nameof(MoveAffectionTypes.Other)}, but {nameof(MoveProperties.OtherEntTypes)} is null or empty."); } } //If this move has custom entity targeting logic, add the entities to the list if (UtilityGlobals.MoveAffectionTypesHasFlag(MoveProperties.MoveAffectionType, MoveAffectionTypes.Custom) == true) { BattleEntity[] customAffected = GetCustomAffectedEntities(); if (customAffected != null && customAffected.Length > 0) { entities.AddRange(customAffected); } } //Filter out untargetable BattleEntities BattleManager.Instance.FilterEntitiesByTargetable(entities); //Set to the true targets in the event something is defending them for (int i = 0; i < entities.Count; i++) { entities[i] = entities[i].GetTrueTarget(); } return(entities.ToArray()); }