using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AstronautAnimator : MonoBehaviour
{
    private Animator _animator;
    private float _ratio = 1f;
    private float _massFactor = 1f;
    private bool _isJumping;

    void Start()
    {
        _animator = GetComponent<Animator>();
    }

    public void SetRatio(float ratio)
    {
        _ratio = ratio;
    }

    public void SetMass(float mass)
    {
        float clamped = Mathf.Clamp(mass, 30f, 100f);
        float t = Mathf.InverseLerp(30f, 100f, clamped);
        _massFactor = Mathf.Lerp(1.05f, 0.95f, t);
    }

    public void SetWalking(bool isWalking)
    {
        _animator.SetBool("isWalking", isWalking);
    }

    public void Jump()
    {
        _animator.SetTrigger("Jump");
        _isJumping = true;
    }

    void Update()
    {
        bool isGrounded = Physics.CheckSphere(transform.parent.position + Vector3.down * 0.5f, 0.3f);
        float animSpeed = (_ratio <= 0.2f)
            ? 1.5f
            : Mathf.Clamp(1f / (_ratio * 1.5f), 0.15f, 3f);
        if (isGrounded)
        {
            _isJumping = false;
            _animator.speed = animSpeed * _massFactor;
        }
        else if (_isJumping)
        {
            _animator.speed = animSpeed * _massFactor;
        }
    }
}
