Files
BABA_YAGA/Assets/Scripts/Player Controller/PlayerStateMachine.cs

289 lines
12 KiB
C#
Raw Normal View History

2026-03-26 20:27:19 +07:00
using System.Collections.Generic;
using UnityEngine;
2026-04-03 22:46:17 +07:00
using Fusion;
2026-03-26 20:27:19 +07:00
namespace OnlyScove.Scripts
{
[RequireComponent(typeof(CharacterController), typeof(InputReader), typeof(Animator))]
2026-04-03 22:46:17 +07:00
public class PlayerStateMachine : NetworkBehaviour
2026-03-26 20:27:19 +07:00
{
[field: Header("References")]
[field: SerializeField] public CharacterController Controller { get; private set; }
[field: SerializeField] public virtual InputReader Input { get; private set; }
[field: SerializeField] public Animator Anim { get; private set; }
[field: SerializeField] public EnvironmentScanner Scanner { get; private set; }
public CameraController Cam { get; private set; }
2026-04-05 00:08:43 +07:00
[field: Header("Animator Settings")]
[SerializeField] private string speedParamName = "Speed";
[SerializeField] private string velocityXParamName = "Velocity X";
[SerializeField] private string velocityZParamName = "Velocity Z";
private int speedHash;
private int velocityXHash;
private int velocityZHash;
2026-03-26 20:27:19 +07:00
[field: Header("Movement Settings")]
[field: SerializeField] public float WalkSpeed { get; private set; } = 3f;
[field: SerializeField] public float RunSpeed { get; private set; } = 6f;
2026-04-05 00:08:43 +07:00
[field: SerializeField] public float SprintSpeed { get; private set; } = 9f;
2026-03-26 20:27:19 +07:00
[field: SerializeField] public float SneakSpeed { get; private set; } = 1.5f;
[field: SerializeField] public float DashForce { get; private set; } = 10f;
[field: SerializeField] public float RotationSpeed { get; private set; } = 500f;
[field: SerializeField] public float AnimationDamping { get; private set; } = 0.2f;
[field: Header("Airborne Settings")]
[field: SerializeField] public float JumpHeight { get; private set; } = 2f;
2026-04-05 00:08:43 +07:00
[field: SerializeField] public float Gravity { get; private set; } = -15f;
2026-03-26 20:27:19 +07:00
[field: SerializeField] public float ThrustDownwardForce { get; private set; } = -20f;
[field: Header("Ground Check")]
[field: SerializeField] public float GroundCheckRadius { get; private set; } = 0.2f;
[field: SerializeField] public Vector3 GroundCheckOffset { get; private set; }
[field: SerializeField] public LayerMask GroundMask { get; private set; }
[field: Header("Interaction")]
[field: SerializeField] public float InteractionRange { get; private set; } = 2f;
[field: SerializeField] public LayerMask InteractionMask { get; private set; }
2026-04-03 22:46:17 +07:00
[Networked] public Quaternion NetworkedCameraRotation { get; set; }
2026-04-05 00:08:43 +07:00
[Networked] public Vector2 NetworkedMoveInput { get; set; }
[Networked] public float NetworkedSpeed { get; set; }
2026-04-08 12:38:39 +07:00
[Networked] public Vector3 NetworkedPosition { get; set; }
2026-04-03 22:46:17 +07:00
2026-04-23 23:09:54 +07:00
[Header("Player Stats")]
2026-04-25 18:20:16 +07:00
[Networked, OnChangedRender(nameof(OnHealthChangedRender))] public float Health { get; set; } = 100f;
[Networked, OnChangedRender(nameof(OnStaminaChangedRender))] public float Stamina { get; set; } = 100f;
[Networked, OnChangedRender(nameof(OnNoiseLevelChangedRender))] public float NoiseLevel { get; set; } = 0f;
2026-04-23 23:09:54 +07:00
public event System.Action<float> OnHealthChanged;
public event System.Action<float> OnStaminaChanged;
public event System.Action<float> OnNoiseLevelChanged;
public event System.Action<IInteractable> OnInteractableTargetChanged;
2026-04-05 00:08:43 +07:00
public Vector2 MoveInput { get; private set; }
public bool IsSprintHeld { get; private set; }
2026-03-26 20:27:19 +07:00
public float VelocityY { get; set; }
public bool IsGrounded { get; private set; }
public bool WasGrounded { get; private set; }
public string CurrentStateName => currentState != null ? currentState.GetType().Name : "None";
2026-04-05 00:08:43 +07:00
public static PlayerStateMachine Local { get; private set; }
2026-04-25 18:20:16 +07:00
public Quaternion CameraRotation
{
get
{
if (Runner != null && Runner.IsRunning && Object != null) return NetworkedCameraRotation;
return Cam != null ? Cam.PlanarRotation : transform.rotation;
}
}
2026-04-03 22:46:17 +07:00
2026-03-26 20:27:19 +07:00
private PlayerBaseState currentState;
private bool hasControl = true;
2026-04-08 12:38:39 +07:00
private bool hasSpeedParam;
private bool hasVelocityXParam;
private bool hasVelocityZParam;
2026-04-25 18:20:16 +07:00
private List<IInteractable> interactablesNearby = new List<IInteractable>();
private int currentInteractableIndex = 0;
private float localAnimatorSpeed;
2026-04-08 12:38:39 +07:00
2026-03-26 20:27:19 +07:00
protected virtual void Awake()
{
Controller = GetComponent<CharacterController>();
Input = GetComponent<InputReader>();
Anim = GetComponentInChildren<Animator>();
Scanner = GetComponent<EnvironmentScanner>();
2026-04-05 00:08:43 +07:00
2026-04-08 12:38:39 +07:00
if (Anim != null)
{
foreach (AnimatorControllerParameter param in Anim.parameters)
{
if (param.name == speedParamName) hasSpeedParam = true;
if (param.name == velocityXParamName) hasVelocityXParam = true;
if (param.name == velocityZParamName) hasVelocityZParam = true;
}
}
2026-04-05 00:08:43 +07:00
speedHash = Animator.StringToHash(speedParamName);
velocityXHash = Animator.StringToHash(velocityXParamName);
velocityZHash = Animator.StringToHash(velocityZParamName);
2026-03-26 20:27:19 +07:00
}
2026-04-15 19:53:29 +07:00
private void Start()
{
2026-04-25 18:20:16 +07:00
if (Runner == null || !Runner.IsRunning) InitializePlayer();
2026-04-15 19:53:29 +07:00
}
2026-04-03 22:46:17 +07:00
public override void Spawned()
2026-03-26 20:27:19 +07:00
{
2026-04-15 19:53:29 +07:00
InitializePlayer();
if (Object != null && !Object.HasInputAuthority && Runner.IsClient)
{
if (Controller != null) Controller.enabled = false;
}
}
2026-04-23 23:09:54 +07:00
void OnHealthChangedRender() => OnHealthChanged?.Invoke(Health);
void OnStaminaChangedRender() => OnStaminaChanged?.Invoke(Stamina);
void OnNoiseLevelChangedRender() => OnNoiseLevelChanged?.Invoke(NoiseLevel);
2026-04-15 19:53:29 +07:00
private void InitializePlayer()
{
2026-04-25 18:20:16 +07:00
if (currentState == null) SwitchState(new PlayerIdleState(this));
2026-03-26 20:27:19 +07:00
2026-04-15 19:53:29 +07:00
bool isOffline = Runner == null || !Runner.IsRunning;
2026-04-25 18:20:16 +07:00
if (isOffline || (Object != null && Object.HasInputAuthority))
2026-03-26 20:27:19 +07:00
{
2026-04-03 22:46:17 +07:00
Local = this;
CameraController cameraController = GameObject.FindAnyObjectByType<CameraController>();
if (cameraController != null)
{
Cam = cameraController;
2026-04-05 00:08:43 +07:00
Cam.followTarget = transform;
Cam.inputReader = Input;
2026-04-03 22:46:17 +07:00
}
Input.OnNextInteractEvent += OnNextInteract;
Input.OnPreviousInteractEvent += OnPreviousInteract;
2026-04-15 19:53:29 +07:00
if (Controller != null) Controller.enabled = true;
2026-04-08 12:38:39 +07:00
}
2026-03-26 20:27:19 +07:00
}
2026-04-08 12:38:39 +07:00
public void Rotate(Vector3 moveDirection, float deltaTime)
{
if (moveDirection == Vector3.zero) return;
Quaternion targetRot = Quaternion.LookRotation(moveDirection);
2026-04-25 18:20:16 +07:00
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRot, RotationSpeed * deltaTime);
2026-04-08 12:38:39 +07:00
}
public void Move(Vector3 velocity, float animatorSpeed, float deltaTime)
2026-04-05 00:08:43 +07:00
{
2026-04-15 19:53:29 +07:00
bool canMove = (Runner == null || !Runner.IsRunning) || Object.HasInputAuthority || Runner.IsServer;
if (!canMove) return;
2026-04-08 12:38:39 +07:00
2026-04-05 00:08:43 +07:00
if (Controller != null && Controller.enabled)
{
2026-04-08 12:38:39 +07:00
Controller.Move(velocity * deltaTime);
2026-04-25 18:20:16 +07:00
if (Object != null && Runner != null && Runner.IsRunning) NetworkedPosition = transform.position;
2026-04-05 00:08:43 +07:00
}
2026-04-08 12:38:39 +07:00
localAnimatorSpeed = animatorSpeed;
2026-04-15 19:53:29 +07:00
if (Object != null && Object.HasStateAuthority)
2026-04-05 00:08:43 +07:00
{
2026-04-08 12:38:39 +07:00
NetworkedSpeed = animatorSpeed;
2026-04-05 00:08:43 +07:00
NetworkedMoveInput = MoveInput;
}
UpdateAnimator(deltaTime);
}
private void UpdateAnimator(float deltaTime)
{
if (Anim == null) return;
2026-04-25 18:20:16 +07:00
float speedValue = (Runner == null || !Runner.IsRunning || Object.HasInputAuthority) ? localAnimatorSpeed : NetworkedSpeed;
Vector2 inputVector = (Runner == null || !Runner.IsRunning || Object.HasInputAuthority) ? MoveInput : NetworkedMoveInput;
2026-04-05 00:08:43 +07:00
2026-04-08 12:38:39 +07:00
if (hasSpeedParam) Anim.SetFloat(speedHash, speedValue, AnimationDamping, deltaTime);
if (hasVelocityXParam) Anim.SetFloat(velocityXHash, inputVector.x * speedValue, AnimationDamping, deltaTime);
if (hasVelocityZParam) Anim.SetFloat(velocityZHash, inputVector.y * speedValue, AnimationDamping, deltaTime);
2026-04-05 00:08:43 +07:00
}
2026-04-03 22:46:17 +07:00
public override void FixedUpdateNetwork()
2026-03-26 20:27:19 +07:00
{
2026-04-15 19:53:29 +07:00
bool isRunning = Runner != null && Runner.IsRunning;
if (Object == null && isRunning) return;
2026-04-03 22:46:17 +07:00
2026-04-25 18:20:16 +07:00
if (isRunning && NetworkedPosition != Vector3.zero && !Object.HasInputAuthority)
2026-04-08 12:38:39 +07:00
{
2026-04-25 18:20:16 +07:00
Controller.enabled = false;
transform.position = NetworkedPosition;
Controller.enabled = true;
2026-04-08 12:38:39 +07:00
}
2026-04-05 00:08:43 +07:00
if (GetInput(out PlayerInputData data))
2026-04-03 22:46:17 +07:00
{
2026-04-05 00:08:43 +07:00
MoveInput = data.Direction;
IsSprintHeld = data.sprint;
2026-04-15 19:53:29 +07:00
if (isRunning) NetworkedCameraRotation = data.rot;
}
else if (!isRunning)
{
MoveInput = new Vector2(UnityEngine.Input.GetAxisRaw("Horizontal"), UnityEngine.Input.GetAxisRaw("Vertical"));
IsSprintHeld = UnityEngine.Input.GetKey(KeyCode.LeftShift);
2026-04-03 22:46:17 +07:00
}
2026-04-05 00:08:43 +07:00
else
{
MoveInput = Vector2.zero;
IsSprintHeld = false;
}
2026-04-03 22:46:17 +07:00
2026-04-25 18:20:16 +07:00
if (!isRunning || Object.HasInputAuthority || Runner.IsServer)
{
if (hasControl)
{
WasGrounded = IsGrounded;
CheckGround();
UpdateInteractablesList();
currentState?.Tick(isRunning ? Runner.DeltaTime : Time.fixedDeltaTime);
}
}
else
2026-04-05 00:08:43 +07:00
{
UpdateAnimator(Runner.DeltaTime);
}
2026-04-15 19:53:29 +07:00
}
private void Update()
{
2026-04-25 18:20:16 +07:00
if (Runner == null || !Runner.IsRunning) FixedUpdateNetwork();
2026-03-26 20:27:19 +07:00
}
2026-04-25 18:20:16 +07:00
private void CheckGround() => IsGrounded = Physics.CheckSphere(transform.TransformPoint(GroundCheckOffset), GroundCheckRadius, GroundMask);
2026-03-26 20:27:19 +07:00
private void UpdateInteractablesList()
{
interactablesNearby.Clear();
2026-04-01 02:41:07 +07:00
IInteractable target = Scanner.ScanForInteractable(InteractionRange, InteractionMask);
2026-04-25 18:20:16 +07:00
if (target != null) interactablesNearby.Add(target);
OnInteractableTargetChanged?.Invoke(target);
2026-04-01 02:41:07 +07:00
currentInteractableIndex = 0;
2026-03-26 20:27:19 +07:00
}
private void OnNextInteract()
{
if (interactablesNearby.Count <= 1) return;
currentInteractableIndex = (currentInteractableIndex + 1) % interactablesNearby.Count;
2026-04-23 23:09:54 +07:00
OnInteractableTargetChanged?.Invoke(GetInteractable());
2026-03-26 20:27:19 +07:00
}
private void OnPreviousInteract()
{
if (interactablesNearby.Count <= 1) return;
2026-04-25 18:20:16 +07:00
currentInteractableIndex = (currentInteractableIndex - 1 + interactablesNearby.Count) % interactablesNearby.Count;
2026-04-23 23:09:54 +07:00
OnInteractableTargetChanged?.Invoke(GetInteractable());
2026-03-26 20:27:19 +07:00
}
2026-04-25 18:20:16 +07:00
public IInteractable GetInteractable() => interactablesNearby.Count == 0 ? null : interactablesNearby[currentInteractableIndex];
2026-03-26 20:27:19 +07:00
2026-04-25 18:20:16 +07:00
public void SetGroundCheck(float radius, Vector3 offset) { GroundCheckRadius = radius; GroundCheckOffset = offset; }
2026-03-26 20:27:19 +07:00
public void SwitchState(PlayerBaseState newState)
{
currentState?.Exit();
currentState = newState;
currentState?.Enter();
}
public void SetControl(bool control)
{
hasControl = control;
2026-04-05 00:08:43 +07:00
if (Controller != null) Controller.enabled = control;
if (!control && Anim != null) Anim.SetFloat(speedHash, 0f);
2026-03-26 20:27:19 +07:00
}
private void OnDrawGizmosSelected()
{
Gizmos.color = new Color(0, 1, 0, 0.5f);
Gizmos.DrawSphere(transform.TransformPoint(GroundCheckOffset), GroundCheckRadius);
}
}
2026-04-23 23:09:54 +07:00
}