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

155 lines
4.2 KiB
C#
Raw Normal View History

using QSB.Localization;
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;
2022-03-03 03:46:33 +00:00
namespace QSB.TimeSync;
2023-07-28 18:30:57 +00:00
public class TimeSyncUI : MonoBehaviour, IAddComponentOnStart
2020-11-25 09:54:52 +00:00
{
2022-03-03 03:46:33 +00:00
private static TimeSyncUI _instance;
2020-11-26 13:09:34 +00:00
2022-03-03 03:46:33 +00:00
public static float TargetTime;
2020-11-26 13:09:34 +00:00
2022-03-03 03:46:33 +00:00
private Canvas _canvas;
private Text _text;
private float _startTime;
private bool _isSetUp;
private TimeSyncType _currentType;
private Enum _currentReason;
2020-11-25 09:54:52 +00:00
2022-03-03 03:46:33 +00:00
private void Awake()
{
_instance = this;
enabled = false;
2020-11-25 09:54:52 +00:00
2022-03-03 03:46:33 +00:00
QSBSceneManager.OnUniverseSceneLoaded += OnUniverseSceneLoad;
}
2020-12-03 08:28:05 +00:00
2022-03-03 03:46:33 +00:00
private void OnUniverseSceneLoad(OWScene oldScene, OWScene newScene)
{
_isSetUp = true;
2022-05-03 07:48:24 +00:00
var obj = QSBWorldSync.GetUnityObject<SleepTimerUI>();
2022-03-03 03:46:33 +00:00
_canvas = obj._canvas;
_text = obj._text;
_canvas.enabled = false;
var langController = QSBWorldSync.GetUnityObject<PauseMenuManager>().transform.GetChild(0).GetComponent<FontAndLanguageController>();
langController.AddTextElement(_text);
2022-03-03 03:46:33 +00:00
}
2020-11-25 09:54:52 +00:00
2022-03-03 03:46:33 +00:00
public void OnDestroy()
{
QSBSceneManager.OnUniverseSceneLoaded -= OnUniverseSceneLoad;
if (_canvas != null && _canvas.enabled)
2020-11-25 09:54:52 +00:00
{
2022-03-03 03:46:33 +00:00
Canvas.willRenderCanvases -= OnWillRenderCanvases;
2020-11-25 09:54:52 +00:00
}
2022-03-03 03:46:33 +00:00
}
2020-11-25 09:54:52 +00:00
2022-03-03 03:46:33 +00:00
public static void Start(TimeSyncType type, Enum reason) =>
Delay.RunWhen(() => _instance._isSetUp, () => _instance.StartTimeSync(type, reason));
2022-03-03 03:46:33 +00:00
public static void Stop() =>
Delay.RunWhen(() => _instance._isSetUp, () => _instance.EndTimeSync());
2022-03-03 03:46:33 +00:00
private void StartTimeSync(TimeSyncType type, Enum reason)
{
if (!QSBSceneManager.IsInUniverse)
2020-11-25 09:54:52 +00:00
{
2022-03-03 03:46:33 +00:00
DebugLog.ToConsole("Error - Tried to start time sync UI when not in universe!", OWML.Common.MessageType.Error);
return;
2020-11-25 09:54:52 +00:00
}
2022-03-03 03:46:33 +00:00
_currentType = type;
_currentReason = reason;
_startTime = Time.timeSinceLevelLoad;
enabled = true;
_canvas.enabled = true;
Canvas.willRenderCanvases += OnWillRenderCanvases;
2023-02-11 04:24:09 +00:00
// silly hack that shouldnt be in the ui component but oh well
Locator.GetPlayerTransform().GetComponent<PlayerResources>()._invincible = true;
Locator.GetDeathManager()._invincible = true;
var shipTransform = Locator.GetShipTransform();
if (shipTransform)
{
shipTransform.GetComponentInChildren<ShipDamageController>()._invincible = true;
}
2022-03-03 03:46:33 +00:00
}
private void EndTimeSync()
{
_currentType = TimeSyncType.None;
enabled = false;
_canvas.enabled = false;
Canvas.willRenderCanvases -= OnWillRenderCanvases;
2023-02-11 04:24:09 +00:00
// silly hack that shouldnt be in the ui component but oh well
Locator.GetPlayerTransform().GetComponent<PlayerResources>()._invincible = false;
Locator.GetDeathManager()._invincible = false;
var shipTransform = Locator.GetShipTransform();
if (shipTransform)
{
shipTransform.GetComponentInChildren<ShipDamageController>()._invincible = false;
}
2022-03-03 03:46:33 +00:00
}
private void OnWillRenderCanvases()
{
if (!_isSetUp)
2020-11-25 09:54:52 +00:00
{
2022-03-03 03:46:33 +00:00
return;
2020-11-25 09:54:52 +00:00
}
2022-03-03 03:46:33 +00:00
var text = "";
switch (_currentType)
{
2022-03-03 03:46:33 +00:00
case TimeSyncType.FastForwarding:
switch ((FastForwardReason)_currentReason)
{
case FastForwardReason.TooFarBehind:
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 = string.Format(QSBLocalization.Current.TimeSyncTooFarBehind, $"{minutes:D2}:{seconds:D2}.{milliseconds:000}");
2022-03-03 03:46:33 +00:00
break;
}
break;
case TimeSyncType.Pausing:
switch ((PauseReason)_currentReason)
{
case PauseReason.ServerNotStarted:
text = QSBLocalization.Current.TimeSyncWaitingForStartOfServer;
2022-03-03 03:46:33 +00:00
break;
case PauseReason.TooFarAhead:
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 = string.Format(QSBLocalization.Current.TimeSyncTooFarAhead, $"{minutes:D2}:{seconds:D2}.{milliseconds:000}");
2022-03-03 03:46:33 +00:00
break;
case PauseReason.WaitingForAllPlayersToBeReady:
text = QSBLocalization.Current.TimeSyncWaitForAllToReady;
2022-03-03 03:46:33 +00:00
break;
case PauseReason.WaitingForAllPlayersToDie:
text = QSBLocalization.Current.TimeSyncWaitForAllToDie;
2022-03-03 03:46:33 +00:00
break;
}
break;
}
2022-03-03 03:46:33 +00:00
_text.text = text;
2020-11-25 09:54:52 +00:00
}
2023-02-11 04:24:09 +00:00
}