Skip to content

Commit

Permalink
Rework shard behavior
Browse files Browse the repository at this point in the history
* Decrease time the shards are active
* Fadeout shards when becoming inactive

Fixes #3134
  • Loading branch information
Brockengespenst committed Jan 11, 2025
1 parent 15dfac1 commit ec48e26
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/object/shard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@
#include "supertux/sector.hpp"
#include "util/reader_mapping.hpp"

static const float STICKING_TIME = 0.7f;
static const float FADEOUT_TIME = 0.3f;

Shard::Shard(const ReaderMapping& reader) :
StickyObject(reader, "images/creatures/crystallo/shard.sprite", LAYER_TILES - 2, COLGROUP_MOVING),
m_physic(),
m_stick_timer()
m_stick_timer(),
m_fadeout_timer()
{
m_physic.enable_gravity(true);
SoundManager::current()->preload("sounds/crystallo-shardhit.ogg");
Expand All @@ -36,7 +40,8 @@ Shard::Shard(const ReaderMapping& reader) :
Shard::Shard(const Vector& pos, const Vector& velocity, const std::string& sprite) :
StickyObject(pos, sprite, LAYER_TILES - 2, COLGROUP_MOVING),
m_physic(),
m_stick_timer()
m_stick_timer(),
m_fadeout_timer()
{
m_physic.enable_gravity(true);
m_physic.set_velocity(velocity);
Expand All @@ -51,8 +56,14 @@ Shard::update(float dt_sec)

if (m_physic.get_velocity() != Vector(0.f, 0.f) && !m_sticking)
m_sprite->set_angle(math::degrees(math::angle(Vector(m_physic.get_velocity_x(), m_physic.get_velocity_y()))));

if (m_stick_timer.check())
m_fadeout_timer.start(FADEOUT_TIME);

if (m_fadeout_timer.check())
remove_me();
else if (m_fadeout_timer.started())
m_sprite->set_alpha(1.0f - m_fadeout_timer.get_progress());

m_col.set_movement(m_physic.get_movement(dt_sec));

Expand All @@ -65,16 +76,22 @@ Shard::collision_solid(const CollisionHit& hit)
m_physic.set_velocity(0.f, 0.f);
m_physic.set_acceleration(0.f, 0.f);
m_physic.enable_gravity(hit.bottom);
m_sticking = true;

if (!m_stick_timer.started())
{
m_stick_timer.start(5.f);
m_stick_timer.start(STICKING_TIME);
SoundManager::current()->play("sounds/crystallo-shardhit.ogg", get_pos());
}
}

HitResponse
Shard::collision(MovingObject& other, const CollisionHit&)
{
// Do not hurt anyone while fading out
if (m_fadeout_timer.started())
return ABORT_MOVE;

// ignore collisions with other shards
auto shard = dynamic_cast<Shard*>(&other);
if (&other == shard)
Expand Down
1 change: 1 addition & 0 deletions src/object/shard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Shard final : public StickyObject

private:
Timer m_stick_timer;
Timer m_fadeout_timer;

private:
Shard(const Shard&) = delete;
Expand Down

0 comments on commit ec48e26

Please sign in to comment.