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 --- res/maps/map1.tmx | 2 +- res/maps/map2.tmx | 2 +- res/scripts/common.lua | 2 +- res/scripts/default.lua | 5 ----- res/scripts/global.lua | 7 +++++++ res/scripts/map1.lua | 38 ++++++++++++++++++++++++++++++++++++++ res/scripts/map1_off_right.lua | 3 --- res/scripts/map2.lua | 5 +++++ res/scripts/map2_off_left.lua | 3 --- res/scripts/script0001.lua | 24 ------------------------ res/scripts/test_trigger.lua | 7 ------- 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 +- 18 files changed, 73 insertions(+), 64 deletions(-) delete mode 100644 res/scripts/default.lua create mode 100644 res/scripts/global.lua create mode 100644 res/scripts/map1.lua delete mode 100644 res/scripts/map1_off_right.lua create mode 100644 res/scripts/map2.lua delete mode 100644 res/scripts/map2_off_left.lua delete mode 100644 res/scripts/script0001.lua delete mode 100644 res/scripts/test_trigger.lua diff --git a/res/maps/map1.tmx b/res/maps/map1.tmx index 8afb909..e1a9049 100644 --- a/res/maps/map1.tmx +++ b/res/maps/map1.tmx @@ -781,7 +781,7 @@ - + diff --git a/res/maps/map2.tmx b/res/maps/map2.tmx index c1d2f2d..43fa850 100644 --- a/res/maps/map2.tmx +++ b/res/maps/map2.tmx @@ -642,7 +642,7 @@ - + diff --git a/res/scripts/common.lua b/res/scripts/common.lua index 2023e63..3c412c6 100644 --- a/res/scripts/common.lua +++ b/res/scripts/common.lua @@ -112,7 +112,7 @@ function ChangeMap(map, warp) playerSprite.controllable = false FadeToBlack(150) - loadMap("../res/maps/" .. map .. ".tmx", warp, direction) + loadMap(map, warp, direction) local newPlayerId = getPlayerSprite() local newPlayerSprite = getSprite(newPlayerId) diff --git a/res/scripts/default.lua b/res/scripts/default.lua deleted file mode 100644 index b8f7768..0000000 --- a/res/scripts/default.lua +++ /dev/null @@ -1,5 +0,0 @@ -function default() - StartCutscene() - DisplayMessage("* No problem here.", "", SpeakerType.NONE) - HideCutsceneBars() -end diff --git a/res/scripts/global.lua b/res/scripts/global.lua new file mode 100644 index 0000000..48084c8 --- /dev/null +++ b/res/scripts/global.lua @@ -0,0 +1,7 @@ +global = {} + +function global.no_problem_here() + StartCutscene() + DisplayMessage("* No problem here.", "", SpeakerType.NONE) + HideCutsceneBars() +end diff --git a/res/scripts/map1.lua b/res/scripts/map1.lua new file mode 100644 index 0000000..7d6014a --- /dev/null +++ b/res/scripts/map1.lua @@ -0,0 +1,38 @@ +map1 = {} + +function map1.script0001() + StartCutscene() + SetAnimation("boney", "barking") + local barkingNoise = LoopSound("barking_at_hallucination.wav") + + SetAnimation("flint", "talk") + DisplayMessage("* Lucas. It's me, Flint. Your father.\n\f* I found Claus. He's here. After three years I've finally found your brother.\n\fAre you ready to see him again?", "Flint", SpeakerType.MAN) + ShowChoice("Yes", "No") + WaitForEndOfMessage() + + SetAnimation("boney", "crouch") + StopSound(barkingNoise) + PlaySound("boney_growl.wav") + + if GetChoiceSelection() == 0 then + DisplayMessage("* I can hear the tremor in your voice, Lucas.\n\fLies are little games a mouth plays while the person attached withers away slowly.\n\fClaus is coming, whether you like it or not.\nWill you be able to look him in the eye?", "Flint", SpeakerType.MAN) + else + DisplayMessage("* Look at me when I'm talking to you, Lucas.\n\f* When Claus gets home, we won't need you anymore.\nYou're nothing compared to him.", "Flint", SpeakerType.MAN) + end + + WaitForEndOfMessage() + SetAnimation("flint", "still") + HideCutsceneBars() +end + +function map1.off_right() + ChangeMap("map2", "fromLeft") +end + +function map1.test_trigger() + StartCutscene() + PlaySound("boney_growl.wav") + DisplayMessage("* Hi! Welcome to the funky zone.", "", SpeakerType.NONE) + WaitForEndOfMessage() + HideCutsceneBars() +end diff --git a/res/scripts/map1_off_right.lua b/res/scripts/map1_off_right.lua deleted file mode 100644 index 7dd0113..0000000 --- a/res/scripts/map1_off_right.lua +++ /dev/null @@ -1,3 +0,0 @@ -function map1_off_right() - ChangeMap("map2", "fromLeft") -end diff --git a/res/scripts/map2.lua b/res/scripts/map2.lua new file mode 100644 index 0000000..1f14b31 --- /dev/null +++ b/res/scripts/map2.lua @@ -0,0 +1,5 @@ +map2 = {} + +function map2.off_left() + ChangeMap("map1", "fromRight") +end diff --git a/res/scripts/map2_off_left.lua b/res/scripts/map2_off_left.lua deleted file mode 100644 index d4d9ce4..0000000 --- a/res/scripts/map2_off_left.lua +++ /dev/null @@ -1,3 +0,0 @@ -function map2_off_left() - ChangeMap("map1", "fromRight") -end diff --git a/res/scripts/script0001.lua b/res/scripts/script0001.lua deleted file mode 100644 index bf6a0c2..0000000 --- a/res/scripts/script0001.lua +++ /dev/null @@ -1,24 +0,0 @@ -function script0001() - StartCutscene() - SetAnimation("boney", "barking") - local barkingNoise = LoopSound("barking_at_hallucination.wav") - - SetAnimation("flint", "talk") - DisplayMessage("* Lucas. It's me, Flint. Your father.\n\f* I found Claus. He's here. After three years I've finally found your brother.\n\fAre you ready to see him again?", "Flint", SpeakerType.MAN) - ShowChoice("Yes", "No") - WaitForEndOfMessage() - - SetAnimation("boney", "crouch") - StopSound(barkingNoise) - PlaySound("boney_growl.wav") - - if GetChoiceSelection() == 0 then - DisplayMessage("* I can hear the tremor in your voice, Lucas.\n\fLies are little games a mouth plays while the person attached withers away slowly.\n\fClaus is coming, whether you like it or not.\nWill you be able to look him in the eye?", "Flint", SpeakerType.MAN) - else - DisplayMessage("* Look at me when I'm talking to you, Lucas.\n\f* When Claus gets home, we won't need you anymore.\nYou're nothing compared to him.", "Flint", SpeakerType.MAN) - end - - WaitForEndOfMessage() - SetAnimation("flint", "still") - HideCutsceneBars() -end diff --git a/res/scripts/test_trigger.lua b/res/scripts/test_trigger.lua deleted file mode 100644 index 4d9d06c..0000000 --- a/res/scripts/test_trigger.lua +++ /dev/null @@ -1,7 +0,0 @@ -function test_trigger() - StartCutscene() - PlaySound("boney_growl.wav") - DisplayMessage("* Hi! Welcome to the funky zone.", "", SpeakerType.NONE) - WaitForEndOfMessage() - HideCutsceneBars() -end 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