Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Basic/Invaders/Assets/Prefabs/player.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ MonoBehaviour:
m_HitParticleSystem: {fileID: 2274621649305592229, guid: 4bf8467f048bdca478f6702a5286ef53,
type: 3}
m_PlayerColorInGame: {r: 0.32941177, g: 1, b: 0.19215687, a: 1}
m_PlayerVisual: {fileID: 21200000}
--- !u!61 &6100000
BoxCollider2D:
m_ObjectHideFlags: 0
Expand Down
19 changes: 18 additions & 1 deletion Basic/Invaders/Assets/Scripts/InvadersGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ public override void OnNetworkDespawn()
m_Enemies.Clear();
m_Shields.Clear();
}

NetworkManager.Singleton.OnClientConnectedCallback -= OnClientConnected;
}

internal static event Action OnSingletonReady;
Expand Down Expand Up @@ -183,9 +185,23 @@ public override void OnNetworkSpawn()
//and in turn makes the players visible and allows for the players to be controlled.
SceneTransitionHandler.sceneTransitionHandler.SetSceneState(SceneTransitionHandler.SceneStates.Ingame);

if (IsServer)
{
NetworkManager.Singleton.OnClientConnectedCallback += OnClientConnected;
}

base.OnNetworkSpawn();
}

private void OnClientConnected(ulong clientId)
{
if (m_ReplicatedTimeSent)
{
// Send the RPC only to the newly connected client
SetReplicatedTimeRemainingClientRPC(m_TimeRemaining, new ClientRpcParams {Send = new ClientRpcSendParams{TargetClientIds = new List<ulong>() {clientId}}});
}
}

/// <summary>
/// ShouldStartCountDown
/// Determines when the countdown should start
Expand Down Expand Up @@ -217,8 +233,9 @@ private bool ShouldStartCountDown()
/// will deal with updating it. For that, we use a ClientRPC
/// </summary>
/// <param name="delayedStartTime"></param>
/// <param name="clientRpcParams"></param>
[ClientRpc]
private void SetReplicatedTimeRemainingClientRPC(float delayedStartTime)
private void SetReplicatedTimeRemainingClientRPC(float delayedStartTime, ClientRpcParams clientRpcParams = new ClientRpcParams())
{
// See the ShouldStartCountDown method for when the server updates the value
if (m_TimeRemaining == 0)
Expand Down
1 change: 1 addition & 0 deletions Basic/Invaders/Assets/Scripts/MenuControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public void StartLocalGame()
if (utpTransport) m_HostIpInput.text = "127.0.0.1";
if (NetworkManager.Singleton.StartHost())
{
SceneTransitionHandler.sceneTransitionHandler.RegisterCallbacks();
SceneTransitionHandler.sceneTransitionHandler.SwitchScene(m_LobbySceneName);
}
else
Expand Down
39 changes: 13 additions & 26 deletions Basic/Invaders/Assets/Scripts/PlayerControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class PlayerControl : NetworkBehaviour
[SerializeField]
Color m_PlayerColorInGame;

private SceneTransitionHandler.SceneStates m_CurrentSceneState;
private bool m_HasGameStarted;

private bool m_IsAlive = true;
Expand All @@ -36,6 +35,7 @@ public class PlayerControl : NetworkBehaviour
private GameObject m_MyBullet;
private ClientRpcParams m_OwnerRPCParams;

[SerializeField]
private SpriteRenderer m_PlayerVisual;
private NetworkVariable<int> m_Score = new NetworkVariable<int>(0);

Expand All @@ -46,15 +46,9 @@ private void Awake()
m_HasGameStarted = false;
}

private void Start()
{
m_PlayerVisual = GetComponent<SpriteRenderer>();
if (m_PlayerVisual != null) m_PlayerVisual.color = Color.clear;
}

