using System; using System.Threading; using Alchemy.Inspector; using Code.Audio; using LitMotion; using LitMotion.Extensions; using UnityEngine; using UnityEngine.InputSystem; namespace Code.Player { public class Player : MonoBehaviour { private static readonly int OnGround = Animator.StringToHash("OnGround"); [TabGroup("Movement")] [SerializeField] private Transform[] runPositions = new Transform[3]; [TabGroup("Movement")] [SerializeField] private float jumpStrength; [TabGroup("Movement")] [SerializeField] private float sideSpeed; [TabGroup("Movement")] [SerializeField] private LayerMask groundMask; private Animator _animator; private Rigidbody _rigidbody; private void Awake() { _rigidbody = GetComponent(); _animator = GetComponentInChildren(); InputSystem.actions.actionMaps[0].actions[0].performed += Jump; InputSystem.actions.actionMaps[0].actions[1].performed += MoveRight; InputSystem.actions.actionMaps[0].actions[2].performed += MoveLeft; } private void OnDestroy() { InputSystem.actions.actionMaps[0].actions[0].performed -= Jump; InputSystem.actions.actionMaps[0].actions[1].performed -= MoveRight; InputSystem.actions.actionMaps[0].actions[2].performed -= MoveLeft; } private void Jump(InputAction.CallbackContext context) { if(!_onGround) return; _rigidbody.AddForce(new(0,jumpStrength,0), ForceMode.Impulse); AudioManager.Instance.PlayJump(); } private int _currentTarget = 1; private void MoveRight(InputAction.CallbackContext context) { if(_currentTarget != 2) _currentTarget++; ChangePosition(); } private void MoveLeft(InputAction.CallbackContext context) { if(_currentTarget != 0) _currentTarget--; ChangePosition(); } private MotionHandle _sideMotion; private bool _onGround; private Collider[] _overlapResults = new Collider[1]; private void ChangePosition() { if(_sideMotion.IsActive()) _sideMotion.Cancel(); _sideMotion = LMotion .Create(transform.position.x, runPositions[_currentTarget].position.x, sideSpeed) .WithEase(Ease.Linear) .BindToPositionX(transform); // transform.position = runPositions[_currentTarget].position; } private void FixedUpdate() { var groundedCheck = Physics.OverlapSphereNonAlloc(transform.position, 1f, _overlapResults, groundMask); _onGround = groundedCheck > 0; _animator.SetBool(OnGround, _onGround); } } }