浏览代码

Pulling the countdown logic out of the GameManager and the LocalLobby; the latter fixes a bug where the countdown would prevent any lobby changes from being pushed from the host, since every from the LocalLobby's OnChanged event would fire.

/main/staging/host_handshake
nathaniel.buck@unity3d.com 3 年前
当前提交
a1a07b92
共有 9 个文件被更改,包括 111 次插入68 次删除
  1. 13
      Assets/Prefabs/UI/PlayerInteractionPanel.prefab
  2. 27
      Assets/Scenes/mainScene.unity
  3. 37
      Assets/Scripts/Game/GameManager.cs
  4. 12
      Assets/Scripts/Game/LocalLobby.cs
  5. 1
      Assets/Scripts/Infrastructure/Messenger.cs
  6. 2
      Assets/Scripts/Infrastructure/Observed.cs
  7. 10
      Assets/Scripts/UI/CountdownUI.cs
  8. 66
      Assets/Scripts/Game/Countdown.cs
  9. 11
      Assets/Scripts/Game/Countdown.cs.meta

13
Assets/Prefabs/UI/PlayerInteractionPanel.prefab


m_Component:
- component: {fileID: 7303921398628037483}
- component: {fileID: 4969504009400302254}
- component: {fileID: 1918870058264573867}
m_Layer: 5
m_Name: CountDownUI
m_TagString: Untagged

m_PersistentCalls:
m_Calls: []
m_CountDownText: {fileID: 4467363028704636643}
--- !u!114 &1918870058264573867
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 580481917308754637}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d125c6cac111c6442ac5b07a1f313fa4, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &609596107351326384
GameObject:
m_ObjectHideFlags: 0

27
Assets/Scenes/mainScene.unity


m_Script: {fileID: 11500000, guid: 78d292f3bd9f1614cb744dcb4fe3ac12, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &1014339014 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 7853401853899595651, guid: f1d618bdc6f1813449d428126e640aa5, type: 3}
m_PrefabInstance: {fileID: 2637199315837045693}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5b3b588e7ae40ec4ca35fdb9404513ab, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &1217229506 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 354886978664675623, guid: f1d618bdc6f1813449d428126e640aa5, type: 3}

objectReference: {fileID: 0}
- target: {fileID: 7716713811812636910, guid: f80fc24bab3dcda459a2669321e2e5a4, type: 3}
propertyPath: m_LocalLobbyObservers.Array.size
value: 8
value: 7
objectReference: {fileID: 0}
- target: {fileID: 7716713811812636910, guid: f80fc24bab3dcda459a2669321e2e5a4, type: 3}
propertyPath: m_vivoxUserHandlers.Array.data[0]

- target: {fileID: 7716713811812636910, guid: f80fc24bab3dcda459a2669321e2e5a4, type: 3}
propertyPath: m_LocalLobbyObservers.Array.data[0]
value:
objectReference: {fileID: 1014339014}
objectReference: {fileID: 1412109061}
objectReference: {fileID: 1412109061}
objectReference: {fileID: 297599733}
objectReference: {fileID: 297599733}
objectReference: {fileID: 2130620598}
objectReference: {fileID: 2130620598}
objectReference: {fileID: 2074106027}
objectReference: {fileID: 2074106027}
objectReference: {fileID: 309485569}
objectReference: {fileID: 309485569}
objectReference: {fileID: 2126854580}
objectReference: {fileID: 2126854580}
objectReference: {fileID: 1511612118}
- target: {fileID: 7716713811812636910, guid: f80fc24bab3dcda459a2669321e2e5a4, type: 3}
propertyPath: m_LocalLobbyObservers.Array.data[7]
value:

37
Assets/Scripts/Game/GameManager.cs


{ m_localUser.UserStatus = (UserStatus)msg;
}
else if (type == MessageType.StartCountdown)
{ BeginCountDown();
{ m_localLobby.State = LobbyState.CountDown;
m_localLobby.CountDownTime = 0;
}
else if (type == MessageType.CompleteCountdown)
{ if (m_relayClient is RelayUtpHost)
(m_relayClient as RelayUtpHost).SendInGameState();
}
else if (type == MessageType.ConfirmInGameState)
{ m_localUser.UserStatus = UserStatus.InGame;

{ m_localLobby.State = LobbyState.Lobby;
m_localLobby.CountDownTime = 0;
SetUserLobbyState();
}
else if (type == MessageType.QuickJoin)

else if (type == MessageType.SetPlayerSound)
{
var playerSound = (LobbyUserAudio)msg;
}
}
}
private void SetGameState(GameState state)

