Skip to content

Commit

Permalink
Check if MovingSprite type change call is initial (SuperTux#2646)
Browse files Browse the repository at this point in the history
Removes an old `MovingSprite::has_found_sprite()` hack, which allowed getting whether a custom sprite has been found only the first time it's called. After the first call, `m_sprite_found` always equaled to `false`.

`MovingSprite::on_type_change` now checks if the call is initial, and if so, only sets the default sprite for the type, if a custom sprite hasn't just been loaded.
  • Loading branch information
Vankata453 authored Jan 30, 2024
1 parent 4e0f879 commit 88674c6
Show file tree
Hide file tree
Showing 17 changed files with 34 additions and 30 deletions.
7 changes: 7 additions & 0 deletions src/badguy/corrupted_granito.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ CorruptedGranito::CorruptedGranito(const ReaderMapping& reader) :
SoundManager::current()->preload(CORRUPTED_GRANITO_SOUND);
}

CorruptedGranito::CorruptedGranito(const ReaderMapping& reader, int type) :
CorruptedGranito(reader)
{
m_type = type;
on_type_change(TypeChange::INITIAL);
}

void
CorruptedGranito::initialize()
{
Expand Down
1 change: 1 addition & 0 deletions src/badguy/corrupted_granito.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class CorruptedGranito final : public BadGuy

public:
CorruptedGranito(const ReaderMapping& reader);
CorruptedGranito(const ReaderMapping& reader, int type);

virtual void initialize() override;
virtual void collision_solid(const CollisionHit& hit) override;
Expand Down
4 changes: 2 additions & 2 deletions src/badguy/crusher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ Crusher::get_default_sprite_name() const
void
Crusher::on_type_change(int old_type)
{
MovingSprite::on_type_change(old_type);

m_ic_size = (m_type % 2 == 0 ? NORMAL : LARGE);
switch (m_type)
{
Expand All @@ -114,8 +116,6 @@ Crusher::on_type_change(int old_type)
m_ic_type = CORRUPTED;
break;
}

MovingSprite::on_type_change();
}

HitResponse
Expand Down
2 changes: 1 addition & 1 deletion src/badguy/flame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Flame::Flame(const ReaderMapping& reader, int type) :
if (type >= 0)
{
m_type = type;
on_type_change();
on_type_change(TypeChange::INITIAL);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/badguy/mrtree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ MrTree::get_default_sprite_name() const
void
MrTree::on_type_change(int old_type)
{
MovingSprite::on_type_change();
MovingSprite::on_type_change(old_type);

switch (m_type)
{
Expand Down
2 changes: 1 addition & 1 deletion src/badguy/viciousivy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ ViciousIvy::get_default_sprite_name() const
void
ViciousIvy::on_type_change(int old_type)
{
MovingSprite::on_type_change();
MovingSprite::on_type_change(old_type);

switch (m_type)
{
Expand Down
2 changes: 1 addition & 1 deletion src/badguy/walkingleaf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ WalkingLeaf::get_default_sprite_name() const
void
WalkingLeaf::on_type_change(int old_type)
{
MovingSprite::on_type_change();
MovingSprite::on_type_change(old_type);

switch (m_type)
{
Expand Down
2 changes: 1 addition & 1 deletion src/object/bonus_block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ BonusBlock::get_default_sprite_name() const
void
BonusBlock::on_type_change(int old_type)
{
Block::on_type_change();
Block::on_type_change(old_type);

m_hit_counter = get_default_hit_counter();
m_coin_sprite = get_default_coin_sprite();
Expand Down
12 changes: 3 additions & 9 deletions src/object/moving_sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,12 @@ MovingSprite::update(float )
{
}

bool
MovingSprite::has_found_sprite()
{
bool found = m_sprite_found;
m_sprite_found = false; // After the first call, indicate that a custom sprite has not been found.
return found;
}

void
MovingSprite::on_type_change(int old_type)
{
if (!has_found_sprite()) // Change sprite only if a custom sprite has not just been loaded.
/** Don't change the sprite to the default one for the current type,
if this is the initial `on_type_change()` call, and a custom sprite has just been loaded. */
if (old_type >= 0 || !m_sprite_found)
change_sprite(get_default_sprite_name());
}

Expand Down
4 changes: 2 additions & 2 deletions src/object/moving_sprite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ class MovingSprite : public MovingObject

virtual ObjectSettings get_settings() override;
virtual void after_editor_set() override;
virtual void on_type_change(int old_type = -1) override;
virtual void on_type_change(int old_type) override;

virtual int get_layer() const override { return m_layer; }

bool has_found_sprite();
bool has_found_sprite() const { return m_sprite_found; }
const std::string& get_sprite_name() const { return m_sprite_name; }
virtual std::string get_default_sprite_name() const { return m_default_sprite_name; }

Expand Down
2 changes: 1 addition & 1 deletion src/object/powerup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ PowerUp::PowerUp(const Vector& pos, int type) :
lightsprite(SpriteManager::current()->create("images/objects/lightmap_light/lightmap_light-small.sprite"))
{
m_type = type;
on_type_change();
on_type_change(TypeChange::INITIAL);

update_version();
initialize();
Expand Down
10 changes: 6 additions & 4 deletions src/object/trampoline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,17 @@ namespace {
Trampoline::Trampoline(const ReaderMapping& mapping) :
Rock(mapping, "images/objects/trampoline/trampoline.sprite")
{
parse_type(mapping);

// Older levels use the "portable" property to determine the type.
bool portable = true;
mapping.get("portable", portable);
if (!portable)
{
m_type = STATIONARY;
on_type_change();
on_type_change(TypeChange::INITIAL);
}
else
{
parse_type(mapping);
}

SoundManager::current()->preload(TRAMPOLINE_SOUND);
Expand All @@ -52,7 +54,7 @@ Trampoline::Trampoline(const Vector& pos, int type) :
Rock(pos, "images/objects/trampoline/trampoline.sprite")
{
m_type = type;
on_type_change();
on_type_change(TypeChange::INITIAL);

SoundManager::current()->preload(TRAMPOLINE_SOUND);
}
Expand Down
2 changes: 1 addition & 1 deletion src/object/unstable_tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ UnstableTile::UnstableTile(const ReaderMapping& mapping, int type) :
if (type >= 0)
{
m_type = type;
on_type_change();
on_type_change(TypeChange::INITIAL);
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions src/object/weak_block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ WeakBlock::WeakBlock(const ReaderMapping& mapping) :
if (linked)
m_type = HAY;

on_type_change();
on_type_change(TypeChange::INITIAL);
}
else
{
Expand All @@ -73,7 +73,7 @@ WeakBlock::update_version()
if (get_version() == 1)
{
m_type = ICE;
on_type_change();
on_type_change(m_type);
}

GameObject::update_version();
Expand Down
4 changes: 2 additions & 2 deletions src/supertux/game_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ GameObject::parse_type(const ReaderMapping& reader)
{
try
{
set_type(type_id_to_value(type));
m_type = type_id_to_value(type);
}
catch (...)
{
Expand All @@ -216,7 +216,7 @@ GameObject::parse_type(const ReaderMapping& reader)
}
}

on_type_change(-1); // Initial object type initialization
on_type_change(TypeChange::INITIAL); // Initial object type initialization
}

GameObjectTypes
Expand Down
2 changes: 1 addition & 1 deletion src/supertux/game_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ class GameObject
/** Get all types of the object, if available. **/
virtual GameObjectTypes get_types() const;
int get_type() const { return m_type; }
void set_type(int type) { m_type = type; }

virtual void after_editor_set();

Expand Down Expand Up @@ -205,6 +204,7 @@ class GameObject
void parse_type(const ReaderMapping& reader);

/** When the type has been changed from the editor. **/
enum TypeChange { INITIAL = -1 }; // "old_type < 0" indicates initial call
virtual void on_type_change(int old_type) {}

/** Conversion between type ID and value. **/
Expand Down
2 changes: 1 addition & 1 deletion src/supertux/game_object_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ GameObjectFactory::init_factories()
add_factory<AngryStone>("angrystone");
add_factory<BouncingSnowball>("bouncingsnowball", OBJ_PARAM_DISPENSABLE);
add_factory<CaptainSnowball>("captainsnowball", OBJ_PARAM_DISPENSABLE);
add_factory<CorruptedGranito>("skullyhop"); // backward compatibility
add_type_factory<CorruptedGranito>("skullyhop", CorruptedGranito::SKULLYHOP); // backward compatibility
add_factory<CorruptedGranito>("corrupted_granito", OBJ_PARAM_DISPENSABLE);
add_factory<CorruptedGranitoBig>("corrupted_granito_big", OBJ_PARAM_DISPENSABLE);
add_factory<Crusher>("icecrusher"); // backward compatibility
Expand Down

0 comments on commit 88674c6

Please sign in to comment.