2026-03-26 20:27:19 +07:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace OnlyScove.Scripts
|
|
|
|
|
{
|
|
|
|
|
public class PlayerMoveState : PlayerBaseState
|
|
|
|
|
{
|
|
|
|
|
private readonly int speedHash = Animator.StringToHash("Speed");
|
|
|
|
|
|
|
|
|
|
public PlayerMoveState(PlayerStateMachine stateMachine) : base(stateMachine) {}
|
|
|
|
|
|
|
|
|
|
public override void Enter()
|
|
|
|
|
{
|
|
|
|
|
stateMachine.Input.OnJumpEvent += OnJump;
|
|
|
|
|
stateMachine.Input.OnDodgeEvent += OnDodge;
|
|
|
|
|
stateMachine.Input.OnCrouchEvent += OnCrouch;
|
|
|
|
|
stateMachine.Input.OnInteractEvent += OnInteract;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Tick(float deltaTime)
|
|
|
|
|
{
|
|
|
|
|
Vector2 input = stateMachine.Input.MoveInput;
|
|
|
|
|
float moveAmount = Mathf.Clamp01(Mathf.Abs(input.x) + Mathf.Abs(input.y));
|
|
|
|
|
|
|
|
|
|
if (moveAmount <= 0.01f)
|
|
|
|
|
{
|
|
|
|
|
stateMachine.SwitchState(new PlayerIdleState(stateMachine));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stateMachine.Input.IsSprintHeld)
|
|
|
|
|
{
|
|
|
|
|
stateMachine.SwitchState(new PlayerDashState(stateMachine));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector3 inputDir = new Vector3(input.x, 0, input.y).normalized;
|
|
|
|
|
Vector3 moveDirection = stateMachine.Cam != null ? stateMachine.Cam.PlanarRotation * inputDir : inputDir;
|
|
|
|
|
|
2026-03-27 20:20:06 +07:00
|
|
|
if (stateMachine.Cam != null)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log($"[PlayerMoveState] View: {(stateMachine.Cam.PlanarRotation.eulerAngles.y)}, InputDir: {inputDir}, MoveDir: {moveDirection}, PlayerRot: {stateMachine.transform.rotation.eulerAngles.y}");
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 20:27:19 +07:00
|
|
|
Vector3 velocity = moveDirection * stateMachine.RunSpeed;
|
|
|
|
|
|
|
|
|
|
if (stateMachine.IsGrounded && stateMachine.VelocityY < 0)
|
|
|
|
|
{
|
|
|
|
|
stateMachine.VelocityY = -2f;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
stateMachine.VelocityY += Physics.gravity.y * deltaTime;
|
|
|
|
|
}
|
|
|
|
|
velocity.y = stateMachine.VelocityY;
|
|
|
|
|
|
|
|
|
|
stateMachine.Controller.Move(velocity * deltaTime);
|
|
|
|
|
|
|
|
|
|
if (moveDirection != Vector3.zero)
|
|
|
|
|
{
|
|
|
|
|
Quaternion targetRot = Quaternion.LookRotation(moveDirection);
|
|
|
|
|
stateMachine.transform.rotation = Quaternion.RotateTowards(
|
|
|
|
|
stateMachine.transform.rotation,
|
|
|
|
|
targetRot,
|
|
|
|
|
stateMachine.RotationSpeed * deltaTime
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stateMachine.Anim.SetFloat(speedHash, 0.7f, stateMachine.AnimationDamping, deltaTime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void PhysicsTick(float fixedDeltaTime) {}
|
|
|
|
|
|
|
|
|
|
public override void Exit()
|
|
|
|
|
{
|
|
|
|
|
stateMachine.Input.OnJumpEvent -= OnJump;
|
|
|
|
|
stateMachine.Input.OnDodgeEvent -= OnDodge;
|
|
|
|
|
stateMachine.Input.OnCrouchEvent -= OnCrouch;
|
|
|
|
|
stateMachine.Input.OnInteractEvent -= OnInteract;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnJump()
|
|
|
|
|
{
|
|
|
|
|
if (stateMachine.IsGrounded)
|
|
|
|
|
{
|
|
|
|
|
if (stateMachine.Scanner != null)
|
|
|
|
|
{
|
|
|
|
|
var hitData = stateMachine.Scanner.ObstacleCheck();
|
|
|
|
|
if (hitData.forwardHitFound)
|
|
|
|
|
{
|
|
|
|
|
stateMachine.SwitchState(new PlayerParkourState(stateMachine));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
stateMachine.SwitchState(new PlayerJumpState(stateMachine, stateMachine.RunSpeed));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDodge() => stateMachine.SwitchState(new PlayerDodgeState(stateMachine));
|
|
|
|
|
private void OnCrouch() => stateMachine.SwitchState(new PlayerCrouchState(stateMachine));
|
|
|
|
|
private void OnInteract() => stateMachine.SwitchState(new PlayerInteractState(stateMachine));
|
|
|
|
|
}
|
|
|
|
|
}
|