OnReceiveMessage(MessageType.LobbyUserStatus, UserStatus.Lobby);
}
private void BeginCountDown()
{
if (m_localLobby.State == LobbyState.CountDown)
return;
m_localLobby.CountDownTime = 4;
m_localLobby.State = LobbyState.CountDown;
StartCoroutine(CountDown());
}
/// <summary>
/// The CountdownUI will pick up on changes to the lobby's countdown timer. This can be interrupted if the lobby leaves the countdown state (via a CancelCountdown message).
/// </summary>
private IEnumerator CountDown()
{
while (m_localLobby.CountDownTime > 0)
{
yield return null;
if (m_localLobby.State != LobbyState.CountDown)
yield break;
m_localLobby.CountDownTime -= Time.deltaTime;
}
if (m_relayClient is RelayUtpHost)
(m_relayClient as RelayUtpHost).SendInGameState();
}
private void SetUserLobbyState()
{
SetGameState(GameState.Lobby);

{
m_localLobby.CopyObserved(new LocalLobby.LobbyData(), new Dictionary<string, LobbyUser>());
m_localLobby.AddPlayer(m_localUser); // As before, the local player will need to be plugged into UI before the lobby join actually happens.
m_localLobby.CountDownTime = 0;
m_localLobby.RelayServer = null;
}

12
Assets/Scripts/Game/LocalLobby.cs


get { return new LobbyData(m_data); }
}
float m_CountDownTime;
public float CountDownTime
{
get { return m_CountDownTime; }
set
{
m_CountDownTime = value;
OnChanged(this);
}
}
ServerAddress m_relayServer;
/// <summary>Used only for visual output of the Relay connection info. The obfuscated Relay server IP is obtained during allocation in the RelayUtpSetup.</summary>

1
Assets/Scripts/Infrastructure/Messenger.cs


QuickJoin = 14,
ClientUserApproved = 15,
ClientUserSeekingDisapproval = 16,
CompleteCountdown = 17,
}
/// <summary>

2
Assets/Scripts/Infrastructure/Observed.cs


onChanged?.Invoke(observed);
}
protected void OnDestroy(T observed)
protected void OnDestroyed(T observed)
{
onDestroyed?.Invoke(observed);
}

10
Assets/Scripts/UI/CountdownUI.cs


/// This countdown is purely visual, to give clients a moment if they need to un-ready before entering the game;
/// clients will actually wait for a message from the host confirming that they are in the game, instead of assuming the game is ready to go when the countdown ends.
/// </summary>
public class CountdownUI : LocalLobbyObserver
public class CountdownUI : ObserverBehaviour<Countdown.Data>
protected override void UpdateObserver(LocalLobby obs)
protected override void UpdateObserver(Countdown.Data data)
base.UpdateObserver(obs);
if (observed.CountDownTime <= 0)
base.UpdateObserver(data);
if (observed.TimeLeft <= 0)
m_CountDownText.SetText($"Starting in: {observed.CountDownTime:0}");
m_CountDownText.SetText($"Starting in: {observed.TimeLeft:0}"); // Note that the ":0" formatting rounds, not truncates.
}
}
}

66
Assets/Scripts/Game/Countdown.cs


using System;
using UnityEngine;
namespace LobbyRelaySample
{
/// <summary>
/// Runs the countdown to the in-game state. While the start of the countdown is synced via Relay, the countdown itself is handled locally,
/// since precise timing isn't necessary.
/// </summary>
[RequireComponent(typeof(UI.CountdownUI))]
public class Countdown : MonoBehaviour, IReceiveMessages
{
public class Data : Observed<Countdown.Data>
{
private float m_timeLeft;
public float TimeLeft
{
get => m_timeLeft;
set
{ m_timeLeft = value;
OnChanged(this);
}
}
public override void CopyObserved(Data oldObserved) { /*No-op, since this is unnecessary.*/ }
}
private Data m_data = new Data();
private UI.CountdownUI m_ui;
private const int k_countdownTime = 4;
public void OnEnable()
{
if (m_ui == null)
m_ui = GetComponent<UI.CountdownUI>();
m_data.TimeLeft = -1;
Locator.Get.Messenger.Subscribe(this);
m_ui.BeginObserving(m_data);
}
public void OnDisable()
{
Locator.Get.Messenger.Unsubscribe(this);
m_ui.EndObserving();
}
public void OnReceiveMessage(MessageType type, object msg)
{
if (type == MessageType.StartCountdown)
{
m_data.TimeLeft = k_countdownTime;
}
else if (type == MessageType.CancelCountdown)
{
m_data.TimeLeft = -1;
}
}
public void Update()
{
if (m_data.TimeLeft < 0)
return;
m_data.TimeLeft -= Time.deltaTime;
if (m_data.TimeLeft < 0)
Locator.Get.Messenger.OnReceiveMessage(MessageType.CompleteCountdown, null);
}
}
}

11
Assets/Scripts/Game/Countdown.cs.meta


fileFormatVersion: 2
guid: d125c6cac111c6442ac5b07a1f313fa4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存