Trigger/Runtime/TriggerBase.cs

49 lines
1.3 KiB
C#

using System;
using Syntriax.Modules.State;
using UnityEngine;
namespace Syntriax.Modules.Trigger
{
public class TriggerBase : MonoBehaviour, ITrigger
{
public Action<bool> OnTriggerStateChanged { get; set; } = null;
private IStateEnable stateEnable = null;
public IStateEnable StateEnable
{
get
{
if (stateEnable == null)
{
stateEnable = GetComponent<IStateEnable>() ?? gameObject.AddComponent<StateEnableMonoBehaviour>();
stateEnable.OnEnabledChanged += OnEnabledChanged;
}
return stateEnable;
}
}
private void OnEnabledChanged(bool enabled)
{
if (enabled)
return;
_isTrigerred = false;
OnTriggerStateChanged?.Invoke(false);
}
private bool _isTrigerred = false;
public bool IsTrigerred
{
get => _isTrigerred;
protected set
{
if (value == _isTrigerred || !StateEnable.IsEnabledNullChecked())
return;
_isTrigerred = value;
OnTriggerStateChanged?.Invoke(value);
}
}
}
}