using Opsive.Shared.Events;
using Opsive.Shared.Game;
using Opsive.UltimateCharacterController.Character.Abilities;
using UnityEngine;
[DefaultAbilityIndex(74)]
[DefaultState("Inspect")]
[DefaultInputName("Action")]
[DefaultStartType(AbilityStartType.ButtonDown)]
[DefaultStopType(AbilityStopType.ButtonToggle)]
public class Inspect : DetectObjectAbilityBase
{
[Tooltip("You can use an animation event to stop the Ability")]
[SerializeField] protected bool m_UseAnimationEvents = false;
[Tooltip("You can use a timer to stop the Ability if not using animation events")]
[SerializeField] protected float m_DefaultInspectTime = 3f;
private float m_StopTime = 0;
private IInspectable m_Inspectable;
public enum InspectState { Inspecting, Finished }
private InspectState m_InspectState = InspectState.Finished;
public override int AbilityIntData => m_Inspectable.AnimatorID + (int)m_InspectState;
public override void Awake()
{
base.Awake();
EventHandler.RegisterEvent(m_GameObject, "OnStartInspectObject", OnStartInspecting);
EventHandler.RegisterEvent(m_GameObject, "OnStopInspectObject", OnStopInspecting);
}
public override void OnDestroy()
{
EventHandler.RegisterEvent(m_GameObject, "OnStartInspectObject", OnStartInspecting);
EventHandler.RegisterEvent(m_GameObject, "OnStopInspectObject", OnStopInspecting);
base.OnDestroy();
}
/// <summary>
/// Validates the object to ensure it is valid for the current ability.
/// </summary>
/// <param name="obj">The object being validated.</param>
/// <param name="raycastHit">The raycast hit of the detected object. Will be null for trigger detections.</param>
/// <returns>True if the object is valid. The object may not be valid if it doesn't have an ability-specific component attached.</returns>
protected override bool ValidateObject(GameObject obj, RaycastHit? raycastHit)
{
if (!base.ValidateObject(obj, raycastHit))
return false;
m_Inspectable = obj.GetCachedParentComponent<IInspectable>();
Debug.Log(m_Inspectable);
if (m_Inspectable == null)
return false;
m_AbilityMessageText = "Press 'Action' button to inspect " + m_Inspectable.HintDisplayName;
return true;
}
public override bool ShouldBlockAbilityStart(Ability startingAbility)
{
return true;
}
public override bool ShouldStopActiveAbility(Ability activeAbility)
{
return true;
}
public override bool CanStartAbility()
{
return base.CanStartAbility();
}
protected override void AbilityStarted()
{
base.AbilityStarted();
m_InspectState = InspectState.Inspecting;
m_CharacterLocomotion.UpdateAbilityAnimatorParameters();
if (m_UseAnimationEvents == false)
{
m_StopTime = Time.time + m_DefaultInspectTime;
OnStartInspecting();
}
}
private void OnStartInspecting()
{
m_Inspectable.Inspect(true);
}
public override bool CanStopAbility()
{
return m_InspectState == InspectState.Finished;
}
protected override void AbilityStopped(bool force)
{
if (force)
{
m_InspectState = InspectState.Finished;
m_Inspectable.Inspect(false);
}
base.AbilityStopped(force);
}
public override void Update()
{
if (m_UseAnimationEvents == false && m_InspectState == InspectState.Inspecting && Time.time > m_StopTime)
OnStopInspecting();
}
private void OnStopInspecting()
{
m_InspectState = InspectState.Finished;
m_CharacterLocomotion.UpdateAbilityAnimatorParameters();
m_Inspectable.Inspect(false);
StopAbility();
}
}