Skip to content

Commit

Permalink
replace direction bools with Direction for bumpers and pushbuttons
Browse files Browse the repository at this point in the history
code quality
  • Loading branch information
Narre committed Aug 5, 2023
1 parent 1b61e53 commit 106c2cf
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 48 deletions.
23 changes: 17 additions & 6 deletions data/images/objects/pushbutton/pushbutton.sprite
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
(supertux-sprite
(action
(name "off")
(name "off-up")
(hitbox 0 0 30 16)
(images "pushbutton-0.png")
)
(action
(name "on")
(name "on-up")
(hitbox 0 0 30 10)
(loops 1)
(fps 20)
(loops 1)
(fps 20)
(images "pushbutton-1.png"
"pushbutton-2.png"
"pushbutton-3.png")
"pushbutton-2.png"
"pushbutton-3.png"
)
)
(action
(name "off-down")
(hitbox 0 0 30 16)
(flip-action "off-up")
)
(action
(name "on-down")
(hitbox 0 0 30 10)
(flip-action "on-up")
)
)
12 changes: 6 additions & 6 deletions data/images/objects/trampoline/bumper.sprite
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
(supertux-sprite
(action
(name "right-normal")
(name "normal-right")
(hitbox 0 2 18 27)
(images "right-0.png")
)
(action
(name "right-swinging")
(name "swinging-right")
(fps 24)
(hitbox 0 2 18 27)
(images "right-0.png"
Expand All @@ -17,14 +17,14 @@
)
)
(action
(name "left-normal")
(name "normal-left")
(hitbox 14 2 18 27)
(mirror-action "right-normal")
(mirror-action "normal-right")
)
(action
(name "left-swinging")
(name "swinging-left")
(fps 24)
(hitbox 14 2 18 27)
(mirror-action "right-swinging")
(mirror-action "swinging-right")
)
)
39 changes: 28 additions & 11 deletions src/object/bumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,45 @@ const float BOUNCE_X = 700.0f;
Bumper::Bumper(const ReaderMapping& reader) :
MovingSprite(reader, "images/objects/trampoline/bumper.sprite", LAYER_OBJECTS, COLGROUP_MOVING),
m_physic(),
m_facing_left()
m_dir(Direction::RIGHT),
m_dir_in_allowed(0),
m_allowed_directions({Direction::LEFT, Direction::RIGHT})
{
reader.get("left", m_facing_left);
set_action(m_facing_left ? "left-normal" : "right-normal");
std::string dir_str;
bool old_facing_left = false;

if (reader.get("direction", dir_str))
m_dir = string_to_dir(dir_str);
else if (reader.get("left", old_facing_left) && old_facing_left)
m_dir = Direction::LEFT;
set_action("normal", m_dir);
m_physic.enable_gravity(false);

for (int i = 0; i < static_cast<int>(m_allowed_directions.size()); ++i)
{
if (m_allowed_directions[i] == m_dir)
{
m_dir_in_allowed = i;
break;
}
}
}

ObjectSettings
Bumper::get_settings()
{
ObjectSettings result = MovingSprite::get_settings();

result.add_bool(_("Facing Left"), &m_facing_left, "left", false);
result.reorder({"left", "sprite", "x", "y"});
result.add_direction(_("Direction"), reinterpret_cast<Direction*>(&m_dir_in_allowed), Direction::RIGHT, "direction", 0, m_allowed_directions);
result.reorder({"direction", "sprite", "x", "y"});
return result;
}

void
Bumper::update(float dt_sec)
{
if (m_sprite->animation_done())
{
set_action(m_facing_left ? "left-normal" : "right-normal");
}
set_action("normal", m_dir);
m_col.set_movement(m_physic.get_movement (dt_sec));
}

Expand All @@ -65,11 +80,11 @@ Bumper::collision(GameObject& other, const CollisionHit& hit)
auto player = dynamic_cast<Player*> (&other);
if (player)
{
float BOUNCE_DIR = m_facing_left ? -BOUNCE_X : BOUNCE_X;
float BOUNCE_DIR = m_dir == Direction::LEFT ? -BOUNCE_X : BOUNCE_X;
player->get_physic().set_velocity(0.f, player->is_swimming() ? 0.f : BOUNCE_Y);
player->sideways_push(BOUNCE_DIR);
SoundManager::current()->play(TRAMPOLINE_SOUND, get_pos());
set_action((m_facing_left ? "left-swinging" : "right-swinging"), 1);
set_action("swinging", m_dir, 1);
}
auto bumper = dynamic_cast<Bumper*> (&other);
if (bumper)
Expand All @@ -90,7 +105,9 @@ void
Bumper::after_editor_set()
{
MovingSprite::after_editor_set();
set_action(m_facing_left ? "left-normal" : "right-normal");

m_dir = m_allowed_directions[m_dir_in_allowed];
set_action("normal", m_dir);
}

void
Expand Down
6 changes: 5 additions & 1 deletion src/object/bumper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "supertux/physic.hpp"

enum class Direction;
class Player;

class Bumper final : public MovingSprite
Expand All @@ -44,7 +45,10 @@ class Bumper final : public MovingSprite

private:
Physic m_physic;
bool m_facing_left;

