197 lines
5.6 KiB
C#
Raw Normal View History

2021-08-08 19:57:58 +01:00
using QSB.Events;
using QSB.Player;
using QSB.Player.TransformSync;
using QSB.Utility;
using UnityEngine;
namespace QSB.ClientServerStateSync
{
2021-08-09 11:49:58 +01:00
internal class ClientStateManager : MonoBehaviour
2021-08-08 19:57:58 +01:00
{
public static ClientStateManager Instance { get; private set; }
public event ChangeStateEvent OnChangeState;
public delegate void ChangeStateEvent(ClientState newState);
2021-08-22 16:54:48 +01:00
private void Awake()
2021-08-08 19:57:58 +01:00
=> Instance = this;
private void Start()
{
QSBSceneManager.OnSceneLoaded += OnSceneLoaded;
QSBCore.UnityEvents.RunWhen(() => PlayerTransformSync.LocalInstance != null, () => QSBEventManager.FireEvent(EventNames.QSBClientState, ForceGetCurrentState()));
}
public void ChangeClientState(ClientState newState)
{
if (QSBPlayerManager.LocalPlayer.State == newState)
{
return;
}
2021-08-22 17:17:46 +01:00
2021-08-08 19:57:58 +01:00
DebugLog.DebugWrite($"CHANGE CLIENT STATE FROM {QSBPlayerManager.LocalPlayer.State} to {newState}");
QSBPlayerManager.LocalPlayer.State = newState;
OnChangeState?.Invoke(newState);
}
private void OnSceneLoaded(OWScene oldScene, OWScene newScene, bool inUniverse)
{
var serverState = ServerStateManager.Instance.GetServerState();
2021-09-25 09:47:07 +01:00
ClientState newState;
2021-08-08 19:57:58 +01:00
if (QSBCore.IsHost)
{
2021-10-15 21:08:17 +01:00
2021-09-25 09:47:07 +01:00
switch (newScene)
2021-08-08 19:57:58 +01:00
{
2021-09-25 09:47:07 +01:00
case OWScene.TitleScreen:
DebugLog.DebugWrite($"SERVER LOAD TITLESCREEN");
newState = ClientState.InTitleScreen;
break;
case OWScene.Credits_Fast:
DebugLog.DebugWrite($"SERVER LOAD SHORT CREDITS");
newState = ClientState.WatchingShortCredits;
break;
case OWScene.Credits_Final:
case OWScene.PostCreditsScene:
DebugLog.DebugWrite($"SERVER LOAD LONG CREDITS");
newState = ClientState.WatchingLongCredits;
break;
case OWScene.SolarSystem:
if (oldScene == OWScene.SolarSystem)
{
// reloading scene
DebugLog.DebugWrite($"SERVER RELOAD SOLARSYSTEM");
newState = ClientState.WaitingForOthersToReadyInSolarSystem;
}
else
{
// loading in from title screen
DebugLog.DebugWrite($"SERVER LOAD SOLARSYSTEM");
newState = ClientState.AliveInSolarSystem;
}
break;
default:
newState = ClientState.NotLoaded;
break;
2021-08-08 19:57:58 +01:00
}
}
else
{
2021-09-25 09:47:07 +01:00
switch (newScene)
2021-08-08 19:57:58 +01:00
{
2021-09-25 09:47:07 +01:00
case OWScene.TitleScreen:
DebugLog.DebugWrite($"CLIENT LOAD TITLESCREEN");
newState = ClientState.InTitleScreen;
break;
case OWScene.Credits_Fast:
DebugLog.DebugWrite($"CLIENT LOAD SHORT CREDITS");
newState = ClientState.WatchingShortCredits;
break;
case OWScene.Credits_Final:
case OWScene.PostCreditsScene:
DebugLog.DebugWrite($"CLIENT LOAD LONG CREDITS");
newState = ClientState.WatchingLongCredits;
break;
case OWScene.SolarSystem:
if (serverState == ServerState.WaitingForAllPlayersToDie)
{
DebugLog.DebugWrite($"SEVER IN DEATH PHASE - WAIT");
newState = ClientState.WaitingForOthersToReadyInSolarSystem;
break;
}
if (oldScene == OWScene.SolarSystem)
{
// reloading scene
DebugLog.DebugWrite($"CLIENT RELOAD SOLARSYSTEM");
newState = ClientState.WaitingForOthersToReadyInSolarSystem;
}
else
{
// loading in from title screen
DebugLog.DebugWrite($"CLIENT LOAD SOLARSYSTEM");
if (serverState == ServerState.WaitingForAllPlayersToReady)
{
newState = ClientState.WaitingForOthersToReadyInSolarSystem;
}
else
{
newState = ClientState.AliveInSolarSystem;
}
}
break;
default:
newState = ClientState.NotLoaded;
break;
2021-08-08 19:57:58 +01:00
}
}
2021-09-25 09:47:07 +01:00
QSBEventManager.FireEvent(EventNames.QSBClientState, newState);
2021-08-08 19:57:58 +01:00
}
2021-08-22 16:54:48 +01:00
2021-08-08 19:57:58 +01:00
public void OnDeath()
{
var currentScene = QSBSceneManager.CurrentScene;
if (currentScene == OWScene.SolarSystem)
{
QSBEventManager.FireEvent(EventNames.QSBClientState, ClientState.DeadInSolarSystem);
}
else if (currentScene == OWScene.EyeOfTheUniverse)
{
DebugLog.ToConsole($"Error - You died in the Eye? HOW DID YOU DO THAT?!", OWML.Common.MessageType.Error);
}
else
{
// whaaaaaaaaa
DebugLog.ToConsole($"Error - You died... in a menu? In the credits? In any case, you should never see this. :P", OWML.Common.MessageType.Error);
}
}
2021-09-13 19:44:47 +01:00
public void OnRespawn()
{
var currentScene = QSBSceneManager.CurrentScene;
if (currentScene == OWScene.SolarSystem)
{
2021-09-25 09:47:07 +01:00
DebugLog.DebugWrite($"RESPAWN!");
2021-09-13 19:44:47 +01:00
QSBEventManager.FireEvent(EventNames.QSBClientState, ClientState.AliveInSolarSystem);
}
else
{
DebugLog.ToConsole($"Error - Player tried to respawn in scene {currentScene}", OWML.Common.MessageType.Error);
}
}
2021-08-08 19:57:58 +01:00
private ClientState ForceGetCurrentState()
{
2021-09-25 09:47:07 +01:00
DebugLog.DebugWrite($"ForceGetCurrentState");
2021-08-08 19:57:58 +01:00
var currentScene = LoadManager.GetCurrentScene();
var lastScene = LoadManager.GetPreviousScene();
2021-09-25 09:47:07 +01:00
switch (currentScene)
2021-08-08 19:57:58 +01:00
{
2021-09-25 09:47:07 +01:00
case OWScene.TitleScreen:
DebugLog.DebugWrite($"- TitleScreen");
return ClientState.InTitleScreen;
case OWScene.Credits_Fast:
DebugLog.DebugWrite($"- Short Credits");
return ClientState.WatchingShortCredits;
case OWScene.Credits_Final:
case OWScene.PostCreditsScene:
DebugLog.DebugWrite($"- Long Credits");
return ClientState.WatchingLongCredits;
case OWScene.SolarSystem:
DebugLog.DebugWrite($"- SolarSystem");
return ClientState.AliveInSolarSystem;
case OWScene.EyeOfTheUniverse:
DebugLog.DebugWrite($"- Eye");
return ClientState.AliveInEye;
default:
DebugLog.DebugWrite($"- Not Loaded");
return ClientState.NotLoaded;
2021-08-08 19:57:58 +01:00
}
}
}
}