Rewrite
- Movement & Controller - Special Actions - ToggleState - ColliderChecker - and 2D Implementations
This commit is contained in:
62
2D/MovementController.cs
Normal file
62
2D/MovementController.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using System.Collections.Generic;
|
||||
using Syntriax.Modules.Movement.State;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
|
||||
namespace Syntriax.Modules.Movement
|
||||
{
|
||||
public class MovementController : MonoBehaviour, IMovementController
|
||||
{
|
||||
|
||||
public IMovement ActiveMovement { get; protected set; } = null;
|
||||
public UnityEvent<IMovement> OnMovementChanged { get; protected set; } = new UnityEvent<IMovement>();
|
||||
|
||||
protected List<IMovement> movements = new List<IMovement>(32);
|
||||
protected IToggleState toggleState = null;
|
||||
|
||||
protected virtual void Start()
|
||||
{
|
||||
toggleState = GetComponent<IToggleState>();
|
||||
UpdateMovementCache();
|
||||
}
|
||||
|
||||
protected virtual void FixedUpdate()
|
||||
{
|
||||
if (!toggleState.Toggled)
|
||||
return;
|
||||
|
||||
ActiveMovement.ApplyMovement();
|
||||
}
|
||||
|
||||
protected virtual void UpdateMovementCache()
|
||||
{
|
||||
foreach (IMovement movement in movements)
|
||||
movement.OnTakeOverStateChanged.RemoveListener(OnTakeOverListener);
|
||||
|
||||
movements.Clear();
|
||||
GetComponents<IMovement>(movements);
|
||||
UpdateActiveMovement();
|
||||
|
||||
foreach (IMovement movement in movements)
|
||||
movement.OnTakeOverStateChanged.AddListener(OnTakeOverListener);
|
||||
|
||||
}
|
||||
|
||||
private void OnTakeOverListener(bool arg0) => UpdateActiveMovement();
|
||||
|
||||
protected virtual void UpdateActiveMovement()
|
||||
{
|
||||
foreach (IMovement movement in movements)
|
||||
if (movement.CanTakeOver)
|
||||
{
|
||||
ActiveMovement = movement;
|
||||
OnMovementChanged.Invoke(ActiveMovement);
|
||||
return;
|
||||
}
|
||||
|
||||
ActiveMovement = movements[movements.Count - 1];
|
||||
OnMovementChanged.Invoke(ActiveMovement);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 12a58daabbf3fa8418a164390a593753
|
||||
guid: f9a57082945be29498d2785f8be6484d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
@@ -1,54 +0,0 @@
|
||||
using Syntriax.Modules.Movement.ColliderCheck;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Syntriax.Modules.Movement
|
||||
{
|
||||
[RequireComponent(typeof(Rigidbody2D))]
|
||||
public class OneDimensional2DAirMovement : MonoBehaviour, IMovement, IState
|
||||
{
|
||||
private IGroundCheck groundCheck = null;
|
||||
private Rigidbody2D rigid = null;
|
||||
private float moveValue = 0f;
|
||||
|
||||
|
||||
public bool IsActive => StateEnabled && !groundCheck.IsCollided();
|
||||
public bool StateEnabled { get; set; } = true;
|
||||
public float BaseSpeed { get; set; } = 1f;
|
||||
public float MovementMultiplier { get; set; } = 1f;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
rigid = GetComponent<Rigidbody2D>();
|
||||
groundCheck = GetComponentInChildren<IGroundCheck>();
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (!IsActive)
|
||||
return;
|
||||
|
||||
ApplyMovement();
|
||||
}
|
||||
|
||||
private 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;
|
||||
}
|
||||
|
||||
public void Move(float x = 0, float y = 0, float z = 0)
|
||||
{
|
||||
moveValue = x * BaseSpeed * MovementMultiplier;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,44 +0,0 @@
|
||||
using Syntriax.Modules.Movement.ColliderCheck;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Syntriax.Modules.Movement
|
||||
{
|
||||
[RequireComponent(typeof(Rigidbody2D))]
|
||||
public class OneDimensional2DGroundMovement : MonoBehaviour, IMovement, IState
|
||||
{
|
||||
private IGroundCheck groundCheck = null;
|
||||
private Rigidbody2D rigid = null;
|
||||
private float moveValue = 0f;
|
||||
|
||||
public bool IsActive => StateEnabled && groundCheck.IsCollided();
|
||||
public bool StateEnabled { get; set; } = true;
|
||||
public float BaseSpeed { get; set; } = 1f;
|
||||
public float MovementMultiplier { get; set; } = 1f;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
rigid = GetComponent<Rigidbody2D>();
|
||||
groundCheck = GetComponentInChildren<IGroundCheck>();
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (!IsActive)
|
||||
return;
|
||||
|
||||
ApplyMovement();
|
||||
}
|
||||
|
||||
private void ApplyMovement()
|
||||
{
|
||||
Vector2 velocity = rigid.velocity;
|
||||
velocity.x = moveValue;
|
||||
rigid.velocity = velocity;
|
||||
}
|
||||
|
||||
public void Move(float x = 0, float y = 0, float z = 0)
|
||||
{
|
||||
moveValue = x * BaseSpeed * MovementMultiplier;
|
||||
}
|
||||
}
|
||||
}
|
37
2D/OneWay2DAirMovement.cs
Normal file
37
2D/OneWay2DAirMovement.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using Syntriax.Modules.Movement.ColliderTrigger;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Syntriax.Modules.Movement
|
||||
{
|
||||
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.AddListener(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;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 849a6316df6662a48ae6cfcd4c2fbc1e
|
||||
guid: 2f38efc38cadfd94ba8698189577f60f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
28
2D/OneWay2DGroundMovement.cs
Normal file
28
2D/OneWay2DGroundMovement.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using Syntriax.Modules.Movement.ColliderTrigger;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Syntriax.Modules.Movement
|
||||
{
|
||||
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.AddListener(OnGroundTrigger);
|
||||
}
|
||||
|
||||
private void OnGroundTrigger(bool isGrounded)
|
||||
=> CanTakeOver = isGrounded;
|
||||
|
||||
public override void ApplyMovement()
|
||||
{
|
||||
Vector2 velocity = rigid.velocity;
|
||||
velocity.x = moveValue;
|
||||
rigid.velocity = velocity;
|
||||
}
|
||||
}
|
||||
}
|
11
2D/OneWay2DGroundMovement.cs.meta
Normal file
11
2D/OneWay2DGroundMovement.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9c73429b47407634daead7ee1b52629d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
10
2D/OneWay2DMovementBase.cs
Normal file
10
2D/OneWay2DMovementBase.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace Syntriax.Modules.Movement
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
11
2D/OneWay2DMovementBase.cs.meta
Normal file
11
2D/OneWay2DMovementBase.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 51ee606e68723324b92b9d16835c1625
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
48
2D/TwoDimensionalMovementBase.cs
Normal file
48
2D/TwoDimensionalMovementBase.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using Syntriax.Modules.Movement.State;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
namespace Syntriax.Modules.Movement
|
||||
{
|
||||
[RequireComponent(typeof(Rigidbody2D))]
|
||||
public abstract class TwoDimensionalMovementBase : MonoBehaviour, IMovement
|
||||
{
|
||||
protected Rigidbody2D rigid = null;
|
||||
protected IToggleState toggleState = null;
|
||||
private bool _canTakeOver = false;
|
||||
|
||||
public float BaseSpeed { get; set; } = 1f;
|
||||
public float MovementMultiplier { get; set; } = 1f;
|
||||
|
||||
public UnityEvent<bool> OnTakeOverStateChanged { get; protected set; } = null;
|
||||
public bool CanTakeOver
|
||||
{
|
||||
get => _canTakeOver;
|
||||
protected set
|
||||
{
|
||||
bool isNewValue = _canTakeOver != value;
|
||||
|
||||
_canTakeOver = value;
|
||||
|
||||
if (isNewValue)
|
||||
OnTakeOverStateChanged.Invoke(value);
|
||||
}
|
||||
}
|
||||
|
||||
public IToggleState ToggleState { get; protected set; } = null;
|
||||
|
||||
protected virtual void Awake()
|
||||
{
|
||||
OnTakeOverStateChanged = new UnityEvent<bool>();
|
||||
}
|
||||
|
||||
protected virtual void Start()
|
||||
{
|
||||
rigid = GetComponent<Rigidbody2D>();
|
||||
toggleState = GetComponent<ToggleState>();
|
||||
}
|
||||
|
||||
public abstract void Move(float x = 0, float y = 0, float z = 0);
|
||||
public abstract void ApplyMovement();
|
||||
}
|
||||
}
|
11
2D/TwoDimensionalMovementBase.cs.meta
Normal file
11
2D/TwoDimensionalMovementBase.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b4a8f74afcbe280448862bac89545353
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user