VintageRoots
Member
I have implemented a Hunger and a Thirst system as "Abilities" for UCC, this is the code im using:
I want to implement "Item Actions" for Food, a simple system that increases the Attribute of "Hunger" +10 points per food item.
how would you implement this?
Code:
using Opsive.Shared.Events;
using Opsive.Shared.Game;
using Opsive.UltimateCharacterController.Character.Abilities;
using Opsive.UltimateCharacterController.Traits;
using UnityEngine;
/// <summary>
/// Plays a starving animation when the character is out of hunger.
/// </summary>
[DefaultStartType(AbilityStartType.Manual)]
[DefaultAbilityIndex(304)] // Change this to a starving animation TO-DO
[DefaultAllowPositionalInput(false)]
[DefaultAllowRotationalInput(false)]
[DefaultUseGravity(AbilityBoolOverride.False)]
[DefaultEquippedSlots(0)]
public class Hunger : Ability
{
[Tooltip("The name of the hunger attribute.")]
[SerializeField] protected string m_HungerAttributeName = "Hunger";
public string BreathAttributeName { get { return m_HungerAttributeName; } set { m_HungerAttributeName = value; } }
private Attribute m_Attribute;
private Attribute.AutoUpdateValue m_AutoUpdateValue;
private Health m_Health;
private Respawner m_Respawner;
public override bool CanStayActivatedOnDeath { get { return true; } }
/// <summary>
/// Initialize the default values.
/// </summary>
public override void Awake()
{
base.Awake();
if (string.IsNullOrEmpty(m_HungerAttributeName))
{
Debug.LogError("Error: A hunger attribute name on the suffocate ability must be specified.");
return;
}
var attributeManager = m_GameObject.GetCachedComponent<AttributeManager>();
if (attributeManager == null)
{
Debug.LogError("Error: The character must have an Attribute Manager.");
return;
}
m_Attribute = attributeManager.GetAttribute(m_HungerAttributeName);
if (m_Attribute == null)
{
Debug.LogError("Error: Unable to find the attribute with name " + m_HungerAttributeName + ".");
return;
}
m_Health = m_GameObject.GetCachedComponent<Health>();
m_Respawner = m_GameObject.GetCachedComponent<Respawner>();
EventHandler.RegisterEvent(m_Attribute, "OnAttributeReachedDestinationValue", OnOutOfHunger);
EventHandler.RegisterEvent(m_GameObject, "OnAnimatorDrownComplete", OnStarveComplete);
}
/// <summary>
/// The character no longer hs any breath left.
/// </summary>
private void OnOutOfHunger()
{
if (m_Attribute.Value != m_Attribute.MinValue)
{
return;
}
StartAbility();
}
/// <summary>
/// The ability has started.
/// </summary>
protected override void AbilityStarted()
{
base.AbilityStarted();
m_AutoUpdateValue = m_Attribute.AutoUpdateValueType;
m_Attribute.AutoUpdateValueType = Attribute.AutoUpdateValue.None;
if (m_Health != null)
{
m_Health.ImmediateDeath();
}
}
/// <summary>
/// The character has finished drowning.
/// </summary>
private void OnStarveComplete()
{
if (IsActive && m_Respawner != null)
{
m_Respawner.Respawn();
}
StopAbility();
}
/// <summary>
/// The ability has stopped running.
/// </summary>
/// <param name="force">Was the ability force stopped?</param>
protected override void AbilityStopped(bool force)
{
base.AbilityStopped(force);
m_Attribute.ResetValue();
m_Attribute.AutoUpdateValueType = m_AutoUpdateValue;
}
/// <summary>
/// The character has been destroyed.
/// </summary>
public override void OnDestroy()
{
base.OnDestroy();
if (m_Attribute != null)
{
EventHandler.UnregisterEvent(m_Attribute, "OnAttributeReachedDestinationValue", OnOutOfHunger);
EventHandler.UnregisterEvent(m_GameObject, "OnAnimatorDrownComplete", OnStarveComplete);
}
}
}
I want to implement "Item Actions" for Food, a simple system that increases the Attribute of "Hunger" +10 points per food item.
how would you implement this?