fix di chuyen update maze ui update map

This commit is contained in:
2026-04-22 13:22:42 +07:00
parent 666b4a5058
commit 0e6e763b64
11 changed files with 985 additions and 449 deletions

View File

@@ -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
@@ -65,7 +64,11 @@ namespace OnlyScove.Scripts
[Networked] public Vector2 NetworkedMoveInput { get; set; }
[Networked] public float NetworkedSpeed { get; set; }
[Networked] public Vector3 NetworkedPosition { get; set; }
[Networked] public int StartTick { get; set; }
public float FinishTime { get; set; }
public bool IsFinished { get; set; }
private float startTimeOffline;
public Vector2 MoveInput { get; private set; }
public bool IsSprintHeld { get; private set; }
@@ -82,6 +85,7 @@ namespace OnlyScove.Scripts
private PlayerBaseState currentState;
private bool hasControl = true;
private float localAnimatorSpeed;
private bool hasSpeedParam;
private bool hasVelocityXParam;
@@ -94,9 +98,10 @@ 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)
{
// Ép tắt Root Motion để tránh lỗi cộng dồn tốc độ
Anim.applyRootMotion = false;
foreach (AnimatorControllerParameter param in Anim.parameters)
{
if (param.name == speedParamName) hasSpeedParam = true;
@@ -112,8 +117,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,11 +125,11 @@ 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)
// QUAN TRỌNG: Vô hiệu hóa Controller của người chơi khác để tránh giật hình.
// NetworkTransform sẽ tự lo việc làm mượt vị trí của họ.
if (Object != null && !Object.HasInputAuthority && !Runner.IsServer)
{
if (Controller != null) Controller.enabled = false;
}
@@ -145,6 +148,7 @@ namespace OnlyScove.Scripts
if (isOffline || hasAuthority)
{
Local = this;
startTimeOffline = Time.time;
CameraController cameraController = GameObject.FindAnyObjectByType<CameraController>();
if (cameraController != null)
@@ -157,12 +161,14 @@ namespace OnlyScove.Scripts
Input.OnNextInteractEvent += OnNextInteract;
Input.OnPreviousInteractEvent += OnPreviousInteract;
// Đảm bảo Controller được bật
if (Controller != null) Controller.enabled = true;
}
}
private float localAnimatorSpeed;
if (!isOffline && Object.HasStateAuthority)
{
StartTick = (int)Runner.Tick;
}
}
public void Rotate(Vector3 moveDirection, float deltaTime)
{
@@ -178,20 +184,16 @@ 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)
// SỬA LỖI CHẠY NHANH: Chỉ thực hiện lệnh Move vật lý trong Forward Tick.
// Điều này ngăn việc cộng dồn vận tốc khi Fusion chạy Re-simulation.
if (Runner == null || !Runner.IsRunning || Runner.IsForward)
{
NetworkedPosition = transform.position;
Controller.Move(velocity * deltaTime);
}
}
@@ -224,7 +226,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);
@@ -232,33 +233,13 @@ namespace OnlyScove.Scripts
public override void FixedUpdateNetwork()
{
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)
{
Controller.enabled = false;
transform.position = NetworkedPosition;
Controller.enabled = true;
}
}
if (Object == null || !Runner.IsRunning) return;
if (GetInput(out PlayerInputData data))
{
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
NetworkedCameraRotation = data.rot;
}
else
{
@@ -266,7 +247,8 @@ namespace OnlyScove.Scripts
IsSprintHeld = false;
}
bool isSimulating = !isRunning || Object.HasInputAuthority || Runner.IsServer;
// Chỉ giả lập cho máy có quyền điều khiển hoặc Server
bool isSimulating = Object.HasInputAuthority || Runner.IsServer;
if (!isSimulating)
{
@@ -280,17 +262,19 @@ namespace OnlyScove.Scripts
CheckGround();
UpdateInteractablesList();
float dt = isRunning ? Runner.DeltaTime : Time.fixedDeltaTime;
currentState?.Tick(dt);
currentState?.Tick(Runner.DeltaTime);
}
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)
bool isOffline = Runner == null || !Runner.IsRunning;
if (isOffline)
{
FixedUpdateNetwork();
if (!IsFinished && UI.MazeUI.Instance != null)
{
UI.MazeUI.Instance.UpdateTimer(Time.time - startTimeOffline);
}
}
}
@@ -351,5 +335,62 @@ namespace OnlyScove.Scripts
Gizmos.color = new Color(0, 1, 0, 0.5f);
Gizmos.DrawSphere(transform.TransformPoint(GroundCheckOffset), GroundCheckRadius);
}
#region Maze Winning Logic
public void CompleteMaze()
{
if (IsFinished) return;
float duration;
if (Runner != null && Runner.IsRunning)
{
duration = ((int)Runner.Tick - StartTick) * Runner.DeltaTime;
Rpc_BroadcastWin(Object.InputAuthority, duration);
}
else
{
duration = Time.time - startTimeOffline;
if (UI.MazeUI.Instance != null)
{
UI.MazeUI.Instance.ShowWinMessage("YOU (Offline)", duration);
}
}
IsFinished = true;
FinishTime = duration;
}
[Rpc(RpcSources.All, RpcTargets.All)]
public void Rpc_BroadcastWin(PlayerRef player, float time)
{
string playerName = $"Player {player.PlayerId}";
if (Runner != null && player == Runner.LocalPlayer) playerName = "YOU";
Debug.Log($"<color=yellow><b>[WINNER]</b></color> {playerName} reached the goal in <b>{time:F2}s</b>!");
if (UI.MazeUI.Instance != null)
{
UI.MazeUI.Instance.ShowWinMessage(playerName, time);
}
}
public override void Render()
{
bool isOffline = Runner == null || !Runner.IsRunning;
bool hasAuthority = Object != null && Object.HasInputAuthority;
if ((isOffline || hasAuthority) && !IsFinished)
{
float currentTimer = isOffline ? (Time.time - startTimeOffline) : (((int)Runner.Tick - StartTick) * Runner.DeltaTime);
if (UI.MazeUI.Instance != null)
{
UI.MazeUI.Instance.UpdateTimer(currentTimer);
}
}
}
#endregion
}
}