Skip to content

Commit

Permalink
patch gameplay stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
kermeow committed Oct 29, 2023
1 parent ca86416 commit 58acdf7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Helper.cs
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;
}
}
}
48 changes: 48 additions & 0 deletions Patches/Game.cs
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;
}
}
}
}
}

0 comments on commit 58acdf7

Please sign in to comment.