Action/Runtime/ActionBase.cs

34 lines
1005 B
C#
Raw Normal View History

2022-11-20 21:56:53 +03:00
using System;
2022-11-20 18:45:36 +03:00
using Syntriax.Modules.ToggleState;
using UnityEngine;
namespace Syntriax.Modules.Action
{
2022-11-20 21:56:53 +03:00
public abstract class ActionBase : MonoBehaviour, IAction
2022-11-20 18:45:36 +03:00
{
2022-12-16 23:24:02 +03:00
public IToggleState MemberToggleState { get; protected set; } = null;
2022-11-20 21:56:53 +03:00
public Action<IAction> OnActivated { get; set; } = null;
2022-11-20 18:45:36 +03:00
2022-12-16 23:24:02 +03:00
protected IToggleState toggleState = null;
2022-11-20 21:56:53 +03:00
protected virtual void Awake()
{
2022-12-16 23:24:02 +03:00
toggleState = GetComponent<IToggleState>();
MemberToggleState = new ToggleStateMember(true);
2022-11-20 21:56:53 +03:00
OnActivated += (_) => OnActionActivated();
}
2022-11-20 18:45:36 +03:00
2022-11-20 21:56:53 +03:00
/// <summary>
/// Called when the current <see cref="ActionBase"/> gets activated
/// </summary>
protected abstract void OnActionActivated();
2022-11-20 18:45:36 +03:00
public virtual void Activate()
{
2022-12-16 23:24:02 +03:00
if (!MemberToggleState.IsToggledNullChecked() || !toggleState.IsToggledNullChecked())
2022-11-20 18:45:36 +03:00
return;
2022-11-20 21:56:53 +03:00
OnActivated?.Invoke(this);
2022-11-20 18:45:36 +03:00
}
}
}