diff --git a/.idea/.idea.HALLUCINATE/.idea/workspace.xml b/.idea/.idea.HALLUCINATE/.idea/workspace.xml index 61e14203..1fc4bacf 100644 --- a/.idea/.idea.HALLUCINATE/.idea/workspace.xml +++ b/.idea/.idea.HALLUCINATE/.idea/workspace.xml @@ -6,25 +6,22 @@ - - - - - - - - - - - - - - - - + + + + + + + + + + + + + - - + + - @@ -163,6 +160,8 @@ + + diff --git a/Assets/Scenes/Menu.unity b/Assets/Scenes/Menu.unity deleted file mode 100644 index e7b61f48..00000000 --- a/Assets/Scenes/Menu.unity +++ /dev/null @@ -1,316 +0,0 @@ -%YAML 1.1 -%TAG !u! tag:unity3d.com,2011: ---- !u!29 &1 -OcclusionCullingSettings: - m_ObjectHideFlags: 0 - serializedVersion: 2 - m_OcclusionBakeSettings: - smallestOccluder: 5 - smallestHole: 0.25 - backfaceThreshold: 100 - m_SceneGUID: 00000000000000000000000000000000 - m_OcclusionCullingData: {fileID: 0} ---- !u!104 &2 -RenderSettings: - m_ObjectHideFlags: 0 - serializedVersion: 10 - m_Fog: 0 - m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1} - m_FogMode: 3 - m_FogDensity: 0.01 - m_LinearFogStart: 0 - m_LinearFogEnd: 300 - m_AmbientSkyColor: {r: 0.212, g: 0.227, b: 0.259, a: 1} - m_AmbientEquatorColor: {r: 0.114, g: 0.125, b: 0.133, a: 1} - m_AmbientGroundColor: {r: 0.047, g: 0.043, b: 0.035, a: 1} - m_AmbientIntensity: 1 - m_AmbientMode: 0 - m_SubtractiveShadowColor: {r: 0.42, g: 0.478, b: 0.627, a: 1} - m_SkyboxMaterial: {fileID: 10304, guid: 0000000000000000f000000000000000, type: 0} - m_HaloStrength: 0.5 - m_FlareStrength: 1 - m_FlareFadeSpeed: 3 - m_HaloTexture: {fileID: 0} - m_SpotCookie: {fileID: 10001, guid: 0000000000000000e000000000000000, type: 0} - m_DefaultReflectionMode: 0 - m_DefaultReflectionResolution: 128 - m_ReflectionBounces: 1 - m_ReflectionIntensity: 1 - m_CustomReflection: {fileID: 0} - m_Sun: {fileID: 0} - m_UseRadianceAmbientProbe: 0 ---- !u!157 &3 -LightmapSettings: - m_ObjectHideFlags: 0 - serializedVersion: 13 - m_BakeOnSceneLoad: 0 - m_GISettings: - serializedVersion: 2 - m_BounceScale: 1 - m_IndirectOutputScale: 1 - m_AlbedoBoost: 1 - m_EnvironmentLightingMode: 0 - m_EnableBakedLightmaps: 1 - m_EnableRealtimeLightmaps: 0 - m_LightmapEditorSettings: - serializedVersion: 12 - m_Resolution: 2 - m_BakeResolution: 40 - m_AtlasSize: 1024 - m_AO: 0 - m_AOMaxDistance: 1 - m_CompAOExponent: 1 - m_CompAOExponentDirect: 0 - m_ExtractAmbientOcclusion: 0 - m_Padding: 2 - m_LightmapParameters: {fileID: 0} - m_LightmapsBakeMode: 1 - m_TextureCompression: 1 - m_ReflectionCompression: 2 - m_MixedBakeMode: 2 - m_BakeBackend: 2 - m_PVRSampling: 1 - m_PVRDirectSampleCount: 32 - m_PVRSampleCount: 512 - m_PVRBounces: 2 - m_PVREnvironmentSampleCount: 256 - m_PVREnvironmentReferencePointCount: 2048 - m_PVRFilteringMode: 1 - m_PVRDenoiserTypeDirect: 1 - m_PVRDenoiserTypeIndirect: 1 - m_PVRDenoiserTypeAO: 1 - m_PVRFilterTypeDirect: 0 - m_PVRFilterTypeIndirect: 0 - m_PVRFilterTypeAO: 0 - m_PVREnvironmentMIS: 1 - m_PVRCulling: 1 - m_PVRFilteringGaussRadiusDirect: 1 - m_PVRFilteringGaussRadiusIndirect: 1 - m_PVRFilteringGaussRadiusAO: 1 - m_PVRFilteringAtrousPositionSigmaDirect: 0.5 - m_PVRFilteringAtrousPositionSigmaIndirect: 2 - m_PVRFilteringAtrousPositionSigmaAO: 1 - m_ExportTrainingData: 0 - m_TrainingDataDestination: TrainingData - m_LightProbeSampleCountMultiplier: 4 - m_LightingDataAsset: {fileID: 20201, guid: 0000000000000000f000000000000000, type: 0} - m_LightingSettings: {fileID: 0} ---- !u!196 &4 -NavMeshSettings: - serializedVersion: 2 - m_ObjectHideFlags: 0 - m_BuildSettings: - serializedVersion: 3 - agentTypeID: 0 - agentRadius: 0.5 - agentHeight: 2 - agentSlope: 45 - agentClimb: 0.4 - ledgeDropHeight: 0 - maxJumpAcrossDistance: 0 - minRegionArea: 2 - manualCellSize: 0 - cellSize: 0.16666667 - manualTileSize: 0 - tileSize: 256 - buildHeightMesh: 0 - maxJobWorkers: 0 - preserveTilesOutsideBounds: 0 - debug: - m_Flags: 0 - m_NavMeshData: {fileID: 0} ---- !u!1 &813769180 -GameObject: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - serializedVersion: 6 - m_Component: - - component: {fileID: 813769182} - - component: {fileID: 813769181} - m_Layer: 0 - m_Name: Directional Light - m_TagString: Untagged - m_Icon: {fileID: 0} - m_NavMeshLayer: 0 - m_StaticEditorFlags: 0 - m_IsActive: 1 ---- !u!108 &813769181 -Light: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 813769180} - m_Enabled: 1 - serializedVersion: 12 - m_Type: 1 - m_Color: {r: 1, g: 0.95686275, b: 0.8392157, a: 1} - m_Intensity: 1 - m_Range: 10 - m_SpotAngle: 30 - m_InnerSpotAngle: 21.80208 - m_CookieSize2D: {x: 0.5, y: 0.5} - m_Shadows: - m_Type: 2 - m_Resolution: -1 - m_CustomResolution: -1 - m_Strength: 1 - m_Bias: 0.05 - m_NormalBias: 0.4 - m_NearPlane: 0.2 - m_CullingMatrixOverride: - e00: 1 - e01: 0 - e02: 0 - e03: 0 - e10: 0 - e11: 1 - e12: 0 - e13: 0 - e20: 0 - e21: 0 - e22: 1 - e23: 0 - e30: 0 - e31: 0 - e32: 0 - e33: 1 - m_UseCullingMatrixOverride: 0 - m_Cookie: {fileID: 0} - m_DrawHalo: 0 - m_Flare: {fileID: 0} - m_RenderMode: 0 - m_CullingMask: - serializedVersion: 2 - m_Bits: 4294967295 - m_RenderingLayerMask: 1 - m_Lightmapping: 4 - m_LightShadowCasterMode: 0 - m_AreaSize: {x: 1, y: 1} - m_BounceIntensity: 1 - m_ColorTemperature: 6570 - m_UseColorTemperature: 0 - m_BoundingSphereOverride: {x: 0, y: 0, z: 0, w: 0} - m_UseBoundingSphereOverride: 0 - m_UseViewFrustumForShadowCasterCull: 1 - m_ForceVisible: 0 - m_ShadowRadius: 0 - m_ShadowAngle: 0 - m_LightUnit: 1 - m_LuxAtDistance: 1 - m_EnableSpotReflector: 1 ---- !u!4 &813769182 -Transform: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 813769180} - serializedVersion: 2 - m_LocalRotation: {x: 0.40821788, y: -0.23456968, z: 0.10938163, w: 0.8754261} - m_LocalPosition: {x: 0, y: 3, z: 0} - m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 - m_Children: [] - m_Father: {fileID: 0} - m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0} ---- !u!1 &1467441182 -GameObject: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - serializedVersion: 6 - m_Component: - - component: {fileID: 1467441185} - - component: {fileID: 1467441184} - - component: {fileID: 1467441183} - m_Layer: 0 - m_Name: Main Camera - m_TagString: MainCamera - m_Icon: {fileID: 0} - m_NavMeshLayer: 0 - m_StaticEditorFlags: 0 - m_IsActive: 1 ---- !u!81 &1467441183 -AudioListener: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1467441182} - m_Enabled: 1 ---- !u!20 &1467441184 -Camera: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1467441182} - m_Enabled: 1 - serializedVersion: 2 - m_ClearFlags: 1 - m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0} - m_projectionMatrixMode: 1 - m_GateFitMode: 2 - m_FOVAxisMode: 0 - m_Iso: 200 - m_ShutterSpeed: 0.005 - m_Aperture: 16 - m_FocusDistance: 10 - m_FocalLength: 50 - m_BladeCount: 5 - m_Curvature: {x: 2, y: 11} - m_BarrelClipping: 0.25 - m_Anamorphism: 0 - m_SensorSize: {x: 36, y: 24} - m_LensShift: {x: 0, y: 0} - m_NormalizedViewPortRect: - serializedVersion: 2 - x: 0 - y: 0 - width: 1 - height: 1 - near clip plane: 0.3 - far clip plane: 1000 - field of view: 60 - orthographic: 0 - orthographic size: 5 - m_Depth: -1 - m_CullingMask: - serializedVersion: 2 - m_Bits: 4294967295 - m_RenderingPath: -1 - m_TargetTexture: {fileID: 0} - m_TargetDisplay: 0 - m_TargetEye: 3 - m_HDR: 1 - m_AllowMSAA: 1 - m_AllowDynamicResolution: 0 - m_ForceIntoRT: 0 - m_OcclusionCulling: 1 - m_StereoConvergence: 10 - m_StereoSeparation: 0.022 ---- !u!4 &1467441185 -Transform: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1467441182} - serializedVersion: 2 - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 1, z: -10} - m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 - m_Children: [] - m_Father: {fileID: 0} - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!1660057539 &9223372036854775807 -SceneRoots: - m_ObjectHideFlags: 0 - m_Roots: - - {fileID: 1467441185} - - {fileID: 813769182} diff --git a/Assets/Scenes/Menu.unity.meta b/Assets/Scenes/Menu.unity.meta deleted file mode 100644 index f9e47d65..00000000 --- a/Assets/Scenes/Menu.unity.meta +++ /dev/null @@ -1,7 +0,0 @@ -fileFormatVersion: 2 -guid: 8f773407bc476cf41b2775da171f51f4 -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scove/UIScaleTest.unity b/Assets/Scove/UIScaleTest.unity index 7b7ece2a..cbada702 100644 --- a/Assets/Scove/UIScaleTest.unity +++ b/Assets/Scove/UIScaleTest.unity @@ -119,6 +119,52 @@ NavMeshSettings: debug: m_Flags: 0 m_NavMeshData: {fileID: 0} +--- !u!1 &417583765 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 417583767} + - component: {fileID: 417583766} + m_Layer: 0 + m_Name: Spawner + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &417583766 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 417583765} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: ca752d01bdc2c5e42938776307031da3, type: 3} + m_Name: + m_EditorClassIdentifier: Assembly-CSharp::Hallucinate.UI.BasicSpawner + _playerPrefab: + RawGuidValue: 761bdf2e5c0cff4488527355acb975e5 +--- !u!4 &417583767 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 417583765} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 1, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &458228298 GameObject: m_ObjectHideFlags: 0 @@ -497,6 +543,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 5962d8f2c8e40e240a4a4907c7b539fa, type: 3} m_Name: m_EditorClassIdentifier: Assembly-CSharp::OnlyScove.Scripts.InputReader + inputActions: {fileID: 0} --- !u!4 &2128442885 Transform: m_ObjectHideFlags: 0 @@ -774,3 +821,4 @@ SceneRoots: - {fileID: 626355270} - {fileID: 2128442885} - {fileID: 458228301} + - {fileID: 417583767} diff --git a/Assets/Scripts/Duy/_BasicSpawner.cs b/Assets/Scripts/Network/BasicSpawner.cs similarity index 62% rename from Assets/Scripts/Duy/_BasicSpawner.cs rename to Assets/Scripts/Network/BasicSpawner.cs index 00af20bf..c65d53b6 100644 --- a/Assets/Scripts/Duy/_BasicSpawner.cs +++ b/Assets/Scripts/Network/BasicSpawner.cs @@ -9,9 +9,10 @@ using OnlyScove.Scripts; namespace Hallucinate.UI { - public class _BasicSpawner : MonoBehaviour, INetworkRunnerCallbacks + public class BasicSpawner : MonoBehaviour, INetworkRunnerCallbacks { - private NetworkRunner _runner { get; set; } + public static BasicSpawner Instance { get; private set; } + private NetworkRunner _runner; public event Action> OnSessionListUpdatedEvent; public event Action OnShutdownEvent; @@ -20,29 +21,74 @@ namespace Hallucinate.UI private void Awake() { - if (_runner == null) _runner = gameObject.AddComponent(); + if (Instance != null && Instance != this) + { + Destroy(gameObject); + return; + } + Instance = this; DontDestroyOnLoad(gameObject); } - public _PlayerProfile LocalPlayerProfile { get; private set; } - public void SetLocalPlayerProfile(_PlayerProfile _profile) + public PlayerProfile LocalPlayerProfile { get; private set; } + public void SetLocalPlayerProfile(PlayerProfile _profile) { LocalPlayerProfile = _profile; } - public async Task StartLobby() + private async Task EnsureRunnerExists() { - if (_runner == null) _runner = gameObject.AddComponent(); + if (_runner != null) + { + // Nếu runner cũ vẫn đang chạy hoặc lỗi, dọn dẹp nó + await _runner.Shutdown(); + if (_runner != null && _runner.gameObject != null) + { + Destroy(_runner); + } + _runner = null; + } + + _runner = gameObject.AddComponent(); _runner.ProvideInput = true; _runner.AddCallbacks(this); - var result = await _runner.JoinSessionLobby(SessionLobby.ClientServer); - if (!result.Ok) Debug.LogError($"Failed to join lobby: {result.ShutdownReason}"); } - public async Task StartHost(string sessionName, string password = null) + public async Task StartLobby() + { + await EnsureRunnerExists(); + + var result = await _runner.JoinSessionLobby(SessionLobby.ClientServer); + if (!result.Ok) + { + Debug.LogError($"Failed to join lobby: {result.ShutdownReason}. Check AppID type or Network/Firewall."); + } + } + + public async Task StartHost(string sessionName, string password = null) { OnJoinStartedEvent?.Invoke(); + // 1. Kiểm tra Build Settings + bool sceneExists = false; + for (int i = 0; i < UnityEngine.SceneManagement.SceneManager.sceneCountInBuildSettings; i++) + { + if (UnityEngine.SceneManagement.SceneUtility.GetScenePathByBuildIndex(i).Contains("Main Scene")) + { + sceneExists = true; + break; + } + } + + if (!sceneExists) + { + Debug.LogError("CRITICAL: 'Main Scene' is NOT in Build Settings!"); + return false; + } + + // 2. Khởi tạo Runner mới sạch sẽ + await EnsureRunnerExists(); + var customProps = new Dictionary(); if (!string.IsNullOrEmpty(password)) { @@ -55,34 +101,48 @@ namespace Hallucinate.UI SessionName = sessionName, SessionProperties = customProps, PlayerCount = 2, - SceneManager = gameObject.AddComponent() + // Thêm fixed region để tránh timeout khi tìm server + // SceneManager sẽ tự động add nếu thiếu + SceneManager = gameObject.GetComponent() ?? gameObject.AddComponent() }); - if (!result.Ok) + if (result.Ok) { - Debug.LogError($"Failed to start host: {result.ShutdownReason}"); + return true; + } + else + { + Debug.LogError($"Fusion StartHost Failed: {result.ShutdownReason}."); OnJoinFailedEvent?.Invoke(); + return false; } } - public async Task StartClient(string sessionName, string password = null) + public async Task StartClient(string sessionName, string password = null) { OnJoinStartedEvent?.Invoke(); + await EnsureRunnerExists(); var result = await _runner.StartGame(new StartGameArgs() { GameMode = GameMode.Client, SessionName = sessionName, - SceneManager = gameObject.AddComponent() + SceneManager = gameObject.GetComponent() ?? gameObject.AddComponent() }); - if (!result.Ok) + if (result.Ok) { - Debug.LogError($"Failed to join client: {result.ShutdownReason}"); + return true; + } + else + { + Debug.LogError($"Fusion StartClient Failed: {result.ShutdownReason}"); OnJoinFailedEvent?.Invoke(); + return false; } } + // --- Các phương thức Callbacks --- [SerializeField] private NetworkPrefabRef _playerPrefab; private Dictionary _spawnedCharacters = new Dictionary(); @@ -90,13 +150,21 @@ namespace Hallucinate.UI { if (player == runner.LocalPlayer) { - var pdm = FindFirstObjectByType<_PlayerDataManager>(); + var pdm = FindFirstObjectByType(); if (pdm != null) { + string playerName = "Player"; + _Role playerRole = _Role.Seeker; + + if (LocalPlayerProfile != null) + { + playerName = LocalPlayerProfile.Name; + } + var metaData = new _PlayerMetaData() { - Name = LocalPlayerProfile.Name, - Role = LocalPlayerProfile.Role, + Name = playerName, + Role = playerRole, IsReady = false }; pdm.RPC_UpdatePlayerMetaData(player, metaData); @@ -106,7 +174,7 @@ namespace Hallucinate.UI public void StartGame() { - if (_runner.IsServer) + if (_runner != null && _runner.IsServer) { _runner.LoadScene("Main Scene"); } @@ -135,26 +203,13 @@ namespace Hallucinate.UI public void OnInput(NetworkRunner runner, NetworkInput input) { var data = new PlayerInputData(); - - // Try to get input from the local player's InputReader if (PlayerStateMachine.Local != null && PlayerStateMachine.Local.Input != null) { - var inputReader = PlayerStateMachine.Local.Input; - data.Direction = inputReader.MoveInput; - data.sprint = inputReader.IsSprintHeld; - + data.Direction = PlayerStateMachine.Local.Input.MoveInput; + data.sprint = PlayerStateMachine.Local.Input.IsSprintHeld; if (PlayerStateMachine.Local.Cam != null) - { data.rot = PlayerStateMachine.Local.Cam.PlanarRotation; - } } - else - { - // Fallback to basic input if player not spawned or InputReader missing - data.Direction = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")); - data.sprint = Input.GetKey(KeyCode.LeftShift); - } - input.Set(data); } @@ -174,7 +229,6 @@ namespace Hallucinate.UI public void OnSceneLoadDone(NetworkRunner runner) { string currentSceneName = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name; - if (runner.IsServer && currentSceneName == "Main Scene") { foreach (var player in runner.ActivePlayers) @@ -184,15 +238,10 @@ namespace Hallucinate.UI _spawnedCharacters.Add(player, networkPlayerObject); } } - if (currentSceneName == "Main Scene") - { - if (UIManager.Instance != null) UIManager.Instance.OnGameStarted(); - } + UIManager.Instance?.OnGameStarted(); else if (currentSceneName == "Lobby" || currentSceneName == "Menu") - { - if (UIManager.Instance != null) UIManager.Instance.OnBackToMenu(); - } + UIManager.Instance?.OnBackToMenu(); } public void OnSceneLoadStart(NetworkRunner runner) { } diff --git a/Assets/Scripts/Duy/_BasicSpawner.cs.meta b/Assets/Scripts/Network/BasicSpawner.cs.meta similarity index 100% rename from Assets/Scripts/Duy/_BasicSpawner.cs.meta rename to Assets/Scripts/Network/BasicSpawner.cs.meta diff --git a/Assets/Scripts/Duy/_PlayerData.cs b/Assets/Scripts/Network/PlayerData.cs similarity index 92% rename from Assets/Scripts/Duy/_PlayerData.cs rename to Assets/Scripts/Network/PlayerData.cs index 01dff510..9b2ad151 100644 --- a/Assets/Scripts/Duy/_PlayerData.cs +++ b/Assets/Scripts/Network/PlayerData.cs @@ -1,7 +1,7 @@ using Fusion; using UnityEngine; -public class _PlayerData : NetworkBehaviour +public class PlayerData : NetworkBehaviour { [Networked] public _Role PlayerRole { get; set; } diff --git a/Assets/Scripts/Duy/_PlayerData.cs.meta b/Assets/Scripts/Network/PlayerData.cs.meta similarity index 100% rename from Assets/Scripts/Duy/_PlayerData.cs.meta rename to Assets/Scripts/Network/PlayerData.cs.meta diff --git a/Assets/Scripts/Duy/_PlayerDataManager.cs b/Assets/Scripts/Network/PlayerDataManager.cs similarity index 94% rename from Assets/Scripts/Duy/_PlayerDataManager.cs rename to Assets/Scripts/Network/PlayerDataManager.cs index 10efcd46..eecf67ca 100644 --- a/Assets/Scripts/Duy/_PlayerDataManager.cs +++ b/Assets/Scripts/Network/PlayerDataManager.cs @@ -9,7 +9,7 @@ public struct _PlayerMetaData : INetworkStruct public NetworkBool IsReady; } -public class _PlayerDataManager : NetworkBehaviour +public class PlayerDataManager : NetworkBehaviour { [Networked] public NetworkDictionary Players => default; diff --git a/Assets/Scripts/Duy/_PlayerDataManager.cs.meta b/Assets/Scripts/Network/PlayerDataManager.cs.meta similarity index 100% rename from Assets/Scripts/Duy/_PlayerDataManager.cs.meta rename to Assets/Scripts/Network/PlayerDataManager.cs.meta diff --git a/Assets/Scripts/Duy/_PlayerInfo.cs b/Assets/Scripts/Network/PlayerInfo.cs similarity index 56% rename from Assets/Scripts/Duy/_PlayerInfo.cs rename to Assets/Scripts/Network/PlayerInfo.cs index 57786fb1..44a9a716 100644 --- a/Assets/Scripts/Duy/_PlayerInfo.cs +++ b/Assets/Scripts/Network/PlayerInfo.cs @@ -8,17 +8,18 @@ public enum _Role Trapper } -public struct _PlayerProfile +[System.Serializable] +public class PlayerProfile { - public string Name; - public _Role Role; + public string Name = "Player"; + public _Role Role = _Role.Seeker; } -public class _PlayerInfo : NetworkBehaviour +public class PlayerInfo : NetworkBehaviour { [Networked] public string playerName { get; set; } - public _PlayerDataManager playerDataManager; + public PlayerDataManager playerDataManager; public TextMeshProUGUI nameText; public GameObject[] characterIcons; // mảng chứa icon tương ứng với từng class, có thể gán trong inspector @@ -27,7 +28,7 @@ public class _PlayerInfo : NetworkBehaviour // sẽ gọi phương thức này để khởi tạo thông tin player public override void Spawned() { - playerDataManager = FindFirstObjectByType<_PlayerDataManager>(); // tìm PlayerDataManager trong scene + playerDataManager = FindFirstObjectByType(); // tìm PlayerDataManager trong scene } // phương thức này sẽ được gọi mỗi frame để cập nhật thông tin hiển thị của player @@ -39,12 +40,17 @@ public class _PlayerInfo : NetworkBehaviour var name = metadata.Name; var charClass = metadata.Role; - nameText.text = $"{name} ({charClass})"; + if (nameText != null) + nameText.text = $"{name} ({charClass})"; - for (var i = 0; i < characterIcons.Length; i++) + if (characterIcons != null) { - characterIcons[i].SetActive(i == (int)charClass); // hiển thị icon tương ứng với class của player + for (var i = 0; i < characterIcons.Length; i++) + { + if (characterIcons[i] != null) + characterIcons[i].SetActive(i == (int)charClass); // hiển thị icon tương ứng với class của player + } } } } -} \ No newline at end of file +} diff --git a/Assets/Scripts/Duy/_PlayerInfo.cs.meta b/Assets/Scripts/Network/PlayerInfo.cs.meta similarity index 100% rename from Assets/Scripts/Duy/_PlayerInfo.cs.meta rename to Assets/Scripts/Network/PlayerInfo.cs.meta diff --git a/Assets/Scripts/Duy/PlayerInputData.cs b/Assets/Scripts/Network/PlayerInputData.cs similarity index 100% rename from Assets/Scripts/Duy/PlayerInputData.cs rename to Assets/Scripts/Network/PlayerInputData.cs diff --git a/Assets/Scripts/Duy/PlayerInputData.cs.meta b/Assets/Scripts/Network/PlayerInputData.cs.meta similarity index 100% rename from Assets/Scripts/Duy/PlayerInputData.cs.meta rename to Assets/Scripts/Network/PlayerInputData.cs.meta diff --git a/Assets/Scripts/UI/LobbyController.cs b/Assets/Scripts/UI/LobbyController.cs index b6a033a5..323d75b3 100644 --- a/Assets/Scripts/UI/LobbyController.cs +++ b/Assets/Scripts/UI/LobbyController.cs @@ -10,8 +10,7 @@ namespace Hallucinate.UI public class LobbyController : BaseUIController { private VisualTreeAsset _roomItemTemplate; - private _BasicSpawner _spawner; - private _PlayerDataManager _playerDataManager; + private PlayerDataManager _playerDataManager; // Containers private VisualElement _joinContainer, _createContainer, _loungeContainer, _passOverlay; @@ -34,7 +33,6 @@ namespace Hallucinate.UI public override void Initialize(VisualElement uxmlRoot, UIManager manager) { base.Initialize(uxmlRoot, manager); - _spawner = Object.FindFirstObjectByType<_BasicSpawner>(); // Query Elements _joinContainer = root.Q("JoinContainer"); @@ -91,13 +89,24 @@ namespace Hallucinate.UI }); } - // Đăng ký sự kiện từ Spawner - if (_spawner != null) + // Đăng ký sự kiện từ Spawner (Sử dụng Instance) + if (BasicSpawner.Instance != null) { - _spawner.OnSessionListUpdatedEvent += UpdateRoomList; - _spawner.OnJoinFailedEvent += () => _joinPassError.style.display = DisplayStyle.Flex; - _spawner.OnJoinStartedEvent += () => { /* Show loading if needed */ }; + RegisterSpawnerEvents(); } + else + { + // Nếu chưa có, thử tìm sau một chút + Invoke(nameof(RegisterSpawnerEvents), 0.1f); + } + } + + private void RegisterSpawnerEvents() + { + if (BasicSpawner.Instance == null) return; + BasicSpawner.Instance.OnSessionListUpdatedEvent += UpdateRoomList; + BasicSpawner.Instance.OnJoinFailedEvent += () => { if(_joinPassError != null) _joinPassError.style.display = DisplayStyle.Flex; }; + BasicSpawner.Instance.OnJoinStartedEvent += () => { /* Show loading if needed */ }; } public void SetRoomTemplate(VisualTreeAsset template) => _roomItemTemplate = template; @@ -110,38 +119,60 @@ namespace Hallucinate.UI public void ShowJoin() { - _joinContainer.style.display = DisplayStyle.Flex; - _createContainer.style.display = DisplayStyle.None; - _loungeContainer.style.display = DisplayStyle.None; - _spawner?.StartLobby(); + if (_joinContainer != null) _joinContainer.style.display = DisplayStyle.Flex; + if (_createContainer != null) _createContainer.style.display = DisplayStyle.None; + if (_loungeContainer != null) _loungeContainer.style.display = DisplayStyle.None; + BasicSpawner.Instance?.StartLobby(); } public void ShowCreate() { - _joinContainer.style.display = DisplayStyle.None; - _createContainer.style.display = DisplayStyle.Flex; + if (_joinContainer != null) _joinContainer.style.display = DisplayStyle.None; + if (_createContainer != null) _createContainer.style.display = DisplayStyle.Flex; } private void ShowLounge(string roomName) { - _joinContainer.style.display = DisplayStyle.None; - _createContainer.style.display = DisplayStyle.None; - _loungeContainer.style.display = DisplayStyle.Flex; - _loungeRoomName.text = $"Room: {roomName}"; + if (_joinContainer != null) _joinContainer.style.display = DisplayStyle.None; + if (_createContainer != null) _createContainer.style.display = DisplayStyle.None; + if (_loungeContainer != null) _loungeContainer.style.display = DisplayStyle.Flex; + if (_loungeRoomName != null) _loungeRoomName.text = $"Room: {roomName}"; - _playerDataManager = Object.FindFirstObjectByType<_PlayerDataManager>(); + _playerDataManager = Object.FindFirstObjectByType(); } private async void OnCreateRoomClicked() { - string id = _roomIDInput.value.Trim(); - string name = string.IsNullOrEmpty(_roomNameInput.value) ? id : _roomNameInput.value; - string pass = _passToggle.value ? _roomPassInput.value : null; + var spawner = BasicSpawner.Instance; + if (spawner == null) + { + Debug.LogError("[LobbyController] BasicSpawner.Instance is missing!"); + return; + } - if (string.IsNullOrEmpty(id)) return; + string id = _roomIDInput != null && !string.IsNullOrEmpty(_roomIDInput.value) + ? _roomIDInput.value.Trim() + : Random.Range(1000, 9999).ToString(); - await _spawner.StartHost(id, pass); - ShowLounge(name); + if (_roomIDInput != null) _roomIDInput.value = id; + + string name = _roomNameInput != null && !string.IsNullOrEmpty(_roomNameInput.value) + ? _roomNameInput.value + : $"Room {id}"; + + string pass = (_passToggle != null && _passToggle.value && _roomPassInput != null) + ? _roomPassInput.value + : null; + + bool success = await spawner.StartHost(id, pass); + if (success) + { + ShowLounge(name); + } + else + { + Debug.LogWarning("[LobbyController] Failed to create room. Please check AppID/Region."); + } } private void UpdateRoomList(List sessions) @@ -150,48 +181,53 @@ namespace Hallucinate.UI _roomList.Clear(); foreach (var session in sessions) { + if (_roomItemTemplate == null) continue; var item = _roomItemTemplate.Instantiate(); item.Q