Movement/Bases/MovementBase.cs

79 lines
2.3 KiB
C#
Raw Normal View History

using System;
2022-11-14 13:04:09 +03:00
using Syntriax.Modules.ToggleState;
using UnityEngine;
namespace Syntriax.Modules.Movement
{
public abstract class MovementBase : MonoBehaviour, IMovement
{
protected IToggleState toggleState = null;
protected IMovementController movementController = null;
public float BaseSpeed { get; set; } = 1f;
public float MovementMultiplier { get; set; } = 1f;
public Action<bool> OnTakeOverStateChanged { get; set; } = null;
private bool _canTakeOver = false;
public bool CanTakeOver
{
get => _canTakeOver;
protected set
{
bool oldValue = _canTakeOver;
_canTakeOver = value;
if (oldValue != value)
OnTakeOverStateChanged?.Invoke(value);
}
}
public IToggleState ToggleState { get; protected set; } = null;
2022-11-20 13:32:06 +03:00
/// <inheritdoc/>
public abstract void ApplyMovement();
public abstract void Move(float x = 0, float y = 0, float z = 0);
2022-11-20 13:32:06 +03:00
/// <summary>
/// Called when this <see cref="IMovement"/> is activated.
/// </summary>
protected abstract void OnActivated();
2022-11-20 13:32:06 +03:00
/// <summary>
/// Called when this <see cref="IMovement"/> is deactivated.
/// </summary>
protected abstract void OnDeactivated();
protected virtual void Start()
{
2022-11-14 13:04:09 +03:00
toggleState = GetComponent<IToggleState>();
movementController = GetComponent<IMovementController>();
movementController.OnMovementActivated += OnActivated;
movementController.OnMovementDeactivated += OnDeactivated;
}
2022-11-20 13:32:06 +03:00
/// <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();
}
2022-11-20 13:32:06 +03:00
/// <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();
}
}
}