Direction m_dir;
int m_dir_in_allowed;
std::vector<Direction> m_allowed_directions;

private:
Bumper(const Bumper&) = delete;
Expand Down
59 changes: 38 additions & 21 deletions src/object/pushbutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "object/player.hpp"
#include "object/rock.hpp"
#include "sprite/sprite.hpp"
#include "supertux/direction.hpp"
#include "supertux/flip_level_transformer.hpp"
#include "supertux/sector.hpp"
#include "util/reader_mapping.hpp"
Expand All @@ -31,32 +32,48 @@ const std::string BUTTON_SOUND = "sounds/switch.ogg";

PushButton::PushButton(const ReaderMapping& mapping) :
MovingSprite(mapping, "images/objects/pushbutton/pushbutton.sprite", LAYER_BACKGROUNDTILES+1, COLGROUP_MOVING),
script(),
state(OFF),
m_upside_down(false)
m_script(),
m_state(OFF),
m_dir(Direction::UP),
m_dir_in_allowed(0),
m_allowed_directions({Direction::UP, Direction::DOWN})
{
SoundManager::current()->preload(BUTTON_SOUND);
set_action("off", -1);

if (!mapping.get("script", script))
if (!mapping.get("script", m_script))
{
log_warning << "No script set for pushbutton." << std::endl;
}

mapping.get("upside-down", m_upside_down);
if (m_upside_down)
FlipLevelTransformer::transform_flip(m_flip);
bool old_upside_down;
std::string dir_str;

if (mapping.get("direction", dir_str))
m_dir = string_to_dir(dir_str);
else if (mapping.get("upside-down", old_upside_down) && old_upside_down)
m_dir = Direction::DOWN;

for (int i = 0; i < static_cast<int>(m_allowed_directions.size()); ++i)
{
if (m_allowed_directions[i] == m_dir)
{
m_dir_in_allowed = i;
break;
}
}

set_action("off", m_dir, -1);
}

ObjectSettings
PushButton::get_settings()
{
ObjectSettings result = MovingSprite::get_settings();

result.add_script(_("Script"), &script, "script");
result.add_bool(_("Upside down"), &m_upside_down, "upside-down");
result.add_direction(_("Direction"), reinterpret_cast<Direction*>(&m_dir_in_allowed), Direction::UP, "direction", 0, m_allowed_directions);
result.add_script(_("Script"), &m_script, "script");

result.reorder({"script", "upside-down", "x", "y"});
result.reorder({"direction", "script", "x", "y"});

return result;
}
Expand All @@ -65,8 +82,8 @@ void
PushButton::after_editor_set()
{
MovingSprite::after_editor_set();
if ((m_upside_down && m_flip == NO_FLIP) || (!m_upside_down && m_flip == VERTICAL_FLIP))
FlipLevelTransformer::transform_flip(m_flip);
m_dir = m_allowed_directions[m_dir_in_allowed];
set_action("off", m_dir);
}

void
Expand All @@ -85,7 +102,7 @@ PushButton::collision(GameObject& other, const CollisionHit& hit)
{
float vy = player->get_physic().get_velocity_y();

if (m_upside_down)
if (m_dir == Direction::DOWN)
{
if (vy >= 0)
return FORCE_MOVE;
Expand All @@ -106,22 +123,22 @@ PushButton::collision(GameObject& other, const CollisionHit& hit)
}
}

if (state != OFF || !(m_upside_down ? hit.bottom : hit.top))
if (m_state != OFF || !(m_dir == Direction::DOWN ? hit.bottom : hit.top))
return FORCE_MOVE;

// change appearance
state = ON;
m_state = ON;
float old_bbox_height = m_col.m_bbox.get_height();
set_action("on", -1);
set_action("on", m_dir, -1);
float new_bbox_height = m_col.m_bbox.get_height();
Vector delta(0, old_bbox_height - new_bbox_height);
set_pos(get_pos() + delta * (m_upside_down ? 0 : 1.f));
set_pos(get_pos() + delta * (m_dir == Direction::DOWN ? 0 : 1.f));

// play sound
SoundManager::current()->play(BUTTON_SOUND, get_pos());

// run script
Sector::get().run_script(script, "PushButton");
Sector::get().run_script(m_script, "PushButton");

return FORCE_MOVE;
}
Expand All @@ -130,8 +147,8 @@ void
PushButton::on_flip(float height)
{
MovingSprite::on_flip(height);
FlipLevelTransformer::transform_flip(m_flip);
m_upside_down = !m_upside_down;
m_dir = m_dir == Direction::UP ? Direction::DOWN : Direction::UP;
set_action(m_state == OFF ? "off" : "on", m_dir);
}

/* EOF */
11 changes: 8 additions & 3 deletions src/object/pushbutton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include "object/moving_sprite.hpp"

enum class Direction;

/** PushButton - jump on it to run a script */
class PushButton final : public MovingSprite
{
Expand All @@ -43,9 +45,12 @@ class PushButton final : public MovingSprite
ON
};

std::string script;
PushButtonState state;
bool m_upside_down;
std::string m_script;
PushButtonState m_state;

Direction m_dir;
int m_dir_in_allowed;
std::vector<Direction> m_allowed_directions;

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

0 comments on commit 106c2cf

Please sign in to comment.