Movement/Runtime/Bases/MovementBase.cs

78 lines
2.4 KiB
C#

using System;
using Syntriax.Modules.State;
using UnityEngine;
namespace Syntriax.Modules.Movement
{
public abstract class MovementBase : MonoBehaviour, IMovement
{
protected IStateEnable stateEnable = null;
protected IMovementController movementController = null;
[SerializeField] private float _baseSpeed = 1f;
public float BaseSpeed { get => _baseSpeed; set => _baseSpeed = value; }
[SerializeField] private float _movementMultiplier = 1f;
public float MovementMultiplier { get => _movementMultiplier; set => _movementMultiplier = value; }
public Action<bool> OnTakeOverStateChanged { get; set; } = null;
private bool _canTakeOver = false;
public bool CanTakeOver
{
get => _canTakeOver;
protected set
{
if (value == _canTakeOver)
return;
_canTakeOver = value;
OnTakeOverStateChanged?.Invoke(value);
}
}
/// <inheritdoc/>
public abstract void ApplyMovement();
public abstract void Move(float x = 0, float y = 0, float z = 0);
/// <summary>
/// Called when this <see cref="IMovement"/> is activated.
/// </summary>
protected abstract void OnActivated();
/// <summary>
/// Called when this <see cref="IMovement"/> is deactivated.
/// </summary>
protected abstract void OnDeactivated();
protected virtual void Start()
{
stateEnable = GetComponent<IStateEnable>();
movementController = GetComponent<IMovementController>();
movementController.OnMovementActivated += OnActivated;
movementController.OnMovementDeactivated += OnDeactivated;
}
/// <summary>
/// Called when the <see cref="IMovementController"/> activates one of it's <see cref="IMovement"/>s.
/// </summary>
private void OnActivated(IMovement movement)
{
if ((object)movement != this)
return;
OnActivated();
}
/// <summary>
/// Called when the <see cref="IMovementController"/> activates one of it's <see cref="IMovement"/>s.
/// </summary>
private void OnDeactivated(IMovement movement)
{
if ((object)movement != this)
return;
OnDeactivated();
}
}
}