Skip to content

Commit

Permalink
Make it possible to remove inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
toberge committed Jun 2, 2024
1 parent 60baf47 commit c1c9702
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Assets/Scenes/CraterTown.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: 662445327}
m_IndirectSpecularColor: {r: 0.06470767, g: 0.22111607, b: 0.4958092, a: 1}
m_IndirectSpecularColor: {r: 0.06792183, g: 0.22349986, b: 0.4966975, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
Expand Down
31 changes: 31 additions & 0 deletions Assets/ScriptableObjects/Input/PlayerControls.inputactions
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
"interactions": "",
"initialStateCheck": false
},
{
"name": "Leave",
"type": "Button",
"id": "4e0d1143-3cae-4b1f-acd4-92dfb16f396c",
"expectedControlType": "Button",
"processors": "",
"interactions": "",
"initialStateCheck": false
},
{
"name": "Select",
"type": "Button",
Expand Down Expand Up @@ -478,6 +487,28 @@
"action": "LeftTab",
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "",
"id": "351250c1-9eb6-4827-b455-6e51e6d1e4f4",
"path": "<Gamepad>/select",
"interactions": "",
"processors": "",
"groups": "",
"action": "Leave",
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "",
"id": "45c966a3-cecf-411b-876a-05bf51012ec4",
"path": "<Keyboard>/backspace",
"interactions": "",
"processors": "",
"groups": "",
"action": "Leave",
"isComposite": false,
"isPartOfComposite": false
}
]
},
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Auction/AuctionDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private void Start()
private IEnumerator WaitAndStartAuction()
{
// TODO add a timeout to this kinda thing
while (FindObjectsOfType<PlayerManager>().Count() < Peer2PeerTransport.NumPlayers)
while (FindObjectsOfType<PlayerManager>().Count() < RPRNetworkManager.NumPlayers)
yield return null;

availableAuctionStages = MatchController.Singleton.RoundCount switch
Expand Down Expand Up @@ -180,7 +180,7 @@ private IEnumerator WaitAndSwitchToItemSelect()
// Wait a couple o' frames so gun parts are sent to their respective players
yield return null;
yield return null;
Peer2PeerTransport.UpdatePlayerDetailsAfterAuction();
RPRNetworkManager.UpdatePlayerDetailsAfterAuction();
RpcSwitchToItemSelect();
}

Expand Down
6 changes: 3 additions & 3 deletions Assets/Scripts/Auction/BiddingPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private void CmdPlaceBid(uint playerID)
}

