95 lines
2.7 KiB
C#
95 lines
2.7 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Unity.Netcode;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public class NetworkedGameSetup : NetworkBehaviour
|
|
{
|
|
[SerializeField] private GameObject PlayerPrefab;
|
|
|
|
private void Awake()
|
|
{
|
|
if (NetworkManager.Singleton == null || NetworkManager.Singleton.SceneManager == null) return;
|
|
|
|
DontDestroyOnLoad(this);
|
|
NetworkManager.Singleton.SceneManager.OnLoadEventCompleted += SceneLoaded;
|
|
}
|
|
|
|
private void SceneLoaded(string sceneName, LoadSceneMode loadSceneMode, List<ulong> clientsCompleted, List<ulong> clientsTimedOut)
|
|
{
|
|
if (!sceneName.ToLower().Contains("game")) return;
|
|
|
|
NetworkManager.Singleton.SceneManager.OnLoadEventCompleted -= SceneLoaded; // Only run once
|
|
StartSetupProcedure(clientsCompleted);
|
|
}
|
|
|
|
private void StartSetupProcedure(List<ulong> playerIds)
|
|
{
|
|
GameObject[] players;
|
|
if (IsHost || IsServer)
|
|
players = SpawnPlayers(playerIds);
|
|
else
|
|
players = GetPlayers(playerIds);
|
|
|
|
InitRope(players);
|
|
|
|
InitUpgrader(players);
|
|
|
|
InitBlood(players);
|
|
}
|
|
|
|
private GameObject[] GetPlayers(List<ulong> playerIds)
|
|
{
|
|
GameObject[] players = new GameObject[playerIds.Count];
|
|
|
|
for (int i = 0; i < playerIds.Count; i++)
|
|
{
|
|
ulong playerId = playerIds[i];
|
|
players[i] = NetworkManager.SpawnManager.SpawnedObjects[playerId].gameObject;
|
|
}
|
|
|
|
return players;
|
|
}
|
|
|
|
private GameObject[] SpawnPlayers(List<ulong> playerIds)
|
|
{
|
|
GameObject[] players = new GameObject[playerIds.Count];
|
|
|
|
for (int i = 0; i < playerIds.Count; i++)
|
|
{
|
|
GameObject player = Instantiate(PlayerPrefab, Vector2.up * 3, Quaternion.identity);
|
|
player.GetComponent<NetworkObject>().SpawnAsPlayerObject(playerIds[i], true);
|
|
players[i] = player;
|
|
}
|
|
|
|
return players;
|
|
}
|
|
|
|
private void InitRope(GameObject[] players)
|
|
{
|
|
RopeSimulator ropeSim = GetComponentInChildren<RopeSimulator>();
|
|
|
|
// Assuming 2 players
|
|
ropeSim.BuildRope(players[0].GetComponent<RopeJoint>(), players[1].GetComponent<RopeJoint>());
|
|
}
|
|
|
|
private void InitUpgrader(GameObject[] players)
|
|
{
|
|
Upgrader upgradeSystem = Upgrader.instance;
|
|
|
|
upgradeSystem.player1 = players[0];
|
|
upgradeSystem.player2 = players[1];
|
|
}
|
|
|
|
private void InitBlood(GameObject[] players)
|
|
{
|
|
BloodComputeShader bloodScript = BloodComputeShader.Instance;
|
|
|
|
bloodScript.mop1 = players[0].GetComponent<Mop>();
|
|
bloodScript.mop2 = players[1].GetComponent<Mop>();
|
|
}
|
|
}
|