
using UnityEngine;

public class TM_MovementComponent : MonoBehaviour
{
    protected float _moveSpeed;
    protected Vector3 _vDesiredMoveThisFrame;
    protected Vector2 _vGoalPosition;

    protected Vector2 _vImpulse;
    protected float _fImpulseDecay;

    protected bool bWasStuck = false;

    protected bool bWasAskedToStopThisTick = false;

    public virtual void AddImpulse(Vector2 vUnitsPerSecond, float fPercentDecayPerSecond)
    {
        _vImpulse += vUnitsPerSecond;
        _fImpulseDecay = fPercentDecayPerSecond;
    }

    public virtual void StopAllMotion()
    {
        _vImpulse = Vector2.zero;
        _fImpulseDecay = 0f;
        bWasAskedToStopThisTick = true;
    }

    public virtual void CalculateMoveThisFrame()
    {
        if (bWasAskedToStopThisTick)
        {
            bWasAskedToStopThisTick = false;
            _vDesiredMoveThisFrame = Vector3.zero;
        }

        _vDesiredMoveThisFrame = _vImpulse * Time.deltaTime;
        if (_fImpulseDecay > 0f)
        {
            _vImpulse -= (_vImpulse * _fImpulseDecay * Time.deltaTime);
            if (_vImpulse.sqrMagnitude <= 0.05f)
            {
                _vImpulse = Vector2.zero;
                _fImpulseDecay = 0f;
            }
        }
    }

    public bool WasIStuckThisTick()
    {
        return bWasStuck;
    }

    public virtual Vector2 CalculateWorldCollision(Vector2 oldPosition, Vector2 desiredPosition)
    {
        return desiredPosition;
    }
    public virtual Vector3 GetDesiredMove()
    {
        return _vDesiredMoveThisFrame;
    }

    public virtual void SetGoalPosition(Vector2 vGoal)
    {
        _vGoalPosition = vGoal;
    }
}
