From c590bc893ff4c98beb8375725023f36ccea1f096 Mon Sep 17 00:00:00 2001 From: BOTAlex Date: Tue, 20 Feb 2024 03:19:28 +0100 Subject: [PATCH] Multiplayer now joinable!!! NO ERRORS ON JOIN!!! --- Assets/Scenes/GameScene.unity | 4 ++ .../EnemySpawner => Enemy}/EnemyList.cs | 0 .../EnemySpawner => Enemy}/EnemyList.cs.meta | 0 .../EnemySpawner => Enemy}/EnemySpawner.cs | 16 +++++- .../EnemySpawner.cs.meta | 0 .../Enemy/Project.Scritps.Enemy.asmdef | 20 +++++++ .../Enemy/Project.Scritps.Enemy.asmdef.meta | 7 +++ Assets/Scripts/{ => Misc}/CameraShaker.cs | 0 .../Scripts/{ => Misc}/CameraShaker.cs.meta | 0 .../Scripts/Misc/Project.Scripts.Misc.asmdef | 3 +- Assets/Scripts/Multiplayer/LocalManager.cs | 5 +- .../Scripts/Multiplayer/NetworkedGameSetup.cs | 55 ++++++++++++++++--- .../Project.Scripts.Multiplayer.asmdef | 3 +- Assets/Scripts/Multiplayer/SteamManager.cs | 5 +- Assets/Scripts/Multiplayer/ZNetworkData.cs | 10 ++++ .../Scripts/Multiplayer/ZNetworkData.cs.meta | 11 ++++ 16 files changed, 126 insertions(+), 13 deletions(-) rename Assets/Scripts/{Controller/EnemySpawner => Enemy}/EnemyList.cs (100%) rename Assets/Scripts/{Controller/EnemySpawner => Enemy}/EnemyList.cs.meta (100%) rename Assets/Scripts/{Controller/EnemySpawner => Enemy}/EnemySpawner.cs (94%) rename Assets/Scripts/{Controller/EnemySpawner => Enemy}/EnemySpawner.cs.meta (100%) create mode 100644 Assets/Scripts/Enemy/Project.Scritps.Enemy.asmdef create mode 100644 Assets/Scripts/Enemy/Project.Scritps.Enemy.asmdef.meta rename Assets/Scripts/{ => Misc}/CameraShaker.cs (100%) rename Assets/Scripts/{ => Misc}/CameraShaker.cs.meta (100%) create mode 100644 Assets/Scripts/Multiplayer/ZNetworkData.cs create mode 100644 Assets/Scripts/Multiplayer/ZNetworkData.cs.meta diff --git a/Assets/Scenes/GameScene.unity b/Assets/Scenes/GameScene.unity index 3a0cc5a..62f0f33 100644 --- a/Assets/Scenes/GameScene.unity +++ b/Assets/Scenes/GameScene.unity @@ -3958,6 +3958,10 @@ PrefabInstance: propertyPath: m_Targets.Array.data[1].target value: objectReference: {fileID: 0} + - target: {fileID: 3603265075407754381, guid: c53e6971c95afb1429cd82616a7b6737, type: 3} + propertyPath: players.Array.size + value: 0 + objectReference: {fileID: 0} - target: {fileID: 3603265075407754381, guid: c53e6971c95afb1429cd82616a7b6737, type: 3} propertyPath: players.Array.data[0] value: diff --git a/Assets/Scripts/Controller/EnemySpawner/EnemyList.cs b/Assets/Scripts/Enemy/EnemyList.cs similarity index 100% rename from Assets/Scripts/Controller/EnemySpawner/EnemyList.cs rename to Assets/Scripts/Enemy/EnemyList.cs diff --git a/Assets/Scripts/Controller/EnemySpawner/EnemyList.cs.meta b/Assets/Scripts/Enemy/EnemyList.cs.meta similarity index 100% rename from Assets/Scripts/Controller/EnemySpawner/EnemyList.cs.meta rename to Assets/Scripts/Enemy/EnemyList.cs.meta diff --git a/Assets/Scripts/Controller/EnemySpawner/EnemySpawner.cs b/Assets/Scripts/Enemy/EnemySpawner.cs similarity index 94% rename from Assets/Scripts/Controller/EnemySpawner/EnemySpawner.cs rename to Assets/Scripts/Enemy/EnemySpawner.cs index 7698809..b231f26 100644 --- a/Assets/Scripts/Controller/EnemySpawner/EnemySpawner.cs +++ b/Assets/Scripts/Enemy/EnemySpawner.cs @@ -22,7 +22,7 @@ public class EnemySpawner : MonoBehaviour [SerializeField] private List enemyDifficulties; [SerializeField] private float SpawnRadius = 10; [SerializeField] private int NumEnemies = 6; - [SerializeField] private GameObject[] players; + [SerializeField] public GameObject[] players; [SerializeField] private float initialSpawnDelay = 5; // local @@ -34,6 +34,20 @@ public class EnemySpawner : MonoBehaviour private bool SpawnerStarted = false; + public static EnemySpawner instance; + + private void Awake() + { + if (instance == null) + { + instance = this; + } + else + { + Destroy(instance); + } + } + private void Start() { mainCam = Camera.main; diff --git a/Assets/Scripts/Controller/EnemySpawner/EnemySpawner.cs.meta b/Assets/Scripts/Enemy/EnemySpawner.cs.meta similarity index 100% rename from Assets/Scripts/Controller/EnemySpawner/EnemySpawner.cs.meta rename to Assets/Scripts/Enemy/EnemySpawner.cs.meta diff --git a/Assets/Scripts/Enemy/Project.Scritps.Enemy.asmdef b/Assets/Scripts/Enemy/Project.Scritps.Enemy.asmdef new file mode 100644 index 0000000..41e0ec7 --- /dev/null +++ b/Assets/Scripts/Enemy/Project.Scritps.Enemy.asmdef @@ -0,0 +1,20 @@ +{ + "name": "Project.Scritps.Enemy", + "rootNamespace": "", + "references": [ + "GUID:d8b63aba1907145bea998dd612889d6b", + "GUID:2ea4a18a75f268848b43865100892489", + "GUID:055e16077a1232f4780a04598b3bfe00", + "GUID:1031dfc67c8f1a645b71d679ac3bf7db", + "GUID:0ba5c175a7b2c8345a4e996560a9d0ab" + ], + "includePlatforms": [], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/Assets/Scripts/Enemy/Project.Scritps.Enemy.asmdef.meta b/Assets/Scripts/Enemy/Project.Scritps.Enemy.asmdef.meta new file mode 100644 index 0000000..1ff77ed --- /dev/null +++ b/Assets/Scripts/Enemy/Project.Scritps.Enemy.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 62165c64e8180c441b8cfaa6b82208af +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/CameraShaker.cs b/Assets/Scripts/Misc/CameraShaker.cs similarity index 100% rename from Assets/Scripts/CameraShaker.cs rename to Assets/Scripts/Misc/CameraShaker.cs diff --git a/Assets/Scripts/CameraShaker.cs.meta b/Assets/Scripts/Misc/CameraShaker.cs.meta similarity index 100% rename from Assets/Scripts/CameraShaker.cs.meta rename to Assets/Scripts/Misc/CameraShaker.cs.meta diff --git a/Assets/Scripts/Misc/Project.Scripts.Misc.asmdef b/Assets/Scripts/Misc/Project.Scripts.Misc.asmdef index 21de8ec..97170a4 100644 --- a/Assets/Scripts/Misc/Project.Scripts.Misc.asmdef +++ b/Assets/Scripts/Misc/Project.Scripts.Misc.asmdef @@ -6,7 +6,8 @@ "GUID:2ea4a18a75f268848b43865100892489", "GUID:f4c364e1215051e4dbc6c0bc8fb49793", "GUID:42d1898a72cfe6848ae89835fb20acd2", - "GUID:0ba5c175a7b2c8345a4e996560a9d0ab" + "GUID:0ba5c175a7b2c8345a4e996560a9d0ab", + "GUID:4307f53044263cf4b835bd812fc161a4" ], "includePlatforms": [], "excludePlatforms": [], diff --git a/Assets/Scripts/Multiplayer/LocalManager.cs b/Assets/Scripts/Multiplayer/LocalManager.cs index c29b2cc..6d9efaa 100644 --- a/Assets/Scripts/Multiplayer/LocalManager.cs +++ b/Assets/Scripts/Multiplayer/LocalManager.cs @@ -1,3 +1,4 @@ +using Assets.Scripts.Multiplayer; using System; using System.Collections; using System.Collections.Generic; @@ -7,7 +8,7 @@ using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; -public class LocalManager : MonoBehaviour +public class LocalManager : ZNetworkData { [SerializeField] private Button HostBtn; [SerializeField] private Button JoinBtn; @@ -30,6 +31,8 @@ public class LocalManager : MonoBehaviour private void HostLobby() { + IsServer = true; + NetworkManager.Singleton.StartHost(); UpdateUI(); diff --git a/Assets/Scripts/Multiplayer/NetworkedGameSetup.cs b/Assets/Scripts/Multiplayer/NetworkedGameSetup.cs index 10c3827..2fb0a2b 100644 --- a/Assets/Scripts/Multiplayer/NetworkedGameSetup.cs +++ b/Assets/Scripts/Multiplayer/NetworkedGameSetup.cs @@ -1,8 +1,11 @@ +using Assets.Scripts.Multiplayer; +using Steamworks.ServerList; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using Unity.Netcode; +using Unity.Netcode.Transports.UTP; using UnityEngine; using UnityEngine.SceneManagement; using static UnityEditor.Experimental.GraphView.GraphView; @@ -29,8 +32,10 @@ public class NetworkedGameSetup : NetworkBehaviour private void StartSetupProcedure(List playerIds) { + bool isServer = SteamManager.IsServer || LocalManager.IsServer; + GameObject[] players; - if (IsHost || IsServer) + if (isServer) players = SpawnPlayers(playerIds); else players = GetPlayers(playerIds); @@ -47,11 +52,13 @@ public class NetworkedGameSetup : NetworkBehaviour InitUpgrader(players); InitBlood(players); + + InitSpawner(players); } private GameObject[] GetPlayers(List playerIds) { - GameObject[] players = NetworkManager.SpawnManager.SpawnedObjects.Select(x=>x.Value.gameObject).ToArray(); + GameObject[] players = NetworkManager.SpawnManager.SpawnedObjects.Where(x => playerIds.Contains(x.Value.NetworkObjectId)).Select(x => x.Value.gameObject).ToArray(); return players; } @@ -72,23 +79,55 @@ public class NetworkedGameSetup : NetworkBehaviour private void InitRope(GameObject[] players) { - // Assuming 2 players - RopeSimulator.instance.BuildRope(players[0].GetComponent(), players[1].GetComponent()); + if (players.Length == 1) + { + RopeSimulator.instance.BuildRope(players[0].GetComponent(), players[0].GetComponent()); + } + else // 2 players + { + RopeSimulator.instance.BuildRope(players[0].GetComponent(), players[1].GetComponent()); + } } private void InitUpgrader(GameObject[] players) { + return; // fuck this upgrade system + Upgrader upgradeSystem = Upgrader.instance; - upgradeSystem.player1 = players[0]; - upgradeSystem.player2 = players[1]; + if (players.Length == 1) + { + upgradeSystem.player1 = players[0]; + upgradeSystem.player2 = players[0]; + } + else // 2 players + { + upgradeSystem.player1 = players[0]; + upgradeSystem.player2 = players[1]; + } } private void InitBlood(GameObject[] players) { BloodComputeShader bloodScript = BloodComputeShader.Instance; - bloodScript.mop1 = players[0].GetComponent(); - bloodScript.mop2 = players[1].GetComponent(); + if (players.Length == 1) + { + bloodScript.mop1 = players[0].GetComponent(); + bloodScript.mop2 = players[0].GetComponent(); + } + else // 2 players + { + bloodScript.mop1 = players[0].GetComponent(); + bloodScript.mop2 = players[1].GetComponent(); + } + + } + + private void InitSpawner(GameObject[] players) + { + EnemySpawner spawner = EnemySpawner.instance; + + spawner.players = players; } } diff --git a/Assets/Scripts/Multiplayer/Project.Scripts.Multiplayer.asmdef b/Assets/Scripts/Multiplayer/Project.Scripts.Multiplayer.asmdef index 3550c75..79cc588 100644 --- a/Assets/Scripts/Multiplayer/Project.Scripts.Multiplayer.asmdef +++ b/Assets/Scripts/Multiplayer/Project.Scripts.Multiplayer.asmdef @@ -11,7 +11,8 @@ "GUID:dfa0fc7c5444edd619a15e6f8c8f242a", "GUID:42d1898a72cfe6848ae89835fb20acd2", "GUID:ff1c299121c93f34ca827a253fc30a61", - "GUID:1eb4e3e6c04cdc848bab71651b1e2ecd" + "GUID:1eb4e3e6c04cdc848bab71651b1e2ecd", + "GUID:62165c64e8180c441b8cfaa6b82208af" ], "includePlatforms": [], "excludePlatforms": [], diff --git a/Assets/Scripts/Multiplayer/SteamManager.cs b/Assets/Scripts/Multiplayer/SteamManager.cs index fb2869b..b3362e8 100644 --- a/Assets/Scripts/Multiplayer/SteamManager.cs +++ b/Assets/Scripts/Multiplayer/SteamManager.cs @@ -1,3 +1,4 @@ +using Assets.Scripts.Multiplayer; using Netcode.Transports.Facepunch; using Steamworks; using Steamworks.Data; @@ -10,7 +11,7 @@ using Unity.Netcode.Transports.UTP; using UnityEngine; using UnityEngine.UI; -public class SteamManager : MonoBehaviour +public class SteamManager : ZNetworkData { [SerializeField] private Button HostBtn; [SerializeField] private Button JoinBtn; @@ -72,6 +73,8 @@ public class SteamManager : MonoBehaviour public async void HostLobby() { + IsServer = true; + await SteamMatchmaking.CreateLobbyAsync(4); } diff --git a/Assets/Scripts/Multiplayer/ZNetworkData.cs b/Assets/Scripts/Multiplayer/ZNetworkData.cs new file mode 100644 index 0000000..6af6b4e --- /dev/null +++ b/Assets/Scripts/Multiplayer/ZNetworkData.cs @@ -0,0 +1,10 @@ +using System.Collections; +using UnityEngine; + +namespace Assets.Scripts.Multiplayer +{ + public abstract class ZNetworkData : MonoBehaviour + { + public static bool IsServer = false; // Only used during game setup. Do not use after + } +} \ No newline at end of file diff --git a/Assets/Scripts/Multiplayer/ZNetworkData.cs.meta b/Assets/Scripts/Multiplayer/ZNetworkData.cs.meta new file mode 100644 index 0000000..59fa14e --- /dev/null +++ b/Assets/Scripts/Multiplayer/ZNetworkData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c78c95d904ecac24dba98ce72f5020af +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: