Make OnControlPhase take a float, representing the delta time.

This is not ACTUALLY the amount of time that has passed, since things in the gameplay loop appear to assume they are being called thirty times per second, even if more or less time has passed. Thus to keep the scripts in sync with the the rest of the engine, we force a 1/30 second delta time.
This commit is contained in:
hispidence 2021-08-12 18:20:14 +01:00
parent b480ea23f7
commit 8b7815c8a0
3 changed files with 10 additions and 7 deletions

View file

@ -189,9 +189,7 @@ extern int lockInput;
GAME_STATUS ControlPhase(int numFrames, int demoMode) GAME_STATUS ControlPhase(int numFrames, int demoMode)
{ {
g_GameScript->OnControlPhase();
short oldLaraFrame; short oldLaraFrame;
GameScriptLevel* level = g_GameFlow->GetLevel(CurrentLevel); GameScriptLevel* level = g_GameFlow->GetLevel(CurrentLevel);
RegeneratePickups(); RegeneratePickups();
@ -216,6 +214,10 @@ GAME_STATUS ControlPhase(int numFrames, int demoMode)
{ {
GlobalCounter++; GlobalCounter++;
// This might not be the exact amount of time that has passed, but giving it a
// value of 1/30 keeps it in lock-step with the rest of the game logic,
// which assumes 30 iterations per second.
g_GameScript->OnControlPhase(1.0f/30.0f);
UpdateSky(); UpdateSky();
// Poll the keyboard and update input variables // Poll the keyboard and update input variables

View file

@ -650,8 +650,9 @@ void GameScript::ExecuteFunction(std::string const & name)
} }
} }
static void doCallback(sol::protected_function const & func) { static void doCallback(sol::protected_function const & func, std::optional<float> dt = std::nullopt) {
auto r = func(); auto r = dt.has_value() ? func(dt) : func();
if (!r.valid()) if (!r.valid())
{ {
sol::error err = r; sol::error err = r;
@ -671,10 +672,10 @@ void GameScript::OnLoad()
doCallback(m_onLoad); doCallback(m_onLoad);
} }
void GameScript::OnControlPhase() void GameScript::OnControlPhase(float dt)
{ {
if(m_onControlPhase.valid()) if(m_onControlPhase.valid())
doCallback(m_onControlPhase); doCallback(m_onControlPhase, dt);
} }
void GameScript::OnSave() void GameScript::OnSave()

View file

@ -116,7 +116,7 @@ public:
void InitCallbacks(); void InitCallbacks();
void OnStart(); void OnStart();
void OnLoad(); void OnLoad();
void OnControlPhase(); void OnControlPhase(float dt);
void OnSave(); void OnSave();
void OnEnd(); void OnEnd();
}; };