Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* Portables: Remember owner, fix SuperTux#1058

Portables now know their current owner. This enables them not to force
delete being out of level bounds, when held by a player, which
solves SuperTux#1058.

* Removed some redundant class members

* Marking methods as constant for Portable
  • Loading branch information
Zwatotem authored and tobbi committed Sep 12, 2019
1 parent cb6b11c commit 2565458
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 84 deletions.
2 changes: 1 addition & 1 deletion INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ libraries installed. Note that most of these things should already be
available prepackaged and optimized for your distribution, it is
recommended that you check your distribution first before downloading
from the websites. You can also check
<hhttps://github.com/SuperTux/wiki/blob/master/Building-SuperTux.md> for up-to-date
<https://github.com/SuperTux/wiki/blob/master/Building-SuperTux.md> for up-to-date
build instructions for a variety of different platforms and
distributions.

Expand Down
32 changes: 18 additions & 14 deletions src/badguy/badguy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,24 @@ void
BadGuy::update(float dt_sec)
{
if (!Sector::get().inside(m_col.m_bbox)) {
run_dead_script();
m_is_active_flag = false;
remove_me();
// This was removed due to fixing a bug. If is it needed somewhere, then I'm sorry. --Hume2
/**if(countMe) {
// get badguy name from sprite_name ignoring path and extension
std::string badguy = sprite_name.substr(0, sprite_name.length() - 7);
int path_chars = badguy.rfind("/",badguy.length());
badguy = badguy.substr(path_chars + 1, badguy.length() - path_chars);
// log warning since badguys_killed can no longer reach total_badguys
std::string current_level = "[" + Sector::get().get_level()->filename + "] ";
log_warning << current_level << "Counted badguy " << badguy << " starting at " << start_position << " has left the sector" <<std::endl;;
}*/
return;
auto this_portable = dynamic_cast<Portable*> (this);
if (!this_portable || !this_portable->is_grabbed())
{
run_dead_script();
m_is_active_flag = false;
remove_me();
// This was removed due to fixing a bug. If is it needed somewhere, then I'm sorry. --Hume2
/**if(countMe) {
// get badguy name from sprite_name ignoring path and extension
std::string badguy = sprite_name.substr(0, sprite_name.length() - 7);
int path_chars = badguy.rfind("/",badguy.length());
badguy = badguy.substr(path_chars + 1, badguy.length() - path_chars);
// log warning since badguys_killed can no longer reach total_badguys
std::string current_level = "[" + Sector::get().get_level()->filename + "] ";
log_warning << current_level << "Counted badguy " << badguy << " starting at " << start_position << " has left the sector" <<std::endl;;
}*/
return;
}
}
if ((m_state != STATE_INACTIVE) && is_offscreen()) {
if (m_state == STATE_ACTIVE) deactivate();
Expand Down
15 changes: 6 additions & 9 deletions src/badguy/bomb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

Bomb::Bomb(const Vector& pos, Direction dir_, const std::string& custom_sprite /*= "images/creatures/mr_bomb/mr_bomb.sprite"*/ ) :
BadGuy( pos, dir_, custom_sprite ),
grabbed(false),
grabber(nullptr),
ticking(SoundManager::current()->create_sound_source("sounds/fizz.wav"))
{
set_action(dir_ == Direction::LEFT ? "ticking-left" : "ticking-right", 1);
Expand All @@ -42,7 +40,7 @@ Bomb::Bomb(const Vector& pos, Direction dir_, const std::string& custom_sprite /
void
Bomb::collision_solid(const CollisionHit& hit)
{
if (grabbed) {
if (is_grabbed()) {
return;
}
if (hit.top || hit.bottom)
Expand Down Expand Up @@ -76,7 +74,7 @@ Bomb::active_update(float dt_sec)
if (m_sprite->animation_done()) {
explode();
}
else if (!grabbed) {
else if (!is_grabbed()) {
m_col.m_movement = m_physic.get_movement(dt_sec);
}
}
Expand All @@ -89,8 +87,8 @@ Bomb::explode()
// Make the player let go before we explode, otherwise the player is holding
// an invalid object. There's probably a better way to do this than in the
// Bomb class.
if (grabber != nullptr) {
auto player = dynamic_cast<Player*>(grabber);
if (is_grabbed()) {
auto player = dynamic_cast<Player*>(m_owner);

if (player)
player->stop_grabbing();
Expand Down Expand Up @@ -119,15 +117,14 @@ Bomb::ignite()
void
Bomb::grab(MovingObject& object, const Vector& pos, Direction dir_)
{
Portable::grab(object, pos, dir_);
m_col.m_movement = pos - get_pos();
m_dir = dir_;

// We actually face the opposite direction of Tux here to make the fuse more
// visible instead of hiding it behind Tux
m_sprite->set_action_continued(m_dir == Direction::LEFT ? "ticking-right" : "ticking-left");
set_colgroup_active(COLGROUP_DISABLED);
grabbed = true;
grabber = &object;
}

void
Expand Down Expand Up @@ -156,7 +153,7 @@ Bomb::ungrab(MovingObject& object, Direction dir_)
static_cast<float>(toss_velocity_y));

set_colgroup_active(COLGROUP_MOVING);
grabbed = false;
Portable::ungrab(object, dir_);
}

void Bomb::stop_looping_sounds()
Expand Down
3 changes: 0 additions & 3 deletions src/badguy/bomb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ class Bomb final : public BadGuy,
virtual void play_looping_sounds() override;

private:
bool grabbed;
MovingObject* grabber;

std::unique_ptr<SoundSource> ticking;

private:
Expand Down
20 changes: 8 additions & 12 deletions src/badguy/goldbomb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
GoldBomb::GoldBomb(const ReaderMapping& reader) :
WalkingBadguy(reader, "images/creatures/gold_bomb/gold_bomb.sprite", "left", "right"),
tstate(STATE_NORMAL),
grabbed(false),
grabber(nullptr),
ticking()
{
walk_speed = 80;
Expand Down Expand Up @@ -80,7 +78,7 @@ GoldBomb::collision(GameObject& object, const CollisionHit& hit)
return ABORT_MOVE;
}
}
if (grabbed)
if (is_grabbed())
return FORCE_MOVE;
return WalkingBadguy::collision(object, hit);
}
Expand All @@ -90,7 +88,7 @@ GoldBomb::collision_player(Player& player, const CollisionHit& hit)
{
if (tstate == STATE_TICKING)
return FORCE_MOVE;
if (grabbed)
if (is_grabbed())
return FORCE_MOVE;
return WalkingBadguy::collision_player(player, hit);
}
Expand Down Expand Up @@ -140,12 +138,12 @@ GoldBomb::active_update(float dt_sec)
if (m_sprite->animation_done()) {
kill_fall();
}
else if (!grabbed) {
else if (!is_grabbed()) {
m_col.m_movement = m_physic.get_movement(dt_sec);
}
return;
}
if (grabbed)
if (is_grabbed())
return;
WalkingBadguy::active_update(dt_sec);
}
Expand All @@ -159,8 +157,8 @@ GoldBomb::kill_fall()
// Make the player let go before we explode, otherwise the player is holding
// an invalid object. There's probably a better way to do this than in the
// GoldBomb class.
if (grabber != nullptr) {
Player* player = dynamic_cast<Player*>(grabber);
if (is_grabbed()) {
Player* player = dynamic_cast<Player*>(m_owner);

if (player)
player->stop_grabbing();
Expand All @@ -184,6 +182,7 @@ GoldBomb::ignite()
void
GoldBomb::grab(MovingObject& object, const Vector& pos, Direction dir_)
{
Portable::grab(object,pos,dir_);
if (tstate == STATE_TICKING){
m_col.m_movement = pos - get_pos();
m_dir = dir_;
Expand All @@ -192,15 +191,12 @@ GoldBomb::grab(MovingObject& object, const Vector& pos, Direction dir_)
// visible instead of hiding it behind Tux
m_sprite->set_action_continued(m_dir == Direction::LEFT ? "ticking-right" : "ticking-left");
set_colgroup_active(COLGROUP_DISABLED);
grabbed = true;
grabber = &object;
}
else if (m_frozen){
m_col.m_movement = pos - get_pos();
m_dir = dir_;
m_sprite->set_action(dir_ == Direction::LEFT ? "iced-left" : "iced-right");
set_colgroup_active(COLGROUP_DISABLED);
grabbed = true;
}
}

Expand Down Expand Up @@ -229,7 +225,7 @@ GoldBomb::ungrab(MovingObject& object, Direction dir_)
m_physic.set_velocity(static_cast<float>(toss_velocity_x),
static_cast<float>(toss_velocity_y));
set_colgroup_active(COLGROUP_MOVING);
grabbed = false;
Portable::ungrab(object, dir_);
}

void
Expand Down
2 changes: 0 additions & 2 deletions src/badguy/goldbomb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ class GoldBomb final : public WalkingBadguy, public Portable

private:
Ticking_State tstate;
bool grabbed;
MovingObject* grabber;

std::unique_ptr<SoundSource> ticking;

Expand Down
17 changes: 8 additions & 9 deletions src/badguy/mrbomb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
#include "util/reader_mapping.hpp"

MrBomb::MrBomb(const ReaderMapping& reader) :
WalkingBadguy(reader, "images/creatures/mr_bomb/mr_bomb.sprite", "left", "right"),
grabbed(false)
WalkingBadguy(reader, "images/creatures/mr_bomb/mr_bomb.sprite", "left", "right")
{
walk_speed = 80;
max_drop_height = 16;
Expand All @@ -51,15 +50,15 @@ MrBomb::MrBomb(const ReaderMapping& reader) :
HitResponse
MrBomb::collision(GameObject& object, const CollisionHit& hit)
{
if (grabbed)
if (is_grabbed())
return FORCE_MOVE;
return WalkingBadguy::collision(object, hit);
}

HitResponse
MrBomb::collision_player(Player& player, const CollisionHit& hit)
{
if (grabbed)
if (is_grabbed())
return FORCE_MOVE;
return WalkingBadguy::collision_player(player, hit);
}
Expand Down Expand Up @@ -93,7 +92,7 @@ MrBomb::collision_squished(GameObject& object)
void
MrBomb::active_update(float dt_sec)
{
if (grabbed)
if (is_grabbed())
return;
WalkingBadguy::active_update(dt_sec);
}
Expand All @@ -116,22 +115,22 @@ MrBomb::ignite()
}

void
MrBomb::grab(MovingObject&, const Vector& pos, Direction dir_)
MrBomb::grab(MovingObject& object, const Vector& pos, Direction dir_)
{
Portable::grab(object, pos, dir_);
assert(m_frozen);
m_col.m_movement = pos - get_pos();
m_dir = dir_;
m_sprite->set_action(dir_ == Direction::LEFT ? "iced-left" : "iced-right");
set_colgroup_active(COLGROUP_DISABLED);
grabbed = true;
}

void
MrBomb::ungrab(MovingObject& , Direction dir_)
MrBomb::ungrab(MovingObject& object, Direction dir_)
{
m_dir = dir_;
set_colgroup_active(COLGROUP_MOVING);
grabbed = false;
Portable::ungrab(object, dir_);
}

bool
Expand Down
3 changes: 0 additions & 3 deletions src/badguy/mrbomb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ class MrBomb final : public WalkingBadguy,
protected:
virtual bool collision_squished(GameObject& object) override;

private:
bool grabbed;

private:
MrBomb(const MrBomb&) = delete;
MrBomb& operator=(const MrBomb&) = delete;
Expand Down
6 changes: 4 additions & 2 deletions src/badguy/mriceblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,9 @@ MrIceBlock::set_state(IceState state_, bool up)
}

void
MrIceBlock::grab(MovingObject&, const Vector& pos, Direction dir_)
MrIceBlock::grab(MovingObject& object, const Vector& pos, Direction dir_)
{
Portable::grab(object, pos, dir_);
m_col.m_movement = pos - get_pos();
m_dir = dir_;
set_action(dir_ == Direction::LEFT ? "flat-left" : "flat-right", /* loops = */ -1);
Expand All @@ -271,7 +272,7 @@ MrIceBlock::grab(MovingObject&, const Vector& pos, Direction dir_)
}

void
MrIceBlock::ungrab(MovingObject& , Direction dir_)
MrIceBlock::ungrab(MovingObject& object, Direction dir_)
{
if (dir_ == Direction::UP) {
set_state(ICESTATE_FLAT, true);
Expand All @@ -280,6 +281,7 @@ MrIceBlock::ungrab(MovingObject& , Direction dir_)
set_state(ICESTATE_KICKED);
}
set_colgroup_active(COLGROUP_MOVING);
Portable::ungrab(object, dir_);
}

bool
Expand Down
15 changes: 6 additions & 9 deletions src/badguy/skydive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
#include "supertux/tile.hpp"

SkyDive::SkyDive(const ReaderMapping& reader) :
BadGuy(reader, "images/creatures/skydive/skydive.sprite"),
is_grabbed(false)
BadGuy(reader, "images/creatures/skydive/skydive.sprite")
{
}

Expand Down Expand Up @@ -52,13 +51,12 @@ SkyDive::collision_badguy(BadGuy&, const CollisionHit& hit)
}

void
SkyDive::grab(MovingObject&, const Vector& pos, Direction dir_)
SkyDive::grab(MovingObject& object, const Vector& pos, Direction dir_)
{
Portable::grab(object, pos, dir_);
m_col.m_movement = pos - get_pos();
m_dir = dir_;

is_grabbed = true;

m_physic.set_velocity_x(m_col.m_movement.x * LOGICAL_FPS);
m_physic.set_velocity_y(0.0);
m_physic.set_acceleration_y(0.0);
Expand All @@ -67,14 +65,13 @@ SkyDive::grab(MovingObject&, const Vector& pos, Direction dir_)
}

void
SkyDive::ungrab(MovingObject& , Direction)
SkyDive::ungrab(MovingObject& object, Direction dir_)
{
is_grabbed = false;

m_physic.set_velocity_y(0);
m_physic.set_acceleration_y(0);
m_physic.enable_gravity(true);
set_colgroup_active(COLGROUP_MOVING);
Portable::ungrab(object, dir_);
}

HitResponse
Expand Down Expand Up @@ -113,7 +110,7 @@ SkyDive::collision_tile(uint32_t tile_attributes)
void
SkyDive::active_update(float dt_sec)
{
if (!is_grabbed)
if (!is_grabbed())
m_col.m_movement = m_physic.get_movement(dt_sec);
}

Expand Down
3 changes: 0 additions & 3 deletions src/badguy/skydive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ class SkyDive final : public BadGuy, public Portable

void explode();

private:
bool is_grabbed;

private:
SkyDive(const SkyDive&) = delete;
SkyDive& operator=(const SkyDive&) = delete;
Expand Down
Loading

0 comments on commit 2565458

Please sign in to comment.