Jo Jo funguje Kappa

This commit is contained in:
Dominik G.
2025-10-06 18:15:27 +02:00
parent 807e8fc5f3
commit 515160b1ec
11 changed files with 3103 additions and 76 deletions

View File

@@ -17,6 +17,8 @@ namespace MegaKoop.Game.Networking
[DefaultExecutionOrder(-500)]
public class LobbyGameSceneCoordinator : MonoBehaviour
{
private static LobbyGameSceneCoordinator Instance;
[SerializeField] private string characterSceneName = "CharacterScene";
[SerializeField] private float spawnRadius = 3f;
[SerializeField] private float minimumSpawnSpacing = 2.5f;
@@ -28,6 +30,14 @@ namespace MegaKoop.Game.Networking
private void Awake()
{
// Singleton to avoid double spawners (e.g., one from DontDestroyOnLoad and one from scene contents)
if (Instance != null && Instance != this)
{
// Another coordinator already exists and persists; remove this duplicate.
Destroy(gameObject);
return;
}
Instance = this;
DontDestroyOnLoad(gameObject);
SceneManager.sceneLoaded += HandleSceneLoaded;
}
@@ -58,7 +68,12 @@ namespace MegaKoop.Game.Networking
}
else
{
// Already in the target scene (e.g., during hot reload).
// Already in the target scene (e.g., during hot reload). Avoid double-spawn.
if (hasSpawned)
{
Debug.Log("[LobbyGameSceneCoordinator] BeginGame called but players already spawned; ignoring.");
return;
}
loadPending = false;
hasSpawned = false;
SpawnPlayersInScene(SceneManager.GetActiveScene());
@@ -131,11 +146,13 @@ namespace MegaKoop.Game.Networking
hasSpawned = true;
// Proactively remove any previously spawned character clones (e.g., from a duplicate coordinator)
DespawnExistingClones(scene, template);
Vector3 basePosition = template.transform.position;
Quaternion baseRotation = template.transform.rotation;
Transform parent = template.transform.parent;
// Hide template so only spawned copies are visible.
template.SetActive(false);
int total = Mathf.Max(1, pendingPlayers.Count);
@@ -155,6 +172,28 @@ namespace MegaKoop.Game.Networking
}
}
private static void DespawnExistingClones(Scene scene, GameObject template)
{
// Destroy any GameObjects in the scene that look like spawned player avatars, excluding the template hierarchy.
foreach (var root in scene.GetRootGameObjects())
{
// Skip the template root itself
if (root == template) continue;
// Heuristic: if it has a SteamCharacterNetworkBridge or ThirdPersonCharacterController, treat it as a character clone
var bridge = root.GetComponentInChildren<SteamCharacterNetworkBridge>(true);
var tpc = root.GetComponentInChildren<ThirdPersonCharacterController>(true);
if (bridge != null || tpc != null)
{
// Avoid deleting the template if nested under a different root (shouldn't happen)
if (!template.transform.IsChildOf(root.transform) && !root.transform.IsChildOf(template.transform))
{
UnityEngine.Object.Destroy(root);
}
}
}
}
private static GameObject FindWizardTemplate(Scene scene)
{
foreach (var root in scene.GetRootGameObjects())

View File

@@ -19,7 +19,11 @@ namespace MegaKoop.Game.Networking
private void Update()
{
#if STEAMWORKSNET
if (!MegaKoop.Steam.SteamManager.Initialized)
#else
if (!SteamBootstrap.IsInitialized)
#endif
{
return;
}
@@ -34,7 +38,11 @@ namespace MegaKoop.Game.Networking
public void Send(CSteamID recipient, NetworkMessageType type, byte[] payload, EP2PSend sendType = EP2PSend.k_EP2PSendReliable)
{
#if STEAMWORKSNET
if (!MegaKoop.Steam.SteamManager.Initialized)
#else
if (!SteamBootstrap.IsInitialized)
#endif
{
return;
}