/// --------------------------------------------- /// Ultimate Character Controller /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.UltimateCharacterController.Character.Abilities { using Opsive.Shared.Events; using Opsive.Shared.Utility; using UnityEngine; /// /// Plays a death animation when the character dies. /// [DefaultStartType(AbilityStartType.Manual)] [DefaultState("Death")] [DefaultAbilityIndex(4)] public class Die : Ability { [Tooltip("The amount of force to add to the camera. This value will be multiplied by the death force magnitude.")] [SerializeField] protected Vector3 m_CameraRotationalForce = new Vector3(0, 0, 0.75f); public Vector3 CameraRotationalForce { get { return m_CameraRotationalForce; } set { m_CameraRotationalForce = value; } } /// /// The type of animation that the ability should play. /// private enum DeathType { Forward, // Play a forward death animation. Backward // Play a backward death animation. } private int m_DeathTypeIndex; private Vector3 m_Force; private Vector3 m_Position; [NonSerialized] public Vector3 Force { get { return m_Force; } set { m_Force = value; } } [NonSerialized] public Vector3 Position { get { return m_Position; } set { m_Position = value; } } public override int AbilityIntData { get { return m_DeathTypeIndex; } } public override bool CanStayActivatedOnDeath { get { return true; } } /// /// Initialize the default values. /// public override void Awake() { base.Awake(); EventHandler.RegisterEvent(m_GameObject, "OnDeath", OnDeath); EventHandler.RegisterEvent(m_GameObject, "OnRespawn", OnRespawn); } /// /// The character has died. Start the ability. /// /// The position of the force. /// The amount of force which killed the character. /// The GameObject that killed the character. private void OnDeath(Vector3 position, Vector3 force, GameObject attacker) { if (!Enabled) { return; } m_Force = force; m_Position = position; m_DeathTypeIndex = GetDeathTypeIndex(position, force, attacker); StartAbility(); } /// /// Returns the value that the AbilityIntData parameter should be set to. /// /// The position of the force. /// The amount of force which killed the character. /// The GameObject that killed the character. /// The value that the AbilityIntData parameter should be set to. protected virtual int GetDeathTypeIndex(Vector3 position, Vector3 force, GameObject attacker) { return (int)(m_Transform.InverseTransformPoint(position).z > 0 ? DeathType.Forward : DeathType.Backward); } /// /// The ability has started. /// protected override void AbilityStarted() { base.AbilityStarted(); m_CharacterLocomotion.ResetRotationPosition(); EventHandler.ExecuteEvent(m_GameObject, "OnCameraRotationalForce", m_CameraRotationalForce * m_Force.magnitude); } /// /// The character has respawned. Stop the die ability. /// private void OnRespawn() { if (!Enabled) { return; } StopAbility(); } /// /// Called when the character is destroyed. /// public override void OnDestroy() { base.OnDestroy(); EventHandler.UnregisterEvent(m_GameObject, "OnDeath", OnDeath); EventHandler.UnregisterEvent(m_GameObject, "OnRespawn", OnRespawn); } } }