From f784234444cf0cc09bfda4c36d1b16b8fad7cb48 Mon Sep 17 00:00:00 2001 From: AyakorK Date: Thu, 17 Oct 2024 19:54:08 +0200 Subject: [PATCH 1/3] feat: Add auto implementation from website of traps --- Assets/Scripts/Trap/TrapManager.cs | 123 ++++++++++++++++++++++++----- Packages/manifest.json | 1 + Packages/packages-lock.json | 2 +- 3 files changed, 106 insertions(+), 20 deletions(-) diff --git a/Assets/Scripts/Trap/TrapManager.cs b/Assets/Scripts/Trap/TrapManager.cs index 27438cdb..c988f1c4 100644 --- a/Assets/Scripts/Trap/TrapManager.cs +++ b/Assets/Scripts/Trap/TrapManager.cs @@ -1,62 +1,147 @@ +using System; using System.Collections; using System.Collections.Generic; -using System.Diagnostics; using UnityEngine; +using SocketIOClient; +using Newtonsoft.Json.Linq; public class TrapManager : MonoBehaviour { - public List trapPrefabs = new List(); private Dictionary trapPrefabDictionary = new Dictionary(); private int gridSizeX = 9; private int gridSizeY = 9; private string[,] grid; + private Queue placementQueue = new Queue(); + private SocketIOUnity clientSocket; // SocketIO client + void Start() { - + // Initialize the grid grid = new string[gridSizeX, gridSizeY]; - + // Map trap names to their prefabs trapPrefabDictionary.Add("crossbow_down_prefab", trapPrefabs[0]); trapPrefabDictionary.Add("crossbow_up_prefab", trapPrefabs[1]); trapPrefabDictionary.Add("crossbow_side_prefab", trapPrefabs[2]); - // Exemple - SpawnTrapAtPosition(3, 5, "crossbow_down_prefab"); + // Set up Socket.IO client + SetupSocket(); + StartCoroutine(ProcessPlacementQueue()); + } + + async void SetupSocket() + { + try + { + var uri = new Uri("https://d4cc-86-245-196-157.ngrok-free.app"); // Replace with your backend URL + clientSocket = new SocketIOUnity(uri); + + // Connect to the server + await clientSocket.ConnectAsync(); + + Debug.Log("Connected to backend and listening for trap placement events."); + } + catch (Exception e) + { + Debug.LogError("Socket connection error: " + e.Message); + } + } + + public void Update() + { + clientSocket.On("traps:place", response => + { + JArray trapDataArray = JArray.Parse(response.ToString()); + + foreach (var trapData in trapDataArray) + { + JObject trap = (JObject)trapData["trap"]; + + int x = trap["x"].Value(); + int y = trap["y"].Value(); + string trapType = trap["trapType"].Value(); + + EnqueuePlacementOrder(x, y, trapType); + } + }); } + private void EnqueuePlacementOrder(int x, int y, string trapType) + { + placementQueue.Enqueue(new TrapPlacement(x, y, trapType)); + } + + private IEnumerator ProcessPlacementQueue() + { + while (true) + { + if (placementQueue.Count > 0) + { + TrapPlacement placement = placementQueue.Dequeue(); + SpawnTrapAtPosition(placement.x, placement.y, placement.trapType); + } + + yield return null; // Wait for the next frame + } + } - public void SpawnTrapAtPosition(int x, int y, string trapType) + private void SpawnTrapAtPosition(int x, int y, string trapType) { + // Check if the coordinates are valid and the spot is not occupied if (x >= 0 && x < gridSizeX && y >= 0 && y < gridSizeY && grid[x, y] == null) { - - if (trapPrefabDictionary.ContainsKey(trapType)) + + // Check if the trap type exists in the dictionary + if (trapPrefabDictionary.TryGetValue(trapType, out GameObject prefabToSpawn)) { - GameObject prefabToSpawn = trapPrefabDictionary[trapType]; + if (prefabToSpawn != null) + { - Vector3 spawnPosition = new Vector3(x, y, 0); - Instantiate(prefabToSpawn, spawnPosition, Quaternion.identity); + // Instantiate the prefab at the calculated position + Vector3 spawnPosition = new Vector3(x, y, 0); + GameObject spawnedTrap = Instantiate(prefabToSpawn, spawnPosition, Quaternion.identity); - - grid[x, y] = trapType; + // Mark the grid spot as occupied + grid[x, y] = trapType; + } + else + { + Debug.LogError($"Prefab for '{trapType}' is null! Check if the prefab is correctly assigned in the Inspector."); + } } else { - UnityEngine.Debug.LogWarning($"Le type de prefab '{trapType}' n'existe pas."); + Debug.LogWarning($"The trap type '{trapType}' does not exist in the dictionary."); } } else { - UnityEngine.Debug.LogWarning($"Position ({x}, {y}) est déjà occupée ou hors de la grille."); + Debug.LogWarning($"Position ({x}, {y}) is either occupied or out of bounds."); + } + } + + void OnApplicationQuit() + { + if (clientSocket != null) + { + clientSocket.Dispose(); // Properly close the connection } } - public void ReceivePlacementOrder(int x, int y, string trapType) + private class TrapPlacement { - // Méthode pour recevoir des commandes WebSocket et placer les pièges - SpawnTrapAtPosition(x, y, trapType); + public int x; + public int y; + public string trapType; + + public TrapPlacement(int x, int y, string trapType) + { + this.x = x; + this.y = y; + this.trapType = trapType; + } } } diff --git a/Packages/manifest.json b/Packages/manifest.json index f35e37ee..62cd7506 100644 --- a/Packages/manifest.json +++ b/Packages/manifest.json @@ -4,6 +4,7 @@ "com.unity.2d.animation": "9.1.2", "com.unity.2d.sprite": "1.0.0", "com.unity.2d.tilemap": "1.0.0", + "com.unity.nuget.newtonsoft-json": "3.2.1", "com.unity.ugui": "1.0.0", "com.unity.modules.ai": "1.0.0", "com.unity.modules.androidjni": "1.0.0", diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index 03c45cd2..b96bc9d6 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -86,7 +86,7 @@ }, "com.unity.nuget.newtonsoft-json": { "version": "3.2.1", - "depth": 1, + "depth": 0, "source": "registry", "dependencies": {}, "url": "https://packages.unity.com" From f2333915695abe3cb9271fcc5e539c8d96a35a08 Mon Sep 17 00:00:00 2001 From: Guillaume MORET <90462045+AyakorK@users.noreply.github.com> Date: Fri, 18 Oct 2024 11:41:10 +0200 Subject: [PATCH 2/3] chore: Add environment variables to UnityProject (#5) --- .gitignore | 4 ++++ Assets/Scripts/PlayerController.cs | 5 ++++- Assets/Scripts/Trap/TrapManager.cs | 6 +++++- Packages/manifest.json | 1 + Packages/packages-lock.json | 7 +++++++ 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 7fbba8f6..33aba243 100644 --- a/.gitignore +++ b/.gitignore @@ -290,4 +290,8 @@ $RECYCLE.BIN/ # Windows shortcuts *.lnk + +# Environment file +.env + # End of https://www.toptal.com/developers/gitignore/api/macos,windows,linux,unity,intellij+all,clion+all \ No newline at end of file diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index a53e9c98..8dfb2f64 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -3,6 +3,7 @@ using System.Text; using UnityEngine; using SocketIOClient; +using CandyCoded.env; public class PlayerMovement : MonoBehaviour { @@ -23,11 +24,13 @@ public class PlayerMovement : MonoBehaviour private Vector2 previousMovement = Vector2.zero; + private string socketUrl; async void Start() { try { - var uri = new Uri("http://localhost:3000"); + env.TryParseEnvironmentVariable("SOCKET_URL", out string socketUrl); + var uri = new Uri(socketUrl); clientSocket = new SocketIOUnity(uri); await clientSocket.ConnectAsync(); diff --git a/Assets/Scripts/Trap/TrapManager.cs b/Assets/Scripts/Trap/TrapManager.cs index c988f1c4..32014a38 100644 --- a/Assets/Scripts/Trap/TrapManager.cs +++ b/Assets/Scripts/Trap/TrapManager.cs @@ -4,6 +4,7 @@ using UnityEngine; using SocketIOClient; using Newtonsoft.Json.Linq; +using CandyCoded.env; public class TrapManager : MonoBehaviour { @@ -16,6 +17,8 @@ public class TrapManager : MonoBehaviour private Queue placementQueue = new Queue(); private SocketIOUnity clientSocket; // SocketIO client + private string socketUrl; + void Start() { // Initialize the grid @@ -35,7 +38,8 @@ async void SetupSocket() { try { - var uri = new Uri("https://d4cc-86-245-196-157.ngrok-free.app"); // Replace with your backend URL + env.TryParseEnvironmentVariable("SOCKET_URL", out string socketUrl); + var uri = new Uri(socketUrl); // Replace with your backend URL clientSocket = new SocketIOUnity(uri); // Connect to the server diff --git a/Packages/manifest.json b/Packages/manifest.json index 62cd7506..faeceae4 100644 --- a/Packages/manifest.json +++ b/Packages/manifest.json @@ -6,6 +6,7 @@ "com.unity.2d.tilemap": "1.0.0", "com.unity.nuget.newtonsoft-json": "3.2.1", "com.unity.ugui": "1.0.0", + "xyz.candycoded.env": "https://github.com/CandyCoded/env.git#v1.1.0", "com.unity.modules.ai": "1.0.0", "com.unity.modules.androidjni": "1.0.0", "com.unity.modules.animation": "1.0.0", diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index b96bc9d6..4e20f47f 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -111,6 +111,13 @@ "com.unity.modules.imgui": "1.0.0" } }, + "xyz.candycoded.env": { + "version": "https://github.com/CandyCoded/env.git#v1.1.0", + "depth": 0, + "source": "git", + "dependencies": {}, + "hash": "c6951bc02488dbe99f48509a9e289ee73c74acda" + }, "com.unity.modules.ai": { "version": "1.0.0", "depth": 0, From 4dcfd1383e9f42520fa49495ed0d56838dffa398 Mon Sep 17 00:00:00 2001 From: AyakorK Date: Fri, 18 Oct 2024 11:49:02 +0200 Subject: [PATCH 3/3] fix: Remove useless Log that was crashed --- Assets/Scripts/Trap/TrapManager.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Assets/Scripts/Trap/TrapManager.cs b/Assets/Scripts/Trap/TrapManager.cs index 3d88cae9..32014a38 100644 --- a/Assets/Scripts/Trap/TrapManager.cs +++ b/Assets/Scripts/Trap/TrapManager.cs @@ -132,7 +132,6 @@ void OnApplicationQuit() if (clientSocket != null) { clientSocket.Dispose(); // Properly close the connection - UnityEngine.Debug.LogWarning($"Position ({x}, {y}) est d�j� occup�e ou hors de la grille."); } }