Untitled

mail@pastecode.io avatar
unknown
csharp
a month ago
3.6 kB
3
Indexable
Never
public class PlayerMovement : MonoBehaviour
{

    private static float _runSpeed = 90f;

    private static float _walkSpeed = 70f;

    public float collisionOffset = 1f;

    private static Vector2 _movementInput;

    private static Rigidbody2D _rb;

    private static SpriteRenderer _spriteRenderer;
    private static bool _isRunning;

    public bool isLocked;

    private static Animator _animator;

    private static PlayerControls _playerControls;

    private static DayCycleManager _dayCycleManager;

    private void Start()
    {
        _rb = GetComponent<Rigidbody2D>();
        _animator = GetComponent<Animator>();
        _spriteRenderer = GetComponent<SpriteRenderer>();
        _animator.SetFloat("moveX", -1);
        _animator.SetFloat("moveY", 0); 
        _playerControls = new PlayerControls();
        _playerControls.Enable();
        _dayCycleManager = FindObjectOfType<DayCycleManager>(true);

    }


void FixedUpdate()
{
    HandleInputs();

    _rb.velocity = _movementInput * (_isRunning ? _runSpeed : _walkSpeed);

    HandleRunningAndWalking();

    HandleFacingDirection();
    
}

private void HandleInputs()
{
    if (!isLocked && _movementInput == Vector2.zero)
    {
        if (_playerControls.Player.Sit.ReadValue<float>() > 0.1f)
        {
            _animator.SetTrigger("Sittinganim");
            isLocked = true;
        }
        else if (_playerControls.Player.Sleep.ReadValue<float>() > 0.1f && DayCycleManager.CurrentTime == WorldTime.TALL_MOON)
        {
            _animator.SetTrigger("Sleepinganim");
            isLocked = true;
        }
    }
}

private void HandleRunningAndWalking()
{
    bool isHoldingShift = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
    float velocityMagnitude = _rb.velocity.magnitude;


    if (velocityMagnitude > 0)
    {
        bool movingHorizontally = Mathf.Abs(_rb.velocity.x) > Mathf.Abs(_rb.velocity.y);

        _spriteRenderer.flipX = movingHorizontally && _rb.velocity.x > 0;

        _animator.SetBool("isSitting", false);

        _isRunning = isHoldingShift && velocityMagnitude > 1f;

        _animator.SetBool("isRunning", _isRunning);
        _animator.SetBool("isWalking", !_isRunning);
    }
    else
    {
        _animator.SetBool("isRunning", false);
        _animator.SetBool("isWalking", false);
    }
}

    private void HandleFacingDirection()
    {
        if (_movementInput != Vector2.zero)
        {
            _animator.SetFloat("moveX", _movementInput.x);
            _animator.SetFloat("moveY", _movementInput.y); 
        }
    }

    
    private void OnMove(InputValue movementValue)
    {
        if (!isLocked) 
        {
             _movementInput = movementValue.Get<Vector2>();
        } else if (_animator.GetBool("isSleeping") && !_animator.GetBool("transitioning"))
        {
            _animator.SetBool("transitioning", true);
            _animator.SetTrigger("Wakeupanim");
        }
    }

    public void InitSittingIdle() {

        _animator.SetBool("isSitting", true);
    }

    public void SleepingState(int state) {

        if (state == 0) {_dayCycleManager.StartCycle();}
        _animator.SetBool("isSleeping", (state == 1) ? false : true);
    }

    public void LockState(int state)
    {
        isLocked = (state == 1) ? false : true;    
    }

    public void StopTransition()
    {
        _animator.SetBool("transitioning", false);
    }
}
Leave a Comment