Trigger/Runtime/TriggerBase.cs

49 lines
1.3 KiB
C#
Raw Normal View History

2022-11-14 12:15:32 +03:00
using System;
2023-03-20 22:22:42 +03:00
using Syntriax.Modules.State;
2022-11-14 12:15:32 +03:00
using UnityEngine;
namespace Syntriax.Modules.Trigger
{
public class TriggerBase : MonoBehaviour, ITrigger
2022-11-14 12:15:32 +03:00
{
public Action<bool> OnTriggerStateChanged { get; set; } = null;
2023-03-20 22:22:42 +03:00
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;
2022-11-14 12:15:32 +03:00
public bool IsTrigerred
{
get => _isTrigerred;
protected set
{
2023-03-20 22:22:42 +03:00
if (value == _isTrigerred || !StateEnable.IsEnabledNullChecked())
2022-12-02 00:09:14 +03:00
return;
2022-11-14 12:15:32 +03:00
_isTrigerred = value;
OnTriggerStateChanged?.Invoke(value);
2022-11-14 12:15:32 +03:00
}
}
}
}