From d9b1b18328f6ab6cfa363725d00e7deabd060a1a Mon Sep 17 00:00:00 2001 From: Syntriax Date: Sun, 20 Nov 2022 18:45:36 +0300 Subject: [PATCH] Action Bases --- ActionBase.cs | 21 +++++++++++++++++ ActionBase.cs.meta | 11 +++++++++ ActionBaseWithDeactivation.cs | 16 +++++++++++++ ActionBaseWithDeactivation.cs.meta | 11 +++++++++ Actions/PlatformerJump.cs | 36 +++++++++++++++++++----------- 5 files changed, 82 insertions(+), 13 deletions(-) create mode 100644 ActionBase.cs create mode 100644 ActionBase.cs.meta create mode 100644 ActionBaseWithDeactivation.cs create mode 100644 ActionBaseWithDeactivation.cs.meta diff --git a/ActionBase.cs b/ActionBase.cs new file mode 100644 index 0000000..0d0f46d --- /dev/null +++ b/ActionBase.cs @@ -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(); + } + } +} diff --git a/ActionBase.cs.meta b/ActionBase.cs.meta new file mode 100644 index 0000000..b35e7fd --- /dev/null +++ b/ActionBase.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d07cd6f6a7a635c41add4964a6aba382 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ActionBaseWithDeactivation.cs b/ActionBaseWithDeactivation.cs new file mode 100644 index 0000000..52a72f6 --- /dev/null +++ b/ActionBaseWithDeactivation.cs @@ -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(); + } + } +} diff --git a/ActionBaseWithDeactivation.cs.meta b/ActionBaseWithDeactivation.cs.meta new file mode 100644 index 0000000..043bd8c --- /dev/null +++ b/ActionBaseWithDeactivation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7bce8bd8b35a51f4785a3933e3526ddf +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Actions/PlatformerJump.cs b/Actions/PlatformerJump.cs index 1573a9f..402c354 100644 --- a/Actions/PlatformerJump.cs +++ b/Actions/PlatformerJump.cs @@ -4,20 +4,22 @@ using UnityEngine; 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; public float FallMultiplier { get => fallMultiplier; set => fallMultiplier = value * Time.fixedDeltaTime; } + [SerializeField] private float suspensionMultiplier = 1f; public float SuspensionMultiplier { @@ -25,17 +27,19 @@ namespace Syntriax.Modules.Action set => suspensionMultiplier = value * Time.fixedDeltaTime; } + protected IToggleState gameObjectToggleState = null; protected bool airSuspension = false; protected IGroundTrigger groundCheck = null; - protected IToggleState gameObjectToggleState = null; protected Rigidbody2D rigid = null; protected virtual void Start() { rigid = GetComponent(); groundCheck = GetComponentInChildren(); - ToggleState = new ToggleStateMember(true); gameObjectToggleState = GetComponent(); + + FallMultiplier = fallMultiplier; + SuspensionMultiplier = suspensionMultiplier; } protected virtual void FixedUpdate() @@ -59,9 +63,17 @@ namespace Syntriax.Modules.Action 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; if (groundCheck.IsTrigerred) @@ -69,7 +81,5 @@ namespace Syntriax.Modules.Action airSuspension = true; } - - public virtual void Deactivate() => airSuspension = false; } }