Skip to content

Commit

Permalink
Improvements to trigger objects code
Browse files Browse the repository at this point in the history
Issues improvements to the code of trigger objects.

Triggers, utilizing sprites, instead of managing sprites on their own, now inherit the new `SpritedTrigger` class, which inherits both `MovingSprite` and `TriggerBase`.
Triggers, which do not utilize sprites, inherit the new `Trigger` class, which inherits both `MovingObject` and `TriggerBase`.

A lot of unneeded leftover code has been removed. This includes unused constructors, leftover variables and functions.
  • Loading branch information
Vankata453 committed Jul 25, 2023
1 parent 263ec93 commit de5682f
Show file tree
Hide file tree
Showing 16 changed files with 156 additions and 373 deletions.
37 changes: 5 additions & 32 deletions src/trigger/climbable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,14 @@ const float POSITION_FIX_AY = 50; // y-wise acceleration applied to player when
}

Climbable::Climbable(const ReaderMapping& reader) :
Trigger(reader),
climbed_by(),
trying_to_climb(),
message(),
new_size(0.0f, 0.0f)
message()
{
reader.get("x", m_col.m_bbox.get_left());
reader.get("y", m_col.m_bbox.get_top());
float w = 32, h = 32;
reader.get("width", w);
reader.get("height", h);
m_col.m_bbox.set_size(w, h);
new_size.x = w;
new_size.y = h;
reader.get("message", message);
}

Climbable::Climbable(const Rectf& area) :
climbed_by(),
trying_to_climb(),
message(),
new_size(0.0f, 0.0f)
{
m_col.m_bbox = area;
}

