-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactoring LobbyReady to use new features
BREAKING CHANGE: LobbyReady and ReadyCheck components changed fix: using Start instead so Server can be added by test test: trying to fix lobby test fix: fixing lobby ready setting syncvar in another asm doesn't work all the time. Unity ILPP is weird
- Loading branch information
1 parent
8bf12dd
commit b9e13e3
Showing
10 changed files
with
431 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
|
||
namespace Mirage.Components | ||
{ | ||
/// <summary> | ||
/// Simple component to track if a player is ready in a lobby | ||
/// <para> | ||
/// To best use this component Set Sync Direction from owner to server | ||
/// </para> | ||
/// </summary> | ||
public class ReadyCheck : NetworkBehaviour | ||
{ | ||
public event Action<bool> OnReadyChanged; | ||
|
||
[SyncVar(hook = nameof(OnReadyChanged), invokeHookOnServer = true)] | ||
private bool _isReady; | ||
|
||
public bool IsReady => _isReady; | ||
|
||
// note need a methods to set syncvar, otherwise scripts in another asmdef will not set if via weaver | ||
public void SetReady(bool ready) | ||
{ | ||
_isReady = ready; | ||
} | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
Assets/Mirage/Components/ObjectReady.cs.meta → Assets/Mirage/Components/ReadyCheck.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System.Linq; | ||
using Mirage.Components; | ||
using UnityEngine; | ||
using UnityEngine.UI; | ||
|
||
namespace Mirage.Snippets.LobbyReadyCheck | ||
{ | ||
// CodeEmbed-Start: send-to-ready | ||
[NetworkMessage] | ||
// Make sure to regieter message on client | ||
public struct MyMessage | ||
{ | ||
public string message; | ||
} | ||
|
||
public class LobbyController : MonoBehaviour | ||
{ | ||
public LobbyReady LobbyReady; | ||
|
||
public void SendToReady() | ||
{ | ||
var myMessage = new MyMessage { message = "Hello, world!" }; | ||
// Send message to ready players | ||
LobbyReady.SendToReady(myMessage); | ||
} | ||
} | ||
// CodeEmbed-End: send-to-ready | ||
|
||
public class LobbyController2 : MonoBehaviour | ||
{ | ||
public LobbyReady LobbyReady; | ||
|
||
// CodeEmbed-Start: send-to-not-ready | ||
public void SendToNotReady() | ||
{ | ||
var myMessage = new MyMessage { message = "Hello, world!" }; | ||
// Send message to ready players | ||
LobbyReady.SendToReady(myMessage, sendToReady: false); | ||
} | ||
// CodeEmbed-End: send-to-not-ready | ||
|
||
// CodeEmbed-Start: set-all-not-ready | ||
public void ClearReady() | ||
{ | ||
LobbyReady.SetAllClientsNotReady(); | ||
} | ||
// CodeEmbed-End: set-all-not-ready | ||
|
||
public void SetReady() | ||
{ | ||
var readyCheck = LobbyReady.Players.First().Value; | ||
// CodeEmbed-Start: set-ready | ||
readyCheck.SetReady(true); | ||
// CodeEmbed-End: set-ready | ||
} | ||
} | ||
|
||
// CodeEmbed-Start: ready-ui | ||
public class ReadyUI : MonoBehaviour | ||
{ | ||
public ReadyCheck ReadyCheck; | ||
public Image Image; | ||
|
||
public void Start() | ||
{ | ||
ReadyCheck.OnReadyChanged += OnReadyChanged; | ||
// invoke right away to set the current value | ||
OnReadyChanged(ReadyCheck.IsReady); | ||
} | ||
|
||
private void OnReadyChanged(bool ready) | ||
{ | ||
Image.color = ready ? Color.green : Color.red; | ||
} | ||
} | ||
// CodeEmbed-End: ready-ui | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.