// TODO verify that this player belongs to the source connection
if (!Peer2PeerTransport.PlayerInstanceByID.TryGetValue(playerID, out var player))
if (!RPRNetworkManager.PlayerInstanceByID.TryGetValue(playerID, out var player))
{
Debug.LogError($"Bidding platform received invalid player {playerID} from client!");
return;
Expand All @@ -174,7 +174,7 @@ private void CmdPlaceBid(uint playerID)
[ClientRpc]
private void RpcAcceptBid(uint playerID)
{
if (!Peer2PeerTransport.PlayerInstanceByID.TryGetValue(playerID, out var player))
if (!RPRNetworkManager.PlayerInstanceByID.TryGetValue(playerID, out var player))
{
Debug.LogError($"Bidding platform received invalid player {playerID} from server!");
return;
Expand Down Expand Up @@ -247,7 +247,7 @@ private void EndAuction()
[ClientRpc]
private void RpcPerformTransaction(uint playerID, string itemID)
{
if (!Peer2PeerTransport.PlayerInstanceByID.TryGetValue(playerID, out var player))
if (!RPRNetworkManager.PlayerInstanceByID.TryGetValue(playerID, out var player))
{
Debug.LogError($"Bidding platform received invalid player {playerID} from server!");
return;
Expand Down
8 changes: 4 additions & 4 deletions Assets/Scripts/Control&Input/ItemSelectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public void StartTrackingMenus()

private void InitializeServerState()
{
clientReadyByID = Peer2PeerTransport.Connections
clientReadyByID = RPRNetworkManager.Connections
.ToDictionary(c => c.connectionId, c => false);
((Peer2PeerTransport)NetworkManager.singleton).OnDisconnect += OnDisconnect;
((RPRNetworkManager)NetworkManager.singleton).OnDisconnect += OnDisconnect;
NetworkServer.RegisterHandler<ClientReadyMessage>(OnClientReady);
NetworkServer.RegisterHandler<ClientNotReadyMessage>(OnClientNotReady);
}
Expand All @@ -54,7 +54,7 @@ private void OnDestroy()
{
if (isServer)
{
((Peer2PeerTransport)Peer2PeerTransport.singleton).OnDisconnect -= OnDisconnect;
((RPRNetworkManager)RPRNetworkManager.singleton).OnDisconnect -= OnDisconnect;
NetworkServer.UnregisterHandler<ClientReadyMessage>();
NetworkServer.UnregisterHandler<ClientNotReadyMessage>();
}
Expand All @@ -69,7 +69,7 @@ private void Finish()
[ClientRpc]
private void RpcFinish()
{
((Peer2PeerTransport)Peer2PeerTransport.singleton).UpdateLoadout();
((RPRNetworkManager)RPRNetworkManager.singleton).UpdateLoadout();
StartCoroutine(MatchController.Singleton.WaitAndStartNextRound());
}

Expand Down
28 changes: 18 additions & 10 deletions Assets/Scripts/Control&Input/PlayerInputManagerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ public class PlayerInputManagerController : MonoBehaviour
public List<InputManager> LocalPlayerInputs = new();
public Dictionary<uint, InputManager> InputByPlayer = new();

public List<NetworkConnectionToClient> NetworkClients = new List<NetworkConnectionToClient>();
public int PlayerCount => Peer2PeerTransport.NumPlayers;
public int PlayerCount => RPRNetworkManager.NumPlayers;

public PlayerInputManager PlayerInputManager;

Expand All @@ -28,11 +27,12 @@ public class PlayerInputManagerController : MonoBehaviour

public delegate void JoinEvent(InputManager inputManager);

public JoinEvent onPlayerInputJoined;
public JoinEvent OnPlayerInputJoined;
public JoinEvent OnPlayerInputLeft;

public bool MatchHasAI = false;

void Awake()
private void Awake()
{
#region Singleton boilerplate

Expand All @@ -57,13 +57,16 @@ void Awake()

public void RemoveJoinListener()
{
PlayerInputManager.DisableJoining();
PlayerInputManager.onPlayerJoined -= OnPlayerJoined;
PlayerInputManager.onPlayerLeft -= OnPlayerLeft;
}

public void AddJoinListener()
{
PlayerInputManager.EnableJoining();
PlayerInputManager.onPlayerJoined += OnPlayerJoined;
PlayerInputManager.onPlayerLeft += OnPlayerLeft;
}

public void RemoveListeners()
Expand All @@ -73,21 +76,26 @@ public void RemoveListeners()

private void OnPlayerJoined(PlayerInput playerInput)
{
// TODO refactor this for online (should not source info from here)
var playerIdentity = playerInput.GetComponent<PlayerIdentity>();
playerIdentity.color = playerColors[LocalPlayerInputs.Count];
playerIdentity.playerName = playerNames[LocalPlayerInputs.Count];

var inputManager = playerInput.GetComponent<InputManager>();
inputManager.PlayerCamera.enabled = false;
LocalPlayerInputs.Add(inputManager);
onPlayerInputJoined?.Invoke(inputManager);
OnPlayerInputJoined?.Invoke(inputManager);
// TODO: Make cursor visible if mouseandkeyboard input joims when our buttons can be clicked by a mouse..

if (NetworkManager.singleton.isNetworkActive)
NetworkClient.Send(new PlayerConnectedMessage(LocalPlayerInputs.Count - 1));
}

private void OnPlayerLeft(PlayerInput playerInput)
{
var inputManager = playerInput.GetComponent<InputManager>();
OnPlayerInputLeft?.Invoke(inputManager);
LocalPlayerInputs.Remove(inputManager);

if (NetworkManager.singleton.isNetworkActive)
NetworkClient.Send(new PlayerDisconnectedInputMessage());
}

public void JoinAllInputs()
{
for (int i = 0; i < LocalPlayerInputs.Count; i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public PlayerConnectedMessage(int inputID)
public int inputID;
}

public struct PlayerDisconnectedInputMessage : NetworkMessage { }

public struct PlayerLeftMessage : NetworkMessage
{
public PlayerLeftMessage(uint id)
Expand Down Expand Up @@ -116,7 +118,7 @@ public enum PlayerType
Remote
}

public class Peer2PeerTransport : NetworkManager
public class RPRNetworkManager : NetworkManager
{
private const int FPSPlayerPrefabIndex = 0;
private const int BiddingPlayerPrefabIndexOffset = 1;
Expand Down Expand Up @@ -178,6 +180,7 @@ private void ResetState()
public override void OnStartServer()
{
NetworkServer.RegisterHandler<PlayerConnectedMessage>(OnSpawnPlayerInput);
NetworkServer.RegisterHandler<PlayerDisconnectedInputMessage>(OnRemovePlayerInput);
NetworkServer.RegisterHandler<UpdateLoadoutMessage>(OnReceiveUpdateLoadout);

ResetState();
Expand Down Expand Up @@ -223,6 +226,28 @@ public override void OnServerDisconnect(NetworkConnectionToClient connection)
OnDisconnect?.Invoke(connection.connectionId);
}

private void OnRemovePlayerInput(NetworkConnectionToClient connection, PlayerDisconnectedInputMessage message)
{
if (!playersForConnection.TryGetValue(connection.connectionId, out var playerIDs))
return;
var playerToRemove = PlayerDetails.Where(p => playerIDs.Contains(p.id)).OrderByDescending(p => p.localInputID).First();
Debug.Log($"Removed player {playerToRemove.id} with local id {playerToRemove.localInputID} for connection {connection.connectionId}");

playerIDs.Remove(playerToRemove.id);
if (playerIDs.Count == 0)
{
connection.Disconnect();
return; // OnServerDisconnect handles the rest of the job!
}

connectedPlayers.Remove(playerToRemove.id);
if (!isInMatch)
{
availableColors.Push(playerToRemove.color);
NetworkServer.SendToAll(new PlayerLeftMessage(playerToRemove.id));
}
}

public override void OnStopServer()
{
Debug.Log("Stopped server");
Expand Down Expand Up @@ -353,7 +378,6 @@ private void OnSpawnPlayerInput(NetworkConnectionToClient connection, PlayerConn
return;

// Register connection
PlayerInputManagerController.Singleton.NetworkClients.Add(connection);
if (!connections.Contains(connection))
{
connections.Add(connection);
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Gamestate/MatchController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ private IEnumerator WaitForClientsAndInitialize()
// TODO add a timeout thingy for when one player doesn't join in time
// TODO keep loading screen open while this while loop spins
// Spin while waiting for players to spawn
while (players.Count < Peer2PeerTransport.NumPlayers)
while (players.Count < RPRNetworkManager.NumPlayers)
{
#if UNITY_EDITOR
Debug.Log($"{players.Count} of {Peer2PeerTransport.NumPlayers} players spawned");
Debug.Log($"{players.Count} of {RPRNetworkManager.NumPlayers} players spawned");
#endif
yield return null;
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/HealthController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void DealDamage(DamageInfo info)
[ClientRpc]
private void DealDamageRpc(NetworkDamageInfo networkInfo)
{
var source = Peer2PeerTransport.PlayerInstanceByID[networkInfo.sourcePlayer];
var source = RPRNetworkManager.PlayerInstanceByID[networkInfo.sourcePlayer];
var info = new DamageInfo(source, networkInfo);
ActuallyDealDamage(info);
}
Expand Down
18 changes: 9 additions & 9 deletions Assets/Scripts/PlayerSelect/PlayerSelectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,23 @@ private void Start()
animatorParameters.Add(parameter.name);
}

((Peer2PeerTransport)NetworkManager.singleton).OnPlayerRecieved += UpdateLobby;
((Peer2PeerTransport)NetworkManager.singleton).OnPlayerRemoved += UpdateLobby;
((RPRNetworkManager)NetworkManager.singleton).OnPlayerRecieved += UpdateLobby;
((RPRNetworkManager)NetworkManager.singleton).OnPlayerRemoved += UpdateLobby;
}

private void OnDestroy()
{
if (NetworkManager.singleton)
{
((Peer2PeerTransport)NetworkManager.singleton).OnPlayerRecieved -= UpdateLobby;
((Peer2PeerTransport)NetworkManager.singleton).OnPlayerRemoved -= UpdateLobby;
((RPRNetworkManager)NetworkManager.singleton).OnPlayerRecieved -= UpdateLobby;
((RPRNetworkManager)NetworkManager.singleton).OnPlayerRemoved -= UpdateLobby;
}
}

public void UpdateLobby()
{
var i = 0;
foreach (var player in Peer2PeerTransport.PlayerDetails)
foreach (var player in RPRNetworkManager.PlayerDetails)
{
SetupPlayerModel(player, i);
i++;
Expand All @@ -93,7 +93,7 @@ public void SetupPlayerModel(PlayerDetails player, int index)
playerModels[index].GetComponentInChildren<SkinnedMeshRenderer>().material.color = player.color; // Set player model color
playerModels[index].SetActive(true); // Show corresponding player model
playerModels[index].transform.LookAt(new Vector3(playerSelectCam.transform.position.x, playerModels[index].transform.position.y, playerSelectCam.transform.position.z)); // Orient player model to look at camera
nameTags[index].text = Peer2PeerTransport.PlayerNameWithIndex(player);
nameTags[index].text = RPRNetworkManager.PlayerNameWithIndex(player);
nameTags[index].enabled = true;
joinText[index].enabled = false;
}
Expand Down Expand Up @@ -139,13 +139,13 @@ IEnumerator PlayRandomAnimation()
yield return new WaitForSeconds(5f);
while (true)
{
int randomAnimatorNumber = Random.Range(0, playerInputManagerController.PlayerCount); // Choose random playermodel to animate
int randomAnimatorNumber = Random.Range(0, RPRNetworkManager.NumPlayers); // Choose random playermodel to animate

Animator randomAnimator = playerAnimators[randomAnimatorNumber]; // Get the animator for one of the players that has a connected input

// If randomAnimatorNumber is player all the way to the right, don't include cardpeek trigger
string randomTrigger = "";
if (randomAnimatorNumber == playerInputManagerController.PlayerCount - 1)
if (randomAnimatorNumber == RPRNetworkManager.NumPlayers - 1)
{
randomTrigger = randomAnimator.GetParameter(Random.Range(2, randomAnimator.parameterCount)).name; // Choose a random trigger to set, excluding CardPeek
}
Expand All @@ -159,7 +159,7 @@ IEnumerator PlayRandomAnimation()
}


if ((randomTrigger == "CardPeek" || randomTrigger == "CardPeekReaction") && (playerInputManagerController.PlayerCount > 1) && (cardPeekCounter == 0))
if ((randomTrigger == "CardPeek" || randomTrigger == "CardPeekReaction") && RPRNetworkManager.NumPlayers > 1 && cardPeekCounter == 0)
{
randomAnimator.SetTrigger("CardPeek");
playerAnimators[randomAnimatorNumber + 1].SetTrigger("CardPeekReaction");
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/UI/ClientLobby.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ClientLobby : MonoBehaviour
void Start()
{
environmentCamera.position = lobbyPosition.position;
((Peer2PeerTransport)NetworkManager.singleton).OnPlayerRecieved += AddPlayer;
((RPRNetworkManager)NetworkManager.singleton).OnPlayerRecieved += AddPlayer;
}

public void QuitLobby()
Expand All @@ -28,6 +28,6 @@ private void AddPlayer(PlayerDetails details)

private void OnDestroy()
{
((Peer2PeerTransport)NetworkManager.singleton).OnPlayerRecieved -= AddPlayer;
((RPRNetworkManager)NetworkManager.singleton).OnPlayerRecieved -= AddPlayer;
}
}
Loading

0 comments on commit c1c9702

Please sign in to comment.