diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-02-24 22:19:00 -0500 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-02-24 22:19:00 -0500 |
commit | e4251457fa46d22071c034e04d1f5ac53ba29593 (patch) | |
tree | 8b61201a9a2d109041a298fa8ae43ae2f03cda3f /src | |
parent | 4effe126d5b04d7e8572f8d785735a66150aa4ee (diff) | |
download | tanetane-e4251457fa46d22071c034e04d1f5ac53ba29593.tar.gz tanetane-e4251457fa46d22071c034e04d1f5ac53ba29593.tar.bz2 tanetane-e4251457fa46d22071c034e04d1f5ac53ba29593.zip |
Added map init scripts
Map scripts also now actually run in their own lua thread.
Diffstat (limited to 'src')
-rw-r--r-- | src/game.cpp | 6 | ||||
-rw-r--r-- | src/script_system.cpp | 14 | ||||
-rw-r--r-- | src/script_system.h | 4 |
3 files changed, 22 insertions, 2 deletions
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 @@ | |||
3 | #include "animation_system.h" | 3 | #include "animation_system.h" |
4 | #include "character_system.h" | 4 | #include "character_system.h" |
5 | #include "camera_system.h" | 5 | #include "camera_system.h" |
6 | #include "script_system.h" | ||
6 | 7 | ||
7 | int Game::emplaceSprite(std::string alias) { | 8 | int Game::emplaceSprite(std::string alias) { |
8 | int id; | 9 | int id; |
@@ -84,4 +85,9 @@ void Game::loadMap(std::string filename) { | |||
84 | getSystem<TransformSystem>().setUpCollision(spriteId, {0, 0}, t.size, false); | 85 | getSystem<TransformSystem>().setUpCollision(spriteId, {0, 0}, t.size, false); |
85 | getSprite(spriteId).walkthroughScript = t.script; | 86 | getSprite(spriteId).walkthroughScript = t.script; |
86 | } | 87 | } |
88 | |||
89 | // Run the map's init script. | ||
90 | if (getSystem<ScriptSystem>().mapHasScript(map_->getName(), "init")) { | ||
91 | getSystem<ScriptSystem>().runScript(map_->getName(), "init"); | ||
92 | } | ||
87 | } | 93 | } |
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) { | |||
206 | }); | 206 | }); |
207 | } | 207 | } |
208 | 208 | ||
209 | void ScriptSystem::runScript(std::string mapName, std::string scriptName) { | 209 | void ScriptSystem::loadMapScripts(std::string mapName) { |
210 | if (!loadedScripts_.count(mapName)) { | 210 | if (!loadedScripts_.count(mapName)) { |
211 | engine_.script_file("../res/scripts/" + mapName + ".lua"); | 211 | engine_.script_file("../res/scripts/" + mapName + ".lua"); |
212 | loadedScripts_.insert(mapName); | 212 | loadedScripts_.insert(mapName); |
213 | } | 213 | } |
214 | } | ||
215 | |||
216 | bool ScriptSystem::mapHasScript(std::string mapName, std::string scriptName) { | ||
217 | loadMapScripts(mapName); | ||
218 | |||
219 | return !!engine_.traverse_get<sol::function>(mapName, scriptName); | ||
220 | } | ||
221 | |||
222 | void ScriptSystem::runScript(std::string mapName, std::string scriptName) { | ||
223 | loadMapScripts(mapName); | ||
214 | 224 | ||
215 | Script newScript; | 225 | Script newScript; |
216 | newScript.runner.reset(new sol::thread(sol::thread::create(engine_.lua_state()))); | 226 | newScript.runner.reset(new sol::thread(sol::thread::create(engine_.lua_state()))); |
217 | newScript.callable.reset(new sol::coroutine(engine_.traverse_get<sol::function>(mapName, scriptName))); | 227 | newScript.callable.reset(new sol::coroutine(newScript.runner->state().traverse_get<sol::function>(mapName, scriptName))); |
218 | 228 | ||
219 | if (!*newScript.callable) { | 229 | if (!*newScript.callable) { |
220 | throw std::runtime_error("Error running script: " + mapName + "." + scriptName); | 230 | 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: | |||
19 | 19 | ||
20 | void tick(double dt) override; | 20 | void tick(double dt) override; |
21 | 21 | ||
22 | bool mapHasScript(std::string mapName, std::string scriptName); | ||
23 | |||
22 | void runScript(std::string mapName, std::string scriptName); | 24 | void runScript(std::string mapName, std::string scriptName); |
23 | 25 | ||
24 | void runDebugScript(std::string script); | 26 | void runDebugScript(std::string script); |
25 | 27 | ||
26 | private: | 28 | private: |
27 | 29 | ||
30 | void loadMapScripts(std::string mapName); | ||
31 | |||
28 | struct Script { | 32 | struct Script { |
29 | std::unique_ptr<sol::thread> runner; | 33 | std::unique_ptr<sol::thread> runner; |
30 | std::unique_ptr<sol::coroutine> callable; | 34 | std::unique_ptr<sol::coroutine> callable; |