Skip to content

Commit

Permalink
Avoid unnecessary dynamic_casts, LayerObject base class, remove `…
Browse files Browse the repository at this point in the history
…CollisionListener` (#3110)

Unnecessary `dynamic_cast`s have been replaced with `static_cast`s, or the code requiring them has been improved.

## Add `LayerObject` class

`LayerObject` is a base class for all `GameObject`s which are shown under the `EditorLayersWidget`. Used to prevent multiple casts every single frame for every single layer icon when determining z-pos.

Closes #701.

## Remove `CollisionListener` class

The changes done by commit fa82f91 have been reverted. The reason is that `CollisionListener` was not used as a base anywhere beside `MovingObject`, and made it so `CollisionObject::collide()`/`collision()` had to use `dynamic_cast` to side-cast the `other` object's listener to a `GameObject`.

`CollisionObject` now contains the `MovingObject& m_parent` member, as opposed to `CollisionListener& m_listener`.

Additionally, the now base `MovingObject::collision...` functions now accept `MovingObject&` in arguments, instead of `GameObject&`. This saves on a few other casts along the way.
  • Loading branch information
Vankata453 authored Nov 26, 2024
1 parent 3d4b418 commit 725a20a
Show file tree
Hide file tree
Showing 225 changed files with 556 additions and 646 deletions.
2 changes: 1 addition & 1 deletion src/audio/sound_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ SoundManager::manage_source(std::unique_ptr<SoundSource> source)
assert(source);
if (dynamic_cast<OpenALSoundSource*>(source.get()))
{
std::unique_ptr<OpenALSoundSource> openal_source(dynamic_cast<OpenALSoundSource*>(source.release()));
std::unique_ptr<OpenALSoundSource> openal_source(static_cast<OpenALSoundSource*>(source.release()));
m_sources.push_back(std::move(openal_source));
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/badguy/badguy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ BadGuy::collision_tile(uint32_t tile_attributes)
}

HitResponse
BadGuy::collision(GameObject& other, const CollisionHit& hit)
BadGuy::collision(MovingObject& other, const CollisionHit& hit)
{
if (!is_active()) return ABORT_MOVE;

Expand Down Expand Up @@ -598,7 +598,7 @@ BadGuy::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
}

bool
BadGuy::collision_squished(GameObject& object)
BadGuy::collision_squished(MovingObject& object)
{
// Frozen badguys can be killed with butt-jump.
if (m_frozen)
Expand Down
4 changes: 2 additions & 2 deletions src/badguy/badguy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class BadGuy : public MovingSprite,
/** Called when a collision with another object occurred. The
default implementation calls collision_player, collision_solid,
collision_badguy and collision_squished */
virtual HitResponse collision(GameObject& other, const CollisionHit& hit) override;
virtual HitResponse collision(MovingObject& other, const CollisionHit& hit) override;

/** Called when a collision with tile with special attributes
occurred */
Expand Down Expand Up @@ -188,7 +188,7 @@ class BadGuy : public MovingSprite,
/** Called when the player hit the badguy from above. You should
return true if the badguy was squished, false if squishing
wasn't possible */
virtual bool collision_squished(GameObject& object);
virtual bool collision_squished(MovingObject& object);

/** Called when the badguy collided with a bullet */
virtual HitResponse collision_bullet(Bullet& bullet, const CollisionHit& hit);
Expand Down
2 changes: 1 addition & 1 deletion src/badguy/bouncing_snowball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ BouncingSnowball::is_freezable() const
}

bool
BouncingSnowball::collision_squished(GameObject& object)
BouncingSnowball::collision_squished(MovingObject& object)
{
if (m_frozen)
return BadGuy::collision_squished(object);
Expand Down
2 changes: 1 addition & 1 deletion src/badguy/bouncing_snowball.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class BouncingSnowball final : public BadGuy
virtual bool is_freezable() const override;

protected:
virtual bool collision_squished(GameObject& object) override;
virtual bool collision_squished(MovingObject& object) override;
void turn_around();

private:
Expand Down
2 changes: 1 addition & 1 deletion src/badguy/captainsnowball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ CaptainSnowball::collision_solid(const CollisionHit& hit)
}

bool
CaptainSnowball::collision_squished(GameObject& object)
CaptainSnowball::collision_squished(MovingObject& object)
{
set_action("squished", m_dir);
kill_squished(object);
Expand Down
2 changes: 1 addition & 1 deletion src/badguy/captainsnowball.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CaptainSnowball final : public WalkingBadguy
bool might_climb(int width, int height) const;

protected:
virtual bool collision_squished(GameObject& object) override;
virtual bool collision_squished(MovingObject& object) override;

private:
bool m_jumping;
Expand Down
2 changes: 1 addition & 1 deletion src/badguy/corrupted_granito.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ CorruptedGranito::set_state(CorruptedGranitoState newState)
}

bool
CorruptedGranito::collision_squished(GameObject& object)
CorruptedGranito::collision_squished(MovingObject& object)
{
if (m_frozen)
return BadGuy::collision_squished(object);
Expand Down
2 changes: 1 addition & 1 deletion src/badguy/corrupted_granito.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class CorruptedGranito final : public BadGuy
virtual void initialize() override;
virtual void collision_solid(const CollisionHit& hit) override;
virtual HitResponse collision_badguy(BadGuy& badguy, const CollisionHit& hit) override;
virtual bool collision_squished(GameObject& object) override;
virtual bool collision_squished(MovingObject& object) override;
virtual void active_update(float dt_sec) override;

virtual void unfreeze(bool melt = true) 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 @@ -120,7 +120,7 @@ Crusher::on_type_change(int old_type)
}

HitResponse
Crusher::collision(GameObject& other, const CollisionHit& hit)
Crusher::collision(MovingObject& other, const CollisionHit& hit)
{
auto* player = dynamic_cast<Player*>(&other);

Expand Down Expand Up @@ -686,7 +686,7 @@ CrusherRoot::CrusherRoot(Vector position, Crusher::Direction direction, float de
}

HitResponse
CrusherRoot::collision(GameObject& other, const CollisionHit& hit)
CrusherRoot::collision(MovingObject& other, const CollisionHit& hit)
{
if (delay_gone())
{
Expand Down
4 changes: 2 additions & 2 deletions src/badguy/crusher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Crusher final : public MovingSprite
public:
Crusher(const ReaderMapping& reader);

virtual HitResponse collision(GameObject& other, const CollisionHit& hit) override;
virtual HitResponse collision(MovingObject& other, const CollisionHit& hit) override;
virtual void collision_solid(const CollisionHit& hit) override;
virtual void update(float dt_sec) override;
virtual void draw(DrawingContext& context) override;
Expand Down Expand Up @@ -118,7 +118,7 @@ class CrusherRoot : public MovingSprite
CrusherRoot(Vector position, Crusher::Direction direction, float delay, int layer);
virtual GameObjectClasses get_class_types() const override { return MovingSprite::get_class_types().add(typeid(CrusherRoot)); }

virtual HitResponse collision(GameObject& other, const CollisionHit& hit) override;
virtual HitResponse collision(MovingObject& other, const CollisionHit& hit) override;
virtual void update(float dt_sec) override;

private:
Expand Down
2 changes: 1 addition & 1 deletion src/badguy/crystallo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Crystallo::active_update(float dt_sec)
}

bool
Crystallo::collision_squished(GameObject& object)
Crystallo::collision_squished(MovingObject& object)
{
set_action(m_dir == Direction::LEFT ? "shattered-left" : "shattered-right", /* loops = */ -1, ANCHOR_BOTTOM);
kill_squished(object);
Expand Down
2 changes: 1 addition & 1 deletion src/badguy/crystallo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Crystallo final : public WalkingBadguy
virtual void on_flip(float height) override;

protected:
virtual bool collision_squished(GameObject& object) override;
virtual bool collision_squished(MovingObject& object) override;

private:
float m_radius;
Expand Down
2 changes: 1 addition & 1 deletion src/badguy/dispenser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ Dispenser::deactivate()
}

HitResponse
Dispenser::collision(GameObject& other, const CollisionHit& hit)
Dispenser::collision(MovingObject& other, const CollisionHit& hit)
{
auto bullet = dynamic_cast<Bullet*> (&other);
if (bullet)
Expand Down
2 changes: 1 addition & 1 deletion src/badguy/dispenser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class Dispenser final : public BadGuy
protected:
void add_object(std::unique_ptr<GameObject> object);

virtual HitResponse collision(GameObject& other, const CollisionHit& hit) override;
virtual HitResponse collision(MovingObject& other, const CollisionHit& hit) override;
void launch_object();

void on_type_change(int old_type) override;
Expand Down
2 changes: 1 addition & 1 deletion src/badguy/flyingsnowball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ FlyingSnowBall::activate()
}

bool
FlyingSnowBall::collision_squished(GameObject& object)
FlyingSnowBall::collision_squished(MovingObject& object)
{
set_action("squished", m_dir);
m_physic.enable_gravity(true);
Expand Down
2 changes: 1 addition & 1 deletion src/badguy/flyingsnowball.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class FlyingSnowBall final : public BadGuy
virtual bool is_snipable() const override { return true; }

protected:
virtual bool collision_squished(GameObject& object) override;
virtual bool collision_squished(MovingObject& object) override;
virtual std::vector<Direction> get_allowed_directions() const override;

private:
Expand Down
4 changes: 2 additions & 2 deletions src/badguy/ghosttree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ GhostTree::draw(DrawingContext& context)
}

bool
GhostTree::collides(GameObject& other, const CollisionHit& ) const
GhostTree::collides(MovingObject& other, const CollisionHit& ) const
{
if (mystate != STATE_SUCKING) return false;
if (dynamic_cast<Lantern*>(&other)) return true;
Expand All @@ -254,7 +254,7 @@ GhostTree::collides(GameObject& other, const CollisionHit& ) const
}

HitResponse
GhostTree::collision(GameObject& other, const CollisionHit& )
GhostTree::collision(MovingObject& other, const CollisionHit& )
{
if (mystate != STATE_SUCKING) return ABORT_MOVE;

Expand Down
4 changes: 2 additions & 2 deletions src/badguy/ghosttree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class GhostTree final : public Boss
virtual void active_update(float dt_sec) override;
virtual void draw(DrawingContext& context) override;

virtual bool collides(GameObject& other, const CollisionHit& hit) const override;
virtual HitResponse collision(GameObject& other, const CollisionHit& hit) override;
virtual bool collides(MovingObject& other, const CollisionHit& hit) const override;
virtual HitResponse collision(MovingObject& other, const CollisionHit& hit) override;

static std::string class_name() { return "ghosttree"; }
virtual std::string get_class_name() const override { return class_name(); }
Expand Down
2 changes: 1 addition & 1 deletion src/badguy/ghoul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Ghoul::Ghoul(const ReaderMapping& reader) :
}

bool
Ghoul::collision_squished(GameObject& object)
Ghoul::collision_squished(MovingObject& object)
{
auto player = Sector::get().get_nearest_player(m_col.m_bbox);
if (player)
Expand Down
2 changes: 1 addition & 1 deletion src/badguy/ghoul.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Ghoul final : public BadGuy,
void move_to(const Vector& pos) override;

protected:
bool collision_squished(GameObject& object) override;
bool collision_squished(MovingObject& object) override;
std::vector<Direction> get_allowed_directions() const override;

private:
Expand Down
2 changes: 2 additions & 0 deletions src/badguy/goldbomb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class GoldBomb final : public MrBomb
virtual GameObjectClasses get_class_types() const override { return WalkingBadguy::get_class_types().add(typeid(GoldBomb)); }
virtual bool is_snipable() const override { return true; }

int get_coins_worth() const override { return 10; }

virtual void explode() override;

private:
Expand Down
4 changes: 2 additions & 2 deletions src/badguy/granito.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ Granito::collision_player(Player& player, const CollisionHit& hit)
}

HitResponse
Granito::collision(GameObject& other, const CollisionHit& hit)
Granito::collision(MovingObject& other, const CollisionHit& hit)
{
if (hit.top)
m_col.propagate_movement(m_col.get_movement());
Expand Down Expand Up @@ -529,7 +529,7 @@ Granito::try_jump()
else
{
auto result_obj = std::get_if<CollisionObject*>(&result.hit);
if (result_obj && !dynamic_cast<Granito*>(&(*result_obj)->get_listener()))
if (result_obj && !dynamic_cast<Granito*>(&(*result_obj)->get_parent()))
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/badguy/granito.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Granito : public WalkingBadguy
virtual void active_update(float dt_sec) override;

virtual HitResponse collision_player(Player& player, const CollisionHit& hit) override;
virtual HitResponse collision(GameObject& other, const CollisionHit& hit) override;
virtual HitResponse collision(MovingObject& other, const CollisionHit& hit) override;

static std::string class_name() { return "granito"; }
virtual std::string get_class_name() const override { return class_name(); }
Expand Down
2 changes: 1 addition & 1 deletion src/badguy/haywire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Haywire::get_player_direction(const Player* player) const
}

bool
Haywire::collision_squished(GameObject& object)
Haywire::collision_squished(MovingObject& object)
{
if (m_frozen)
return WalkingBadguy::collision_squished(object);
Expand Down
2 changes: 1 addition & 1 deletion src/badguy/haywire.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Haywire final : public WalkingBadguy
inline bool is_exploding() const { return m_is_exploding; }

protected:
virtual bool collision_squished(GameObject& object) override;
virtual bool collision_squished(MovingObject& object) override;
virtual void collision_solid(const CollisionHit& hit) override;

private:
Expand Down
4 changes: 2 additions & 2 deletions src/badguy/kamikazesnowball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ KamikazeSnowball::initialize()
}

bool
KamikazeSnowball::collision_squished(GameObject& object)
KamikazeSnowball::collision_squished(MovingObject& object)
{
if (m_frozen)
return BadGuy::collision_squished(object);
Expand Down Expand Up @@ -153,7 +153,7 @@ LeafShot::unfreeze(bool melt)
}

bool
LeafShot::collision_squished(GameObject& object)
LeafShot::collision_squished(MovingObject& object)
{
if (m_frozen)
return BadGuy::collision_squished(object);
Expand Down
4 changes: 2 additions & 2 deletions src/badguy/kamikazesnowball.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class KamikazeSnowball : public BadGuy
virtual bool is_snipable() const override { return true; }

protected:
virtual bool collision_squished(GameObject& object) override;
virtual bool collision_squished(MovingObject& object) override;
virtual HitResponse collision_player(Player& player, const CollisionHit& hit) override;
virtual void kill_collision();

Expand Down Expand Up @@ -72,7 +72,7 @@ class LeafShot final : public KamikazeSnowball
std::string get_default_sprite_name() const override;

protected:
virtual bool collision_squished(GameObject& object) override;
virtual bool collision_squished(MovingObject& object) override;

private:
enum Type {
Expand Down
2 changes: 1 addition & 1 deletion src/badguy/mole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Mole::collision_badguy(BadGuy& , const CollisionHit& )
}

bool
Mole::collision_squished(GameObject& obj)
Mole::collision_squished(MovingObject& obj)
{
set_state(DEAD);

Expand Down
2 changes: 1 addition & 1 deletion src/badguy/mole.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Mole final : public BadGuy

virtual void kill_fall() override;
virtual HitResponse collision_badguy(BadGuy& , const CollisionHit& ) override;
virtual bool collision_squished(GameObject& object) override;
virtual bool collision_squished(MovingObject& object) override;

virtual void activate() override;
virtual void active_update(float) override;
Expand Down
4 changes: 2 additions & 2 deletions src/badguy/mrbomb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ MrBomb::collision_solid(const CollisionHit& hit)
}

HitResponse
MrBomb::collision(GameObject& object, const CollisionHit& hit)
MrBomb::collision(MovingObject& object, const CollisionHit& hit)
{
if (m_state == MB_STATE_TICKING)
{
Expand Down Expand Up @@ -119,7 +119,7 @@ MrBomb::collision_badguy(BadGuy& badguy, const CollisionHit& hit)
}

bool
MrBomb::collision_squished(GameObject& object)
MrBomb::collision_squished(MovingObject& object)
{
if (m_frozen)
return WalkingBadguy::collision_squished(object);
Expand Down
4 changes: 2 additions & 2 deletions src/badguy/mrbomb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class MrBomb : public WalkingBadguy
const std::string& glow_sprite = "images/creatures/mr_bomb/ticking_glow/ticking_glow.sprite");

virtual void collision_solid(const CollisionHit& hit) override;
virtual HitResponse collision(GameObject& object, const CollisionHit& hit) override;
virtual HitResponse collision(MovingObject& object, const CollisionHit& hit) override;
virtual HitResponse collision_player(Player& player, const CollisionHit& hit) override;
virtual HitResponse collision_badguy(BadGuy& badguy, const CollisionHit& hit) override;

Expand Down Expand Up @@ -63,7 +63,7 @@ class MrBomb : public WalkingBadguy
protected:
void update_ticking(float dt_sec);

virtual bool collision_squished(GameObject& object) override;
virtual bool collision_squished(MovingObject& object) override;

protected:
enum State : uint8_t {
Expand Down
Loading

0 comments on commit 725a20a

Please sign in to comment.