Skip to content

Commit

Permalink
Time scale instead of sleep (#43)
Browse files Browse the repository at this point in the history
* Time scale instead of sleep

* Cleanup

* Prevent client from ruining time scale

* Disable input while waiting after wakeup

* Disable input only at start of game

* Inver isInputDisable var name
  • Loading branch information
Raicuparta authored Feb 29, 2020
1 parent c862ac7 commit 1f7e0b9
Showing 1 changed file with 43 additions and 43 deletions.
86 changes: 43 additions & 43 deletions QSB/TimeSync/WakeUpSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ namespace QSB.TimeSync
public class WakeUpSync : NetworkBehaviour
{
private const float TimeThreshold = 0.5f;
private const float FastForwardSpeed = 10f;

private enum State { NotLoaded, EyesClosed, Awake, Sleeping, Pausing }
private enum State { NotLoaded, EyesClosed, Awake, FastForwarding, Pausing }
private State _state = State.NotLoaded;

private MessageHandler<WakeUpMessage> _wakeUpHandler;
private Campfire _campfire;

private float _sendTimer;
private float _serverTime;
private float _timeScale;
private bool _isInputEnabled = true;

private void Start()
{
Expand All @@ -39,7 +41,6 @@ private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
if (scene.name == "SolarSystem")
{
_campfire = GameObject.FindObjectsOfType<Campfire>().Single(x => x.GetValue<Sector>("_sector").name == "Sector_Village");
_state = State.EyesClosed;
}
}
Expand All @@ -60,7 +61,6 @@ private void OnWakeUp()

private void SendServerTime()
{
DebugLog.Screen("Sending server time to all my friends: " + Time.timeSinceLevelLoad);
var message = new WakeUpMessage
{
ServerTime = Time.timeSinceLevelLoad
Expand Down Expand Up @@ -89,26 +89,27 @@ private void WakeUpOrSleep()
{
OpenEyes();
_state = State.Awake;

if (!isServer)
{
DisableInput();
}
}

var myTime = Time.timeSinceLevelLoad;
var diff = myTime - _serverTime;

if (diff > TimeThreshold)
{
DebugLog.Screen($"My time ({myTime}) is {diff} ahead server ({_serverTime})");
StartPausing();
return;
}

if (diff < -TimeThreshold)
{
DebugLog.Screen($"My time ({myTime}) is {-diff} behind server ({_serverTime})");
StartSleeping();
StartFastForwarding();
return;
}

DebugLog.Screen($"My time ({myTime}) is within threshold of server time ({_serverTime})");
}

private void OpenEyes()
Expand All @@ -128,35 +129,16 @@ private void OpenEyes()
Locator.GetPromptManager().RemoveScreenPrompt(cameraEffectController.GetValue<ScreenPrompt>("_wakePrompt"));
OWTime.Unpause(OWTime.PauseType.Sleeping);
cameraEffectController.Invoke("WakeUp");

// Enable all inputs immediately.
OWInput.ChangeInputMode(InputMode.Character);
typeof(OWInput).SetValue("_inputFadeFraction", 0f);
GlobalMessenger.FireEvent("TakeFirstFlashbackSnapshot");
}

private void StartSleeping()
private void StartFastForwarding()
{
if (_state == State.Sleeping)
if (_state == State.FastForwarding)
{
return;
}
DebugLog.Screen("Starting sleeping");
var wakePrompt = _campfire.GetValue<ScreenPrompt>("_wakePrompt");
Locator.GetPromptManager().RemoveScreenPrompt(wakePrompt, PromptPosition.Center);
_campfire.Invoke("StartSleeping");
_state = State.Sleeping;
}

private void StopSleeping()
{
if (_state != State.Sleeping)
{
return;
}
DebugLog.Screen("Stopping sleeping");
_campfire.StopSleeping();
_state = State.Awake;
_timeScale = FastForwardSpeed;
_state = State.FastForwarding;
}

private void StartPausing()
Expand All @@ -165,19 +147,31 @@ private void StartPausing()
{
return;
}
OWTime.Pause(OWTime.PauseType.Menu);
Time.timeScale = 0f;
_timeScale = 0f;
_state = State.Pausing;
}

private void StopPausing()
private void ResetTimeScale()
{
if (_state != State.Pausing)
_timeScale = 1f;
_state = State.Awake;

if (!_isInputEnabled)
{
return;
EnableInput();
}
OWTime.Unpause(OWTime.PauseType.Menu);
_state = State.Awake;
}

private void DisableInput()
{
_isInputEnabled = false;
OWInput.ChangeInputMode(InputMode.None);
}

private void EnableInput()
{
_isInputEnabled = true;
OWInput.ChangeInputMode(InputMode.Character);
}

private void Update()
Expand Down Expand Up @@ -216,13 +210,19 @@ private void UpdateLocal()
return;
}

if (_state == State.Sleeping && Time.timeSinceLevelLoad >= _serverTime)
bool isDoneFastForwarding = _state == State.FastForwarding && Time.timeSinceLevelLoad >= _serverTime;
bool isDonePausing = _state == State.Pausing && Time.timeSinceLevelLoad < _serverTime;

if (isDoneFastForwarding || isDonePausing)
{
StopSleeping();
ResetTimeScale();
}
else if (_state == State.Pausing && Time.timeSinceLevelLoad < _serverTime)

Time.timeScale = _timeScale;

if (!_isInputEnabled && OWInput.GetInputMode() != InputMode.None)
{
StopPausing();
DisableInput();
}
}

Expand Down

0 comments on commit 1f7e0b9

Please sign in to comment.