From e4251457fa46d22071c034e04d1f5ac53ba29593 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Wed, 24 Feb 2021 22:19:00 -0500 Subject: Added map init scripts Map scripts also now actually run in their own lua thread. --- res/scripts/hallucination_hot_spring.lua | 4 ++++ src/game.cpp | 6 ++++++ src/script_system.cpp | 14 ++++++++++++-- src/script_system.h | 4 ++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/res/scripts/hallucination_hot_spring.lua b/res/scripts/hallucination_hot_spring.lua index 0abd63e..27dd6ac 100644 --- a/res/scripts/hallucination_hot_spring.lua +++ b/res/scripts/hallucination_hot_spring.lua @@ -1,5 +1,9 @@ hallucination_hot_spring = {} +function hallucination_hot_spring.init() + ShowExpression("water_ionia", "surprise") +end + function hallucination_hot_spring.off_right() ChangeMap("hallucination_cliff", "fromLeft") end diff --git a/src/game.cpp b/src/game.cpp index af66068..6564da6 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -3,6 +3,7 @@ #include "animation_system.h" #include "character_system.h" #include "camera_system.h" +#include "script_system.h" int Game::emplaceSprite(std::string alias) { int id; @@ -84,4 +85,9 @@ void Game::loadMap(std::string filename) { getSystem().setUpCollision(spriteId, {0, 0}, t.size, false); getSprite(spriteId).walkthroughScript = t.script; } + + // Run the map's init script. + if (getSystem().mapHasScript(map_->getName(), "init")) { + getSystem().runScript(map_->getName(), "init"); + } } diff --git a/src/script_system.cpp b/src/script_system.cpp index 2e02edf..d700738 100644 --- a/src/script_system.cpp +++ b/src/script_system.cpp @@ -206,15 +206,25 @@ void ScriptSystem::tick(double dt) { }); } -void ScriptSystem::runScript(std::string mapName, std::string scriptName) { +void ScriptSystem::loadMapScripts(std::string mapName) { if (!loadedScripts_.count(mapName)) { engine_.script_file("../res/scripts/" + mapName + ".lua"); loadedScripts_.insert(mapName); } +} + +bool ScriptSystem::mapHasScript(std::string mapName, std::string scriptName) { + loadMapScripts(mapName); + + return !!engine_.traverse_get(mapName, scriptName); +} + +void ScriptSystem::runScript(std::string mapName, std::string scriptName) { + loadMapScripts(mapName); Script newScript; newScript.runner.reset(new sol::thread(sol::thread::create(engine_.lua_state()))); - newScript.callable.reset(new sol::coroutine(engine_.traverse_get(mapName, scriptName))); + newScript.callable.reset(new sol::coroutine(newScript.runner->state().traverse_get(mapName, scriptName))); if (!*newScript.callable) { throw std::runtime_error("Error running script: " + mapName + "." + scriptName); diff --git a/src/script_system.h b/src/script_system.h index 18850b3..7ef2dee 100644 --- a/src/script_system.h +++ b/src/script_system.h @@ -19,12 +19,16 @@ public: void tick(double dt) override; + bool mapHasScript(std::string mapName, std::string scriptName); + void runScript(std::string mapName, std::string scriptName); void runDebugScript(std::string script); private: + void loadMapScripts(std::string mapName); + struct Script { std::unique_ptr runner; std::unique_ptr callable; -- cgit 1.4.1