Climbable::~Climbable()
{
for (auto* player : climbed_by)
Expand All @@ -71,13 +54,7 @@ Climbable::~Climbable()
ObjectSettings
Climbable::get_settings()
{
new_size.x = m_col.m_bbox.get_width();
new_size.y = m_col.m_bbox.get_height();

ObjectSettings result = TriggerBase::get_settings();

// result.add_float(_("Width"), &new_size.x, "width");
// result.add_float(_("Height"), &new_size.y, "height");
ObjectSettings result = Trigger::get_settings();

result.add_translatable_text(_("Message"), &message, "message");

Expand All @@ -86,15 +63,11 @@ Climbable::get_settings()
return result;
}

void
Climbable::after_editor_set() {
m_col.m_bbox.set_size(new_size.x, new_size.y);
}

void
Climbable::update(float dt_sec)
{
TriggerBase::update(dt_sec);
Trigger::update(dt_sec);

auto it = climbed_by.begin();
while (it != climbed_by.end())
{
Expand Down
9 changes: 1 addition & 8 deletions src/trigger/climbable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@
#include "supertux/timer.hpp"

class Color;
class DrawingContext;
class Player;
class ReaderMapping;

class Climbable final : public TriggerBase
class Climbable final : public Trigger
{
private:
struct ClimbPlayer
Expand All @@ -42,7 +39,6 @@ class Climbable final : public TriggerBase

public:
Climbable(const ReaderMapping& reader);
Climbable(const Rectf& area);
~Climbable() override;

static std::string class_name() { return "climbable"; }
Expand All @@ -52,7 +48,6 @@ class Climbable final : public TriggerBase
virtual bool has_variable_size() const override { return true; }

virtual ObjectSettings get_settings() override;
virtual void after_editor_set() override;

virtual void event(Player& player, EventType type) override;
virtual void update(float dt_sec) override;
Expand All @@ -67,8 +62,6 @@ class Climbable final : public TriggerBase
std::string message;

private:
Vector new_size;

Climbable(const Climbable&) = delete;
Climbable& operator=(const Climbable&) = delete;
};
Expand Down
79 changes: 17 additions & 62 deletions src/trigger/door.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,73 +30,32 @@
#include "util/reader_mapping.hpp"

Door::Door(const ReaderMapping& mapping) :
TriggerBase(mapping),
SpritedTrigger(mapping, "images/objects/door/door.sprite"),
state(CLOSED),
target_sector(),
target_spawnpoint(),
script(),
sprite_name("images/objects/door/door.sprite"),
sprite(),
lock_sprite(SpriteManager::current()->create("images/objects/door/door_lock.sprite")),
stay_open_timer(),
unlocking_timer(),
lock_warn_timer(),
m_flip(NO_FLIP),
m_locked(),
lock_color(Color::WHITE)
{
mapping.get("x", m_col.m_bbox.get_left());
mapping.get("y", m_col.m_bbox.get_top());
mapping.get("sector", target_sector);
mapping.get("spawnpoint", target_spawnpoint);
mapping.get("sprite", sprite_name);
mapping.get("script", script);
mapping.get("locked", m_locked);

state = m_locked ? DoorState::LOCKED : DoorState::CLOSED;

mapping.get("script", script);

sprite = SpriteManager::current()->create(sprite_name);
sprite->set_action("closed");
m_col.m_bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
set_action("closed");

std::vector<float> vColor;
if (mapping.get("lock-color", vColor)) {
if (mapping.get("lock-color", vColor))
lock_color = Color(vColor);
}
else
{
lock_color = Color::WHITE;
}
lock_sprite->set_color(lock_color);

SoundManager::current()->preload("sounds/door.wav");
// TODO: Add proper sounds
SoundManager::current()->preload("sounds/locked.ogg");
SoundManager::current()->preload("sounds/turnkey.ogg");
}

Door::Door(int x, int y, const std::string& sector, const std::string& spawnpoint) :
TriggerBase(),
state(CLOSED),
target_sector(sector),
target_spawnpoint(spawnpoint),
script(),
sprite_name("images/objects/door/door.sprite"),
sprite(SpriteManager::current()->create(sprite_name)),
lock_sprite(SpriteManager::current()->create("images/objects/door/door_lock.sprite")),
stay_open_timer(),
unlocking_timer(),
lock_warn_timer(),
m_flip(NO_FLIP),
lock_color()
{
state = m_locked ? DoorState::LOCKED : DoorState::CLOSED;
m_col.m_bbox.set_pos(Vector(static_cast<float>(x), static_cast<float>(y)));

sprite->set_action("closed");
m_col.m_bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());

lock_sprite->set_color(lock_color);

SoundManager::current()->preload("sounds/door.wav");
Expand All @@ -108,9 +67,8 @@ Door::Door(int x, int y, const std::string& sector, const std::string& spawnpoin
ObjectSettings
Door::get_settings()
{
ObjectSettings result = TriggerBase::get_settings();
ObjectSettings result = SpritedTrigger::get_settings();

result.add_sprite(_("Sprite"), &sprite_name, "sprite", std::string("images/objects/door/door.sprite"));
result.add_script(_("Script"), &script, "script");
result.add_text(_("Sector"), &target_sector, "sector");
result.add_text(_("Spawn point"), &target_spawnpoint, "spawnpoint");
Expand All @@ -123,14 +81,11 @@ Door::get_settings()
}

void
Door::after_editor_set() {
sprite = SpriteManager::current()->create(sprite_name);
m_col.m_bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
lock_sprite->set_color(lock_color);
}

Door::~Door()
Door::after_editor_set()
{
SpritedTrigger::after_editor_set();

lock_sprite->set_color(lock_color);
}

void
Expand All @@ -141,24 +96,24 @@ Door::update(float )
break;
case OPENING:
// if door has finished opening, start timer and keep door open
if (sprite->animation_done()) {
if (m_sprite->animation_done()) {
state = OPEN;
sprite->set_action("open");
set_action("open");
stay_open_timer.start(1.0);
}
break;
case OPEN:
// if door was open long enough, start closing it
if (stay_open_timer.check()) {
state = CLOSING;
sprite->set_action("closing", 1);
set_action("closing", 1);
}
break;
case CLOSING:
// if door has finished closing, keep it shut
if (sprite->animation_done()) {
if (m_sprite->animation_done()) {
state = CLOSED;
sprite->set_action("closed");
set_action("closed");
}
break;
case LOCKED:
Expand All @@ -181,7 +136,7 @@ Door::update(float )
void
Door::draw(DrawingContext& context)
{
sprite->draw(context.color(), m_col.m_bbox.p1(), LAYER_BACKGROUNDTILES+1, m_flip);
m_sprite->draw(context.color(), m_col.m_bbox.p1(), LAYER_BACKGROUNDTILES+1, m_flip);

if (state == DoorState::LOCKED || state == DoorState::UNLOCKING)
{
Expand All @@ -201,7 +156,7 @@ Door::event(Player& , EventType type)
if (type == EVENT_ACTIVATE) {
state = OPENING;
SoundManager::current()->play("sounds/door.wav", get_pos());
sprite->set_action("opening", 1);
set_action("opening", 1);
ScreenManager::current()->set_screen_fade(std::make_unique<FadeToBlack>(FadeToBlack::FADEOUT, 1.0f));
}
break;
Expand Down Expand Up @@ -236,7 +191,7 @@ Door::collision(GameObject& other, const CollisionHit& hit_)

if (player) {
state = CLOSING;
sprite->set_action("closing", 1);
set_action("closing", 1);
if (!script.empty()) {
Sector::get().run_script(script, "Door");
}
Expand Down
20 changes: 8 additions & 12 deletions src/trigger/door.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,14 @@
#ifndef HEADER_SUPERTUX_TRIGGER_DOOR_HPP
#define HEADER_SUPERTUX_TRIGGER_DOOR_HPP

#include "supertux/timer.hpp"
#include "trigger/trigger_base.hpp"
#include "video/flip.hpp"

class Player;
class ReaderMapping;
#include "supertux/timer.hpp"

class Door final : public TriggerBase
class Door final : public SpritedTrigger
{
public:
Door(const ReaderMapping& reader);
Door(int x, int y, const std::string& sector, const std::string& spawnpoint);
~Door() override;

static std::string class_name() { return "door"; }
virtual std::string get_class_name() const override { return class_name(); }
Expand All @@ -42,10 +37,14 @@ class Door final : public TriggerBase
virtual void update(float dt_sec) override;
virtual void draw(DrawingContext& context) override;
virtual void event(Player& player, EventType type) override;

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

virtual void on_flip(float height) override;
virtual bool is_locked() const { return m_locked; }
virtual void unlock();

bool is_locked() const { return m_locked; }
void unlock();

Color get_lock_color() const { return lock_color; }

private:
Expand All @@ -63,13 +62,10 @@ class Door final : public TriggerBase
std::string target_sector; /**< target sector to teleport to */
std::string target_spawnpoint; /**< target spawnpoint to teleport to */
std::string script;
std::string sprite_name;
SpritePtr sprite; /**< "door" sprite to render */
SpritePtr lock_sprite;
Timer stay_open_timer; /**< time until door will close again */
Timer unlocking_timer;
Timer lock_warn_timer;
Flip m_flip;
bool m_locked;
Color lock_color;

Expand Down
Loading

0 comments on commit de5682f

Please sign in to comment.