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

@@ -8,7 +8,6 @@ using UnityEngine;
using UnityEngine.SceneManagement;
using Random = UnityEngine.Random;
// Struct input đồng bộ giữa Spawner, Movement và StateMachine
public struct PlayerInputData : INetworkInput
{
public Vector2 Direction;
@@ -25,7 +24,6 @@ public class BasicSpawner : MonoBehaviour, INetworkRunnerCallbacks
[SerializeField] private NetworkPrefabRef _playerPrefab;
private Dictionary<PlayerRef, NetworkObject> _spawnedCharacters = new Dictionary<PlayerRef, NetworkObject>();
// Thông tin profile local
public PlayerProfile LocalPlayerProfile { get; private set; }
public void SetLocalPlayerProfile(PlayerProfile profile)
{
@@ -38,10 +36,9 @@ public class BasicSpawner : MonoBehaviour, INetworkRunnerCallbacks
DontDestroyOnLoad(gameObject);
}
// Khởi tạo Game (Host/Client)
public async Task StartGame(GameMode mode, string sessionName = "TestRoom")
{
Debug.Log($"<color=yellow>Fusion:</color> Đang khởi tạo kết nối với Mode: {mode} | Phòng: {sessionName}");
Debug.Log($"<color=yellow>Fusion:</color> Starting with Mode: {mode} | Room: {sessionName}");
if (_runner == null) _runner = gameObject.AddComponent<NetworkRunner>();
_runner.ProvideInput = true;
@@ -57,18 +54,10 @@ public class BasicSpawner : MonoBehaviour, INetworkRunnerCallbacks
SceneManager = sceneManager
});
if (result.Ok)
if (!result.Ok)
{
Debug.Log($"<color=green>Fusion thành công:</color> Đã vào phòng {sessionName}");
}
else
{
Debug.LogError($"<color=red>Fusion thất bại:</color> Lý do: {result.ShutdownReason}");
if (_runner != null)
{
Destroy(_runner);
_runner = null;
}
Debug.LogError($"<color=red>Fusion failed:</color> {result.ShutdownReason}");
if (_runner != null) { Destroy(_runner); _runner = null; }
}
}
@@ -76,20 +65,12 @@ public class BasicSpawner : MonoBehaviour, INetworkRunnerCallbacks
{
if (_runner == null || !_runner.IsRunning)
{
float width = 400;
float height = 300;
float x = (Screen.width - width) / 2f;
float y = (Screen.height - height) / 2f;
float width = 400; float height = 300;
float x = (Screen.width - width) / 2f; float y = (Screen.height - height) / 2f;
GUI.Box(new Rect(x, y, width, height), "FUSION MULTIPLAYER");
float innerX = x + 20;
float innerY = y + 40;
float contentWidth = width - 40;
GUI.Label(new Rect(innerX, innerY, 100, 30), "Tên phòng:");
float innerX = x + 20; float innerY = y + 40; float contentWidth = width - 40;
GUI.Label(new Rect(innerX, innerY, 100, 30), "Room Name:");
_roomName = GUI.TextField(new Rect(innerX + 100, innerY, contentWidth - 100, 30), _roomName);
if (GUI.Button(new Rect(innerX, innerY + 50, contentWidth, 60), "VÀO PHÒNG\n(Tự động Host/Client)"))
{
_ = StartGame(GameMode.AutoHostOrClient, _roomName);
@@ -127,8 +108,8 @@ public class BasicSpawner : MonoBehaviour, INetworkRunnerCallbacks
{
if (runner.IsServer)
{
Vector3 spawnPosition = new Vector3(Random.Range(-10f, 10f), 2f, Random.Range(-10f, 10f));
spawnPosition += new Vector3(player.RawEncoded % 3, 0, player.RawEncoded % 3);
Maze maze = GameObject.FindAnyObjectByType<Maze>();
Vector3 spawnPosition = (maze != null) ? maze.GetPlayerSpawnPoint() : new Vector3(0, 2f, 0);
var networkPlayerObject = runner.Spawn(_playerPrefab, spawnPosition, Quaternion.identity, player);
runner.SetPlayerObject(player, networkPlayerObject);
@@ -148,8 +129,6 @@ public class BasicSpawner : MonoBehaviour, INetworkRunnerCallbacks
public void OnInput(NetworkRunner runner, NetworkInput input)
{
var data = new PlayerInputData();
// ĐỌC TRỰC TIẾP: Không dùng Buffer để tránh bị trôi phím
data.Direction = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
data.sprint = Input.GetKey(KeyCode.LeftShift);
@@ -159,7 +138,6 @@ public class BasicSpawner : MonoBehaviour, INetworkRunnerCallbacks
if (sm.Cam != null) data.rot = sm.Cam.PlanarRotation;
else data.rot = sm.NetworkedCameraRotation;
}
input.Set(data);
}