quantum-space-buddies/QSB/TimeSync/TimeSyncUI.cs

138 lines
3.4 KiB
C#
Raw Normal View History

2021-12-27 05:05:07 +00:00
using QSB.Utility;
using QSB.WorldSync;
2020-11-26 13:09:34 +00:00
using System;
using System.Linq;
2020-11-26 13:09:34 +00:00
using UnityEngine;
2020-11-25 09:54:52 +00:00
using UnityEngine.UI;
namespace QSB.TimeSync
{
2020-12-03 08:28:05 +00:00
internal class TimeSyncUI : MonoBehaviour
2020-11-25 09:54:52 +00:00
{
2020-11-26 19:46:09 +00:00
public static TimeSyncUI Instance;
2020-11-25 09:54:52 +00:00
2020-12-19 19:06:13 +00:00
public static float TargetTime;
2020-11-25 09:54:52 +00:00
private Canvas _canvas;
private Text _text;
2020-11-26 11:43:35 +00:00
private float _startTime;
2020-11-26 13:09:34 +00:00
private bool _isSetUp;
2020-11-26 19:46:09 +00:00
private TimeSyncType _currentType;
private Enum _currentReason;
2020-11-25 09:54:52 +00:00
2020-12-14 20:41:56 +00:00
public void Awake()
2020-11-25 09:54:52 +00:00
{
Instance = this;
enabled = false;
2020-11-26 13:09:34 +00:00
QSBSceneManager.OnUniverseSceneLoaded += OnUniverseSceneLoad;
}
private void OnUniverseSceneLoad(OWScene oldScene, OWScene newScene)
2020-11-26 13:09:34 +00:00
{
_isSetUp = true;
var obj = QSBWorldSync.GetUnityObjects<SleepTimerUI>().First();
2021-12-27 04:35:40 +00:00
_canvas = obj._canvas;
_text = obj._text;
2020-11-25 09:54:52 +00:00
_canvas.enabled = false;
}
2020-12-14 20:41:56 +00:00
public void OnDestroy()
2020-11-25 09:54:52 +00:00
{
2020-11-26 13:09:34 +00:00
QSBSceneManager.OnUniverseSceneLoaded -= OnUniverseSceneLoad;
2021-08-28 11:55:23 +00:00
if (_canvas != null && _canvas.enabled)
2020-11-25 09:54:52 +00:00
{
Canvas.willRenderCanvases -= OnWillRenderCanvases;
}
}
public static void Start(TimeSyncType type, Enum reason) =>
2022-01-29 09:29:02 +00:00
Delay.RunWhen(() => Instance._isSetUp, () => Instance.StartTimeSync(type, reason));
2020-12-03 08:28:05 +00:00
2020-12-14 20:41:56 +00:00
public static void Stop() =>
2022-01-29 09:29:02 +00:00
Delay.RunWhen(() => Instance._isSetUp, () => Instance.EndTimeSync());
2020-11-25 09:54:52 +00:00
private void StartTimeSync(TimeSyncType type, Enum reason)
2020-11-25 09:54:52 +00:00
{
if (!QSBSceneManager.IsInUniverse)
{
DebugLog.ToConsole("Error - Tried to start time sync UI when not in universe!", OWML.Common.MessageType.Error);
return;
}
2021-11-25 15:38:05 +00:00
2020-11-26 19:46:09 +00:00
_currentType = type;
_currentReason = reason;
2020-11-26 11:43:35 +00:00
_startTime = Time.timeSinceLevelLoad;
2020-11-25 09:54:52 +00:00
enabled = true;
_canvas.enabled = true;
Canvas.willRenderCanvases += OnWillRenderCanvases;
}
2020-11-26 19:46:09 +00:00
private void EndTimeSync()
2020-11-25 09:54:52 +00:00
{
2020-11-26 19:46:09 +00:00
_currentType = TimeSyncType.None;
2020-11-25 09:54:52 +00:00
enabled = false;
_canvas.enabled = false;
Canvas.willRenderCanvases -= OnWillRenderCanvases;
}
private void OnWillRenderCanvases()
{
2020-11-26 13:09:34 +00:00
if (!_isSetUp)
{
return;
}
2021-06-18 21:38:32 +00:00
2020-11-26 19:46:09 +00:00
var text = "";
switch (_currentType)
{
2022-01-12 04:08:38 +00:00
case TimeSyncType.FastForwarding:
switch ((FastForwardReason)_currentReason)
{
case FastForwardReason.TooFarBehind:
2022-01-23 03:14:37 +00:00
var totalSeconds = Mathf.Max(TargetTime - Time.timeSinceLevelLoad, 0f);
var minutes = Mathf.FloorToInt(totalSeconds / 60f);
var seconds = Mathf.FloorToInt(totalSeconds) % 60;
var milliseconds = totalSeconds % 1 * 1000;
text = $"{minutes:D2}:{seconds:D2}.{milliseconds:000}"
+ Environment.NewLine
+ "Fast-forwarding to match server time...";
break;
}
2021-08-22 16:17:46 +00:00
2020-11-26 19:46:09 +00:00
break;
2020-12-03 08:28:05 +00:00
2020-11-26 19:46:09 +00:00
case TimeSyncType.Pausing:
switch ((PauseReason)_currentReason)
{
case PauseReason.ServerNotStarted:
text = "Waiting for server to start...";
break;
case PauseReason.TooFarAhead:
2022-01-23 03:14:37 +00:00
var totalSeconds = Mathf.Max(Time.timeSinceLevelLoad - TargetTime, 0f);
var minutes = Mathf.FloorToInt(totalSeconds / 60f);
var seconds = Mathf.FloorToInt(totalSeconds) % 60;
var milliseconds = totalSeconds % 1 * 1000;
text = $"{minutes:D2}:{seconds:D2}.{milliseconds:000}"
+ Environment.NewLine
+ "Pausing to match server time...";
break;
case PauseReason.WaitingForAllPlayersToBeReady:
text = "Waiting for start of loop...";
break;
case PauseReason.WaitingForAllPlayersToDie:
text = "Waiting for end of loop...";
break;
}
2021-08-22 16:17:46 +00:00
2021-05-09 18:55:19 +00:00
break;
2020-11-26 19:46:09 +00:00
}
2021-06-18 21:38:32 +00:00
2020-11-26 19:46:09 +00:00
_text.text = text;
2020-11-25 09:54:52 +00:00
}
}
}