Movement/2D/TwoDimensionalMovementBase.cs

70 lines
2.1 KiB
C#

using Syntriax.Modules.Movement.State;
using UnityEngine;
using UnityEngine.Events;
namespace Syntriax.Modules.Movement.TwoDimensionalImplementations
{
[RequireComponent(typeof(Rigidbody2D))]
public abstract class TwoDimensionalMovementBase : MonoBehaviour, IMovement
{
protected Rigidbody2D rigid = null;
protected IToggleState toggleState = null;
protected IMovementController movementController = 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>();
movementController = GetComponent<IMovementController>();
movementController.OnMovementActivated.AddListener(OnActivated);
movementController.OnMovementDeactivated.AddListener(OnDeactivated);
}
protected abstract void OnDeactivated();
private void OnDeactivated(IMovement movement)
{
if ((object)movement != this)
return;
OnDeactivated();
}
protected abstract void OnActivated();
private void OnActivated(IMovement movement)
{
if ((object)movement != this)
return;
OnActivated();
}
public abstract void Move(float x = 0, float y = 0, float z = 0);
public abstract void ApplyMovement();
}
}