Update
This commit is contained in:
@@ -48,7 +48,6 @@ namespace OnlyScove.Scripts
|
||||
|
||||
[Networked] public Quaternion NetworkedCameraRotation { get; set; }
|
||||
|
||||
// Thuộc tính hỗ trợ lấy rotation của Camera an toàn cho cả Online và Offline
|
||||
public Quaternion CameraRotation
|
||||
{
|
||||
get
|
||||
@@ -67,6 +66,22 @@ namespace OnlyScove.Scripts
|
||||
[Networked] public float NetworkedSpeed { get; set; }
|
||||
[Networked] public Vector3 NetworkedPosition { get; set; }
|
||||
|
||||
[Header("Player Stats")]
|
||||
[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;
|
||||
|
||||
// Sự kiện để UI lắng nghe
|
||||
public event System.Action<float> OnHealthChanged;
|
||||
public event System.Action<float> OnStaminaChanged;
|
||||
public event System.Action<float> OnNoiseLevelChanged;
|
||||
public event System.Action<IInteractable> OnInteractableTargetChanged;
|
||||
|
||||
public Vector2 MoveInput { get; private set; }
|
||||
public bool IsSprintHeld { get; private set; }
|
||||
public float VelocityY { get; set; }
|
||||
@@ -82,7 +97,6 @@ namespace OnlyScove.Scripts
|
||||
|
||||
private PlayerBaseState currentState;
|
||||
private bool hasControl = true;
|
||||
|
||||
private bool hasSpeedParam;
|
||||
private bool hasVelocityXParam;
|
||||
private bool hasVelocityZParam;
|
||||
@@ -94,7 +108,6 @@ namespace OnlyScove.Scripts
|
||||
Anim = GetComponentInChildren<Animator>();
|
||||
Scanner = GetComponent<EnvironmentScanner>();
|
||||
|
||||
// Kiểm tra tham số có tồn tại trong Animator không để tránh lỗi log gây Disconnect
|
||||
if (Anim != null)
|
||||
{
|
||||
foreach (AnimatorControllerParameter param in Anim.parameters)
|
||||
@@ -112,8 +125,6 @@ namespace OnlyScove.Scripts
|
||||
|
||||
private void Start()
|
||||
{
|
||||
// Nếu chạy Offline (kéo prefab vào scene), Spawned() sẽ không được gọi.
|
||||
// Chúng ta khởi tạo tại đây để đảm bảo nhân vật hoạt động.
|
||||
if (Runner == null || !Runner.IsRunning)
|
||||
{
|
||||
InitializePlayer();
|
||||
@@ -122,16 +133,19 @@ namespace OnlyScove.Scripts
|
||||
|
||||
public override void Spawned()
|
||||
{
|
||||
// Fusion gọi Spawned khi object được nạp vào mạng.
|
||||
InitializePlayer();
|
||||
|
||||
// Nếu không có quyền điều khiển và đang ở Client, tắt Controller để tránh xung đột
|
||||
if (Object != null && !Object.HasInputAuthority && Runner.IsClient)
|
||||
{
|
||||
if (Controller != null) Controller.enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Callbacks từ attribute OnChangedRender
|
||||
void OnHealthChangedRender() => OnHealthChanged?.Invoke(Health);
|
||||
void OnStaminaChangedRender() => OnStaminaChanged?.Invoke(Stamina);
|
||||
void OnNoiseLevelChangedRender() => OnNoiseLevelChanged?.Invoke(NoiseLevel);
|
||||
|
||||
private void InitializePlayer()
|
||||
{
|
||||
if (currentState == null)
|
||||
@@ -157,7 +171,6 @@ namespace OnlyScove.Scripts
|
||||
Input.OnNextInteractEvent += OnNextInteract;
|
||||
Input.OnPreviousInteractEvent += OnPreviousInteract;
|
||||
|
||||
// Đảm bảo Controller được bật
|
||||
if (Controller != null) Controller.enabled = true;
|
||||
}
|
||||
}
|
||||
@@ -178,17 +191,12 @@ namespace OnlyScove.Scripts
|
||||
|
||||
public void Move(Vector3 velocity, float animatorSpeed, float deltaTime)
|
||||
{
|
||||
// Cho phép di chuyển nếu:
|
||||
// 1. Không có mạng (Offline test)
|
||||
// 2. Có quyền điều khiển (Input Authority)
|
||||
// 3. Là Server (State Authority)
|
||||
bool canMove = (Runner == null || !Runner.IsRunning) || Object.HasInputAuthority || Runner.IsServer;
|
||||
if (!canMove) return;
|
||||
|
||||
if (Controller != null && Controller.enabled)
|
||||
{
|
||||
Controller.Move(velocity * deltaTime);
|
||||
// Cập nhật vị trí mạng ngay sau khi di chuyển
|
||||
if (Object != null && Runner != null && Runner.IsRunning)
|
||||
{
|
||||
NetworkedPosition = transform.position;
|
||||
@@ -224,7 +232,6 @@ namespace OnlyScove.Scripts
|
||||
inputVector = NetworkedMoveInput;
|
||||
}
|
||||
|
||||
// Chỉ Set nếu tham số thực sự tồn tại (Tránh lỗi Hash does not exist)
|
||||
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);
|
||||
@@ -235,7 +242,6 @@ namespace OnlyScove.Scripts
|
||||
bool isRunning = Runner != null && Runner.IsRunning;
|
||||
if (Object == null && isRunning) return;
|
||||
|
||||
// ĐỒNG BỘ VỊ TRÍ: Ép nhân vật về vị trí mạng trước khi tính toán tick mới
|
||||
if (isRunning && NetworkedPosition != Vector3.zero)
|
||||
{
|
||||
if (Controller != null && !Object.HasInputAuthority)
|
||||
@@ -250,15 +256,12 @@ namespace OnlyScove.Scripts
|
||||
{
|
||||
MoveInput = data.Direction;
|
||||
IsSprintHeld = data.sprint;
|
||||
// Chỉ gán biến Networked nếu đang chạy mạng
|
||||
if (isRunning) NetworkedCameraRotation = data.rot;
|
||||
}
|
||||
else if (!isRunning)
|
||||
{
|
||||
// FALLBACK INPUT: Nếu không có Fusion, lấy input trực tiếp từ Unity để Test
|
||||
MoveInput = new Vector2(UnityEngine.Input.GetAxisRaw("Horizontal"), UnityEngine.Input.GetAxisRaw("Vertical"));
|
||||
IsSprintHeld = UnityEngine.Input.GetKey(KeyCode.LeftShift);
|
||||
// Ở chế độ offline, chúng ta không gán vào NetworkedCameraRotation nữa
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -286,8 +289,6 @@ namespace OnlyScove.Scripts
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// Nếu không có NetworkRunner, Fusion sẽ không gọi FixedUpdateNetwork.
|
||||
// Chúng ta gọi thủ công để logic StateMachine vẫn chạy được khi Test Offline.
|
||||
if (Runner == null || !Runner.IsRunning)
|
||||
{
|
||||
FixedUpdateNetwork();
|
||||
@@ -303,7 +304,17 @@ namespace OnlyScove.Scripts
|
||||
{
|
||||
interactablesNearby.Clear();
|
||||
IInteractable target = Scanner.ScanForInteractable(InteractionRange, InteractionMask);
|
||||
if (target != null) interactablesNearby.Add(target);
|
||||
|
||||
if (target != null)
|
||||
{
|
||||
interactablesNearby.Add(target);
|
||||
OnInteractableTargetChanged?.Invoke(target);
|
||||
}
|
||||
else
|
||||
{
|
||||
OnInteractableTargetChanged?.Invoke(null);
|
||||
}
|
||||
|
||||
currentInteractableIndex = 0;
|
||||
}
|
||||
|
||||
@@ -311,6 +322,7 @@ namespace OnlyScove.Scripts
|
||||
{
|
||||
if (interactablesNearby.Count <= 1) return;
|
||||
currentInteractableIndex = (currentInteractableIndex + 1) % interactablesNearby.Count;
|
||||
OnInteractableTargetChanged?.Invoke(GetInteractable());
|
||||
}
|
||||
|
||||
private void OnPreviousInteract()
|
||||
@@ -318,6 +330,7 @@ namespace OnlyScove.Scripts
|
||||
if (interactablesNearby.Count <= 1) return;
|
||||
currentInteractableIndex--;
|
||||
if (currentInteractableIndex < 0) currentInteractableIndex = interactablesNearby.Count - 1;
|
||||
OnInteractableTargetChanged?.Invoke(GetInteractable());
|
||||
}
|
||||
|
||||
public IInteractable GetInteractable()
|
||||
@@ -352,4 +365,4 @@ namespace OnlyScove.Scripts
|
||||
Gizmos.DrawSphere(transform.TransformPoint(GroundCheckOffset), GroundCheckRadius);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user