Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set background color on lightning #2628

Merged
merged 31 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
3776a56
Set background color on lightning
tobbi Sep 6, 2023
14bcd70
Fix floating point
tobbi Sep 6, 2023
1b8a05c
Brighten sector on lightning hit
tobbi Sep 6, 2023
1ad1b6c
Fix layer to be uppest
tobbi Jan 12, 2024
b06bd7a
Fix clang warning maybe
tobbi Jan 12, 2024
0459916
Increase flash time to 0.5
tobbi Jan 12, 2024
e01fed2
FLASH_DISPLAY_TIME to 0.3f
tobbi Jan 12, 2024
eeb897c
Increase alpha, use additive blend
tobbi Jan 12, 2024
b95c7ff
Add get_progress() method to Timer
tobbi Jan 12, 2024
40e8efd
Adjust flash display time, set proper alpha for flash
tobbi Jan 12, 2024
f59bc91
Change colors for backgrounds properly
tobbi Jan 12, 2024
4fbca54
Fix review comments
tobbi Jan 21, 2024
2b5c1e5
Change layer for lightning to be foremost layer
tobbi Jan 23, 2024
be44ad4
Split get_foremost_layer and get_foremost_opaque_layer
tobbi Jan 24, 2024
26f8ea3
Improvements to lightning
tobbi Jan 28, 2024
7f611db
More fixes for lightning
tobbi Jan 28, 2024
4380652
Reset to working version
tobbi Jan 28, 2024
14a40ae
Cleanup
tobbi Jan 28, 2024
30c1012
More cleanup
tobbi Jan 28, 2024
57c62f3
Smooth background fading
tobbi Jan 28, 2024
1402d92
Change full-intensity flash duration to 0.3
tobbi Jan 28, 2024
363ee3a
Final adjustments Rusty
tobbi Jan 28, 2024
474b129
Reset ambient light to previous default when alpha = 0
tobbi Jan 28, 2024
9d72c02
Set max intensity only for 0.01, don't change ambient light
tobbi Jan 28, 2024
ad88184
Add m_ prefix for newly added variables
tobbi Mar 2, 2024
1256ebf
Adjust timing and color, use gradient drawing method
tobbi Mar 2, 2024
e65ed2e
Fix spaces [skip ci]
tobbi Mar 2, 2024
76088a6
Fix code style
tobbi Mar 10, 2024
8927a2d
Add scale to draw call
tobbi Mar 11, 2024
56a004e
Revert changes in canvas.cpp/canvas.hpp
tobbi Mar 11, 2024
55114db
Remove unused ambient_light.hpp include
tobbi Mar 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/badguy/badguy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ BadGuy::kill_fall()

// Set the badguy layer to be the foremost, so that
// this does not reveal secret tilemaps:
m_layer = Sector::get().get_foremost_layer() + 1;
m_layer = Sector::get().get_foremost_opaque_layer() + 1;
// Start the dead-script.
run_dead_script();
}
Expand Down
2 changes: 1 addition & 1 deletion src/badguy/corrupted_granito_big.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ CorruptedGranitoBig::crack()
else if (!m_crack_timer.started())
m_crack_timer.start(CRACK_TIME);

float progress = m_crack_timer.get_timegone() / m_crack_timer.get_period();
float progress = m_crack_timer.get_progress();

