Skip to content

Commit

Permalink
Fix spawn point edgecases
Browse files Browse the repository at this point in the history
  • Loading branch information
toberge committed May 31, 2024
1 parent 926cb4b commit 39a9a9e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Assets/Scenes/Menu.unity
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 785370674}
m_IndirectSpecularColor: {r: 0.062290687, g: 0.21925586, b: 0.49511063, a: 1}
m_IndirectSpecularColor: {r: 0.062428594, g: 0.2193167, b: 0.49494907, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
Expand Down
23 changes: 18 additions & 5 deletions Assets/Scripts/Control&Input/Peer2PeerTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using CollectionExtensions;
using Mirror;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.SceneManagement;

// TODO consider splitting this into match-specific state and general player metadata
public struct PlayerDetails
{
public uint id;
Expand Down Expand Up @@ -125,6 +127,7 @@ public class Peer2PeerTransport : NetworkManager

private PlayerFactory playerFactory;
private static Transform[] spawnPoints;
private static Stack<Transform> spawnPointStack;
private static int playerIndex;
private static Stack<Color> availableColors = new();

Expand Down Expand Up @@ -313,6 +316,7 @@ private static IEnumerator WaitAndSwitchToTrainingMode()
yield return new WaitForSeconds(LoadingScreen.Singleton.MandatoryDuration);

// Wait for player(s) to have spawned
// TODO add a timeout for these wait-for-spawn spins
while (FindObjectsByType<PlayerManager>(FindObjectsSortMode.None).Count() < players.Count)
yield return new WaitForEndOfFrame();
LoadingScreen.Singleton.Hide();
Expand Down Expand Up @@ -379,9 +383,12 @@ private void OnSpawnPlayerInput(NetworkConnectionToClient connection, PlayerConn

// Pick among the available colors
if (!availableColors.TryPop(out var color))
color = Color.white;
{
// Recycle colors if necessary
availableColors = new(PlayerInputManagerController.Singleton.PlayerColors.Reverse());
color = availableColors.Pop();
}

// TODO consider just putting this stuff into the InitialPlayerDetailsMessage
var details = new PlayerDetails
{
id = (uint)playerIndex,
Expand Down Expand Up @@ -626,25 +633,31 @@ private void SpawnPlayer(NetworkConnectionToClient connection, SpawnPlayerMessag
Debug.LogError($"No such player: id={message.id}");
return;
}
Debug.Log($"Spawning player {message.id}");

if (!playerFactory)
{
playerFactory = FindAnyObjectByType<PlayerFactory>();
if (!playerFactory) // TODO shouldn't happen, seems to occur when you go back to menu after end of match
return;
spawnPoints = playerFactory.GetRandomSpawnpoints();
spawnPointStack = new(spawnPoints);
}
Debug.Log($"Spawning player {message.id}");

var spawnPoint = spawnPoints[message.id];
// Ensure we aren't providing invalid spawnpoints.
// We should never run out of spawnpoints in normal circumstances, but you never know 💀
if (!spawnPointStack.TryPop(out var spawnPoint))
spawnPoint = spawnPoints.RandomElement();

// Instantiate correct prefab for player and mode.
var prefabIndex = playerDetails.type is PlayerType.AI && NetworkServer.active ? AIFPSPlayerPrefabIndex : FPSPlayerPrefabIndex;
if (SceneManager.GetActiveScene().name == Scenes.TrainingMode)
prefabIndex = TrainingPlayerPrefabIndex;
var prefab = spawnPrefabs[prefabIndex + prefabIndexOffset];
var player = Instantiate(prefab, spawnPoint.position, spawnPoint.rotation);
player.GetComponent<PlayerManager>().id = message.id;

// Spawn player, setting it as the player for a connection only for the first local player for each connection
// Spawn player, setting it as the player for a connection only for the first local player for each connection.
var isNotAI = playerDetails.type is not PlayerType.AI;
var isFirstLocalPlayer = playerDetails.localInputID == 0;
var isNotDisconnected = playerDetails.type is not PlayerType.Remote || connectedPlayers.Contains(playerDetails.id);
Expand Down
1 change: 1 addition & 0 deletions Assets/Scripts/UI/MainMenu/MainMenuController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ public void HostLocalLobby()

public void StartTrainingMode()
{
PlayerInputManagerController.Singleton.RemoveJoinListener();
Peer2PeerTransport.StartTrainingMode();
playerSelectManager.UpdateLobby();
}
Expand Down

0 comments on commit 39a9a9e

Please sign in to comment.