From 31dc0e443efc0aae48e11a0d98e699dbed5c7dcf Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Wed, 10 Feb 2021 12:11:27 -0500 Subject: Added fade out around map change Also moved script system first in the loop so that the camera can catch up before rendering. Also added a map change from map 2 back to map 1. --- res/maps/map1.tmx | 5 ++++- res/maps/map2.tmx | 7 ++++++- res/scripts/common.lua | 25 +++++++++++++++++++++++++ res/scripts/map1_off_right.lua | 8 +------- res/scripts/map2_off_left.lua | 3 +++ src/game.h | 5 +++++ src/main.cpp | 2 +- src/renderer.cpp | 6 ++++++ src/script_system.cpp | 10 ++++++++-- 9 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 res/scripts/map2_off_left.lua diff --git a/res/maps/map1.tmx b/res/maps/map1.tmx index 282b2ed..8afb909 100644 --- a/res/maps/map1.tmx +++ b/res/maps/map1.tmx @@ -1,5 +1,5 @@ - + @@ -784,6 +784,9 @@ + + + diff --git a/res/maps/map2.tmx b/res/maps/map2.tmx index 5bbe0c6..0f2e624 100644 --- a/res/maps/map2.tmx +++ b/res/maps/map2.tmx @@ -1,5 +1,5 @@ - + @@ -640,6 +640,11 @@ + + + + + diff --git a/res/scripts/common.lua b/res/scripts/common.lua index e7af1cf..f43b2e0 100644 --- a/res/scripts/common.lua +++ b/res/scripts/common.lua @@ -38,3 +38,28 @@ end function StopSound(soundId) mixer():stopChannel(soundId) end + +function FadeToBlack(length) + local progress = 0.0 + while progress < length do + progress = progress + coroutine.yield() + setFadeoutProgress(progress / length) + end + setFadeoutProgress(1.0) +end + +function RemoveFadeout(length) + local progress = length + while progress > 0 do + progress = progress - coroutine.yield() + setFadeoutProgress(progress / length) + end + setFadeoutProgress(0.0) +end + +function ChangeMap(map, warp) + FadeToBlack(150) + loadMap("../res/maps/" .. map .. ".tmx", warp) + coroutine.yield() + RemoveFadeout(150) +end diff --git a/res/scripts/map1_off_right.lua b/res/scripts/map1_off_right.lua index 026c32e..7dd0113 100644 --- a/res/scripts/map1_off_right.lua +++ b/res/scripts/map1_off_right.lua @@ -1,9 +1,3 @@ function map1_off_right() - -- Because this script gets triggered within the CharacterSystem's tick method, - -- if we immediately cleared out the entities from the old map, we might end up - -- returning there and processing stale data. This yield here ensures that we - -- are no longer in the CharacterSystem, because processing gets picked back up - -- in the ScriptSystem. - coroutine.yield() - loadMap("../res/maps/map2.tmx", "fromLeft") + ChangeMap("map2", "fromLeft") end diff --git a/res/scripts/map2_off_left.lua b/res/scripts/map2_off_left.lua new file mode 100644 index 0000000..d4d9ce4 --- /dev/null +++ b/res/scripts/map2_off_left.lua @@ -0,0 +1,3 @@ +function map2_off_left() + ChangeMap("map1", "fromRight") +end diff --git a/src/game.h b/src/game.h index 8ea7576..bb88dec 100644 --- a/src/game.h +++ b/src/game.h @@ -79,6 +79,10 @@ public: const Font& getFont() const { return font_; } + double getFadeoutProgress() const { return fadeout_; } + + void setFadeoutProgress(double val) { fadeout_ = val; } + private: void clearSprites(); @@ -94,6 +98,7 @@ private: std::map spritesByAlias_; std::unique_ptr map_; Font font_; + double fadeout_ = 0.0; }; #endif /* end of include guard: GAME_H_E6F1396E */ diff --git a/src/main.cpp b/src/main.cpp index 5f28408..8487077 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,13 +14,13 @@ void loop(Renderer& renderer) { Game game(renderer); + game.emplaceSystem(); game.emplaceSystem(); game.emplaceSystem(); game.emplaceSystem(); game.emplaceSystem(); game.emplaceSystem(); game.emplaceSystem(); - game.emplaceSystem(); game.loadMap("../res/maps/map1.tmx", "spawn"); diff --git a/src/renderer.cpp b/src/renderer.cpp index e9db413..db5daed 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -234,6 +234,12 @@ void Renderer::render(Game& game) { } } + if (game.getFadeoutProgress() > 0.0) { + SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_BLEND); + SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, game.getFadeoutProgress() * 255); + SDL_RenderFillRect(ren_.get(), nullptr); + } + SDL_SetRenderTarget(ren_.get(), nullptr); SDL_RenderCopy(ren_.get(), cameraTex.get(), nullptr, nullptr); SDL_RenderPresent(ren_.get()); diff --git a/src/script_system.cpp b/src/script_system.cpp index 6e38905..a5e9403 100644 --- a/src/script_system.cpp +++ b/src/script_system.cpp @@ -56,6 +56,12 @@ ScriptSystem::ScriptSystem(Game& game) : game_(game) { game_.loadMap(filename, warpPoint); }); + engine_.set_function( + "setFadeoutProgress", + [&] (double val) { + game_.setFadeoutProgress(val); + }); + engine_.script_file("../res/scripts/common.lua"); } @@ -64,7 +70,7 @@ void ScriptSystem::tick(double dt) { auto result = (*callable_)(dt); if (!result.valid()) { sol::error e = result; - throw std::runtime_error(e.what()); + throw e; } if (!*callable_) { @@ -90,7 +96,7 @@ void ScriptSystem::runScript(std::string name) { auto result = (*callable_)(); if (!result.valid()) { sol::error e = result; - throw std::runtime_error(e.what()); + throw e; } if (!*callable_) { -- cgit 1.4.1