if (m_state == STATE_CRACK1 && progress >= 0.5f)
{
Expand Down
4 changes: 2 additions & 2 deletions src/badguy/igel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Igel::active_update(float dt_sec)

if (m_ease_timer.started())
{
float progress = m_ease_timer.get_timegone() / m_ease_timer.get_period();
float progress = m_ease_timer.get_progress();
float vel = (static_cast<float>(SineEaseOut(static_cast<double>(progress))) * (ROLL_SPEED - get_normal_walk_speed())) + get_normal_walk_speed();
set_walk_speed(vel);
m_physic.set_velocity_x(vel * (m_dir == Direction::LEFT ? -1 : 1));
Expand Down Expand Up @@ -106,7 +106,7 @@ Igel::active_update(float dt_sec)

if (m_ease_timer.started())
{
float progress = m_ease_timer.get_timegone() / m_ease_timer.get_period();
float progress = m_ease_timer.get_progress();
float vel = (static_cast<float>(SineEaseIn(static_cast<double>(progress))) * (get_normal_walk_speed() - ROLL_SPEED)) + ROLL_SPEED;
set_walk_speed(vel);
m_physic.set_velocity_x(vel * (m_dir == Direction::LEFT ? -1 : 1));
Expand Down
4 changes: 2 additions & 2 deletions src/badguy/yeti.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ void Yeti::take_hit(Player& )

// Set the badguy layer to be above the foremost, so that
// this does not reveal secret tilemaps:
m_layer = Sector::get().get_foremost_layer() + 1;
m_layer = Sector::get().get_foremost_opaque_layer() + 1;
m_state = SQUISHED;
m_state_timer.start(YETI_SQUISH_TIME);
set_colgroup_active(COLGROUP_MOVING_ONLY_STATIC);
Expand Down Expand Up @@ -394,7 +394,7 @@ Yeti::SnowExplosionParticle::SnowExplosionParticle(const Vector& pos, const Vect
m_physic.set_velocity(velocity);
m_physic.enable_gravity(true);
set_state(STATE_FALLING);
m_layer = Sector::get().get_foremost_layer() + 1;
m_layer = Sector::get().get_foremost_opaque_layer() + 1;
}

std::vector<Direction>
Expand Down
2 changes: 1 addition & 1 deletion src/object/background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ Background::update(float dt_sec)
}
else if (m_timer_color.started())
{
float progress = m_timer_color.get_timegone() / m_timer_color.get_period();
float progress = m_timer_color.get_progress();

m_color = (m_src_color + (m_dst_color - m_src_color) * progress).validate();
}
Expand Down
4 changes: 2 additions & 2 deletions src/object/decal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Decal::update(float)
else
{
// Square root makes the background stay at fairly constant color/transparency
float new_alpha = sqrtf(m_sprite_timer.get_timegone() / m_sprite_timer.get_period());
float new_alpha = sqrtf(m_sprite_timer.get_progress());
float old_alpha = sqrtf(m_sprite_timer.get_timeleft() / m_sprite_timer.get_period());
m_sprite.get()->set_alpha(new_alpha);
m_fade_sprite.get()->set_alpha(old_alpha);
Expand All @@ -136,7 +136,7 @@ Decal::update(float)
else
{
float alpha;
if (m_visible) alpha = m_fade_timer.get_timegone() / m_fade_timer.get_period();
if (m_visible) alpha = m_fade_timer.get_progress();
else alpha = m_fade_timer.get_timeleft() / m_fade_timer.get_period();
m_sprite.get()->set_alpha(alpha);
}
Expand Down
2 changes: 1 addition & 1 deletion src/object/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2206,7 +2206,7 @@ Player::draw(DrawingContext& context)
} // don't draw Tux

else if (m_dying)
m_sprite->draw(context.color(), get_pos(), Sector::get().get_foremost_layer() + 1);
m_sprite->draw(context.color(), get_pos(), Sector::get().get_foremost_opaque_layer() + 1);
else
m_sprite->draw(context.color(), get_pos(), LAYER_OBJECTS + 1);

Expand Down
49 changes: 43 additions & 6 deletions src/object/thunderstorm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include "audio/sound_manager.hpp"
#include "editor/editor.hpp"
#include "object/ambient_light.hpp"
tobbi marked this conversation as resolved.
Show resolved Hide resolved
#include "object/background.hpp"
#include "object/electrifier.hpp"
#include "supertux/level.hpp"
#include "supertux/sector.hpp"
Expand All @@ -30,9 +32,8 @@
namespace {

const float LIGHTNING_DELAY = 2.0f;
const float FLASH_DISPLAY_TIME = 0.1f;
const float FLASH_DISPLAY_TIME = 1.3f;
const float ELECTRIFY_TIME = 0.5f;

} // namespace

Thunderstorm::Thunderstorm(const ReaderMapping& reader) :
Expand All @@ -45,7 +46,8 @@ Thunderstorm::Thunderstorm(const ReaderMapping& reader) :
time_to_thunder(),
time_to_lightning(),
flash_display_timer(),
changing_tiles(TileManager::current()->get_tileset(Level::current()->get_tileset())->m_thunderstorm_tiles)
changing_tiles(TileManager::current()->get_tileset(Level::current()->get_tileset())->m_thunderstorm_tiles),
m_flash_color()
{
reader.get("running", running);
reader.get("interval", interval);
Expand Down Expand Up @@ -94,19 +96,39 @@ Thunderstorm::update(float )
lightning();
time_to_thunder.start(interval);
}

if(flash_display_timer.started())
{
float alpha = 0.9f;
if(flash_display_timer.get_timegone() > 0.1f)
{
auto progress = flash_display_timer.get_timegone() / flash_display_timer.get_timeleft() - 0.1f;
if(progress < 0.0f)
progress = 0.0f;

alpha = 0.9f - progress;
}

if(alpha < 0.0f)
{
flash_display_timer.stop();
return;
}

m_flash_color = Color(alpha, alpha, alpha, 1.0);
}
}

void
Thunderstorm::draw(DrawingContext& context)
{
if (!flash_display_timer.started()) return;

float alpha = 0.33f;
context.push_transform();
context.set_translation(Vector(0, 0));
context.color().draw_filled_rect(context.get_rect(), Color(1, 1, 1, alpha), layer);
context.transform().scale = 1.f;
context.color().draw_gradient(m_flash_color, m_flash_color, 500, GradientDirection::HORIZONTAL, context.get_rect(), Blend::ADD);
tobbi marked this conversation as resolved.
Show resolved Hide resolved
context.pop_transform();

}

void
Expand All @@ -131,6 +153,7 @@ void
Thunderstorm::thunder()
{
SoundManager::current()->play("sounds/thunder.wav");
change_background_colors(false);
}

void
Expand All @@ -141,6 +164,8 @@ Thunderstorm::lightning()
if (!m_strike_script.empty()) {
Sector::get().run_script(m_strike_script, "strike-script");
}

change_background_colors(true);
}

