/// ---------------------------------------------
/// Ultimate Character Controller
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.UltimateCharacterController.Character.Abilities.Items
{
using Opsive.Shared.Events;
using Opsive.UltimateCharacterController.Items;
using Opsive.UltimateCharacterController.Utility;
using UnityEngine;
///
/// The EquipNext ability will equip the Next ItemSet in the specified category.
///
[DefaultStartType(AbilityStartType.ButtonDown)]
[DefaultInputName("Equip Next Item")]
[AllowDuplicateTypes]
public class EquipNext : EquipSwitcher
{
private int m_PrevItemSetIndex;
private int m_ItemSetIndex = -1;
///
/// Initialize the default values.
///
public override void Awake()
{
base.Awake();
// The EquipUnequip must exist in order for the item to be able to be equip toggled.
if (m_EquipUnequipItemAbility == null) {
return;
}
EventHandler.RegisterEvent- (m_GameObject, "OnNextItemSet", OnNextItemSet);
}
///
/// The EquipUnequip ability has changed the active ItemSet.
///
/// The updated active ItemSet index value.
protected override void OnItemSetIndexChange(int itemSetIndex)
{
if (itemSetIndex == -1 ||
(m_ItemSetIndex != -1 && itemSetIndex == m_ItemSetManager.GetDefaultItemSetIndex(m_ItemSetCategoryIndex) && !m_ItemSetManager.CategoryItemSets[m_ItemSetCategoryIndex].ItemSetList[itemSetIndex].CanSwitchTo)) {
return;
}
m_PrevItemSetIndex = itemSetIndex;
if (m_ItemSetIndex == -1) {
m_ItemSetIndex = itemSetIndex;
}
}
///
/// Called when the ablity is tried to be started. If false is returned then the ability will not be started.
///
/// True if the ability can be started.
public override bool CanStartAbility()
{
// An attribute may prevent the ability from starting.
if (!base.CanStartAbility()) {
return false;
}
m_ItemSetIndex = m_ItemSetManager.NextActiveItemSetIndex(m_ItemSetCategoryIndex, m_PrevItemSetIndex, true);
return m_ItemSetIndex != -1 && m_ItemSetIndex != m_EquipUnequipItemAbility.ActiveItemSetIndex;
}
///
/// The ability has started.
///
protected override void AbilityStarted()
{
base.AbilityStarted();
m_EquipUnequipItemAbility.StartEquipUnequip(m_ItemSetIndex);
// It is up to the EquipUnequip ability to do the actual equip - stop the current ability.
StopAbility();
}
///
/// The next item from the item set should be equipped.
///
/// The item that should be changed.
/// Should the current ItemSet be unequipped if the next ItemSet cannot be activated?
private void OnNextItemSet(Item item, bool unequipOnFailure)
{
var itemIdentifier = item.ItemIdentifier;
if (!m_ItemSetManager.IsCategoryMember(itemIdentifier.GetItemDefinition(), m_ItemSetCategoryIndex)) {
return;
}
// Don't equip the next item if the current ItemSet doesn't contain the ItemIdentifier.
var activeItemIdentifier = m_ItemSetManager.GetEquipItemIdentifier(m_ItemSetCategoryIndex, item.SlotID);
if (itemIdentifier != activeItemIdentifier) {
return;
}
// Tries to equip the next item. If the ability can't be started then the next item cannot be equipped. If unequip on failure is true
// and the next item is invalid then no items should be shown.
if (!StartAbility() && unequipOnFailure) {
m_EquipUnequipItemAbility.StartEquipUnequip(-1);
}
}
///
/// The character has died.
///
/// The position of the force.
/// The amount of force which killed the character.
/// The GameObject that killed the character.
protected override void OnDeath(Vector3 position, Vector3 force, GameObject attacker)
{
if (m_Inventory.RemoveAllOnDeath) {
m_PrevItemSetIndex = m_ItemSetIndex = -1;
}
}
///
/// Called when the character is destroyed.
///
public override void OnDestroy()
{
base.OnDestroy();
if (m_EquipUnequipItemAbility == null) {
EventHandler.UnregisterEvent
- (m_GameObject, "OnNextItemSet", OnNextItemSet);
}
}
}
}