GroundChecker Improvements

This commit is contained in:
Syntriax 2022-11-14 12:02:31 +03:00
parent d76675b6f1
commit f099d7311b
12 changed files with 11 additions and 140 deletions

View File

@ -1,34 +1,19 @@
using System;
using UnityEngine;
namespace Syntriax.Modules.Movement.ColliderTrigger
{
public class TwoDimensionalBoxChildColliderTrigger : MonoBehaviour, IColliderTrigger
public class Box2DColliderTrigger : Trigger, IColliderTrigger
{
[SerializeField] private LayerMask colliderMask = 0;
private bool _isTrigerred = false;
private Collider2D[] results = new Collider2D[8];
public LayerMask ColliderMask { get => colliderMask; set => colliderMask = value; }
public Action<bool> OnTriggered { get; set; } = null;
public bool IsTrigerred
{
get => _isTrigerred;
protected set
{
bool isNewValue = _isTrigerred != value;
_isTrigerred = value;
if (isNewValue)
OnTriggered.Invoke(value);
}
}
[SerializeField] private LayerMask colliderMask = 0;
protected void FixedUpdate()
=> IsTrigerred = Physics2D.OverlapBoxNonAlloc(transform.position, transform.localScale, 0, results, ColliderMask) > 0;
#if UNITY_EDITOR
private void OnDrawGizmosSelected()
{

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: ccd6df43b281de34fb8ea893de8ec4c6
guid: 20a6baf9a256917448a4d7ad09d0c51a
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@ -0,0 +1,4 @@
namespace Syntriax.Modules.Movement.ColliderTrigger
{
public class Box2DGroundTrigger : Box2DColliderTrigger, IGroundTrigger { }
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: df412a9878b7d5b43ab34aef7261ec3a
guid: 7804f3e8384c7a54482f585b4f43264a
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@ -1,4 +0,0 @@
namespace Syntriax.Modules.Movement.ColliderTrigger
{
public class TwoDimensionalBoxChildGroundTrigger : TwoDimensionalBoxChildColliderTrigger, IGroundTrigger { }
}

View File

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

View File

@ -12,7 +12,7 @@ namespace Syntriax.Modules.Movement.Implementations.TwoDimensional
{
base.Start();
groundTrigger = GetComponentInChildren<IGroundTrigger>();
groundTrigger.OnTriggered.AddListener(OnGroundTrigger);
groundTrigger.OnTriggered += OnGroundTrigger;
}
private void OnGroundTrigger(bool isGrounded)

View File

@ -1,40 +0,0 @@
using Syntriax.Modules.Movement.ColliderTrigger;
using UnityEngine;
namespace Syntriax.Modules.Movement.Implementations.TwoDimensional
{
public class OneWay2DAirMovement : OneWay2DMovementBase
{
protected override float moveValue { get; set; } = 0f;
protected IGroundTrigger groundTrigger = null;
protected override void Start()
{
base.Start();
groundTrigger = GetComponentInChildren<IGroundTrigger>();
groundTrigger.OnTriggered += OnGroundTrigger;
}
private void OnGroundTrigger(bool isGrounded)
=> CanTakeOver = !isGrounded;
public override void ApplyMovement()
{
Vector2 velocity = rigid.velocity;
velocity.x += moveValue * Time.fixedDeltaTime;
if (moveValue != 0f)
{
if (Mathf.Abs(velocity.x) > Mathf.Abs(moveValue))
velocity.x = moveValue;
else if (Mathf.Abs(velocity.x - moveValue) > Mathf.Abs(moveValue))
velocity.x += moveValue * Time.fixedDeltaTime;
}
rigid.velocity = velocity;
}
protected override void OnDeactivated() { }
protected override void OnActivated() { }
}
}

View File

@ -1,31 +0,0 @@
using Syntriax.Modules.Movement.ColliderTrigger;
using UnityEngine;
namespace Syntriax.Modules.Movement.Implementations.TwoDimensional
{
public class OneWay2DGroundMovement : OneWay2DMovementBase
{
protected override float moveValue { get; set; } = 0f;
protected IGroundTrigger groundTrigger = null;
protected override void Start()
{
base.Start();
groundTrigger = GetComponentInChildren<IGroundTrigger>();
groundTrigger.OnTriggered += OnGroundTrigger;
}
private void OnGroundTrigger(bool isGrounded)
=> CanTakeOver = isGrounded;
public override void ApplyMovement()
{
Vector2 velocity = rigid.velocity;
velocity.x = moveValue;
rigid.velocity = velocity;
}
protected override void OnDeactivated() { }
protected override void OnActivated() { }
}
}

View File

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

View File

@ -1,10 +0,0 @@
namespace Syntriax.Modules.Movement.Implementations.TwoDimensional
{
public abstract class OneWay2DMovementBase : TwoDimensionalMovementBase
{
protected abstract float moveValue { get; set; }
public override void Move(float x = 0, float y = 0, float z = 0)
=> moveValue = x * BaseSpeed * MovementMultiplier;
}
}

View File

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