summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt1
-rw-r--r--res/scripts/common.lua29
-rw-r--r--res/scripts/map2.lua6
-rw-r--r--src/effect_system.cpp35
-rw-r--r--src/effect_system.h60
-rw-r--r--src/game.h5
-rw-r--r--src/main.cpp2
-rw-r--r--src/renderer.cpp52
-rw-r--r--src/renderer.h1
-rw-r--r--src/script_system.cpp21
-rw-r--r--src/system.h3
11 files changed, 190 insertions, 25 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 0e5b3ad..b6d6051 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt
@@ -51,6 +51,7 @@ add_executable(tanetane
51 src/input_system.cpp 51 src/input_system.cpp
52 src/message_system.cpp 52 src/message_system.cpp
53 src/script_system.cpp 53 src/script_system.cpp
54 src/effect_system.cpp
54) 55)
55 56
56set_property(TARGET tanetane PROPERTY CXX_STANDARD 17) 57set_property(TARGET tanetane PROPERTY CXX_STANDARD 17)
diff --git a/res/scripts/common.lua b/res/scripts/common.lua index 75b86a8..2197417 100644 --- a/res/scripts/common.lua +++ b/res/scripts/common.lua
@@ -41,6 +41,7 @@ function StartCutscene()
41 local playerSprite = getSprite(playerId) 41 local playerSprite = getSprite(playerId)
42 playerSprite.controllable = false 42 playerSprite.controllable = false
43 character():halt(playerId) 43 character():halt(playerId)
44 message():displayCutsceneBars()
44end 45end
45 46
46function DisplayMessage(msg, name, type) 47function DisplayMessage(msg, name, type)
@@ -107,21 +108,27 @@ function StopSound(soundId)
107end 108end
108 109
109function FadeToBlack(length) 110function FadeToBlack(length)
110 local progress = 0.0 111 effect():fadeScreen(length, 1.0)
111 while progress < length do 112 repeat
112 progress = progress + coroutine.yield() 113 coroutine.yield()
113 setFadeoutProgress(progress / length) 114 until effect():isScreenFadeComplete()
114 end
115 setFadeoutProgress(1.0)
116end 115end
117 116
118function RemoveFadeout(length) 117function RemoveFadeout(length)
119 local progress = length 118 effect():fadeScreen(length, 0.0)
120 while progress > 0 do 119 repeat
121 progress = progress - coroutine.yield() 120 coroutine.yield()
122 setFadeoutProgress(progress / length) 121 until effect():isScreenFadeComplete()
122end
123
124function FadeMap(length, amount)
125 effect():fadeMap(length, amount)
126end
127
128function WaitForMapFade()
129 while not effect():isMapFadeComplete() do
130 coroutine.yield()
123 end 131 end
124 setFadeoutProgress(0.0)
125end 132end
126 133
127function SetPartyDirection(spriteId, direction) 134function SetPartyDirection(spriteId, direction)
diff --git a/res/scripts/map2.lua b/res/scripts/map2.lua index 8058aa2..12a5df0 100644 --- a/res/scripts/map2.lua +++ b/res/scripts/map2.lua
@@ -26,6 +26,9 @@ end
26 26
27function map2.mailbox_lightning() 27function map2.mailbox_lightning()
28 StartCutscene() 28 StartCutscene()
29 FadeMap(500, 0.5)
30 WaitForMapFade()
31
29 DisplayMessage("* ...?", "", SpeakerType.NONE) 32 DisplayMessage("* ...?", "", SpeakerType.NONE)
30 WaitForEndOfMessage() 33 WaitForEndOfMessage()
31 34
@@ -44,6 +47,9 @@ function map2.mailbox_lightning()
44 SetAnimation("lucas", "lightning_collapse!") 47 SetAnimation("lucas", "lightning_collapse!")
45 WaitForAnimation("lucas") 48 WaitForAnimation("lucas")
46 49
50 FadeMap(500, 0.0)
51 WaitForMapFade()
52
47 DisplayMessage("* It was lightning.\n\fAh.", "", SpeakerType.NONE) 53 DisplayMessage("* It was lightning.\n\fAh.", "", SpeakerType.NONE)
48 WaitForEndOfMessage() 54 WaitForEndOfMessage()
49 HideCutsceneBars() 55 HideCutsceneBars()
diff --git a/src/effect_system.cpp b/src/effect_system.cpp new file mode 100644 index 0000000..3a0afe4 --- /dev/null +++ b/src/effect_system.cpp
@@ -0,0 +1,35 @@
1#include "effect_system.h"
2
3void EffectSystem::tick(double dt) {
4 if (screenFade_ != screenFadeDest_) {
5 screenFadeThus_ += dt;
6 if (screenFadeThus_ >= screenFadeLength_) {
7 screenFadeThus_ = screenFadeLength_;
8 }
9
10 screenFade_ = (screenFadeDest_ - screenFadeStart_) / screenFadeLength_ * screenFadeThus_ + screenFadeStart_;
11 }
12
13 if (mapFade_ != mapFadeDest_) {
14 mapFadeThus_ += dt;
15 if (mapFadeThus_ >= mapFadeLength_) {
16 mapFadeThus_ = mapFadeLength_;
17 }
18
19 mapFade_ = (mapFadeDest_ - mapFadeStart_) / mapFadeLength_ * mapFadeThus_ + mapFadeStart_;
20 }
21}
22
23void EffectSystem::fadeScreen(int length, double amount) {
24 screenFadeStart_ = screenFade_;
25 screenFadeDest_ = amount;
26 screenFadeLength_ = length;
27 screenFadeThus_ = 0;
28}
29
30void EffectSystem::fadeMap(int length, double amount) {
31 mapFadeStart_ = mapFade_;
32 mapFadeDest_ = amount;
33 mapFadeLength_ = length;
34 mapFadeThus_ = 0;
35}
diff --git a/src/effect_system.h b/src/effect_system.h new file mode 100644 index 0000000..2cdcb10 --- /dev/null +++ b/src/effect_system.h
@@ -0,0 +1,60 @@
1#ifndef EFFECT_SYSTEM_H_0B497B39
2#define EFFECT_SYSTEM_H_0B497B39
3
4#include "system.h"
5
6class Game;
7
8class EffectSystem : public System {
9public:
10
11 static constexpr SystemKey Key = SystemKey::Effect;
12
13 explicit EffectSystem(Game& game) : game_(game) {}
14
15 void tick(double dt) override;
16
17 /* Commands */
18
19 // Fades the entire screen in or out
20 // - length is in milliseconds
21 // - amount is [0,1]
22 void fadeScreen(int length, double amount);
23
24 // Fades just the map, leaving the sprites, messages, etc unfaded.
25 // - length is in milliseconds
26 // - amount is [0,1]
27 void fadeMap(int length, double amount);
28
29 /* Information */
30
31 bool isScreenFaded() const { return screenFade_ > 0.0; }
32
33 double getScreenFadeProgress() const { return screenFade_; }
34
35 bool isScreenFadeComplete() const { return screenFade_ == screenFadeDest_; }
36
37 bool isMapFaded() const { return mapFade_ > 0.0; }
38
39 double getMapFadeProgress() const { return mapFade_; }
40
41 bool isMapFadeComplete() const { return mapFade_ == mapFadeDest_; }
42
43private:
44
45 Game& game_;
46
47 double screenFade_ = 0.0;
48 double screenFadeDest_ = 0.0;
49 double screenFadeStart_ = 0.0;
50 double screenFadeLength_ = 0;
51 double screenFadeThus_ = 0;
52
53 double mapFade_ = 0.0;
54 double mapFadeDest_ = 0.0;
55 double mapFadeStart_ = 0.0;
56 double mapFadeLength_ = 0.0;
57 double mapFadeThus_ = 0.0;
58};
59
60#endif /* end of include guard: EFFECT_SYSTEM_H_0B497B39 */
diff --git a/src/game.h b/src/game.h index c7926bf..736fa7a 100644 --- a/src/game.h +++ b/src/game.h
@@ -82,10 +82,6 @@ public:
82 82
83 const Font& getFont() const { return font_; } 83 const Font& getFont() const { return font_; }
84 84
85 double getFadeoutProgress() const { return fadeout_; }
86
87 void setFadeoutProgress(double val) { fadeout_ = val; }
88
89private: 85private:
90 86
91 void clearSprites(); 87 void clearSprites();
@@ -101,7 +97,6 @@ private:
101 std::map<std::string, int> spritesByAlias_; 97 std::map<std::string, int> spritesByAlias_;
102 std::unique_ptr<Map> map_; 98 std::unique_ptr<Map> map_;
103 Font font_; 99 Font font_;
104 double fadeout_ = 0.0;
105}; 100};
106 101
107#endif /* end of include guard: GAME_H_E6F1396E */ 102#endif /* end of include guard: GAME_H_E6F1396E */
diff --git a/src/main.cpp b/src/main.cpp index 592b3be..79fa306 100644 --- a/src/main.cpp +++ b/src/main.cpp
@@ -11,6 +11,7 @@
11#include "input_system.h" 11#include "input_system.h"
12#include "message_system.h" 12#include "message_system.h"
13#include "script_system.h" 13#include "script_system.h"
14#include "effect_system.h"
14 15
15void loop(Renderer& renderer) { 16void loop(Renderer& renderer) {
16 Game game(renderer); 17 Game game(renderer);
@@ -21,6 +22,7 @@ void loop(Renderer& renderer) {
21 game.emplaceSystem<AnimationSystem>(); 22 game.emplaceSystem<AnimationSystem>();
22 game.emplaceSystem<CameraSystem>(); 23 game.emplaceSystem<CameraSystem>();
23 game.emplaceSystem<MessageSystem>(); 24 game.emplaceSystem<MessageSystem>();
25 game.emplaceSystem<EffectSystem>();
24 26
25 game.loadMap("map2", "debugWarp_mailboxes", Direction::down); 27 game.loadMap("map2", "debugWarp_mailboxes", Direction::down);
26 game.getSprite(game.getSpriteByAlias("lucas")).controllable = true; 28 game.getSprite(game.getSpriteByAlias("lucas")).controllable = true;
diff --git a/src/renderer.cpp b/src/renderer.cpp index 0e5f8f1..c80b0d3 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp
@@ -6,6 +6,7 @@
6#include "transform_system.h" 6#include "transform_system.h"
7#include "camera_system.h" 7#include "camera_system.h"
8#include "message_system.h" 8#include "message_system.h"
9#include "effect_system.h"
9 10
10Renderer::Renderer() { 11Renderer::Renderer() {
11 win_ = window_ptr( 12 win_ = window_ptr(
@@ -110,10 +111,22 @@ void Renderer::renderSprite(const Sprite& sprite) {
110} 111}
111 112
112void Renderer::render(Game& game) { 113void Renderer::render(Game& game) {
114 auto& effects = game.getSystem<EffectSystem>();
115
113 if (cachedMapName_ != game.getMap().getName()) { 116 if (cachedMapName_ != game.getMap().getName()) {
114 cachedMapName_ = game.getMap().getName(); 117 cachedMapName_ = game.getMap().getName();
115 renLay1_ = renderMapLayer(game.getMap(), 0); 118 renLay1_ = renderMapLayer(game.getMap(), 0);
116 renLay0_ = renderMapLayer(game.getMap(), 1); 119 renLay0_ = renderMapLayer(game.getMap(), 1);
120
121 vec2i mapBounds = game.getMap().getMapSize() * game.getMap().getTileSize();
122 mapSwapTex_.reset(SDL_CreateTexture(
123 ren_.get(),
124 SDL_PIXELFORMAT_RGBA8888,
125 SDL_TEXTUREACCESS_TARGET,
126 mapBounds.w(),
127 mapBounds.h()));
128
129 SDL_SetTextureBlendMode(mapSwapTex_.get(), SDL_BLENDMODE_BLEND);
117 } 130 }
118 131
119 vec2i mapBounds = game.getMap().getMapSize() * game.getMap().getTileSize(); 132 vec2i mapBounds = game.getMap().getMapSize() * game.getMap().getTileSize();
@@ -135,14 +148,48 @@ void Renderer::render(Game& game) {
135 SDL_SetRenderDrawColor(ren_.get(), 255, 255, 255, 255); 148 SDL_SetRenderDrawColor(ren_.get(), 255, 255, 255, 255);
136 SDL_RenderClear(ren_.get()); 149 SDL_RenderClear(ren_.get());
137 150
151 // Render lower map layer
138 SDL_RenderCopy(ren_.get(), renLay1_.get(), nullptr, nullptr); 152 SDL_RenderCopy(ren_.get(), renLay1_.get(), nullptr, nullptr);
139 153
154 if (effects.isMapFaded()) {
155 SDL_SetRenderTarget(ren_.get(), mapSwapTex_.get());
156 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0);
157 SDL_RenderClear(ren_.get());
158 SDL_RenderCopy(ren_.get(), renLay1_.get(), nullptr, nullptr);
159
160 SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_MOD);
161 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 255);
162 SDL_RenderFillRect(ren_.get(), nullptr);
163
164 SDL_SetRenderTarget(ren_.get(), canvas.get());
165 SDL_SetTextureAlphaMod(mapSwapTex_.get(), effects.getMapFadeProgress() * 255);
166 SDL_RenderCopy(ren_.get(), mapSwapTex_.get(), nullptr, nullptr);
167 }
168
169 // Render normal sprite layer
140 for (const Sprite& sprite : game.getSystem<TransformSystem>().getSpritesByY(SpriteLayer::Normal) | game.spriteView()) { 170 for (const Sprite& sprite : game.getSystem<TransformSystem>().getSpritesByY(SpriteLayer::Normal) | game.spriteView()) {
141 renderSprite(sprite); 171 renderSprite(sprite);
142 } 172 }
143 173
174 // Render upper map layer
144 SDL_RenderCopy(ren_.get(), renLay0_.get(), nullptr, nullptr); 175 SDL_RenderCopy(ren_.get(), renLay0_.get(), nullptr, nullptr);
145 176
177 if (effects.isMapFaded()) {
178 SDL_SetRenderTarget(ren_.get(), mapSwapTex_.get());
179 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0);
180 SDL_RenderClear(ren_.get());
181 SDL_RenderCopy(ren_.get(), renLay0_.get(), nullptr, nullptr);
182
183 SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_MOD);
184 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 255);
185 SDL_RenderFillRect(ren_.get(), nullptr);
186
187 SDL_SetRenderTarget(ren_.get(), canvas.get());
188 SDL_SetTextureAlphaMod(mapSwapTex_.get(), effects.getMapFadeProgress() * 255);
189 SDL_RenderCopy(ren_.get(), mapSwapTex_.get(), nullptr, nullptr);
190 }
191
192 // Render above sprite layer
146 for (const Sprite& sprite : game.getSystem<TransformSystem>().getSpritesByY(SpriteLayer::Above) | game.spriteView()) { 193 for (const Sprite& sprite : game.getSystem<TransformSystem>().getSpritesByY(SpriteLayer::Above) | game.spriteView()) {
147 renderSprite(sprite); 194 renderSprite(sprite);
148 } 195 }
@@ -170,6 +217,7 @@ void Renderer::render(Game& game) {
170 SDL_RenderCopy(ren_.get(), canvas.get(), &cameraField, nullptr); 217 SDL_RenderCopy(ren_.get(), canvas.get(), &cameraField, nullptr);
171 218
172 if (game.getSystem<MessageSystem>().getCutsceneBarsProgress() > 0) { 219 if (game.getSystem<MessageSystem>().getCutsceneBarsProgress() > 0) {
220 SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE);
173 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 255); 221 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 255);
174 222
175 int topBarHeight = 16.0 * game.getSystem<MessageSystem>().getCutsceneBarsProgress(); 223 int topBarHeight = 16.0 * game.getSystem<MessageSystem>().getCutsceneBarsProgress();
@@ -277,9 +325,9 @@ void Renderer::render(Game& game) {
277 } 325 }
278 } 326 }
279 327
280 if (game.getFadeoutProgress() > 0.0) { 328 if (effects.isScreenFaded()) {
281 SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_BLEND); 329 SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_BLEND);
282 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, game.getFadeoutProgress() * 255); 330 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, effects.getScreenFadeProgress() * 255);
283 SDL_RenderFillRect(ren_.get(), nullptr); 331 SDL_RenderFillRect(ren_.get(), nullptr);
284 } 332 }
285 333
diff --git a/src/renderer.h b/src/renderer.h index 54dae1c..17dc038 100644 --- a/src/renderer.h +++ b/src/renderer.h
@@ -145,6 +145,7 @@ private:
145 texture_ptr tilesetTex_; 145 texture_ptr tilesetTex_;
146 texture_ptr renLay0_; 146 texture_ptr renLay0_;
147 texture_ptr renLay1_; 147 texture_ptr renLay1_;
148 texture_ptr mapSwapTex_;
148 149
149 // Text rendering 150 // Text rendering
150 struct MessageCache { 151 struct MessageCache {
diff --git a/src/script_system.cpp b/src/script_system.cpp index 08d66d4..7109d98 100644 --- a/src/script_system.cpp +++ b/src/script_system.cpp
@@ -5,6 +5,7 @@
5#include "animation_system.h" 5#include "animation_system.h"
6#include "character_system.h" 6#include "character_system.h"
7#include "transform_system.h" 7#include "transform_system.h"
8#include "effect_system.h"
8#include "vector.h" 9#include "vector.h"
9 10
10ScriptSystem::ScriptSystem(Game& game) : game_(game) { 11ScriptSystem::ScriptSystem(Game& game) : game_(game) {
@@ -31,6 +32,7 @@ ScriptSystem::ScriptSystem(Game& game) : game_(game) {
31 "message", 32 "message",
32 "displayMessage", &MessageSystem::displayMessage, 33 "displayMessage", &MessageSystem::displayMessage,
33 "showChoice", &MessageSystem::showChoice, 34 "showChoice", &MessageSystem::showChoice,
35 "displayCutsceneBars", &MessageSystem::displayCutsceneBars,
34 "hideCutsceneBars", &MessageSystem::hideCutsceneBars, 36 "hideCutsceneBars", &MessageSystem::hideCutsceneBars,
35 "isMessageActive", sol::property(&MessageSystem::isMessageActive), 37 "isMessageActive", sol::property(&MessageSystem::isMessageActive),
36 "getChoiceSelection", &MessageSystem::getChoiceSelection); 38 "getChoiceSelection", &MessageSystem::getChoiceSelection);
@@ -52,6 +54,13 @@ ScriptSystem::ScriptSystem(Game& game) : game_(game) {
52 transform.initSprite(spriteId, vec2i{x, y}, layer); 54 transform.initSprite(spriteId, vec2i{x, y}, layer);
53 }); 55 });
54 56
57 engine_.new_usertype<EffectSystem>(
58 "effect",
59 "fadeScreen", &EffectSystem::fadeScreen,
60 "isScreenFadeComplete", &EffectSystem::isScreenFadeComplete,
61 "fadeMap", &EffectSystem::fadeMap,
62 "isMapFadeComplete", &EffectSystem::isMapFadeComplete);
63
55 engine_.new_usertype<Mixer>( 64 engine_.new_usertype<Mixer>(
56 "mixer", 65 "mixer",
57 "playSound", &Mixer::playSound, 66 "playSound", &Mixer::playSound,
@@ -83,6 +92,12 @@ ScriptSystem::ScriptSystem(Game& game) : game_(game) {
83 }); 92 });
84 93
85 engine_.set_function( 94 engine_.set_function(
95 "effect",
96 [&] () -> EffectSystem& {
97 return game_.getSystem<EffectSystem>();
98 });
99
100 engine_.set_function(
86 "mixer", 101 "mixer",
87 [&] () -> Mixer& { 102 [&] () -> Mixer& {
88 return game_.getMixer(); 103 return game_.getMixer();
@@ -130,12 +145,6 @@ ScriptSystem::ScriptSystem(Game& game) : game_(game) {
130 game_.loadMap(filename, warpPoint, dir); 145 game_.loadMap(filename, warpPoint, dir);
131 }); 146 });
132 147
133 engine_.set_function(
134 "setFadeoutProgress",
135 [&] (double val) {
136 game_.setFadeoutProgress(val);
137 });
138
139 engine_.script_file("../res/scripts/common.lua"); 148 engine_.script_file("../res/scripts/common.lua");
140} 149}
141 150
diff --git a/src/system.h b/src/system.h index 3e84537..0cdc205 100644 --- a/src/system.h +++ b/src/system.h
@@ -2,13 +2,14 @@
2#define SYSTEM_H_6B40E1B9 2#define SYSTEM_H_6B40E1B9
3 3
4enum class SystemKey { 4enum class SystemKey {
5 Script,
5 Transform, 6 Transform,
6 Input, 7 Input,
7 Character, 8 Character,
8 Animation, 9 Animation,
9 Camera, 10 Camera,
10 Message, 11 Message,
11 Script 12 Effect
12}; 13};
13 14
14class System { 15class System {