diff options
-rw-r--r-- | res/scripts/common.lua | 14 | ||||
-rw-r--r-- | src/game.cpp | 9 | ||||
-rw-r--r-- | src/map.cpp | 2 | ||||
-rw-r--r-- | src/map.h | 1 | ||||
-rw-r--r-- | src/script_system.cpp | 9 | ||||
-rw-r--r-- | src/script_system.h | 5 |
6 files changed, 38 insertions, 2 deletions
diff --git a/res/scripts/common.lua b/res/scripts/common.lua index 2e95f26..35eec22 100644 --- a/res/scripts/common.lua +++ b/res/scripts/common.lua | |||
@@ -579,6 +579,20 @@ function FaceTowardSpriteCardinally(spriteName, targetName) | |||
579 | SetDirection(spriteName, dir) | 579 | SetDirection(spriteName, dir) |
580 | end | 580 | end |
581 | 581 | ||
582 | --- Checks whether the given sprite is facing another sprite. | ||
583 | -- @param spriteName the name of the sprite whose facing direction is relevant | ||
584 | -- @param targetName the name of the sprite that is or isn't being looked at | ||
585 | function IsFacingTowardSprite(spriteName, targetName) | ||
586 | local spriteId = getSpriteByAlias(spriteName) | ||
587 | local targetId = getSpriteByAlias(targetName) | ||
588 | local sprite = getSprite(spriteId) | ||
589 | local target = getSprite(targetId) | ||
590 | local diff = vec2i.new(target.loc:x() - sprite.loc:x(), target.loc:y() - sprite.loc:y()) | ||
591 | local dir = directionFacingPoint(diff) | ||
592 | |||
593 | return sprite.dir == dir | ||
594 | end | ||
595 | |||
582 | --- Detaches the sprite's followers and erases their following trails. | 596 | --- Detaches the sprite's followers and erases their following trails. |
583 | function BreakUpParty(spriteName) | 597 | function BreakUpParty(spriteName) |
584 | local spriteId = getSpriteByAlias(spriteName) | 598 | local spriteId = getSpriteByAlias(spriteName) |
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 | } |
diff --git a/src/map.cpp b/src/map.cpp index c8bf18f..f28a976 100644 --- a/src/map.cpp +++ b/src/map.cpp | |||
@@ -121,6 +121,8 @@ Map::Map(std::string_view name) : name_(name) { | |||
121 | p.spriteToMirror = property.getStringValue(); | 121 | p.spriteToMirror = property.getStringValue(); |
122 | } else if (property.getName() == "bumpPlayerScript") { | 122 | } else if (property.getName() == "bumpPlayerScript") { |
123 | p.bumpPlayerScript = property.getStringValue(); | 123 | p.bumpPlayerScript = property.getStringValue(); |
124 | } else if (property.getName() == "backgroundScript") { | ||
125 | p.backgroundScript = property.getStringValue(); | ||
124 | } | 126 | } |
125 | } | 127 | } |
126 | 128 | ||
diff --git a/src/map.h b/src/map.h index 7300e76..48c0223 100644 --- a/src/map.h +++ b/src/map.h | |||
@@ -28,6 +28,7 @@ struct Prototype { | |||
28 | Direction dir = Direction::down; | 28 | Direction dir = Direction::down; |
29 | std::string interactionScript; | 29 | std::string interactionScript; |
30 | std::string bumpPlayerScript; | 30 | std::string bumpPlayerScript; |
31 | std::string backgroundScript; | ||
31 | bool shadow = false; | 32 | bool shadow = false; |
32 | bool wander = false; | 33 | bool wander = false; |
33 | int movementSpeed = -1; | 34 | int movementSpeed = -1; |
diff --git a/src/script_system.cpp b/src/script_system.cpp index b4a7b9b..c820ecb 100644 --- a/src/script_system.cpp +++ b/src/script_system.cpp | |||
@@ -269,6 +269,12 @@ void ScriptSystem::tick(double dt) { | |||
269 | }); | 269 | }); |
270 | } | 270 | } |
271 | 271 | ||
272 | void ScriptSystem::destroySprite(int spriteId) { | ||
273 | scripts_.remove_if([spriteId] (const Script& script) { | ||
274 | return (script.linkedSprite == spriteId); | ||
275 | }); | ||
276 | } | ||
277 | |||
272 | void ScriptSystem::loadMapScripts(std::string mapName) { | 278 | void ScriptSystem::loadMapScripts(std::string mapName) { |
273 | if (!loadedScripts_.count(mapName)) { | 279 | if (!loadedScripts_.count(mapName)) { |
274 | engine_.script_file("../res/scripts/" + mapName + ".lua"); | 280 | engine_.script_file("../res/scripts/" + mapName + ".lua"); |
@@ -282,7 +288,7 @@ bool ScriptSystem::mapHasScript(std::string mapName, std::string scriptName) { | |||
282 | return !!engine_.traverse_get<sol::function>(mapName, scriptName); | 288 | return !!engine_.traverse_get<sol::function>(mapName, scriptName); |
283 | } | 289 | } |
284 | 290 | ||
285 | void ScriptSystem::runScript(std::string mapName, std::string scriptName) { | 291 | void ScriptSystem::runScript(std::string mapName, std::string scriptName, int linkedSprite) { |
286 | loadMapScripts(mapName); | 292 | loadMapScripts(mapName); |
287 | 293 | ||
288 | Script newScript; | 294 | Script newScript; |
@@ -291,6 +297,7 @@ void ScriptSystem::runScript(std::string mapName, std::string scriptName) { | |||
291 | #ifdef TANETANE_DEBUG | 297 | #ifdef TANETANE_DEBUG |
292 | newScript.debugInfo = mapName + "." + scriptName; | 298 | newScript.debugInfo = mapName + "." + scriptName; |
293 | #endif | 299 | #endif |
300 | newScript.linkedSprite = linkedSprite; | ||
294 | 301 | ||
295 | if (!*newScript.callable) { | 302 | if (!*newScript.callable) { |
296 | throw std::runtime_error("Error running script: " + mapName + "." + scriptName); | 303 | throw std::runtime_error("Error running script: " + mapName + "." + scriptName); |
diff --git a/src/script_system.h b/src/script_system.h index 6a28430..0718e90 100644 --- a/src/script_system.h +++ b/src/script_system.h | |||
@@ -19,9 +19,11 @@ public: | |||
19 | 19 | ||
20 | void tick(double dt) override; | 20 | void tick(double dt) override; |
21 | 21 | ||
22 | void destroySprite(int spriteId) override; | ||
23 | |||
22 | bool mapHasScript(std::string mapName, std::string scriptName); | 24 | bool mapHasScript(std::string mapName, std::string scriptName); |
23 | 25 | ||
24 | void runScript(std::string mapName, std::string scriptName); | 26 | void runScript(std::string mapName, std::string scriptName, int linkedSprite = -1); |
25 | 27 | ||
26 | void runDebugScript(std::string script); | 28 | void runDebugScript(std::string script); |
27 | 29 | ||
@@ -33,6 +35,7 @@ private: | |||
33 | std::unique_ptr<sol::thread> runner; | 35 | std::unique_ptr<sol::thread> runner; |
34 | std::unique_ptr<sol::coroutine> callable; | 36 | std::unique_ptr<sol::coroutine> callable; |
35 | std::string debugInfo; | 37 | std::string debugInfo; |
38 | int linkedSprite = -1; | ||
36 | }; | 39 | }; |
37 | 40 | ||
38 | Game& game_; | 41 | Game& game_; |