Movement/Implementations/2D/TwoDimensionalMovementBase.cs

65 lines
1.9 KiB
C#

using System;
using Syntriax.Modules.Movement.State;
using UnityEngine;
namespace Syntriax.Modules.Movement.Implementations.TwoDimensional
{
[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 Action<bool> OnTakeOverStateChanged { get; 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 Start()
{
rigid = GetComponent<Rigidbody2D>();
toggleState = GetComponent<ToggleState>();
movementController = GetComponent<IMovementController>();
movementController.OnMovementActivated += OnActivated;
movementController.OnMovementDeactivated += 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();
}
}