Action Bases

This commit is contained in:
Syntriax 2022-11-20 18:45:36 +03:00
parent 7e154d34e5
commit d9b1b18328
5 changed files with 82 additions and 13 deletions

21
ActionBase.cs Normal file
View File

@ -0,0 +1,21 @@
using Syntriax.Modules.ToggleState;
using UnityEngine;
namespace Syntriax.Modules.Action
{
public abstract class ActionBase : MonoBehaviour, IActionActivate
{
public IToggleState ToggleState { get; protected set; } = null;
protected virtual void Awake() => ToggleState = new ToggleStateMember(true);
protected abstract void OnActivated();
public virtual void Activate()
{
if (!ToggleState.IsToggledNullChecked())
return;
OnActivated();
}
}
}

11
ActionBase.cs.meta Normal file
View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d07cd6f6a7a635c41add4964a6aba382
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,16 @@
using Syntriax.Modules.ToggleState;
namespace Syntriax.Modules.Action
{
public abstract class ActionBaseWithDeactivation : ActionBase, IActionDeactivate
{
protected abstract void OnDeactivated();
public virtual void Deactivate()
{
if (!ToggleState.IsToggledNullChecked())
return;
OnDeactivated();
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7bce8bd8b35a51f4785a3933e3526ddf
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -4,20 +4,22 @@ using UnityEngine;
namespace Syntriax.Modules.Action namespace Syntriax.Modules.Action
{ {
[RequireComponent(typeof(Rigidbody2D))]
public class PlatformerJump : MonoBehaviour, IActionActivate, IActionDeactivate
{
private float jumpSpeed = 10f;
public float JumpSpeed { get => jumpSpeed; set => jumpSpeed = value; }
public IToggleState ToggleState { get; protected set; } = null;
public float FallThreshold { get; set; } = 0f; [RequireComponent(typeof(Rigidbody2D))]
public class PlatformerJump : ActionBaseWithDeactivation
{
[SerializeField] private float jumpSpeed = 10f;
public float JumpSpeed { get => jumpSpeed; set => jumpSpeed = value; }
[SerializeField] private float fallThreshold = 0f;
public float FallThreshold { get => fallThreshold; set => fallThreshold = value; }
[SerializeField] private float fallMultiplier = 1.5f; [SerializeField] private float fallMultiplier = 1.5f;
public float FallMultiplier public float FallMultiplier
{ {
get => fallMultiplier; get => fallMultiplier;
set => fallMultiplier = value * Time.fixedDeltaTime; set => fallMultiplier = value * Time.fixedDeltaTime;
} }
[SerializeField] private float suspensionMultiplier = 1f; [SerializeField] private float suspensionMultiplier = 1f;
public float SuspensionMultiplier public float SuspensionMultiplier
{ {
@ -25,17 +27,19 @@ namespace Syntriax.Modules.Action
set => suspensionMultiplier = value * Time.fixedDeltaTime; set => suspensionMultiplier = value * Time.fixedDeltaTime;
} }
protected IToggleState gameObjectToggleState = null;
protected bool airSuspension = false; protected bool airSuspension = false;
protected IGroundTrigger groundCheck = null; protected IGroundTrigger groundCheck = null;
protected IToggleState gameObjectToggleState = null;
protected Rigidbody2D rigid = null; protected Rigidbody2D rigid = null;
protected virtual void Start() protected virtual void Start()
{ {
rigid = GetComponent<Rigidbody2D>(); rigid = GetComponent<Rigidbody2D>();
groundCheck = GetComponentInChildren<IGroundTrigger>(); groundCheck = GetComponentInChildren<IGroundTrigger>();
ToggleState = new ToggleStateMember(true);
gameObjectToggleState = GetComponent<IToggleState>(); gameObjectToggleState = GetComponent<IToggleState>();
FallMultiplier = fallMultiplier;
SuspensionMultiplier = suspensionMultiplier;
} }
protected virtual void FixedUpdate() protected virtual void FixedUpdate()
@ -59,9 +63,17 @@ namespace Syntriax.Modules.Action
rigid.velocity = velocity; rigid.velocity = velocity;
} }
public virtual void Activate() protected override void OnDeactivated()
{ {
if (!ToggleState.IsToggledNullChecked() || !gameObjectToggleState.IsToggledNullChecked()) if (!gameObjectToggleState.IsToggledNullChecked())
return;
airSuspension = false;
}
protected override void OnActivated()
{
if (!gameObjectToggleState.IsToggledNullChecked())
return; return;
if (groundCheck.IsTrigerred) if (groundCheck.IsTrigerred)
@ -69,7 +81,5 @@ namespace Syntriax.Modules.Action
airSuspension = true; airSuspension = true;
} }
public virtual void Deactivate() => airSuspension = false;
} }
} }