diff options
| author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-02-10 12:11:27 -0500 |
|---|---|---|
| committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-02-10 12:11:27 -0500 |
| commit | 31dc0e443efc0aae48e11a0d98e699dbed5c7dcf (patch) | |
| tree | 17e044698531fc1af82dddcbd144ae93d3ae0252 | |
| parent | fb58cb31804da7e5e40fc861927060a183962441 (diff) | |
| download | tanetane-31dc0e443efc0aae48e11a0d98e699dbed5c7dcf.tar.gz tanetane-31dc0e443efc0aae48e11a0d98e699dbed5c7dcf.tar.bz2 tanetane-31dc0e443efc0aae48e11a0d98e699dbed5c7dcf.zip | |
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.
| -rw-r--r-- | res/maps/map1.tmx | 5 | ||||
| -rw-r--r-- | res/maps/map2.tmx | 7 | ||||
| -rw-r--r-- | res/scripts/common.lua | 25 | ||||
| -rw-r--r-- | res/scripts/map1_off_right.lua | 8 | ||||
| -rw-r--r-- | res/scripts/map2_off_left.lua | 3 | ||||
| -rw-r--r-- | src/game.h | 5 | ||||
| -rw-r--r-- | src/main.cpp | 2 | ||||
| -rw-r--r-- | src/renderer.cpp | 6 | ||||
| -rw-r--r-- | src/script_system.cpp | 10 |
9 files changed, 59 insertions, 12 deletions
| 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 @@ | |||
| 1 | <?xml version="1.0" encoding="UTF-8"?> | 1 | <?xml version="1.0" encoding="UTF-8"?> |
| 2 | <map version="1.4" tiledversion="1.4.3" orientation="orthogonal" renderorder="right-down" width="48" height="32" tilewidth="16" tileheight="16" infinite="0" nextlayerid="4" nextobjectid="9"> | 2 | <map version="1.4" tiledversion="1.4.3" orientation="orthogonal" renderorder="right-down" width="48" height="32" tilewidth="16" tileheight="16" infinite="0" nextlayerid="4" nextobjectid="10"> |
| 3 | <tileset firstgid="1" name="fromRom" tilewidth="16" tileheight="16" tilecount="240" columns="10"> | 3 | <tileset firstgid="1" name="fromRom" tilewidth="16" tileheight="16" tilecount="240" columns="10"> |
| 4 | <image source="map1_tiles.png" width="160" height="384"/> | 4 | <image source="map1_tiles.png" width="160" height="384"/> |
| 5 | <tile id="77"> | 5 | <tile id="77"> |
| @@ -784,6 +784,9 @@ | |||
| 784 | <property name="script" value="map1_off_right"/> | 784 | <property name="script" value="map1_off_right"/> |
| 785 | </properties> | 785 | </properties> |
| 786 | </object> | 786 | </object> |
| 787 | <object id="9" name="fromRight" type="warp" x="748.667" y="382.667"> | ||
| 788 | <point/> | ||
| 789 | </object> | ||
| 787 | </objectgroup> | 790 | </objectgroup> |
| 788 | <layer id="1" name="Layer 0" width="48" height="32"> | 791 | <layer id="1" name="Layer 0" width="48" height="32"> |
| 789 | <data encoding="csv"> | 792 | <data encoding="csv"> |
| 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 @@ | |||
| 1 | <?xml version="1.0" encoding="UTF-8"?> | 1 | <?xml version="1.0" encoding="UTF-8"?> |
| 2 | <map version="1.4" tiledversion="1.4.3" orientation="orthogonal" renderorder="right-down" width="64" height="64" tilewidth="16" tileheight="16" infinite="0" nextlayerid="3" nextobjectid="2"> | 2 | <map version="1.4" tiledversion="1.4.3" orientation="orthogonal" renderorder="right-down" width="64" height="64" tilewidth="16" tileheight="16" infinite="0" nextlayerid="3" nextobjectid="3"> |
| 3 | <tileset firstgid="1" name="fromRom" tilewidth="16" tileheight="16" tilecount="180" columns="10"> | 3 | <tileset firstgid="1" name="fromRom" tilewidth="16" tileheight="16" tilecount="180" columns="10"> |
| 4 | <image source="map2_tiles.png" width="160" height="288"/> | 4 | <image source="map2_tiles.png" width="160" height="288"/> |
| 5 | <tile id="61"> | 5 | <tile id="61"> |
| @@ -640,6 +640,11 @@ | |||
| 640 | <object id="1" name="fromLeft" type="warp" x="16" y="364.667"> | 640 | <object id="1" name="fromLeft" type="warp" x="16" y="364.667"> |
| 641 | <point/> | 641 | <point/> |
| 642 | </object> | 642 | </object> |
| 643 | <object id="2" name="moveToLeft" type="trigger" x="-20.3333" y="304" width="22.6667" height="91.6667"> | ||
| 644 | <properties> | ||
| 645 | <property name="script" value="map2_off_left"/> | ||
| 646 | </properties> | ||
| 647 | </object> | ||
| 643 | </objectgroup> | 648 | </objectgroup> |
| 644 | <layer id="1" name="Layer 0" width="64" height="64"> | 649 | <layer id="1" name="Layer 0" width="64" height="64"> |
| 645 | <data encoding="csv"> | 650 | <data encoding="csv"> |
| 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 | |||
| 38 | function StopSound(soundId) | 38 | function StopSound(soundId) |
| 39 | mixer():stopChannel(soundId) | 39 | mixer():stopChannel(soundId) |
| 40 | end | 40 | end |
| 41 | |||
| 42 | function FadeToBlack(length) | ||
| 43 | local progress = 0.0 | ||
| 44 | while progress < length do | ||
| 45 | progress = progress + coroutine.yield() | ||
| 46 | setFadeoutProgress(progress / length) | ||
| 47 | end | ||
| 48 | setFadeoutProgress(1.0) | ||
| 49 | end | ||
| 50 | |||
| 51 | function RemoveFadeout(length) | ||
| 52 | local progress = length | ||
| 53 | while progress > 0 do | ||
| 54 | progress = progress - coroutine.yield() | ||
| 55 | setFadeoutProgress(progress / length) | ||
| 56 | end | ||
| 57 | setFadeoutProgress(0.0) | ||
| 58 | end | ||
| 59 | |||
| 60 | function ChangeMap(map, warp) | ||
| 61 | FadeToBlack(150) | ||
| 62 | loadMap("../res/maps/" .. map .. ".tmx", warp) | ||
| 63 | coroutine.yield() | ||
| 64 | RemoveFadeout(150) | ||
| 65 | 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 @@ | |||
| 1 | function map1_off_right() | 1 | function map1_off_right() |
| 2 | -- Because this script gets triggered within the CharacterSystem's tick method, | 2 | ChangeMap("map2", "fromLeft") |
| 3 | -- if we immediately cleared out the entities from the old map, we might end up | ||
| 4 | -- returning there and processing stale data. This yield here ensures that we | ||
| 5 | -- are no longer in the CharacterSystem, because processing gets picked back up | ||
| 6 | -- in the ScriptSystem. | ||
| 7 | coroutine.yield() | ||
| 8 | loadMap("../res/maps/map2.tmx", "fromLeft") | ||
| 9 | end | 3 | 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 @@ | |||
| 1 | function map2_off_left() | ||
| 2 | ChangeMap("map1", "fromRight") | ||
| 3 | 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: | |||
| 79 | 79 | ||
| 80 | const Font& getFont() const { return font_; } | 80 | const Font& getFont() const { return font_; } |
| 81 | 81 | ||
| 82 | double getFadeoutProgress() const { return fadeout_; } | ||
| 83 | |||
| 84 | void setFadeoutProgress(double val) { fadeout_ = val; } | ||
| 85 | |||
| 82 | private: | 86 | private: |
| 83 | 87 | ||
| 84 | void clearSprites(); | 88 | void clearSprites(); |
| @@ -94,6 +98,7 @@ private: | |||
| 94 | std::map<std::string, int> spritesByAlias_; | 98 | std::map<std::string, int> spritesByAlias_; |
| 95 | std::unique_ptr<Map> map_; | 99 | std::unique_ptr<Map> map_; |
| 96 | Font font_; | 100 | Font font_; |
| 101 | double fadeout_ = 0.0; | ||
| 97 | }; | 102 | }; |
| 98 | 103 | ||
| 99 | #endif /* end of include guard: GAME_H_E6F1396E */ | 104 | #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 @@ | |||
| 14 | 14 | ||
| 15 | void loop(Renderer& renderer) { | 15 | void loop(Renderer& renderer) { |
| 16 | Game game(renderer); | 16 | Game game(renderer); |
| 17 | game.emplaceSystem<ScriptSystem>(); | ||
| 17 | game.emplaceSystem<TransformSystem>(); | 18 | game.emplaceSystem<TransformSystem>(); |
| 18 | game.emplaceSystem<InputSystem>(); | 19 | game.emplaceSystem<InputSystem>(); |
| 19 | game.emplaceSystem<CharacterSystem>(); | 20 | game.emplaceSystem<CharacterSystem>(); |
| 20 | game.emplaceSystem<AnimationSystem>(); | 21 | game.emplaceSystem<AnimationSystem>(); |
| 21 | game.emplaceSystem<CameraSystem>(); | 22 | game.emplaceSystem<CameraSystem>(); |
| 22 | game.emplaceSystem<MessageSystem>(); | 23 | game.emplaceSystem<MessageSystem>(); |
| 23 | game.emplaceSystem<ScriptSystem>(); | ||
| 24 | 24 | ||
| 25 | game.loadMap("../res/maps/map1.tmx", "spawn"); | 25 | game.loadMap("../res/maps/map1.tmx", "spawn"); |
| 26 | 26 | ||
| 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) { | |||
| 234 | } | 234 | } |
| 235 | } | 235 | } |
| 236 | 236 | ||
| 237 | if (game.getFadeoutProgress() > 0.0) { | ||
| 238 | SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_BLEND); | ||
| 239 | SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, game.getFadeoutProgress() * 255); | ||
| 240 | SDL_RenderFillRect(ren_.get(), nullptr); | ||
| 241 | } | ||
| 242 | |||
| 237 | SDL_SetRenderTarget(ren_.get(), nullptr); | 243 | SDL_SetRenderTarget(ren_.get(), nullptr); |
| 238 | SDL_RenderCopy(ren_.get(), cameraTex.get(), nullptr, nullptr); | 244 | SDL_RenderCopy(ren_.get(), cameraTex.get(), nullptr, nullptr); |
| 239 | SDL_RenderPresent(ren_.get()); | 245 | 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) { | |||
| 56 | game_.loadMap(filename, warpPoint); | 56 | game_.loadMap(filename, warpPoint); |
| 57 | }); | 57 | }); |
| 58 | 58 | ||
| 59 | engine_.set_function( | ||
| 60 | "setFadeoutProgress", | ||
| 61 | [&] (double val) { | ||
| 62 | game_.setFadeoutProgress(val); | ||
| 63 | }); | ||
| 64 | |||
| 59 | engine_.script_file("../res/scripts/common.lua"); | 65 | engine_.script_file("../res/scripts/common.lua"); |
| 60 | } | 66 | } |
| 61 | 67 | ||
| @@ -64,7 +70,7 @@ void ScriptSystem::tick(double dt) { | |||
| 64 | auto result = (*callable_)(dt); | 70 | auto result = (*callable_)(dt); |
| 65 | if (!result.valid()) { | 71 | if (!result.valid()) { |
| 66 | sol::error e = result; | 72 | sol::error e = result; |
| 67 | throw std::runtime_error(e.what()); | 73 | throw e; |
| 68 | } | 74 | } |
| 69 | 75 | ||
| 70 | if (!*callable_) { | 76 | if (!*callable_) { |
| @@ -90,7 +96,7 @@ void ScriptSystem::runScript(std::string name) { | |||
| 90 | auto result = (*callable_)(); | 96 | auto result = (*callable_)(); |
| 91 | if (!result.valid()) { | 97 | if (!result.valid()) { |
| 92 | sol::error e = result; | 98 | sol::error e = result; |
| 93 | throw std::runtime_error(e.what()); | 99 | throw e; |
| 94 | } | 100 | } |
| 95 | 101 | ||
| 96 | if (!*callable_) { | 102 | if (!*callable_) { |