void
Expand All @@ -156,4 +181,16 @@ Thunderstorm::electrify()
Sector::get().add<Electrifier>(changing_tiles, ELECTRIFY_TIME);
}

void
Thunderstorm::change_background_colors(bool is_lightning)
{
auto factor = is_lightning ? (1.0f / 0.7f) : 0.7f;
auto backgrounds = Sector::current()->get_objects_by_type<Background>();
for(auto& background : backgrounds)
{
auto new_color = background.get_color() * factor;
background.fade_color(new_color.validate(), 0.1f);
}
}

/* EOF */
6 changes: 5 additions & 1 deletion src/object/thunderstorm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,22 @@ class Thunderstorm final : public GameObject,

/** @} */

private:
void change_background_colors(bool is_lightning);

private:
bool running; /**< whether we currently automatically trigger lightnings */
float interval; /**< time between two lightnings */
int layer; /**< layer, where flash will be painted */

std::string m_strike_script;

Timer time_to_thunder; /**< counts down until next thunder */
Timer time_to_lightning; /**< counts down until next lightning */
Timer flash_display_timer; /**< counts down while flash is displayed */

std::map<uint32_t, uint32_t> changing_tiles; /**< preserves the tiles which an electrocution should change */
Color m_flash_color;

private:
Thunderstorm(const Thunderstorm&) = delete;
Expand Down
14 changes: 11 additions & 3 deletions src/supertux/sector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Sector::Sector(Level& parent) :
m_level(parent),
m_fully_constructed(false),
m_foremost_layer(),
m_foremost_opaque_layer(),
m_gravity(10.0f),
m_collision_system(new CollisionSystem(*this)),
m_text_object(add<TextObject>("Text"))
Expand Down Expand Up @@ -154,7 +155,8 @@ Sector::finish_construction(bool editable)
m_initialized = false;
flush_game_objects();

m_foremost_layer = calculate_foremost_layer();
m_foremost_layer = calculate_foremost_layer(false);
m_foremost_opaque_layer = calculate_foremost_layer();

process_resolve_requests();

Expand Down Expand Up @@ -300,14 +302,14 @@ Sector::get_active_region() const
}

int
Sector::calculate_foremost_layer() const
Sector::calculate_foremost_layer(bool including_transparent) const
{
int layer = LAYER_BACKGROUND0;
for (auto& tm : get_objects_by_type<TileMap>())
{
if (tm.get_layer() > layer)
{
if ( (tm.get_alpha() < 1.0f) )
if ( including_transparent && tm.get_alpha() < 1.0f )
{
layer = tm.get_layer() - 1;
}
Expand All @@ -321,6 +323,12 @@ Sector::calculate_foremost_layer() const
return layer;
}

int
Sector::get_foremost_opaque_layer() const
{
return m_foremost_opaque_layer;
}

int
Sector::get_foremost_layer() const
{
Expand Down
4 changes: 3 additions & 1 deletion src/supertux/sector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class Sector final : public Base::Sector

Rectf get_active_region() const;

int get_foremost_opaque_layer() const;
int get_foremost_layer() const;

/** returns the editor size (in tiles) of a sector */
Expand All @@ -156,7 +157,7 @@ class Sector final : public Base::Sector
virtual bool before_object_add(GameObject& object) override;
virtual void before_object_remove(GameObject& object) override;

int calculate_foremost_layer() const;
int calculate_foremost_layer(bool including_transparent = true) const;

/** Convert tiles into their corresponding GameObjects (e.g.
bonusblocks, add light to lava tiles) */
Expand All @@ -167,6 +168,7 @@ class Sector final : public Base::Sector

bool m_fully_constructed;
int m_foremost_layer;
int m_foremost_opaque_layer;

float m_gravity;

Expand Down
3 changes: 2 additions & 1 deletion src/supertux/timer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ class Timer final

/** returns the period of the timer or 0 if it isn't started */
float get_period() const { return m_period; }
float get_timeleft() const{ return m_period - (g_game_time - m_cycle_start); }
float get_timeleft() const { return m_period - (g_game_time - m_cycle_start); }
float get_timegone() const { return g_game_time - m_cycle_start; }
float get_progress() const { return get_timegone() / get_period(); }
bool started() const { return (m_period != 0 && get_timeleft() > 0); }
bool paused() const { return m_cycle_pause != 0; }

Expand Down
Loading