354 lines
12 KiB
C#
354 lines
12 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Beyond
|
|
{
|
|
public class ActionTriggerEvent : MonoBehaviour
|
|
{
|
|
public bool debugPrint;
|
|
public Button ActionButton;
|
|
public Button LadderButton;
|
|
public Button CrouchButton;
|
|
public GameObject AttackButton;
|
|
public GameObject SpellButton;
|
|
public GameObject ComsumableButton;
|
|
public GameObject ComsumableFaithButton;
|
|
public GameObject powerButtonsParent;
|
|
public Button RunButton;
|
|
public Button TargetButton;
|
|
public Button JumpButton;
|
|
|
|
public bool disableRun = false;
|
|
public bool disableCrouch = false;
|
|
|
|
public static Action<TriggerDescriptor> ActionTriggerEnter;
|
|
public static Action<TriggerDescriptor> ActionTriggerExit;
|
|
/*
|
|
public static Action<GameObject> LadderTriggerEnter;
|
|
public static Action<GameObject> LadderTriggerExit;
|
|
|
|
public static Action<GameObject> FootbridgeTriggerEnter;
|
|
public static Action<GameObject> FootbridgeTriggerExit;
|
|
|
|
public static Action<GameObject> EnterFootbridge;
|
|
public static Action<GameObject> ExitFootbridge;
|
|
public UnityEvent OnHideOnTriggerEnter;
|
|
public UnityEvent OnHideOnTriggerExit;
|
|
|
|
public UnityEvent OnActionTriggerEnter;
|
|
public UnityEvent OnActionTriggerExit;
|
|
|
|
public UnityEvent OnLadderTriggerEnter;
|
|
public UnityEvent OnLadderTriggerExit;
|
|
|
|
public UnityEvent OnFootbridgeTriggerEnter;
|
|
public UnityEvent OnFootbridgeTriggerExit;
|
|
|
|
public UnityEvent OnEnterFootbridge;
|
|
public UnityEvent OnExitFootbridge;
|
|
*/
|
|
|
|
private bool usingFootbridge;
|
|
|
|
private TriggerDescriptor m_currentDesc;
|
|
|
|
private List<TriggerDescriptor> m_Triggers = new List<TriggerDescriptor>();
|
|
|
|
private GameStateManager.State m_prevGameState = GameStateManager.State.NORMAL;
|
|
/*
|
|
private List<TriggerObject> m_triggers = new List<TriggerObject>();
|
|
public enum TriggerType
|
|
{
|
|
Dialogue,
|
|
Ladder,
|
|
Generic,
|
|
Collectable,
|
|
COUNT
|
|
};
|
|
|
|
public struct TriggerObject : IComparable<TriggerObject>, IComparable
|
|
{
|
|
public GameObject obj;
|
|
public TriggerType type;
|
|
|
|
public int CompareTo(TriggerObject other)
|
|
{
|
|
return type.CompareTo(other.type);
|
|
}
|
|
|
|
public int CompareTo(object obj)
|
|
{
|
|
if (ReferenceEquals(null, obj)) return 1;
|
|
return obj is TriggerObject other ? CompareTo(other) : throw new ArgumentException($"Object must be of type {nameof(TriggerObject)}");
|
|
}
|
|
|
|
public static bool operator <(TriggerObject left, TriggerObject right)
|
|
{
|
|
return left.CompareTo(right) < 0;
|
|
}
|
|
|
|
public static bool operator >(TriggerObject left, TriggerObject right)
|
|
{
|
|
return left.CompareTo(right) > 0;
|
|
}
|
|
|
|
public static bool operator <=(TriggerObject left, TriggerObject right)
|
|
{
|
|
return left.CompareTo(right) <= 0;
|
|
}
|
|
|
|
public static bool operator >=(TriggerObject left, TriggerObject right)
|
|
{
|
|
return left.CompareTo(right) >= 0;
|
|
}
|
|
}
|
|
*/
|
|
|
|
private void Initialize()
|
|
{
|
|
if (LadderButton)
|
|
LadderButton.gameObject.SetActive(false);
|
|
if (ActionButton)
|
|
ActionButton.gameObject.SetActive(false);
|
|
if (CrouchButton)
|
|
CrouchButton.gameObject.SetActive(!disableCrouch);
|
|
if (AttackButton)
|
|
AttackButton.gameObject.SetActive(true);
|
|
if (SpellButton)
|
|
SpellButton.gameObject.SetActive(true);
|
|
if (ComsumableButton)
|
|
ComsumableButton.gameObject.SetActive(true);
|
|
if (ComsumableFaithButton)
|
|
ComsumableFaithButton.gameObject.SetActive(true);
|
|
if (powerButtonsParent)
|
|
{
|
|
powerButtonsParent.SetActive(true);
|
|
}
|
|
if (RunButton)
|
|
RunButton.gameObject.SetActive(!disableRun);
|
|
|
|
//if (TargetButton)
|
|
// TargetButton.gameObject.SetActive(true);
|
|
//if (JumpButton)
|
|
// JumpButton.gameObject.SetActive(true);
|
|
}
|
|
|
|
private void OnActionTrigger(bool activate, TriggerDescriptor.TriggerType type)
|
|
{
|
|
if (type == TriggerDescriptor.TriggerType.Ladder)
|
|
{
|
|
if (LadderButton)
|
|
LadderButton.gameObject.SetActive(activate);
|
|
if (ActionButton)
|
|
ActionButton.gameObject.SetActive(false);
|
|
}
|
|
else
|
|
{
|
|
if (LadderButton)
|
|
LadderButton.gameObject.SetActive(false);
|
|
if (ActionButton)
|
|
ActionButton.gameObject.SetActive(activate);
|
|
}
|
|
if (CrouchButton && !disableCrouch)
|
|
CrouchButton.interactable = !activate;
|
|
if (AttackButton)
|
|
AttackButton.gameObject.SetActive(!activate);
|
|
if (SpellButton)
|
|
SpellButton.gameObject.SetActive(!activate);
|
|
//if (ComsumableButton)
|
|
// ComsumableButton.gameObject.SetActive(!activate);
|
|
//if (ComsumableFaithButton)
|
|
// ComsumableFaithButton.gameObject.SetActive(!activate);
|
|
if (powerButtonsParent)
|
|
{
|
|
powerButtonsParent.SetActive(!activate);
|
|
}
|
|
if (RunButton && !disableRun)
|
|
RunButton.interactable = !activate;
|
|
//if (TargetButton)
|
|
// TargetButton.interactable = !activate;
|
|
//if (JumpButton)
|
|
// JumpButton.interactable = !activate;
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
ActionTriggerEnter += OnInvokeActionTriggerEnter;
|
|
ActionTriggerExit += OnInvokeActionTriggerExit;
|
|
/*
|
|
LadderTriggerEnter += OnInvokeLadderTriggerEnter;
|
|
LadderTriggerExit += OnInvokeLadderTriggerExit;
|
|
FootbridgeTriggerEnter += OnInvokeFootbridgeTriggerEnter;
|
|
FootbridgeTriggerExit += OnInvokeFootbridgeTriggerExit;
|
|
EnterFootbridge += OnInvokeEnterFootbridge;
|
|
ExitFootbridge += OnInvokeExitFootbridge;
|
|
*/
|
|
//OnInvokeActionTriggerExit(gameObject);
|
|
Initialize();
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
ActionTriggerEnter -= OnInvokeActionTriggerEnter;
|
|
ActionTriggerExit -= OnInvokeActionTriggerExit;
|
|
/*
|
|
LadderTriggerEnter -= OnInvokeLadderTriggerEnter;
|
|
LadderTriggerExit -= OnInvokeLadderTriggerExit;
|
|
FootbridgeTriggerEnter -= OnInvokeFootbridgeTriggerEnter;
|
|
FootbridgeTriggerExit -= OnInvokeFootbridgeTriggerExit;
|
|
EnterFootbridge -= OnInvokeEnterFootbridge;
|
|
ExitFootbridge -= OnInvokeExitFootbridge;
|
|
*/
|
|
}
|
|
|
|
private void DebugLog(string text)
|
|
{
|
|
if (debugPrint)
|
|
{
|
|
Debug.Log(text);
|
|
}
|
|
}
|
|
|
|
/*
|
|
private void OnInvokeHideOnTriggerEnter()
|
|
{
|
|
DebugLog("OnInvokeHideOnTriggerEnter");
|
|
OnHideOnTriggerEnter?.Invoke();
|
|
}
|
|
|
|
private void OnInvokeHideOnTriggerExit()
|
|
{
|
|
DebugLog("OnInvokeHideOnTriggerExit");
|
|
OnHideOnTriggerExit?.Invoke();
|
|
}
|
|
|
|
private void OnInvokeEnterFootbridge(GameObject gameObject)
|
|
{
|
|
DebugLog("OnInvokeEnterFootbridge");
|
|
usingFootbridge = true;
|
|
OnInvokeHideOnTriggerEnter();
|
|
OnEnterFootbridge?.Invoke();
|
|
}
|
|
|
|
private void OnInvokeExitFootbridge(GameObject gameObject)
|
|
{
|
|
DebugLog("OnInvokeExitFootbridge");
|
|
usingFootbridge = false;
|
|
OnInvokeHideOnTriggerExit();
|
|
OnExitFootbridge?.Invoke();
|
|
}
|
|
|
|
private void OnInvokeFootbridgeTriggerEnter(GameObject gameObject)
|
|
{
|
|
if (usingFootbridge) return;
|
|
DebugLog("OnInvokeFootbridgeTriggerEnter");
|
|
OnFootbridgeTriggerEnter?.Invoke();
|
|
OnInvokeHideOnTriggerEnter();
|
|
}
|
|
|
|
private void OnInvokeFootbridgeTriggerExit(GameObject gameObject)
|
|
{
|
|
if (usingFootbridge) return;
|
|
DebugLog("OnInvokeFootbridgeTriggerExit");
|
|
OnFootbridgeTriggerExit?.Invoke();
|
|
OnInvokeHideOnTriggerExit();
|
|
}
|
|
*/
|
|
|
|
private void OnInvokeActionTriggerEnter(TriggerDescriptor to)
|
|
{
|
|
DebugLog("OnInvokeActionTriggerEnter");
|
|
//OnActionTrigger(true, to.type);
|
|
//OnActionTriggerEnter?.Invoke();
|
|
//OnInvokeHideOnTriggerEnter();
|
|
if (!m_Triggers.Contains(to))
|
|
m_Triggers.Add(to);
|
|
}
|
|
|
|
private void OnInvokeActionTriggerExit(TriggerDescriptor to)
|
|
{
|
|
//OnActionTrigger(false, to.type);
|
|
DebugLog("OnInvokeActionTriggerExit");
|
|
//OnActionTriggerExit?.Invoke();
|
|
//OnInvokeHideOnTriggerExit();
|
|
if (m_Triggers.Contains(to))
|
|
{
|
|
m_Triggers.Remove(to);
|
|
if (to == m_currentDesc)
|
|
{
|
|
OnActionTrigger(false, to.type);
|
|
m_currentDesc = null;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("Exitting trigger that is not on the list! " + to.obj.name, this);
|
|
}
|
|
}
|
|
|
|
/*
|
|
private void OnInvokeLadderTriggerEnter(GameObject gameObject)
|
|
{
|
|
DebugLog("OnInvokeLadderTriggerEnter");
|
|
OnLadderTriggerEnter?.Invoke();
|
|
OnInvokeHideOnTriggerEnter();
|
|
}
|
|
|
|
private void OnInvokeLadderTriggerExit(GameObject gameObject)
|
|
{
|
|
DebugLog("OnInvokeLadderTriggerExit");
|
|
OnLadderTriggerExit?.Invoke();
|
|
OnInvokeHideOnTriggerExit();
|
|
}
|
|
*/
|
|
|
|
private void Update()
|
|
{
|
|
var gameState = GameStateManager.Instance.CurrentState;
|
|
if (GameStateManager.Instance.CurrentState != m_prevGameState && m_currentDesc != null)
|
|
{
|
|
if (gameState == GameStateManager.State.COMBAT)
|
|
{
|
|
if (m_currentDesc != null)
|
|
{
|
|
OnActionTrigger(false, m_currentDesc.type);
|
|
}
|
|
|
|
m_prevGameState = gameState;
|
|
return;
|
|
}
|
|
else if (gameState == GameStateManager.State.NORMAL)
|
|
{
|
|
OnActionTrigger(true, m_currentDesc.type);
|
|
}
|
|
}
|
|
|
|
m_prevGameState = gameState;
|
|
if (m_Triggers.Count > 0)
|
|
{
|
|
if (m_Triggers.Count > 1)
|
|
{
|
|
m_Triggers.Sort();
|
|
}
|
|
|
|
if (m_currentDesc != m_Triggers[0])
|
|
{
|
|
if (m_currentDesc != null)
|
|
{
|
|
OnActionTrigger(false, m_currentDesc.type);
|
|
}
|
|
|
|
m_currentDesc = null;
|
|
if (gameState != GameStateManager.State.COMBAT)
|
|
{
|
|
m_currentDesc = m_Triggers[0];
|
|
OnActionTrigger(true, m_currentDesc.type);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |