191 lines
4.9 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;
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;
2021-12-22 18:27:12 -08:00
QSBCore.UnityEvents.RunWhen(() => PlayerTransformSync.LocalInstance != null,
() => new ClientStateMessage(ForceGetCurrentState()).Send());
2021-08-08 19:57:58 +01:00
}
private void OnDestroy() =>
QSBSceneManager.OnSceneLoaded -= OnSceneLoaded;
2021-12-22 18:37:24 -08:00
public void SendChangeClientStateMessage(ClientState newState)
2021-12-20 11:49:03 +00:00
{
ChangeClientState(newState);
2021-12-22 18:27:12 -08:00
new ClientStateMessage(newState).Send();
2021-12-20 11:49:03 +00:00
}
2021-08-08 19:57:58 +01:00
public void ChangeClientState(ClientState newState)
{
2021-12-21 14:35:35 -08:00
if (PlayerTransformSync.LocalInstance == null || QSBPlayerManager.LocalPlayer.State == newState)
2021-08-08 19:57:58 +01:00
{
return;
}
2021-08-22 17:17:46 +01:00
2021-08-08 19:57:58 +01:00
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:
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;
2021-09-25 09:47:07 +01:00
}
else
{
// loading in from title screen
newState = ClientState.AliveInSolarSystem;
}
2021-11-25 15:38:05 +00:00
2021-09-25 09:47:07 +01:00
break;
2021-11-12 21:05:58 +00:00
case OWScene.EyeOfTheUniverse:
newState = ClientState.AliveInEye;
break;
2021-09-25 09:47:07 +01:00
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:
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;
2021-09-25 09:47:07 +01:00
break;
}
if (oldScene == OWScene.SolarSystem)
{
// reloading scene
newState = ClientState.WaitingForOthersToBeReady;
2021-09-25 09:47:07 +01:00
}
else
{
// loading in from title screen
if (serverState == ServerState.WaitingForAllPlayersToReady)
{
newState = ClientState.WaitingForOthersToBeReady;
2021-09-25 09:47:07 +01:00
}
else
{
newState = ClientState.AliveInSolarSystem;
}
}
2021-11-25 15:38:05 +00:00
2021-09-25 09:47:07 +01:00
break;
2021-11-12 21:05:58 +00:00
case OWScene.EyeOfTheUniverse:
2021-12-21 20:47:19 +00:00
if (serverState == ServerState.WaitingForAllPlayersToReady)
{
newState = ClientState.WaitingForOthersToBeReady;
}
else
{
newState = ClientState.AliveInEye;
}
2021-11-12 21:05:58 +00:00
break;
2021-09-25 09:47:07 +01:00
default:
newState = ClientState.NotLoaded;
break;
2021-08-08 19:57:58 +01:00
}
}
2021-09-25 09:47:07 +01:00
2021-12-22 18:37:24 -08:00
SendChangeClientStateMessage(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)
{
2021-12-22 18:37:24 -08:00
SendChangeClientStateMessage(ClientState.DeadInSolarSystem);
2021-08-08 19:57:58 +01:00
}
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-12-22 18:37:24 -08:00
SendChangeClientStateMessage(ClientState.AliveInSolarSystem);
2021-09-13 19:44:47 +01:00
}
else
{
DebugLog.ToConsole($"Error - Player tried to respawn in scene {currentScene}", OWML.Common.MessageType.Error);
}
}
2021-12-22 19:06:35 -08:00
private static ClientState ForceGetCurrentState()
=> QSBSceneManager.CurrentScene switch
2021-08-08 19:57:58 +01:00
{
2021-11-21 15:38:11 +00:00
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,
2021-12-22 19:06:35 -08:00
_ => ClientState.NotLoaded
2021-11-21 15:38:11 +00:00
};
2021-08-08 19:57:58 +01:00
}
2021-12-22 19:06:35 -08:00
}