diff --git a/Assets/Scenes/EmptyRoom_2.unity b/Assets/Scenes/EmptyRoom_2.unity index bbcfeded..17e9359b 100644 --- a/Assets/Scenes/EmptyRoom_2.unity +++ b/Assets/Scenes/EmptyRoom_2.unity @@ -690,6 +690,10 @@ PrefabInstance: propertyPath: m_Layer value: 6 objectReference: {fileID: 0} + - target: {fileID: 8096972002182311090, guid: 35bed5d2a54129845bcff6b3abaa746b, type: 3} + propertyPath: trapPrefabs.Array.data[1] + value: + objectReference: {fileID: 411694592779797494, guid: d7c2a5ac1f23afe428f872343ed56d71, type: 3} - target: {fileID: 8327367539400217474, guid: 35bed5d2a54129845bcff6b3abaa746b, type: 3} propertyPath: m_Layer value: 6 @@ -877,6 +881,10 @@ PrefabInstance: propertyPath: m_LocalEulerAnglesHint.z value: 0 objectReference: {fileID: 0} + - target: {fileID: 3966805182529176642, guid: b95a2840877394746b9c857ec618031d, type: 3} + propertyPath: m_IsActive + value: 1 + objectReference: {fileID: 0} - target: {fileID: 4190906850344886629, guid: b95a2840877394746b9c857ec618031d, type: 3} propertyPath: m_Name value: Main Camera diff --git a/Assets/Scripts/Player/PlayerController.cs b/Assets/Scripts/Player/PlayerController.cs index 713f2871..b4c5fa0f 100644 --- a/Assets/Scripts/Player/PlayerController.cs +++ b/Assets/Scripts/Player/PlayerController.cs @@ -2,6 +2,8 @@ using UnityEngine; using SocketIOClient; using CandyCoded.env; +using System.IO; +using System.Collections.Generic; public class PlayerMovement : MonoBehaviour { @@ -28,6 +30,32 @@ public class PlayerMovement : MonoBehaviour public Vector2 maxCameraPosition; private string socketUrl; + private void LoadEnvVariables() + { + string envFilePath = Path.Combine(Application.dataPath, ".env"); + + if (File.Exists(envFilePath)) + { + foreach (var line in File.ReadAllLines(envFilePath)) + { + if (!string.IsNullOrWhiteSpace(line) && !line.StartsWith("#")) + { + var keyValue = line.Split('='); + if (keyValue.Length == 2) + { + var key = keyValue[0].Trim(); + var value = keyValue[1].Trim(); + Environment.SetEnvironmentVariable(key, value); + } + } + } + } + else + { + Debug.LogError(".env file not found at " + envFilePath); + } + } + async void Start() { player = GetComponent(); @@ -53,6 +81,21 @@ async void Start() cameraWidth = cameraHeight * mainCamera.aspect; } + try + { + LoadEnvVariables(); + var socketUrl = Environment.GetEnvironmentVariable("SOCKET_URL"); + var uri = new Uri(socketUrl); + clientSocket = SocketManager.Instance.ClientSocket; + if (clientSocket == null) + { + Debug.LogError("SocketIO client is null! Check SocketManager initialization."); + } + } + catch (Exception e) + { + Debug.LogError("Socket connection error: " + e.Message); + } try { env.TryParseEnvironmentVariable("SOCKET_URL", out string socketUrl); @@ -165,10 +208,11 @@ async void SendData(string channel, string data) { await clientSocket.EmitAsync(channel, data); } + } catch (Exception e) { - Debug.LogError("Socket send error: " + e.Message); + UnityEngine.Debug.LogError("Socket send error: " + e.Message); } } diff --git a/Assets/Scripts/SocketManager.cs b/Assets/Scripts/SocketManager.cs index 14eddfc6..6cdaccf5 100644 --- a/Assets/Scripts/SocketManager.cs +++ b/Assets/Scripts/SocketManager.cs @@ -3,6 +3,8 @@ using UnityEngine; using SocketIOClient; using CandyCoded.env; +using System.IO; +using System.Collections.Generic; public class SocketManager : MonoBehaviour { @@ -25,8 +27,52 @@ private void Awake() } } + private void LoadEnvVariables() + { + string envFilePath = Path.Combine(Application.dataPath, ".env"); + + if (File.Exists(envFilePath)) + { + foreach (var line in File.ReadAllLines(envFilePath)) + { + if (!string.IsNullOrWhiteSpace(line) && !line.StartsWith("#")) + { + var keyValue = line.Split('='); + if (keyValue.Length == 2) + { + var key = keyValue[0].Trim(); + var value = keyValue[1].Trim(); + Environment.SetEnvironmentVariable(key, value); + } + } + } + } + else + { + Debug.LogError(".env file not found at " + envFilePath); + } + } + private async Task SetupSocket() { + + LoadEnvVariables(); + + try + { + var socketUrl = Environment.GetEnvironmentVariable("SOCKET_URL"); + var uri = new Uri(socketUrl); + ClientSocket = new SocketIOUnity(uri); + + // Connect to the server + await ClientSocket.ConnectAsync(); + Debug.Log("Connected to backend via SocketManager."); + } + catch (Exception e) + { + Debug.LogError("Socket connection error: " + e.Message); + } + try { env.TryParseEnvironmentVariable("SOCKET_URL", out string socketUrl); diff --git a/Assets/Scripts/Trap/TrapManager.cs b/Assets/Scripts/Trap/TrapManager.cs index 3c755734..f898d7da 100644 --- a/Assets/Scripts/Trap/TrapManager.cs +++ b/Assets/Scripts/Trap/TrapManager.cs @@ -114,13 +114,6 @@ private void SpawnTrapAtPosition(int x, int y, string trapType) } } - void OnApplicationQuit() - { - if (clientSocket != null) - { - clientSocket.Dispose(); // Properly close the connection - } - } private class TrapPlacement { diff --git a/Assets/Scripts/UI/PlayerHealth.cs b/Assets/Scripts/UI/PlayerHealth.cs index 637b9199..8d5d5057 100644 --- a/Assets/Scripts/UI/PlayerHealth.cs +++ b/Assets/Scripts/UI/PlayerHealth.cs @@ -49,7 +49,6 @@ public void Heal(int amount) if (currentHealth > maxHealth) { currentHealth = maxHealth; - } UpdateHealthBar(); } @@ -60,15 +59,14 @@ private async void SocketEmitter() void Die() { - SocketEmitter(); - if (deadCanvas != null) + + if (deadCanvas != null && !deadCanvas.activeSelf) { + SocketEmitter(); deadCanvas.SetActive(true); } GetComponent().enabled = false; } - - void UpdateHealthBar() { @@ -82,7 +80,7 @@ void UpdateHealthBar() for (int i = 0; i < currentHealth; i++) { GameObject newHeart = Instantiate(heartPrefab, healthBar); - hearts.Add(newHeart); // Ajouter le c�ur � la liste pour le traquer + hearts.Add(newHeart); } }