summary refs log tree commit diff stats
path: root/src/game.cpp
diff options
context:
space:
mode:
authorStar Rauchenberger <fefferburbia@gmail.com>2021-07-05 11:58:29 -0400
committerStar Rauchenberger <fefferburbia@gmail.com>2021-07-05 11:58:29 -0400
commit324346a87c114f0991a092ac8653afecada364b5 (patch)
treef88ac730897056918b366e7fabaad2b136383d59 /src/game.cpp
parentd983ef6b8d66d916ed7502cf6d35b573c5366257 (diff)
downloadtanetane-324346a87c114f0991a092ac8653afecada364b5.tar.gz
tanetane-324346a87c114f0991a092ac8653afecada364b5.tar.bz2
tanetane-324346a87c114f0991a092ac8653afecada364b5.zip
Added background scripts
Background scripts are scripts that are launched when a map is loaded. They differ in use from a map's init script in that they are expected to contain an infinite loop. These scripts are linked to a sprite and will be killed when that sprite is destroyed (usually when the map is unloaded, but if the sprite is made persistent then it may last longer). The thread running the script is given no warning that it is being killed; the coroutine is simply never called again, and the thread is disposed of. Because of this, background scripts MUST ensure the game is in a consistent state before coroutine yielding, because it is not guaranteed that the coroutine will ever be called again.
Diffstat (limited to 'src/game.cpp')
-rw-r--r--src/game.cpp9
1 files changed, 9 insertions, 0 deletions
diff --git a/src/game.cpp b/src/game.cpp index 80ff506..dff8af0 100644 --- a/src/game.cpp +++ b/src/game.cpp
@@ -52,6 +52,7 @@ void Game::loadMap(std::string filename) {
52 // Load the new map. 52 // Load the new map.
53 map_ = std::make_unique<Map>(filename); 53 map_ = std::make_unique<Map>(filename);
54 54
55 std::vector<std::tuple<int, std::string>> backgroundScripts;
55 for (const Prototype& p : map_->getPrototypes()) { 56 for (const Prototype& p : map_->getPrototypes()) {
56 if (spritesByAlias_.count(p.name)) continue; 57 if (spritesByAlias_.count(p.name)) continue;
57 58
@@ -84,6 +85,9 @@ void Game::loadMap(std::string filename) {
84 getSprite(spriteId).mirrorAxis = p.mirrorAxis; 85 getSprite(spriteId).mirrorAxis = p.mirrorAxis;
85 getSprite(spriteId).mirroredSpriteId = getSpriteByAlias(p.spriteToMirror); 86 getSprite(spriteId).mirroredSpriteId = getSpriteByAlias(p.spriteToMirror);
86 } 87 }
88 if (!p.backgroundScript.empty()) {
89 backgroundScripts.push_back({spriteId, p.backgroundScript});
90 }
87 } 91 }
88 92
89 for (const Trigger& t : map_->getTriggers()) { 93 for (const Trigger& t : map_->getTriggers()) {
@@ -99,4 +103,9 @@ void Game::loadMap(std::string filename) {
99 if (getSystem<ScriptSystem>().mapHasScript(map_->getName(), "init")) { 103 if (getSystem<ScriptSystem>().mapHasScript(map_->getName(), "init")) {
100 getSystem<ScriptSystem>().runScript(map_->getName(), "init"); 104 getSystem<ScriptSystem>().runScript(map_->getName(), "init");
101 } 105 }
106
107 // Run any background scripts associated with new sprites.
108 for (auto& [spriteId, scriptName] : backgroundScripts) {
109 getSystem<ScriptSystem>().runScript(map_->getName(), scriptName, spriteId);
110 }
102} 111}