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/Animations/Idle.anim b/Assets/Animations/Idle.anim index c83538ba..8bbc33fe 100644 --- a/Assets/Animations/Idle.anim +++ b/Assets/Animations/Idle.anim @@ -72,11 +72,4 @@ AnimationClip: m_EulerEditorCurves: [] m_HasGenericRootTransform: 0 m_HasMotionFloatCurves: 0 - m_Events: - - time: 0 - functionName: - data: - objectReferenceParameter: {fileID: 0} - floatParameter: 0 - intParameter: 0 - messageOptions: 0 + m_Events: [] diff --git a/Assets/Animations/Idle.anim.meta b/Assets/Animations/Idle.anim.meta index dd9867ac..0e7f9204 100644 --- a/Assets/Animations/Idle.anim.meta +++ b/Assets/Animations/Idle.anim.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 35d46c531904e4f318de4b74f3406e3c +guid: 12864ee0eccb6402590333ee01067028 NativeFormatImporter: externalObjects: {} mainObjectFileID: 7400000 diff --git a/Assets/PlayerMovementAnimatorController.controller b/Assets/PlayerMovementAnimatorController.controller index 3a5b2554..36654f5c 100644 --- a/Assets/PlayerMovementAnimatorController.controller +++ b/Assets/PlayerMovementAnimatorController.controller @@ -74,7 +74,7 @@ AnimatorState: m_MirrorParameterActive: 0 m_CycleOffsetParameterActive: 0 m_TimeParameterActive: 0 - m_Motion: {fileID: 7400000, guid: 35d46c531904e4f318de4b74f3406e3c, type: 2} + m_Motion: {fileID: 7400000, guid: 12864ee0eccb6402590333ee01067028, type: 2} m_Tag: m_SpeedParameter: m_MirrorParameter: diff --git a/Assets/Prefabs/Player.prefab b/Assets/Prefabs/Player.prefab index 7ead723c..112da2b0 100644 --- a/Assets/Prefabs/Player.prefab +++ b/Assets/Prefabs/Player.prefab @@ -14,6 +14,7 @@ GameObject: - component: {fileID: 1803017460098482366} - component: {fileID: 2136192976700920181} - component: {fileID: 2952415180134692435} + - component: {fileID: 7904266034163262073} m_Layer: 0 m_Name: Player m_TagString: Untagged @@ -212,3 +213,15 @@ PolygonCollider2D: - {x: 0.057479188, y: -0.12709153} - {x: 0.046700563, y: -0.12571843} m_UseDelaunayMesh: 0 +--- !u!114 &7904266034163262073 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1463738777561398421} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 9d1d8695cfa6948278518a6a55e0b730, type: 3} + m_Name: + m_EditorClassIdentifier: diff --git a/Assets/Prefabs/RoomSystem/Corridor/CorridorX7.prefab b/Assets/Prefabs/RoomSystem/Corridor/CorridorX7.prefab index 4404662e..822ef877 100644 --- a/Assets/Prefabs/RoomSystem/Corridor/CorridorX7.prefab +++ b/Assets/Prefabs/RoomSystem/Corridor/CorridorX7.prefab @@ -1328,6 +1328,7 @@ GameObject: serializedVersion: 6 m_Component: - component: {fileID: 3022020511561805294} + - component: {fileID: 1349095506272017240} m_Layer: 0 m_Name: CorridorX7 m_TagString: Map @@ -1352,6 +1353,18 @@ Transform: - {fileID: 1238582843666978988} m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &1349095506272017240 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 6369746937129949933} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 9880ea29492da44c995f334244a5d19e, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1 &7452483412117750499 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Prefabs/RoomSystem/Corridor/Room.prefab b/Assets/Prefabs/RoomSystem/Corridor/Room.prefab index 9852a198..7f06052a 100644 --- a/Assets/Prefabs/RoomSystem/Corridor/Room.prefab +++ b/Assets/Prefabs/RoomSystem/Corridor/Room.prefab @@ -2254,6 +2254,7 @@ GameObject: serializedVersion: 6 m_Component: - component: {fileID: 3176463435939833076} + - component: {fileID: 3498959641617480557} m_Layer: 0 m_Name: Room m_TagString: Map @@ -2278,6 +2279,18 @@ Transform: - {fileID: 8436259702988649751} m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &3498959641617480557 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 6459706332820108789} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 9880ea29492da44c995f334244a5d19e, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1 &8327367539400217474 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scenes/EmptyRoom_2.unity b/Assets/Scenes/EmptyRoom_2.unity index e698f038..35649546 100644 --- a/Assets/Scenes/EmptyRoom_2.unity +++ b/Assets/Scenes/EmptyRoom_2.unity @@ -122,7 +122,7 @@ NavMeshSettings: debug: m_Flags: 0 m_NavMeshData: {fileID: 0} ---- !u!1 &98536830 +--- !u!1 &6611451 GameObject: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} @@ -130,10 +130,11 @@ GameObject: m_PrefabAsset: {fileID: 0} serializedVersion: 6 m_Component: - - component: {fileID: 98536832} - - component: {fileID: 98536831} - m_Layer: 0 - m_Name: TrapManager + - component: {fileID: 6611452} + - component: {fileID: 6611454} + - component: {fileID: 6611453} + m_Layer: 5 + m_Name: Image m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 @@ -145,6 +146,7 @@ MonoBehaviour: m_CorrespondingSourceObject: {fileID: 0} m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} +: {fileID: 1250698880} m_GameObject: {fileID: 98536830} m_Enabled: 1 m_EditorHideFlags: 0 @@ -630,8 +632,11 @@ PrefabInstance: m_RemovedComponents: [] m_RemovedGameObjects: [] m_AddedGameObjects: [] - m_AddedComponents: [] - m_SourcePrefab: {fileID: 100100000, guid: 583d4dca14d33eb4094ca6f875781ab9, type: 3} + m_AddedComponents: + - targetCorrespondingSourceObject: {fileID: 6459706332820108789, guid: 35bed5d2a54129845bcff6b3abaa746b, type: 3} + insertIndex: -1 + addedObject: {fileID: 791950512} + m_SourcePrefab: {fileID: 100100000, guid: 35bed5d2a54129845bcff6b3abaa746b, type: 3} --- !u!1001 &8803801296905376457 PrefabInstance: m_ObjectHideFlags: 0 @@ -760,3 +765,4 @@ SceneRoots: - {fileID: 8615776833325847340} - {fileID: 8803801296905376457} - {fileID: 1166055672053893236} + - {fileID: 1655521569} diff --git a/Assets/Scenes/TestRoom.unity b/Assets/Scenes/TestRoom.unity index cba86288..828c1cb0 100644 --- a/Assets/Scenes/TestRoom.unity +++ b/Assets/Scenes/TestRoom.unity @@ -132,6 +132,7 @@ GameObject: m_Component: - component: {fileID: 166584760} - component: {fileID: 166584759} + - component: {fileID: 166584761} m_Layer: 0 m_Name: Grid m_TagString: Untagged @@ -170,6 +171,18 @@ Transform: - {fileID: 542654705} m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &166584761 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 166584758} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 9880ea29492da44c995f334244a5d19e, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1 &371089590 GameObject: m_ObjectHideFlags: 0 @@ -184,7 +197,7 @@ GameObject: - component: {fileID: 371089592} m_Layer: 0 m_Name: Props - m_TagString: Untagged + m_TagString: Props m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 @@ -280,7 +293,7 @@ TilemapRenderer: m_LightmapParameters: {fileID: 0} m_SortingLayerID: 0 m_SortingLayer: 0 - m_SortingOrder: 2 + m_SortingOrder: 10 m_ChunkSize: {x: 32, y: 32, z: 32} m_ChunkCullingBounds: {x: 0, y: 0, z: 0} m_MaxChunkCount: 16 @@ -297,7 +310,77 @@ Tilemap: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 371089590} m_Enabled: 1 - m_Tiles: {} + m_Tiles: + - first: {x: 5, y: 4, z: 0} + second: + serializedVersion: 2 + m_TileIndex: 16 + m_TileSpriteIndex: 16 + m_TileMatrixIndex: 0 + m_TileColorIndex: 0 + m_TileObjectToInstantiateIndex: 65535 + dummyAlignment: 0 + m_AllTileFlags: 1073741825 + - first: {x: 6, y: 4, z: 0} + second: + serializedVersion: 2 + m_TileIndex: 15 + m_TileSpriteIndex: 15 + m_TileMatrixIndex: 0 + m_TileColorIndex: 0 + m_TileObjectToInstantiateIndex: 65535 + dummyAlignment: 0 + m_AllTileFlags: 1073741825 + - first: {x: 7, y: 4, z: 0} + second: + serializedVersion: 2 + m_TileIndex: 14 + m_TileSpriteIndex: 14 + m_TileMatrixIndex: 0 + m_TileColorIndex: 0 + m_TileObjectToInstantiateIndex: 65535 + dummyAlignment: 0 + m_AllTileFlags: 1073741825 + - first: {x: 5, y: 5, z: 0} + second: + serializedVersion: 2 + m_TileIndex: 13 + m_TileSpriteIndex: 13 + m_TileMatrixIndex: 0 + m_TileColorIndex: 0 + m_TileObjectToInstantiateIndex: 65535 + dummyAlignment: 0 + m_AllTileFlags: 1073741825 + - first: {x: 6, y: 5, z: 0} + second: + serializedVersion: 2 + m_TileIndex: 12 + m_TileSpriteIndex: 12 + m_TileMatrixIndex: 0 + m_TileColorIndex: 0 + m_TileObjectToInstantiateIndex: 65535 + dummyAlignment: 0 + m_AllTileFlags: 1073741825 + - first: {x: 7, y: 5, z: 0} + second: + serializedVersion: 2 + m_TileIndex: 11 + m_TileSpriteIndex: 11 + m_TileMatrixIndex: 0 + m_TileColorIndex: 0 + m_TileObjectToInstantiateIndex: 65535 + dummyAlignment: 0 + m_AllTileFlags: 1073741825 + - first: {x: 7, y: 8, z: 0} + second: + serializedVersion: 2 + m_TileIndex: 17 + m_TileSpriteIndex: 17 + m_TileMatrixIndex: 0 + m_TileColorIndex: 0 + m_TileObjectToInstantiateIndex: 65535 + dummyAlignment: 0 + m_AllTileFlags: 1073741825 m_AnimatedTiles: {} m_TileAssetArray: - m_RefCount: 0 @@ -322,20 +405,20 @@ Tilemap: m_Data: {fileID: 0} - m_RefCount: 0 m_Data: {fileID: 0} - - m_RefCount: 0 - m_Data: {fileID: 0} - - m_RefCount: 0 - m_Data: {fileID: 0} - - m_RefCount: 0 - m_Data: {fileID: 0} - - m_RefCount: 0 - m_Data: {fileID: 0} - - m_RefCount: 0 - m_Data: {fileID: 0} - - m_RefCount: 0 - m_Data: {fileID: 0} - - m_RefCount: 0 - m_Data: {fileID: 0} + - m_RefCount: 1 + m_Data: {fileID: 11400000, guid: 20a107a89fc4a45e9a6943b56316bdb8, type: 2} + - m_RefCount: 1 + m_Data: {fileID: 11400000, guid: 4b68c7dccf4024034a0e5bbf76946e58, type: 2} + - m_RefCount: 1 + m_Data: {fileID: 11400000, guid: 64041abb85b474ca8bc6ac42116f1a85, type: 2} + - m_RefCount: 1 + m_Data: {fileID: 11400000, guid: 6b71a2339c12b42f4b7bd7a4b09b6034, type: 2} + - m_RefCount: 1 + m_Data: {fileID: 11400000, guid: 51ca0b128c1864a00a6ad2934f8dd7dd, type: 2} + - m_RefCount: 1 + m_Data: {fileID: 11400000, guid: fb3afcb689bb1455094d893aa9ab5628, type: 2} + - m_RefCount: 1 + m_Data: {fileID: 11400000, guid: c121db5a25a0e412bb83365aa0c6f778, type: 2} m_TileSpriteArray: - m_RefCount: 0 m_Data: {fileID: 0} @@ -359,22 +442,22 @@ Tilemap: m_Data: {fileID: 0} - m_RefCount: 0 m_Data: {fileID: 0} - - m_RefCount: 0 - m_Data: {fileID: 0} - - m_RefCount: 0 - m_Data: {fileID: 0} - - m_RefCount: 0 - m_Data: {fileID: 0} - - m_RefCount: 0 - m_Data: {fileID: 0} - - m_RefCount: 0 - m_Data: {fileID: 0} - - m_RefCount: 0 - m_Data: {fileID: 0} - - m_RefCount: 0 - m_Data: {fileID: 0} + - m_RefCount: 1 + m_Data: {fileID: -1457016805, guid: ad684922a8aff654ebccc527594e7603, type: 3} + - m_RefCount: 1 + m_Data: {fileID: 1866999083, guid: ad684922a8aff654ebccc527594e7603, type: 3} + - m_RefCount: 1 + m_Data: {fileID: -2127686085, guid: ad684922a8aff654ebccc527594e7603, type: 3} + - m_RefCount: 1 + m_Data: {fileID: 2105632713, guid: ad684922a8aff654ebccc527594e7603, type: 3} + - m_RefCount: 1 + m_Data: {fileID: -1052406375, guid: ad684922a8aff654ebccc527594e7603, type: 3} + - m_RefCount: 1 + m_Data: {fileID: 1681623567, guid: ad684922a8aff654ebccc527594e7603, type: 3} + - m_RefCount: 1 + m_Data: {fileID: 1026215578, guid: ad684922a8aff654ebccc527594e7603, type: 3} m_TileMatrixArray: - - m_RefCount: 0 + - m_RefCount: 7 m_Data: e00: 1 e01: 0 @@ -393,13 +476,13 @@ Tilemap: e32: 0 e33: 1 m_TileColorArray: - - m_RefCount: 0 + - m_RefCount: 7 m_Data: {r: 1, g: 1, b: 1, a: 1} m_TileObjectToInstantiateArray: [] m_AnimationFrameRate: 1 m_Color: {r: 1, g: 1, b: 1, a: 1} m_Origin: {x: 0, y: 0, z: 0} - m_Size: {x: 9, y: 7, z: 1} + m_Size: {x: 9, y: 9, z: 1} m_TileAnchor: {x: 0.5, y: 0.5, z: 0} m_TileOrientation: 0 m_TileOrientationMatrix: @@ -988,8 +1071,8 @@ Tilemap: - first: {x: 1, y: 4, z: 0} second: serializedVersion: 2 - m_TileIndex: 16 - m_TileSpriteIndex: 15 + m_TileIndex: 15 + m_TileSpriteIndex: 16 m_TileMatrixIndex: 0 m_TileColorIndex: 0 m_TileObjectToInstantiateIndex: 65535 @@ -1457,9 +1540,9 @@ Tilemap: m_Data: {fileID: 0} - m_RefCount: 0 m_Data: {fileID: 0} - - m_RefCount: 0 - m_Data: {fileID: 0} - - m_RefCount: 40 + - m_RefCount: 1 + m_Data: {fileID: 11400000, guid: 6d8f4121c2ddc4126ab09a32de00e2a6, type: 2} + - m_RefCount: 39 m_Data: {fileID: 11400000, guid: 8b2babf76b74e4944a6bb0895e3d4647, type: 2} - m_RefCount: 41 m_Data: {fileID: 11400000, guid: dcca059947b4d724f90a78f2915a478b, type: 2} @@ -1494,10 +1577,10 @@ Tilemap: m_Data: {fileID: 0} - m_RefCount: 0 m_Data: {fileID: 0} - - m_RefCount: 40 + - m_RefCount: 39 m_Data: {fileID: 92044262, guid: 3ef9fd33020450a4c845f8e9166d5558, type: 3} - - m_RefCount: 0 - m_Data: {fileID: 0} + - m_RefCount: 1 + m_Data: {fileID: 976511200, guid: ad684922a8aff654ebccc527594e7603, type: 3} - m_RefCount: 41 m_Data: {fileID: -1574383922, guid: 3ef9fd33020450a4c845f8e9166d5558, type: 3} m_TileMatrixArray: @@ -2814,6 +2897,54 @@ Tilemap: e31: 0 e32: 0 e33: 1 +--- !u!1 &1314584499 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1314584501} + - component: {fileID: 1314584500} + m_Layer: 0 + m_Name: TrapManager + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &1314584500 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1314584499} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 3e906695758a3d04197ddceb298f6c86, type: 3} + m_Name: + m_EditorClassIdentifier: + trapPrefabs: + - {fileID: 6097000079642511365, guid: 127c53d14a0062f41a16963a87a85dc5, type: 3} + - {fileID: 7646078152559978388, guid: 40bc1f37707e62f4297f85bf5c7b01b4, type: 3} + - {fileID: 6762149095286452822, guid: d7c2a5ac1f23afe428f872343ed56d71, type: 3} +--- !u!4 &1314584501 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1314584499} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: -0.21531442, y: 0.33767503, z: 0.020564094} + 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 &1621253959 GameObject: m_ObjectHideFlags: 0 @@ -3460,6 +3591,7 @@ SceneRoots: m_ObjectHideFlags: 0 m_Roots: - {fileID: 519420032} + - {fileID: 1314584501} - {fileID: 619394802} - {fileID: 647252271} - {fileID: 166584760} diff --git a/Assets/Scripts/Map Control/DoorSystem.cs b/Assets/Scripts/Map Control/DoorSystem.cs index 3b891b82..5a27bd34 100644 --- a/Assets/Scripts/Map Control/DoorSystem.cs +++ b/Assets/Scripts/Map Control/DoorSystem.cs @@ -6,13 +6,11 @@ public class RoomManager : MonoBehaviour { - - // public DialogueManager dialogueManager; public GameObject newRoomPrefab; // Le prefab de la nouvelle pièce à générer private Transform player; // Le joueur à téléporter private GameObject currentRoom; // La pièce actuelle - private void Start() + private void Start() { // Trouver le joueur au début, en supposant qu'il a le tag "Player" GameObject playerObject = GameObject.FindGameObjectWithTag("Player"); @@ -34,7 +32,8 @@ private void OnTriggerEnter2D(Collider2D other) // dialogueManager.DisplayDialogue(null, "Salut ! Bagarre", "Talios"); currentRoom = GameObject.FindGameObjectWithTag("Map"); DeleteCurrentRoom(); - GameObject newRoom = Instantiate(newRoomPrefab, new Vector3(4.1f,2.8f,0.0f), Quaternion.identity); + Vector3 newRoomPosition = new Vector3(4.1f, 2.8f, 0.0f); + GameObject newRoom = Instantiate(newRoomPrefab, newRoomPosition, Quaternion.identity); PlacePlayerInNewRoom(newRoom); currentRoom = newRoom; } @@ -48,7 +47,7 @@ void PlacePlayerInNewRoom(GameObject newRoom) { Transform spawnPoint = newRoom.transform.Find("SpawnPoint"); - bool test = newRoom.transform.Find("SpawnPoint"); + bool test = newRoom.transform.Find("SpawnPoint"); Vector3 spawnPosition = spawnPoint.position; player.position = spawnPosition; //GameObject spawnPointObject = GameObject.FindWithTag("Respawn"); diff --git a/Assets/Scripts/Map Control/PropsSystem.cs b/Assets/Scripts/Map Control/PropsSystem.cs new file mode 100644 index 00000000..bb7175f8 --- /dev/null +++ b/Assets/Scripts/Map Control/PropsSystem.cs @@ -0,0 +1,120 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using UnityEngine; +using UnityEngine.Tilemaps; +using SocketIOClient; +using CandyCoded.env; + +public class PropsManager : MonoBehaviour +{ + private Tilemap propsTilemap; // Reference to the Tilemap component + private SocketIOUnity clientSocket; + private bool PropsSent = false; + + private void Start() + { + clientSocket = SocketManager.Instance.ClientSocket; + InvokeRepeating(nameof(CheckSocketAndSendProps), 0f, 5f); // Check connection every 5 seconds + } + + // Check if the socket is connected before sending props + private async void CheckSocketAndSendProps() + { + Debug.Log("Checking socket connection..."); + + if (!PropsSent && clientSocket != null && clientSocket.Connected) + { + await FindAndSendProps(); + } + else + { + Debug.LogWarning("Socket not connected, skipping props send."); + } + } + + // Find the grid and the props tilemap, then send props positions to the server + private async Task FindAndSendProps() + { + GameObject grid = GameObject.Find("Grid"); + + if (grid != null) + { + propsTilemap = grid.transform.Find("Props")?.GetComponent(); + + if (propsTilemap != null) + { + List propsPositions = GetPropsPositions(); + + if (propsPositions.Count > 0) + { + List propsPositionsString = ConvertPositionsToString(propsPositions); + await EmitPropsToServer(propsPositionsString); + + CancelInvoke(nameof(CheckSocketAndSendProps)); // Stop repeating checks after sending + } + } + else + { + Debug.LogError("Props Tilemap not found in Grid!"); + } + } + else + { + Debug.LogError("Grid object not found!"); + } + } + + // Get all positions where there are props on the Tilemap + private List GetPropsPositions() + { + BoundsInt bounds = propsTilemap.cellBounds; + List propsPositions = new List(); + + for (int x = bounds.xMin; x < bounds.xMax; x++) + { + for (int y = bounds.yMin; y < bounds.yMax; y++) + { + Vector3Int tilePosition = new Vector3Int(x, y, 0); + + if (propsTilemap.HasTile(tilePosition)) + { + propsPositions.Add(tilePosition); + } + } + } + + Debug.Log($"Total props found: {propsPositions.Count}"); + return propsPositions; + } + + // Convert Vector3 positions to strings + private List ConvertPositionsToString(List propsPositions) + { + List propsPositionsString = new List(); + + foreach (Vector3 position in propsPositions) + { + string positionString = $"{position.x}, {position.y}"; + propsPositionsString.Add(positionString); + } + + return propsPositionsString; + } + + // Emit the props positions to the server + private async Task EmitPropsToServer(List propsPositionsString) + { + try + { + Debug.Log("Emitting props to server..."); + await clientSocket.EmitAsync("rooms:create", propsPositionsString); + PropsSent = true; + Debug.Log("Props successfully sent to server."); + } + catch (Exception e) + { + Debug.LogError("Failed to emit props: " + e.Message); + } + } +} diff --git a/Assets/Scripts/Map Control/PropsSystem.cs.meta b/Assets/Scripts/Map Control/PropsSystem.cs.meta new file mode 100644 index 00000000..f3a0d26f --- /dev/null +++ b/Assets/Scripts/Map Control/PropsSystem.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9880ea29492da44c995f334244a5d19e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index aae600c0..fdd58f08 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,14 +24,14 @@ public class PlayerMovement : MonoBehaviour private Vector2 previousMovement = Vector2.zero; + private string socketUrl; async void Start() { try { - var uri = new Uri("http://localhost:3000"); - clientSocket = new SocketIOUnity(uri); - - await clientSocket.ConnectAsync(); + env.TryParseEnvironmentVariable("SOCKET_URL", out string socketUrl); + var uri = new Uri(socketUrl); + clientSocket = SocketManager.Instance.ClientSocket; animator = GetComponent(); } diff --git a/Assets/Scripts/SocketManager.cs b/Assets/Scripts/SocketManager.cs new file mode 100644 index 00000000..14eddfc6 --- /dev/null +++ b/Assets/Scripts/SocketManager.cs @@ -0,0 +1,53 @@ +using System; +using System.Threading.Tasks; +using UnityEngine; +using SocketIOClient; +using CandyCoded.env; + +public class SocketManager : MonoBehaviour +{ + private static SocketManager _instance; + public static SocketManager Instance => _instance; + + public SocketIOUnity ClientSocket { get; private set; } + + private void Awake() + { + if (_instance != null && _instance != this) + { + Destroy(this.gameObject); + } + else + { + _instance = this; + DontDestroyOnLoad(this.gameObject); // Keep the socket alive across scenes + SetupSocket().ConfigureAwait(false); // Ensure proper async handling + } + } + + private async Task SetupSocket() + { + try + { + env.TryParseEnvironmentVariable("SOCKET_URL", out string socketUrl); + 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); + } + } + + private void OnApplicationQuit() + { + if (ClientSocket != null) + { + ClientSocket.Dispose(); // Properly close the connection + } + } +} diff --git a/Assets/Scripts/SocketManager.cs.meta b/Assets/Scripts/SocketManager.cs.meta new file mode 100644 index 00000000..7f14c6f6 --- /dev/null +++ b/Assets/Scripts/SocketManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9d1d8695cfa6948278518a6a55e0b730 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Trap/TrapManager.cs b/Assets/Scripts/Trap/TrapManager.cs index 4fb5169a..329f7df5 100644 --- a/Assets/Scripts/Trap/TrapManager.cs +++ b/Assets/Scripts/Trap/TrapManager.cs @@ -1,62 +1,133 @@ +using System; using System.Collections; using System.Collections.Generic; -using System.Diagnostics; using UnityEngine; +using SocketIOClient; +using Newtonsoft.Json.Linq; +using CandyCoded.env; 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 + + private string socketUrl; + 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 + StartCoroutine(ProcessPlacementQueue()); } + public void Update() + { + clientSocket = SocketManager.Instance.ClientSocket; + + clientSocket.On("traps:place", response => + { + JArray trapDataArray = JArray.Parse(response.ToString()); + + foreach (var trapData in trapDataArray) + { + JObject trap = (JObject)trapData["trap"]; - public void SpawnTrapAtPosition(int x, int y, string trapType) + 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 + } + } + + 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."); } } - public void ReceivePlacementOrder(int x, int y, string trapType) + void OnApplicationQuit() { - // M�thode pour recevoir des commandes WebSocket et placer les pi�ges - SpawnTrapAtPosition(x, y, trapType); + if (clientSocket != null) + { + clientSocket.Dispose(); // Properly close the connection + } + } + + private class TrapPlacement + { + 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 209847d3..8c67d776 100644 --- a/Packages/manifest.json +++ b/Packages/manifest.json @@ -4,8 +4,10 @@ "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.textmeshpro": "3.0.9", "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 d6e9c572..5b6f7321 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" @@ -120,6 +120,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, diff --git a/ProjectSettings/TagManager.asset b/ProjectSettings/TagManager.asset index efde76ec..eed87517 100644 --- a/ProjectSettings/TagManager.asset +++ b/ProjectSettings/TagManager.asset @@ -8,6 +8,7 @@ TagManager: - Wall - Enemy - Map + - Props - Dialogue layers: - Default