185 lines
4.6 KiB
C#
Raw Normal View History

2021-12-23 13:57:33 -08:00
using QSB.ClientServerStateSync.Messages;
2021-12-22 18:27:12 -08:00
using QSB.Messaging;
2021-08-08 19:57:58 +01:00
using QSB.Player;
using QSB.Player.TransformSync;
using QSB.Utility;
using UnityEngine;
2022-03-02 19:46:33 -08:00
namespace QSB.ClientServerStateSync;
internal class ClientStateManager : MonoBehaviour
2021-08-08 19:57:58 +01:00
{
2022-03-02 19:46:33 -08:00
public static ClientStateManager Instance { get; private set; }
2021-08-08 19:57:58 +01:00
2022-03-02 19:46:33 -08:00
public event ChangeStateEvent OnChangeState;
public delegate void ChangeStateEvent(ClientState newState);
2022-03-02 19:46:33 -08:00
private void Awake()
=> Instance = this;
2022-03-02 19:46:33 -08:00
private void Start()
{
QSBSceneManager.OnPostSceneLoad += OnPostSceneLoad;
2022-03-02 19:46:33 -08:00
Delay.RunWhen(() => PlayerTransformSync.LocalInstance != null,
() => new ClientStateMessage(ForceGetCurrentState()).Send());
}
2021-08-08 19:57:58 +01:00
2022-03-02 19:46:33 -08:00
private void OnDestroy() =>
QSBSceneManager.OnPostSceneLoad -= OnPostSceneLoad;
2021-09-25 09:47:07 +01:00
2022-03-02 19:46:33 -08:00
public void ChangeClientState(ClientState newState)
{
if (PlayerTransformSync.LocalInstance == null || QSBPlayerManager.LocalPlayer.State == newState)
{
2022-03-02 19:46:33 -08:00
return;
}
2022-03-02 19:46:33 -08:00
QSBPlayerManager.LocalPlayer.State = newState;
OnChangeState?.Invoke(newState);
}
private static void OnPostSceneLoad(OWScene oldScene, OWScene newScene)
2022-03-02 19:46:33 -08:00
{
var serverState = ServerStateManager.Instance.GetServerState();
2022-03-02 19:46:33 -08:00
ClientState newState;
2022-03-02 19:46:33 -08:00
if (QSBCore.IsHost)
{
2022-01-01 22:19:10 +00:00
2022-03-02 19:46:33 -08:00
switch (newScene)
{
case OWScene.TitleScreen:
newState = ClientState.InTitleScreen;
break;
case OWScene.Credits_Fast:
newState = ClientState.WatchingShortCredits;
break;
case OWScene.Credits_Final:
case OWScene.PostCreditsScene:
newState = ClientState.WatchingLongCredits;
break;
case OWScene.SolarSystem:
if (oldScene == OWScene.SolarSystem)
{
// reloading scene
newState = ClientState.WaitingForOthersToBeReady;
}
else
{
// loading in from title screen
newState = ClientState.AliveInSolarSystem;
}
break;
case OWScene.EyeOfTheUniverse:
newState = ClientState.AliveInEye;
break;
default:
newState = ClientState.NotLoaded;
break;
2021-08-08 19:57:58 +01:00
}
2022-03-02 19:46:33 -08:00
}
else
{
switch (newScene)
{
2022-03-02 19:46:33 -08:00
case OWScene.TitleScreen:
newState = ClientState.InTitleScreen;
break;
case OWScene.Credits_Fast:
newState = ClientState.WatchingShortCredits;
break;
case OWScene.Credits_Final:
case OWScene.PostCreditsScene:
newState = ClientState.WatchingLongCredits;
break;
case OWScene.SolarSystem:
if (serverState == ServerState.WaitingForAllPlayersToDie)
{
newState = ClientState.WaitingForOthersToBeReady;
break;
2022-03-02 19:46:33 -08:00
}
if (oldScene == OWScene.SolarSystem)
{
// reloading scene
newState = ClientState.WaitingForOthersToBeReady;
}
else
{
// loading in from title screen
if (serverState == ServerState.WaitingForAllPlayersToReady)
{
newState = ClientState.WaitingForOthersToBeReady;
}
else
{
2022-03-02 19:46:33 -08:00
newState = ClientState.AliveInSolarSystem;
}
2022-03-02 19:46:33 -08:00
}
break;
case OWScene.EyeOfTheUniverse:
if (serverState == ServerState.WaitingForAllPlayersToReady)
{
newState = ClientState.WaitingForOthersToBeReady;
}
else
{
newState = ClientState.AliveInEye;
}
2022-03-02 19:46:33 -08:00
break;
default:
newState = ClientState.NotLoaded;
break;
}
2021-09-13 19:44:47 +01:00
}
2022-03-02 19:46:33 -08:00
new ClientStateMessage(newState).Send();
}
public void OnDeath()
{
var currentScene = QSBSceneManager.CurrentScene;
if (currentScene == OWScene.SolarSystem)
{
2022-03-02 19:46:33 -08:00
new ClientStateMessage(ClientState.DeadInSolarSystem).Send();
}
2022-03-02 19:46:33 -08:00
else if (currentScene == OWScene.EyeOfTheUniverse)
{
2022-03-02 19:46:33 -08:00
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);
}
2022-03-02 19:46:33 -08:00
}
2022-03-02 19:46:33 -08:00
public void OnRespawn()
{
var currentScene = QSBSceneManager.CurrentScene;
if (currentScene == OWScene.SolarSystem)
{
DebugLog.DebugWrite($"RESPAWN!");
new ClientStateMessage(ClientState.AliveInSolarSystem).Send();
}
else
{
DebugLog.ToConsole($"Error - Player tried to respawn in scene {currentScene}", OWML.Common.MessageType.Error);
}
}
2022-03-02 19:46:33 -08:00
private static ClientState ForceGetCurrentState()
=> QSBSceneManager.CurrentScene switch
{
OWScene.TitleScreen => ClientState.InTitleScreen,
OWScene.Credits_Fast => ClientState.WatchingShortCredits,
OWScene.Credits_Final or OWScene.PostCreditsScene => ClientState.WatchingLongCredits,
OWScene.SolarSystem => ClientState.AliveInSolarSystem,
OWScene.EyeOfTheUniverse => ClientState.AliveInEye,
_ => ClientState.NotLoaded
};
2021-12-22 19:06:35 -08:00
}