From 7e154d34e539926e1eafe565971d94059803d63e Mon Sep 17 00:00:00 2001 From: Syntriax Date: Sun, 20 Nov 2022 18:31:16 +0300 Subject: [PATCH] First Commit --- Actions.meta | 8 ++++ Actions/PlatformerJump.cs | 75 ++++++++++++++++++++++++++++++++++ Actions/PlatformerJump.cs.meta | 11 +++++ IActionActivate.cs | 11 +++++ IActionActivate.cs.meta | 11 +++++ IActionDeactivate.cs | 7 ++++ IActionDeactivate.cs.meta | 11 +++++ README.md.meta | 7 ++++ 8 files changed, 141 insertions(+) create mode 100644 Actions.meta create mode 100644 Actions/PlatformerJump.cs create mode 100644 Actions/PlatformerJump.cs.meta create mode 100644 IActionActivate.cs create mode 100644 IActionActivate.cs.meta create mode 100644 IActionDeactivate.cs create mode 100644 IActionDeactivate.cs.meta create mode 100644 README.md.meta diff --git a/Actions.meta b/Actions.meta new file mode 100644 index 0000000..c3f4d4e --- /dev/null +++ b/Actions.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 341c2035d0254344cad44ff6138adf58 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Actions/PlatformerJump.cs b/Actions/PlatformerJump.cs new file mode 100644 index 0000000..1573a9f --- /dev/null +++ b/Actions/PlatformerJump.cs @@ -0,0 +1,75 @@ +using Syntriax.Modules.ToggleState; +using Syntriax.Modules.Trigger; +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; + [SerializeField] private float fallMultiplier = 1.5f; + public float FallMultiplier + { + get => fallMultiplier; + set => fallMultiplier = value * Time.fixedDeltaTime; + } + [SerializeField] private float suspensionMultiplier = 1f; + public float SuspensionMultiplier + { + get => suspensionMultiplier; + set => suspensionMultiplier = value * Time.fixedDeltaTime; + } + + 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(); + } + + protected virtual void FixedUpdate() + { + if (!ToggleState.IsToggledNullChecked() || !gameObjectToggleState.IsToggledNullChecked()) + return; + + if (rigid.velocity.y < FallThreshold) + ApplySuspension(SuspensionMultiplier); + else if (!airSuspension) + ApplySuspension(FallMultiplier); + } + + protected virtual void ApplySuspension(float multiplier) + => rigid.velocity += Physics2D.gravity * multiplier; + + protected virtual void Jump() + { + Vector2 velocity = rigid.velocity; + velocity.y = JumpSpeed; + rigid.velocity = velocity; + } + + public virtual void Activate() + { + if (!ToggleState.IsToggledNullChecked() || !gameObjectToggleState.IsToggledNullChecked()) + return; + + if (groundCheck.IsTrigerred) + Jump(); + + airSuspension = true; + } + + public virtual void Deactivate() => airSuspension = false; + } +} diff --git a/Actions/PlatformerJump.cs.meta b/Actions/PlatformerJump.cs.meta new file mode 100644 index 0000000..21b147d --- /dev/null +++ b/Actions/PlatformerJump.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e45c80df261a5714e9a22fd73a2cd39a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/IActionActivate.cs b/IActionActivate.cs new file mode 100644 index 0000000..07ff5a0 --- /dev/null +++ b/IActionActivate.cs @@ -0,0 +1,11 @@ +using Syntriax.Modules.ToggleState; + +namespace Syntriax.Modules.Action +{ + public interface IActionActivate + { + IToggleState ToggleState { get; } + + void Activate(); + } +} diff --git a/IActionActivate.cs.meta b/IActionActivate.cs.meta new file mode 100644 index 0000000..9d7d8d9 --- /dev/null +++ b/IActionActivate.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9ff9f1a7b70a47e4aa97dfda371fbf21 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/IActionDeactivate.cs b/IActionDeactivate.cs new file mode 100644 index 0000000..f54d113 --- /dev/null +++ b/IActionDeactivate.cs @@ -0,0 +1,7 @@ +namespace Syntriax.Modules.Action +{ + public interface IActionDeactivate : IActionActivate + { + void Deactivate(); + } +} diff --git a/IActionDeactivate.cs.meta b/IActionDeactivate.cs.meta new file mode 100644 index 0000000..54b93ac --- /dev/null +++ b/IActionDeactivate.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 10a30eb2debabdf40b2f8120a5f1ad01 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/README.md.meta b/README.md.meta new file mode 100644 index 0000000..079916c --- /dev/null +++ b/README.md.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 89a86cb669e56984987cdfabde5bc3bd +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: