2024-02-20 03:19:28 +01:00
|
|
|
using Assets.Scripts.Multiplayer;
|
|
|
|
using Steamworks.ServerList;
|
2024-02-09 02:38:39 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2024-02-14 05:37:52 +01:00
|
|
|
using System.Linq;
|
2024-02-09 02:38:39 +01:00
|
|
|
using Unity.Netcode;
|
2024-02-20 03:19:28 +01:00
|
|
|
using Unity.Netcode.Transports.UTP;
|
2024-02-09 02:38:39 +01:00
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.SceneManagement;
|
2024-02-15 01:32:08 +01:00
|
|
|
using static UnityEditor.Experimental.GraphView.GraphView;
|
2024-02-09 02:38:39 +01:00
|
|
|
|
2024-02-09 04:09:08 +01:00
|
|
|
public class NetworkedGameSetup : NetworkBehaviour
|
2024-02-09 02:38:39 +01:00
|
|
|
{
|
|
|
|
[SerializeField] private GameObject PlayerPrefab;
|
|
|
|
|
2024-02-14 16:29:24 +01:00
|
|
|
private void Awake()
|
2024-02-09 02:38:39 +01:00
|
|
|
{
|
2024-02-14 16:29:24 +01:00
|
|
|
if (NetworkManager.Singleton == null || NetworkManager.Singleton.SceneManager == null) return;
|
|
|
|
|
2024-02-09 02:38:39 +01:00
|
|
|
DontDestroyOnLoad(this);
|
|
|
|
NetworkManager.Singleton.SceneManager.OnLoadEventCompleted += SceneLoaded;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SceneLoaded(string sceneName, LoadSceneMode loadSceneMode, List<ulong> clientsCompleted, List<ulong> clientsTimedOut)
|
|
|
|
{
|
2024-02-14 16:29:24 +01:00
|
|
|
if (!sceneName.ToLower().Contains("game")) return;
|
|
|
|
|
2024-02-14 05:37:52 +01:00
|
|
|
NetworkManager.Singleton.SceneManager.OnLoadEventCompleted -= SceneLoaded; // Only run once
|
2024-02-14 04:30:38 +01:00
|
|
|
StartSetupProcedure(clientsCompleted);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void StartSetupProcedure(List<ulong> playerIds)
|
|
|
|
{
|
2024-02-20 03:19:28 +01:00
|
|
|
bool isServer = SteamManager.IsServer || LocalManager.IsServer;
|
|
|
|
|
2024-02-14 05:37:52 +01:00
|
|
|
GameObject[] players;
|
2024-02-20 03:19:28 +01:00
|
|
|
if (isServer)
|
2024-02-14 05:37:52 +01:00
|
|
|
players = SpawnPlayers(playerIds);
|
|
|
|
else
|
|
|
|
players = GetPlayers(playerIds);
|
2024-02-14 16:29:24 +01:00
|
|
|
|
2024-02-15 01:32:08 +01:00
|
|
|
StartCoroutine(LateSetupProcedue(players));
|
|
|
|
}
|
|
|
|
|
|
|
|
private IEnumerator LateSetupProcedue(GameObject[] players)
|
|
|
|
{
|
|
|
|
yield return new WaitForSecondsRealtime(0.01f);
|
|
|
|
|
2024-02-14 05:37:52 +01:00
|
|
|
InitRope(players);
|
2024-02-14 16:29:24 +01:00
|
|
|
|
|
|
|
InitUpgrader(players);
|
|
|
|
|
|
|
|
InitBlood(players);
|
2024-02-20 03:19:28 +01:00
|
|
|
|
|
|
|
InitSpawner(players);
|
2024-02-14 05:37:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private GameObject[] GetPlayers(List<ulong> playerIds)
|
|
|
|
{
|
2024-02-20 03:19:28 +01:00
|
|
|
GameObject[] players = NetworkManager.SpawnManager.SpawnedObjects.Where(x => playerIds.Contains(x.Value.NetworkObjectId)).Select(x => x.Value.gameObject).ToArray();
|
2024-02-14 05:37:52 +01:00
|
|
|
|
|
|
|
return players;
|
2024-02-14 04:30:38 +01:00
|
|
|
}
|
|
|
|
|
2024-02-14 05:37:52 +01:00
|
|
|
private GameObject[] SpawnPlayers(List<ulong> playerIds)
|
2024-02-14 04:30:38 +01:00
|
|
|
{
|
2024-02-14 05:37:52 +01:00
|
|
|
GameObject[] players = new GameObject[playerIds.Count];
|
2024-02-14 04:30:38 +01:00
|
|
|
|
2024-02-14 05:37:52 +01:00
|
|
|
for (int i = 0; i < playerIds.Count; i++)
|
2024-02-09 02:38:39 +01:00
|
|
|
{
|
2024-02-14 05:37:52 +01:00
|
|
|
GameObject player = Instantiate(PlayerPrefab, Vector2.up * 3, Quaternion.identity);
|
|
|
|
player.GetComponent<NetworkObject>().SpawnAsPlayerObject(playerIds[i], true);
|
|
|
|
players[i] = player;
|
2024-02-09 02:38:39 +01:00
|
|
|
}
|
2024-02-14 05:37:52 +01:00
|
|
|
|
|
|
|
return players;
|
2024-02-09 02:38:39 +01:00
|
|
|
}
|
2024-02-14 05:37:52 +01:00
|
|
|
|
|
|
|
private void InitRope(GameObject[] players)
|
|
|
|
{
|
2024-02-20 03:19:28 +01:00
|
|
|
if (players.Length == 1)
|
|
|
|
{
|
|
|
|
RopeSimulator.instance.BuildRope(players[0].GetComponent<RopeJoint>(), players[0].GetComponent<RopeJoint>());
|
|
|
|
}
|
|
|
|
else // 2 players
|
|
|
|
{
|
|
|
|
RopeSimulator.instance.BuildRope(players[0].GetComponent<RopeJoint>(), players[1].GetComponent<RopeJoint>());
|
|
|
|
}
|
2024-02-14 05:37:52 +01:00
|
|
|
}
|
|
|
|
|
2024-02-14 16:29:24 +01:00
|
|
|
private void InitUpgrader(GameObject[] players)
|
|
|
|
{
|
2024-02-20 03:19:28 +01:00
|
|
|
return; // fuck this upgrade system
|
|
|
|
|
2024-02-14 16:29:24 +01:00
|
|
|
Upgrader upgradeSystem = Upgrader.instance;
|
|
|
|
|
2024-02-20 03:19:28 +01:00
|
|
|
if (players.Length == 1)
|
|
|
|
{
|
|
|
|
upgradeSystem.player1 = players[0];
|
|
|
|
upgradeSystem.player2 = players[0];
|
|
|
|
}
|
|
|
|
else // 2 players
|
|
|
|
{
|
|
|
|
upgradeSystem.player1 = players[0];
|
|
|
|
upgradeSystem.player2 = players[1];
|
|
|
|
}
|
2024-02-14 16:29:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void InitBlood(GameObject[] players)
|
|
|
|
{
|
|
|
|
BloodComputeShader bloodScript = BloodComputeShader.Instance;
|
|
|
|
|
2024-02-20 03:19:28 +01:00
|
|
|
if (players.Length == 1)
|
|
|
|
{
|
|
|
|
bloodScript.mop1 = players[0].GetComponent<Mop>();
|
|
|
|
bloodScript.mop2 = players[0].GetComponent<Mop>();
|
|
|
|
}
|
|
|
|
else // 2 players
|
|
|
|
{
|
|
|
|
bloodScript.mop1 = players[0].GetComponent<Mop>();
|
|
|
|
bloodScript.mop2 = players[1].GetComponent<Mop>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void InitSpawner(GameObject[] players)
|
|
|
|
{
|
|
|
|
EnemySpawner spawner = EnemySpawner.instance;
|
|
|
|
|
|
|
|
spawner.players = players;
|
2024-02-14 16:29:24 +01:00
|
|
|
}
|
2024-02-09 02:38:39 +01:00
|
|
|
}
|