This commit is contained in:
Syntriax 2022-11-20 21:56:53 +03:00
parent 588a5fbdd2
commit 2f16d0d334
9 changed files with 74 additions and 38 deletions

View File

@ -1,21 +1,30 @@
using System;
using Syntriax.Modules.ToggleState; using Syntriax.Modules.ToggleState;
using UnityEngine; using UnityEngine;
namespace Syntriax.Modules.Action namespace Syntriax.Modules.Action
{ {
public abstract class ActionBase : MonoBehaviour, IActionActivate public abstract class ActionBase : MonoBehaviour, IAction
{ {
public IToggleState ToggleState { get; protected set; } = null; public IToggleState ToggleState { get; protected set; } = null;
public Action<IAction> OnActivated { get; set; } = null;
protected virtual void Awake() => ToggleState = new ToggleStateMember(true); protected virtual void Awake()
{
ToggleState = new ToggleStateMember(true);
OnActivated += (_) => OnActionActivated();
}
protected abstract void OnActivated(); /// <summary>
/// Called when the current <see cref="ActionBase"/> gets activated
/// </summary>
protected abstract void OnActionActivated();
public virtual void Activate() public virtual void Activate()
{ {
if (!ToggleState.IsToggledNullChecked()) if (!ToggleState.IsToggledNullChecked())
return; return;
OnActivated(); OnActivated?.Invoke(this);
} }
} }
} }

View File

@ -1,16 +1,28 @@
using System;
using Syntriax.Modules.ToggleState; using Syntriax.Modules.ToggleState;
namespace Syntriax.Modules.Action namespace Syntriax.Modules.Action
{ {
public abstract class ActionBaseWithDeactivation : ActionBase, IActionDeactivate public abstract class ActionBaseWithDeactivation : ActionBase, IActionWithDeactivation
{ {
protected abstract void OnDeactivated(); public Action<IAction> OnDeactivated { get; set; } = null;
protected override void Awake()
{
base.Awake();
OnDeactivated += (_) => OnActionDeactivated();
}
/// <summary>
/// Called when the current <see cref="ActionBase"/> gets deactivated
/// </summary>
protected abstract void OnActionDeactivated();
public virtual void Deactivate() public virtual void Deactivate()
{ {
if (!ToggleState.IsToggledNullChecked()) if (!ToggleState.IsToggledNullChecked())
return; return;
OnDeactivated(); OnDeactivated?.Invoke(this);
} }
} }
} }

View File

@ -59,7 +59,7 @@ namespace Syntriax.Modules.Action
rigid.velocity = velocity; rigid.velocity = velocity;
} }
protected override void OnDeactivated() protected override void OnActionDeactivated()
{ {
if (!gameObjectToggleState.IsToggledNullChecked()) if (!gameObjectToggleState.IsToggledNullChecked())
return; return;
@ -67,7 +67,7 @@ namespace Syntriax.Modules.Action
airSuspension = false; airSuspension = false;
} }
protected override void OnActivated() protected override void OnActionActivated()
{ {
if (!gameObjectToggleState.IsToggledNullChecked()) if (!gameObjectToggleState.IsToggledNullChecked())
return; return;

24
IAction.cs Normal file
View File

@ -0,0 +1,24 @@
using System;
using Syntriax.Modules.ToggleState;
namespace Syntriax.Modules.Action
{
public interface IAction
{
/// <summary>
/// The <see cref="IToggleState"/> the Action uses to check if it's active or not
/// </summary>
IToggleState ToggleState { get; }
/// <summary>
/// Called when the <see cref="IAction"/> is Activated
/// </summary>
/// <value>The <see cref="IAction"/> that Activated</value>
Action<IAction> OnActivated { get; set; }
/// <summary>
/// Triggers the <see cref="IAction"/>
/// </summary>
void Activate();
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 9ff9f1a7b70a47e4aa97dfda371fbf21 guid: 4f1eb00830f616b40afd45bec1425a7f
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View File

@ -1,17 +0,0 @@
using Syntriax.Modules.ToggleState;
namespace Syntriax.Modules.Action
{
public interface IActionActivate
{
/// <summary>
/// The <see cref="IToggleState"> the Action uses to check if it's active or not
/// </summary>
IToggleState ToggleState { get; }
/// <summary>
/// Triggers the Action
/// </summary>
void Activate();
}
}

View File

@ -1,10 +0,0 @@
namespace Syntriax.Modules.Action
{
public interface IActionDeactivate : IActionActivate
{
/// <summary>
/// Deactivates the action
/// </summary>
void Deactivate();
}
}

View File

@ -0,0 +1,18 @@
using System;
namespace Syntriax.Modules.Action
{
public interface IActionWithDeactivation : IAction
{
/// <summary>
/// Called when the <see cref="IAction"/> is Deactivated
/// </summary>
/// <value>The <see cref="IAction"/> that Deactivated</value>
Action<IAction> OnDeactivated { get; set; }
/// <summary>
/// Called when the <see cref="IAction"/> is Deactivated
/// </summary>
void Deactivate();
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 10a30eb2debabdf40b2f8120a5f1ad01 guid: eaf437843a281c74c86f7db482b44077
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2