...
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Fusion;
|
||||
|
||||
namespace OnlyScove.Scripts
|
||||
{
|
||||
[RequireComponent(typeof(CharacterController), typeof(InputReader), typeof(Animator))]
|
||||
public class PlayerStateMachine : MonoBehaviour
|
||||
public class PlayerStateMachine : NetworkBehaviour
|
||||
{
|
||||
[field: Header("References")]
|
||||
[field: SerializeField] public CharacterController Controller { get; private set; }
|
||||
@@ -36,16 +37,19 @@ namespace OnlyScove.Scripts
|
||||
[field: SerializeField] public float InteractionRange { get; private set; } = 2f;
|
||||
[field: SerializeField] public LayerMask InteractionMask { get; private set; }
|
||||
|
||||
[Networked] public Quaternion NetworkedCameraRotation { get; set; }
|
||||
|
||||
public float VelocityY { get; set; }
|
||||
public bool IsGrounded { get; private set; }
|
||||
public bool WasGrounded { get; private set; }
|
||||
|
||||
// Interaction system variables
|
||||
private List<IInteractable> interactablesNearby = new List<IInteractable>();
|
||||
private int currentInteractableIndex = 0;
|
||||
|
||||
public string CurrentStateName => currentState != null ? currentState.GetType().Name : "None";
|
||||
|
||||
public static PlayerStateMachine Local { get; private set; } // THÊM DÒNG NÀY
|
||||
|
||||
private PlayerBaseState currentState;
|
||||
private bool hasControl = true;
|
||||
|
||||
@@ -55,43 +59,56 @@ namespace OnlyScove.Scripts
|
||||
Input = GetComponent<InputReader>();
|
||||
Anim = GetComponentInChildren<Animator>();
|
||||
Scanner = GetComponent<EnvironmentScanner>();
|
||||
Cam = Camera.main?.GetComponent<CameraController>();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
public override void Spawned()
|
||||
{
|
||||
// BẮT BUỘC: Mọi máy (Server và Client) đều phải khởi tạo trạng thái ban đầu
|
||||
SwitchState(new PlayerIdleState(this));
|
||||
|
||||
// Subscribe to cycle events
|
||||
Input.OnNextInteractEvent += OnNextInteract;
|
||||
Input.OnPreviousInteractEvent += OnPreviousInteract;
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (Input != null)
|
||||
if (Object.HasInputAuthority)
|
||||
{
|
||||
Input.OnNextInteractEvent -= OnNextInteract;
|
||||
Input.OnPreviousInteractEvent -= OnPreviousInteract;
|
||||
Local = this;
|
||||
|
||||
CameraController cameraController = GameObject.FindAnyObjectByType<CameraController>();
|
||||
if (cameraController != null)
|
||||
{
|
||||
Cam = cameraController;
|
||||
Cam.followTarget = this.transform;
|
||||
Cam.inputReader = this.Input;
|
||||
}
|
||||
|
||||
Input.OnNextInteractEvent += OnNextInteract;
|
||||
Input.OnPreviousInteractEvent += OnPreviousInteract;
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void Update()
|
||||
public override void FixedUpdateNetwork()
|
||||
{
|
||||
if (Object == null) return;
|
||||
|
||||
// 1. NHẬN DỮ LIỆU TỪ MẠNG
|
||||
if (GetInput(out NetworkInputData data))
|
||||
{
|
||||
// Gán phím bấm vào InputReader để các State (Move, Jump...) sử dụng
|
||||
Input.ApplyNetworkInput(data.move, data.sprint);
|
||||
|
||||
// CẬP NHẬT HƯỚNG CAMERA (Cho cả Server và Client)
|
||||
// Đây là mấu chốt để Server tính toán hướng chạy đúng
|
||||
NetworkedCameraRotation = data.rot;
|
||||
}
|
||||
|
||||
// 2. CHẶN MÁY KHÁCH KHÁC, NHƯNG CHO PHÉP SERVER VÀ LOCAL PLAYER CHẠY LOGIC
|
||||
if (!Object.HasInputAuthority && !Runner.IsServer) return;
|
||||
if (!hasControl) return;
|
||||
|
||||
WasGrounded = IsGrounded;
|
||||
CheckGround();
|
||||
UpdateInteractablesList();
|
||||
|
||||
currentState?.Tick(Time.deltaTime);
|
||||
currentState?.Tick(Runner.DeltaTime);
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (!hasControl) return;
|
||||
currentState?.PhysicsTick(Time.fixedDeltaTime);
|
||||
}
|
||||
protected virtual void Update() { }
|
||||
|
||||
private void CheckGround()
|
||||
{
|
||||
@@ -101,16 +118,8 @@ namespace OnlyScove.Scripts
|
||||
private void UpdateInteractablesList()
|
||||
{
|
||||
interactablesNearby.Clear();
|
||||
|
||||
// Sử dụng Scanner để tìm vật thể người chơi đang nhìn vào
|
||||
IInteractable target = Scanner.ScanForInteractable(InteractionRange, InteractionMask);
|
||||
|
||||
if (target != null)
|
||||
{
|
||||
interactablesNearby.Add(target);
|
||||
}
|
||||
|
||||
// Reset index vì hiện tại Scanner trả về 1 kết quả chính xác nhất
|
||||
if (target != null) interactablesNearby.Add(target);
|
||||
currentInteractableIndex = 0;
|
||||
}
|
||||
|
||||
@@ -118,7 +127,6 @@ namespace OnlyScove.Scripts
|
||||
{
|
||||
if (interactablesNearby.Count <= 1) return;
|
||||
currentInteractableIndex = (currentInteractableIndex + 1) % interactablesNearby.Count;
|
||||
Debug.Log($"[Interaction] Switched to: {interactablesNearby[currentInteractableIndex].InteractionPrompt}");
|
||||
}
|
||||
|
||||
private void OnPreviousInteract()
|
||||
@@ -126,7 +134,6 @@ namespace OnlyScove.Scripts
|
||||
if (interactablesNearby.Count <= 1) return;
|
||||
currentInteractableIndex--;
|
||||
if (currentInteractableIndex < 0) currentInteractableIndex = interactablesNearby.Count - 1;
|
||||
Debug.Log($"[Interaction] Switched to: {interactablesNearby[currentInteractableIndex].InteractionPrompt}");
|
||||
}
|
||||
|
||||
public IInteractable GetInteractable()
|
||||
@@ -152,19 +159,13 @@ namespace OnlyScove.Scripts
|
||||
{
|
||||
hasControl = control;
|
||||
Controller.enabled = control;
|
||||
if (!control)
|
||||
{
|
||||
Anim.SetFloat("Speed", 0f);
|
||||
}
|
||||
if (!control) Anim.SetFloat("Speed", 0f);
|
||||
}
|
||||
|
||||
private void OnDrawGizmosSelected()
|
||||
{
|
||||
Gizmos.color = new Color(0, 1, 0, 0.5f);
|
||||
Gizmos.DrawSphere(transform.TransformPoint(GroundCheckOffset), GroundCheckRadius);
|
||||
|
||||
Gizmos.color = Color.blue;
|
||||
Gizmos.DrawWireSphere(transform.position + transform.forward * (InteractionRange / 2), InteractionRange);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user