-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace BigLobby | ||
{ | ||
public static class Helper | ||
{ | ||
public static T[] ResizeArray<T>(T[] oldArray, int newSize) { | ||
var newArray = new T[newSize]; | ||
oldArray.CopyTo(newArray, 0); | ||
return newArray; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using GameNetcodeStuff; | ||
using HarmonyLib; | ||
using Unity.Netcode; | ||
using UnityEngine; | ||
|
||
namespace BigLobby.Patches | ||
{ | ||
internal class PlayerObjects | ||
{ | ||
[HarmonyPatch(typeof(StartOfRound), "Awake")] | ||
[HarmonyPostfix] | ||
public static void ResizeLists(ref StartOfRound __instance) { | ||
__instance.allPlayerObjects = Helper.ResizeArray(__instance.allPlayerObjects, Plugin.MaxPlayers); | ||
__instance.allPlayerScripts = Helper.ResizeArray(__instance.allPlayerScripts, Plugin.MaxPlayers); | ||
|
||
var playerPrefab = __instance.playerPrefab; | ||
var playerContainer = __instance.allPlayerObjects[1].transform.parent; | ||
if (GameNetworkManager.Instance.isHostingGame) | ||
{ // We are the host, spawn extra players | ||
for (int i = 4; i < Plugin.MaxPlayers; i++) | ||
{ | ||
var newPlayer = Object.Instantiate<GameObject>(playerPrefab, playerContainer); | ||
var newScript = newPlayer.GetComponent<PlayerControllerB>(); | ||
var netObject = newPlayer.GetComponent<NetworkObject>(); | ||
|
||
__instance.allPlayerObjects[i] = newPlayer; | ||
__instance.allPlayerScripts[i] = newScript; | ||
newPlayer.name = $"ExtraPlayer{i}"; | ||
newScript.playerClientId = i; | ||
netObject.Spawn(); | ||
} | ||
} | ||
else // We are the client, look for extra players | ||
{ | ||
var scripts = Object.FindObjectsOfType<PlayerControllerB>(); | ||
foreach (var script in scripts) | ||
{ | ||
if (script.playerClientId < 4) continue; | ||
var player = script.gameObject; | ||
var index = int.Parse(player.name[-1]); | ||
script.playerClientId = index; | ||
__instance.allPlayerObjects[index] = player; | ||
__instance.allPlayerScripts[index] = script; | ||
} | ||
} | ||
} | ||
} | ||
} |