private void Update()
{
switch (m_CurrentSceneState)
switch (SceneTransitionHandler.sceneTransitionHandler.GetCurrentSceneState())
{
case SceneTransitionHandler.SceneStates.Ingame:
{
Expand Down Expand Up @@ -94,21 +88,14 @@ public override void OnNetworkDespawn()
}
}

private void SceneTransitionHandler_clientLoadedScene(ulong clientId)
{
SceneStateChangedClientRpc(m_CurrentSceneState);
}

[ClientRpc]
private void SceneStateChangedClientRpc(SceneTransitionHandler.SceneStates state)
private void SceneTransitionHandler_sceneStateChanged(SceneTransitionHandler.SceneStates newState)
{
if (!IsServer) SceneTransitionHandler.sceneTransitionHandler.SetSceneState(state);
UpdateColor();
}

private void SceneTransitionHandler_sceneStateChanged(SceneTransitionHandler.SceneStates newState)
private void UpdateColor()
{
m_CurrentSceneState = newState;
if (m_CurrentSceneState == SceneTransitionHandler.SceneStates.Ingame)
if (SceneTransitionHandler.sceneTransitionHandler.GetCurrentSceneState() == SceneTransitionHandler.SceneStates.Ingame)
{
if (m_PlayerVisual != null) m_PlayerVisual.color = m_PlayerColorInGame;
}
Expand All @@ -133,10 +120,9 @@ public override void OnNetworkSpawn()
InvadersGame.OnSingletonReady += SubscribeToDelegatesAndUpdateValues;
else
SubscribeToDelegatesAndUpdateValues();

if (IsServer) SceneTransitionHandler.sceneTransitionHandler.OnClientLoadedScene += SceneTransitionHandler_clientLoadedScene;


SceneTransitionHandler.sceneTransitionHandler.OnSceneStateChanged += SceneTransitionHandler_sceneStateChanged;
UpdateColor();
}

private void SubscribeToDelegatesAndUpdateValues()
Expand All @@ -149,6 +135,8 @@ private void SubscribeToDelegatesAndUpdateValues()
InvadersGame.Singleton.SetScore(m_Score.Value);
InvadersGame.Singleton.SetLives(m_Lives.Value);
}

m_HasGameStarted = InvadersGame.Singleton.hasGameStarted.Value;
}

public void IncreasePlayerScore(int amount)
Expand All @@ -165,8 +153,8 @@ private void OnGameStartedChanged(bool previousValue, bool newValue)
private void OnLivesChanged(int previousAmount, int currentAmount)
{
// Hide graphics client side upon death
if (currentAmount <= 0 && IsClient && TryGetComponent<SpriteRenderer>(out var spriteRenderer))
spriteRenderer.enabled = false;
if (currentAmount <= 0 && IsClient)
m_PlayerVisual.enabled = false;

if (!IsOwner) return;
Debug.LogFormat("Lives {0} ", currentAmount);
Expand Down Expand Up @@ -237,8 +225,7 @@ public void HitByBullet()

// Hide graphics of this player object server-side. Note we don't want to destroy the object as it
// may stop the RPC's from reaching on the other side, as there is only one player controlled object
if (TryGetComponent<SpriteRenderer>(out var spriteRenderer))
spriteRenderer.enabled = false;
m_PlayerVisual.enabled = false;
}
else
{
Expand Down
20 changes: 11 additions & 9 deletions Basic/Invaders/Assets/Scripts/SceneTransitionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ private void Start()
}
}

/// <summary>
/// Registers callbacks to the NetworkSceneManager. This should be called when starting the server
/// </summary>
public void RegisterCallbacks()
{
NetworkManager.Singleton.SceneManager.OnLoadComplete += OnLoadComplete;

}

/// <summary>
/// Switches to a new scene
/// </summary>
Expand All @@ -96,8 +105,6 @@ public void SwitchScene(string scenename)
if(NetworkManager.Singleton.IsListening)
{
m_numberOfClientLoaded = 0;
NetworkManager.Singleton.SceneManager.OnLoadComplete += OnLoadComplete;
NetworkManager.Singleton.SceneManager.OnLoadEventCompleted += OnLoadEventCompleted;
NetworkManager.Singleton.SceneManager.LoadScene(scenename, LoadSceneMode.Single);
}
else
Expand All @@ -106,18 +113,12 @@ public void SwitchScene(string scenename)
}
}

void OnLoadComplete(ulong clientId, string sceneName, LoadSceneMode loadSceneMode)
private void OnLoadComplete(ulong clientId, string sceneName, LoadSceneMode loadSceneMode)
{
m_numberOfClientLoaded += 1;
OnClientLoadedScene?.Invoke(clientId);
}

void OnLoadEventCompleted(string sceneName, LoadSceneMode loadSceneMode, List<ulong> clientsCompleted, List<ulong> clientsTimedOut)
{
NetworkManager.Singleton.SceneManager.OnLoadComplete -= OnLoadComplete;
NetworkManager.Singleton.SceneManager.OnLoadEventCompleted -= OnLoadEventCompleted;
}

public bool AllClientsAreLoaded()
{
return m_numberOfClientLoaded == NetworkManager.Singleton.ConnectedClients.Count;
Expand All @@ -129,6 +130,7 @@ public bool AllClientsAreLoaded()
/// </summary>
public void ExitAndLoadStartMenu()
{
NetworkManager.Singleton.SceneManager.OnLoadComplete -= OnLoadComplete;
OnClientLoadedScene = null;
SetSceneState(SceneStates.Start);
SceneManager.LoadScene(1);
Expand Down