From 5ecd0f428dd8292a17c5013c525a4f5d3967acb8 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Fri, 12 Feb 2021 00:33:19 -0500 Subject: Scripts are organised per-map now --- src/character_system.cpp | 4 ++-- src/input_system.cpp | 6 +++--- src/main.cpp | 2 +- src/map.cpp | 7 ++++--- src/map.h | 6 +++--- src/script_system.cpp | 12 ++++++------ src/script_system.h | 2 +- 7 files changed, 20 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/character_system.cpp b/src/character_system.cpp index aad8c1b..a0572b0 100644 --- a/src/character_system.cpp +++ b/src/character_system.cpp @@ -92,7 +92,7 @@ void CharacterSystem::tick(double dt) { Sprite& collider = game_.getSprite(collision.horiz.colliderSprite); if (collider.walkthroughScript != "") { - game_.getSystem().runScript(collider.walkthroughScript); + game_.getSystem().runScript(game_.getMap().getName(), collider.walkthroughScript); } } @@ -102,7 +102,7 @@ void CharacterSystem::tick(double dt) { Sprite& collider = game_.getSprite(collision.vert.colliderSprite); if (collider.walkthroughScript != "") { - game_.getSystem().runScript(collider.walkthroughScript); + game_.getSystem().runScript(game_.getMap().getName(), collider.walkthroughScript); } } diff --git a/src/input_system.cpp b/src/input_system.cpp index 6ee442a..2201907 100644 --- a/src/input_system.cpp +++ b/src/input_system.cpp @@ -54,7 +54,7 @@ void InputSystem::tick(double dt) { Sprite& collider = game_.getSprite(collision.horiz.colliderSprite); if (collider.interactionScript != "") { - game_.getSystem().runScript(collider.interactionScript); + game_.getSystem().runScript(game_.getMap().getName(), collider.interactionScript); activated = true; } } else if (collision.vert.colliderSprite != -1) { @@ -62,7 +62,7 @@ void InputSystem::tick(double dt) { Sprite& collider = game_.getSprite(collision.vert.colliderSprite); if (collider.interactionScript != "") { - game_.getSystem().runScript(collider.interactionScript); + game_.getSystem().runScript(game_.getMap().getName(), collider.interactionScript); activated = true; } } @@ -70,7 +70,7 @@ void InputSystem::tick(double dt) { } if (inFrontOfSomething && !activated) { - game_.getSystem().runScript("default"); + game_.getSystem().runScript("global", "no_problem_here"); } } } else if (e.key.keysym.sym == SDLK_LEFT) { diff --git a/src/main.cpp b/src/main.cpp index 387f7e3..c9bfaf1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,7 +22,7 @@ void loop(Renderer& renderer) { game.emplaceSystem(); game.emplaceSystem(); - game.loadMap("../res/maps/map1.tmx", "spawn", Direction::down); + game.loadMap("map1", "spawn", Direction::down); game.getSprite(game.getSpriteByAlias("lucas")).controllable = true; renderer.render(game); diff --git a/src/map.cpp b/src/map.cpp index 99711c6..8d4aa2d 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -5,10 +5,11 @@ #include #include -Map::Map(std::string_view filename) : filename_(filename) { +Map::Map(std::string_view name) : name_(name) { + std::string filename = "../res/maps/" + std::string(name) + ".tmx"; tmx::Map mapfile; - if (!mapfile.load(filename.data())) { - throw std::invalid_argument("Could not find map file: " + std::string(filename)); + if (!mapfile.load(filename.c_str())) { + throw std::invalid_argument("Could not find map file: " + filename); } const tmx::Vector2u& mapSize = mapfile.getTileCount(); diff --git a/src/map.h b/src/map.h index adf9b80..2d448e4 100644 --- a/src/map.h +++ b/src/map.h @@ -35,9 +35,9 @@ struct Trigger { class Map { public: - explicit Map(std::string_view filename); + explicit Map(std::string_view name); - const std::string& getName() const { return filename_; } + const std::string& getName() const { return name_; } const vec2i& getMapSize() const { return mapSize_; } @@ -61,7 +61,7 @@ public: private: - std::string filename_; + std::string name_; vec2i mapSize_; vec2i tileSize_; std::vector> layers_; diff --git a/src/script_system.cpp b/src/script_system.cpp index 21d4090..5b9b987 100644 --- a/src/script_system.cpp +++ b/src/script_system.cpp @@ -120,17 +120,17 @@ void ScriptSystem::tick(double dt) { } } -void ScriptSystem::runScript(std::string name) { - if (!loadedScripts_.count(name)) { - engine_.script_file("../res/scripts/" + name + ".lua"); - loadedScripts_.insert(name); +void ScriptSystem::runScript(std::string mapName, std::string scriptName) { + if (!loadedScripts_.count(mapName)) { + engine_.script_file("../res/scripts/" + mapName + ".lua"); + loadedScripts_.insert(mapName); } runner_.reset(new sol::thread(sol::thread::create(engine_.lua_state()))); - callable_.reset(new sol::coroutine(runner_->state().get(name))); + callable_.reset(new sol::coroutine(runner_->state().traverse_get(mapName, scriptName))); if (!*callable_) { - throw std::runtime_error("Error running script: " + name); + throw std::runtime_error("Error running script: " + mapName + "." + scriptName); } auto result = (*callable_)(); diff --git a/src/script_system.h b/src/script_system.h index 2832576..9c901b7 100644 --- a/src/script_system.h +++ b/src/script_system.h @@ -18,7 +18,7 @@ public: void tick(double dt) override; - void runScript(std::string name); + void runScript(std::string mapName, std::string scriptName); private: -- cgit 1.4.1