diff --git a/src/trigger/climbable.cpp b/src/trigger/climbable.cpp index cc96cdcd056..c47ed435f1d 100644 --- a/src/trigger/climbable.cpp +++ b/src/trigger/climbable.cpp @@ -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) @@ -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"); @@ -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()) { diff --git a/src/trigger/climbable.hpp b/src/trigger/climbable.hpp index fe35c4f1d7e..ad72a58945e 100644 --- a/src/trigger/climbable.hpp +++ b/src/trigger/climbable.hpp @@ -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 @@ -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"; } @@ -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; @@ -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; }; diff --git a/src/trigger/door.cpp b/src/trigger/door.cpp index 2f5dc6df4c7..3b9e7f0f1f7 100644 --- a/src/trigger/door.cpp +++ b/src/trigger/door.cpp @@ -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 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(x), static_cast(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"); @@ -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"); @@ -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 @@ -141,9 +96,9 @@ 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; @@ -151,14 +106,14 @@ Door::update(float ) // 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: @@ -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) { @@ -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::FADEOUT, 1.0f)); } break; @@ -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"); } diff --git a/src/trigger/door.hpp b/src/trigger/door.hpp index 884a4f24eae..7d9c542fcf7 100644 --- a/src/trigger/door.hpp +++ b/src/trigger/door.hpp @@ -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(); } @@ -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: @@ -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; diff --git a/src/trigger/scripttrigger.cpp b/src/trigger/scripttrigger.cpp index 81d480d0ead..98683307a4c 100644 --- a/src/trigger/scripttrigger.cpp +++ b/src/trigger/scripttrigger.cpp @@ -24,26 +24,18 @@ #include "video/drawing_context.hpp" ScriptTrigger::ScriptTrigger(const ReaderMapping& reader) : - TriggerBase(reader), + Trigger(reader), triggerevent(), script(), - new_size(0.0f, 0.0f), must_activate(false), oneshot(false), runcount(0) { - if (m_col.m_bbox.get_width() == 0.f) - m_col.m_bbox.set_width(32.f); - - if (m_col.m_bbox.get_height() == 0.f) - m_col.m_bbox.set_height(32.f); - reader.get("script", script); reader.get("button", must_activate); reader.get("oneshot", oneshot); - if (script.empty()) { + if (script.empty() && !Editor::is_active()) log_warning << "No script set in script trigger" << std::endl; - } if (must_activate) triggerevent = EVENT_ACTIVATE; @@ -51,26 +43,10 @@ ScriptTrigger::ScriptTrigger(const ReaderMapping& reader) : triggerevent = EVENT_TOUCH; } -ScriptTrigger::ScriptTrigger(const Vector& pos, const std::string& script_) : - TriggerBase(), - triggerevent(EVENT_TOUCH), - script(script_), - new_size(0.0f, 0.0f), - must_activate(), - oneshot(false), - runcount(0) -{ - m_col.m_bbox.set_pos(pos); - m_col.m_bbox.set_size(32, 32); -} - ObjectSettings ScriptTrigger::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(); + ObjectSettings result = Trigger::get_settings(); result.add_script(_("Script"), &script, "script"); result.add_bool(_("Button"), &must_activate, "button"); @@ -81,25 +57,11 @@ ScriptTrigger::get_settings() return result; } -void -ScriptTrigger::after_editor_set() { - //m_col.m_bbox.set_size(new_size.x, new_size.y); - if (must_activate) { - triggerevent = EVENT_ACTIVATE; - } else { - triggerevent = EVENT_TOUCH; - } -} - void ScriptTrigger::event(Player& , EventType type) { - if (type != triggerevent) - return; - - if (oneshot && runcount >= 1) { + if (type != triggerevent || (oneshot && runcount >= 1)) return; - } Sector::get().run_script(script, "ScriptTrigger"); runcount++; @@ -108,10 +70,9 @@ ScriptTrigger::event(Player& , EventType type) void ScriptTrigger::draw(DrawingContext& context) { - if (Editor::is_active() || g_debug.show_collision_rects) { + if (Editor::is_active() || g_debug.show_collision_rects) context.color().draw_filled_rect(m_col.m_bbox, Color(1.0f, 0.0f, 1.0f, 0.6f), 0.0f, LAYER_OBJECTS); - } } /* EOF */ diff --git a/src/trigger/scripttrigger.hpp b/src/trigger/scripttrigger.hpp index a5923593ebd..538990e8752 100644 --- a/src/trigger/scripttrigger.hpp +++ b/src/trigger/scripttrigger.hpp @@ -19,14 +19,10 @@ #include "trigger/trigger_base.hpp" -class ReaderMapping; -class Writer; - -class ScriptTrigger final : public TriggerBase +class ScriptTrigger final : public Trigger { public: ScriptTrigger(const ReaderMapping& reader); - ScriptTrigger(const Vector& pos, const std::string& script); static std::string class_name() { return "scripttrigger"; } virtual std::string get_class_name() const override { return class_name(); } @@ -35,17 +31,13 @@ class ScriptTrigger 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 draw(DrawingContext& context) override; - void write(Writer& writer); - private: EventType triggerevent; std::string script; - Vector new_size; bool must_activate; bool oneshot; int runcount; diff --git a/src/trigger/secretarea_trigger.cpp b/src/trigger/secretarea_trigger.cpp index c8c0ba3a796..857e0c009c6 100644 --- a/src/trigger/secretarea_trigger.cpp +++ b/src/trigger/secretarea_trigger.cpp @@ -32,48 +32,25 @@ static const float MESSAGE_TIME=3.5; SecretAreaTrigger::SecretAreaTrigger(const ReaderMapping& reader) : - TriggerBase(reader), + Trigger(reader), message_timer(), message_displayed(false), message(), fade_tilemap(), - script(), - new_size(0.0f, 0.0f) + script() { - reader.get("x", m_col.m_bbox.get_left()); - reader.get("y", m_col.m_bbox.get_top()); - float w,h; - reader.get("width", w, 32.0f); - reader.get("height", h, 32.0f); - m_col.m_bbox.set_size(w, h); - new_size.x = w; - new_size.y = h; reader.get("fade-tilemap", fade_tilemap); reader.get("message", message); - if (message.empty() && !Editor::is_active()) { - message = _("You found a secret area!"); - } reader.get("script", script); -} -SecretAreaTrigger::SecretAreaTrigger(const Rectf& area, const std::string& fade_tilemap_) : - message_timer(), - message_displayed(false), - message(_("You found a secret area!")), - fade_tilemap(fade_tilemap_), - script(), - new_size(0.0f, 0.0f) -{ - m_col.m_bbox = area; + if (message.empty() && !Editor::is_active()) + message = _("You found a secret area!"); } ObjectSettings SecretAreaTrigger::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(); + ObjectSettings result = Trigger::get_settings(); result.add_text(_("Name"), &m_name); result.add_text(_("Fade tilemap"), &fade_tilemap, "fade-tilemap"); @@ -85,18 +62,6 @@ SecretAreaTrigger::get_settings() return result; } -void -SecretAreaTrigger::after_editor_set() -{ - m_col.m_bbox.set_size(new_size.x, new_size.y); -} - -std::string -SecretAreaTrigger::get_fade_tilemap_name() const -{ - return fade_tilemap; -} - void SecretAreaTrigger::draw(DrawingContext& context) { diff --git a/src/trigger/secretarea_trigger.hpp b/src/trigger/secretarea_trigger.hpp index c6d7b2c04b0..a32a237b5bb 100644 --- a/src/trigger/secretarea_trigger.hpp +++ b/src/trigger/secretarea_trigger.hpp @@ -22,15 +22,14 @@ #include "supertux/timer.hpp" class Color; -class DrawingContext; -class ReaderMapping; -class SecretAreaTrigger final : public TriggerBase +class SecretAreaTrigger final : public Trigger { +public: static Color text_color; + public: SecretAreaTrigger(const ReaderMapping& reader); - SecretAreaTrigger(const Rectf& area, const std::string& fade_tilemap = ""); static std::string class_name() { return "secretarea"; } virtual std::string get_class_name() const override { return class_name(); } @@ -39,12 +38,11 @@ class SecretAreaTrigger 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 draw(DrawingContext& context) override; - std::string get_fade_tilemap_name() const; + const std::string& get_fade_tilemap_name() const { return fade_tilemap; } private: Timer message_timer; @@ -52,7 +50,6 @@ class SecretAreaTrigger final : public TriggerBase std::string message; /**< message to display, default "You found a secret area!" */ std::string fade_tilemap; /**< tilemap to fade away when trigger is activated, or empty if you don't care */ std::string script; /**< optional script to run when trigger is activated */ - Vector new_size; private: SecretAreaTrigger(const SecretAreaTrigger&) = delete; diff --git a/src/trigger/sequence_trigger.cpp b/src/trigger/sequence_trigger.cpp index 95e42d372ad..299bf7f1c58 100644 --- a/src/trigger/sequence_trigger.cpp +++ b/src/trigger/sequence_trigger.cpp @@ -24,53 +24,26 @@ #include "video/drawing_context.hpp" SequenceTrigger::SequenceTrigger(const ReaderMapping& reader) : + Trigger(reader), triggerevent(EVENT_TOUCH), sequence(SEQ_ENDSEQUENCE), - new_size(0.0f, 0.0f), new_spawnpoint(), fade_tilemap(), fade() { - reader.get("x", m_col.m_bbox.get_left(), 0.0f); - reader.get("y", m_col.m_bbox.get_top(), 0.0f); - float w, h; - reader.get("width", w, 32.0f); - reader.get("height", h, 32.0f); - m_col.m_bbox.set_size(w, h); - new_size.x = w; - new_size.y = h; std::string sequence_name; - if (reader.get("sequence", sequence_name)) { + if (reader.get("sequence", sequence_name)) sequence = string_to_sequence(sequence_name); - } reader.get("new_spawnpoint", new_spawnpoint); reader.get("fade_tilemap", fade_tilemap); reader.get("fade", reinterpret_cast(fade)); } -SequenceTrigger::SequenceTrigger(const Vector& pos, const std::string& sequence_name) : - triggerevent(EVENT_TOUCH), - sequence(string_to_sequence(sequence_name)), - new_size(0.0f, 0.0f), - new_spawnpoint(), - fade_tilemap(), - fade() -{ - m_col.m_bbox.set_pos(pos); - m_col.m_bbox.set_size(32, 32); -} - ObjectSettings SequenceTrigger::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_enum(_("Sequence"), reinterpret_cast(&sequence), {_("end sequence"), _("stop Tux"), _("fireworks")}, @@ -88,19 +61,14 @@ SequenceTrigger::get_settings() return result; } -void -SequenceTrigger::after_editor_set() -{ - m_col.m_bbox.set_size(new_size.x, new_size.y); -} - void SequenceTrigger::event(Player& player, EventType type) { - if (type == triggerevent) { - auto data = SequenceData(new_spawnpoint, fade_tilemap, fade); - player.trigger_sequence(sequence, &data); - } + if (type != triggerevent) + return; + + auto data = SequenceData(new_spawnpoint, fade_tilemap, fade); + player.trigger_sequence(sequence, &data); } std::string @@ -112,10 +80,9 @@ SequenceTrigger::get_sequence_name() const void SequenceTrigger::draw(DrawingContext& context) { - if (Editor::is_active() || g_debug.show_collision_rects) { + if (Editor::is_active() || g_debug.show_collision_rects) context.color().draw_filled_rect(m_col.m_bbox, Color(1.0f, 0.0f, 0.0f, 0.6f), 0.0f, LAYER_OBJECTS); - } } /* EOF */ diff --git a/src/trigger/sequence_trigger.hpp b/src/trigger/sequence_trigger.hpp index cf60df6f56a..4de2fcb446b 100644 --- a/src/trigger/sequence_trigger.hpp +++ b/src/trigger/sequence_trigger.hpp @@ -17,17 +17,14 @@ #ifndef HEADER_SUPERTUX_TRIGGER_SEQUENCE_TRIGGER_HPP #define HEADER_SUPERTUX_TRIGGER_SEQUENCE_TRIGGER_HPP -#include "supertux/sequence.hpp" #include "trigger/trigger_base.hpp" -class Player; -class ReaderMapping; +#include "supertux/sequence.hpp" -class SequenceTrigger final : public TriggerBase +class SequenceTrigger final : public Trigger { public: SequenceTrigger(const ReaderMapping& reader); - SequenceTrigger(const Vector& pos, const std::string& sequence_name); static std::string class_name() { return "sequencetrigger"; } virtual std::string get_class_name() const override { return class_name(); } @@ -36,7 +33,6 @@ class SequenceTrigger 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 draw(DrawingContext& context) override; @@ -46,7 +42,6 @@ class SequenceTrigger final : public TriggerBase private: EventType triggerevent; Sequence sequence; - Vector new_size; std::string new_spawnpoint; std::string fade_tilemap; TilemapFadeType fade; diff --git a/src/trigger/switch.cpp b/src/trigger/switch.cpp index 23f958f1d38..4c0fba4ccb3 100644 --- a/src/trigger/switch.cpp +++ b/src/trigger/switch.cpp @@ -19,36 +19,26 @@ #include #include "audio/sound_manager.hpp" -#include "sprite/sprite.hpp" -#include "sprite/sprite_manager.hpp" #include "supertux/flip_level_transformer.hpp" #include "supertux/sector.hpp" #include "util/log.hpp" #include "util/reader_mapping.hpp" namespace { -const std::string SWITCH_SOUND = "sounds/switch.ogg"; -} + const std::string SWITCH_SOUND = "sounds/switch.ogg"; +} // namespace Switch::Switch(const ReaderMapping& reader) : - sprite_name(), - sprite(), + SpritedTrigger(reader, "images/objects/switch/left.sprite"), script(), off_script(), state(OFF), - bistable(), - m_flip(NO_FLIP) + bistable() { - if (!reader.get("x", m_col.m_bbox.get_left())) throw std::runtime_error("no x position set"); - if (!reader.get("y", m_col.m_bbox.get_top())) throw std::runtime_error("no y position set"); - if (!reader.get("sprite", sprite_name)) sprite_name = "images/objects/switch/left.sprite"; - sprite = SpriteManager::current()->create(sprite_name); - m_col.m_bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height()); - reader.get("script", script); bistable = reader.get("off-script", off_script); - SoundManager::current()->preload( SWITCH_SOUND ); + SoundManager::current()->preload(SWITCH_SOUND); } Switch::~Switch() @@ -58,9 +48,8 @@ Switch::~Switch() ObjectSettings Switch::get_settings() { - ObjectSettings result = TriggerBase::get_settings(); + ObjectSettings result = SpritedTrigger::get_settings(); - result.add_sprite(_("Sprite"), &sprite_name, "sprite", std::string("images/objects/switch/left.sprite")); result.add_script(_("Turn on script"), &script, "script"); result.add_script(_("Turn off script"), &off_script, "off-script"); @@ -69,12 +58,6 @@ Switch::get_settings() return result; } -void -Switch::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()); -} - void Switch::update(float ) { @@ -82,42 +65,36 @@ Switch::update(float ) case OFF: break; case TURN_ON: - if (sprite->animation_done()) { + if (m_sprite->animation_done()) { std::ostringstream location; location << "switch" << m_col.m_bbox.p1(); Sector::get().run_script(script, location.str()); - sprite->set_action("on", 1); + set_action("on", 1); state = ON; } break; case ON: - if (sprite->animation_done() && !bistable) { - sprite->set_action("turnoff", 1); + if (m_sprite->animation_done() && !bistable) { + set_action("turnoff", 1); state = TURN_OFF; } break; case TURN_OFF: - if (sprite->animation_done()) { + if (m_sprite->animation_done()) { if (bistable) { std::ostringstream location; location << "switch" << m_col.m_bbox.p1(); Sector::get().run_script(off_script, location.str()); } - sprite->set_action("off"); + set_action("off"); state = OFF; } break; } } -void -Switch::draw(DrawingContext& context) -{ - sprite->draw(context.color(), m_col.m_bbox.p1(), LAYER_TILES, m_flip); -} - void Switch::event(Player& , EventType type) { @@ -125,7 +102,7 @@ Switch::event(Player& , EventType type) switch (state) { case OFF: - sprite->set_action("turnon", 1); + set_action("turnon", 1); SoundManager::current()->play(SWITCH_SOUND, get_pos()); state = TURN_ON; break; @@ -133,7 +110,7 @@ Switch::event(Player& , EventType type) break; case ON: if (bistable) { - sprite->set_action("turnoff", 1); + set_action("turnoff", 1); SoundManager::current()->play(SWITCH_SOUND, get_pos()); state = TURN_OFF; } @@ -146,7 +123,7 @@ Switch::event(Player& , EventType type) void Switch::on_flip(float height) { - TriggerBase::on_flip(height); + SpritedTrigger::on_flip(height); FlipLevelTransformer::transform_flip(m_flip); } diff --git a/src/trigger/switch.hpp b/src/trigger/switch.hpp index af9ba21563a..19a5d728c33 100644 --- a/src/trigger/switch.hpp +++ b/src/trigger/switch.hpp @@ -17,14 +17,9 @@ #ifndef HEADER_SUPERTUX_TRIGGER_SWITCH_HPP #define HEADER_SUPERTUX_TRIGGER_SWITCH_HPP -#include - #include "trigger/trigger_base.hpp" -#include "video/flip.hpp" - -class ReaderMapping; -class Switch final : public TriggerBase +class Switch final : public SpritedTrigger { public: Switch(const ReaderMapping& reader); @@ -36,10 +31,8 @@ class Switch final : public TriggerBase virtual std::string get_display_name() const override { return display_name(); } virtual ObjectSettings get_settings() override; - virtual void after_editor_set() override; virtual void update(float dt_sec) override; - virtual void draw(DrawingContext& context) override; virtual void event(Player& player, EventType type) override; virtual void on_flip(float height) override; @@ -53,13 +46,10 @@ class Switch final : public TriggerBase }; private: - std::string sprite_name; - SpritePtr sprite; std::string script; std::string off_script; SwitchState state; bool bistable; - Flip m_flip; private: Switch(const Switch&) = delete; diff --git a/src/trigger/text_area.cpp b/src/trigger/text_area.cpp index 6f5968b791a..de904bc3dd4 100644 --- a/src/trigger/text_area.cpp +++ b/src/trigger/text_area.cpp @@ -27,7 +27,7 @@ #include "video/layer.hpp" TextArea::TextArea(const ReaderMapping& mapping) : - TriggerBase(mapping), + Trigger(mapping), m_once(false), m_items(), m_delay(4.0f), @@ -38,12 +38,6 @@ TextArea::TextArea(const ReaderMapping& mapping) : m_anchor(AnchorPoint::ANCHOR_MIDDLE), m_anchor_offset(0, 0) { - float w, h; - - mapping.get("x", m_col.m_bbox.get_left(), 0.0f); - mapping.get("y", m_col.m_bbox.get_top(), 0.0f); - mapping.get("width", w, 32.0f); - mapping.get("height", h, 32.0f); mapping.get("strings", m_items); mapping.get("delay", m_delay); mapping.get("once", m_once); @@ -54,22 +48,6 @@ TextArea::TextArea(const ReaderMapping& mapping) : std::string anchor; if (mapping.get("anchor-point", anchor)) m_anchor = string_to_anchor_point(anchor); - - m_col.m_bbox.set_size(w, h); -} - -TextArea::TextArea(const Vector& pos) : - m_once(false), - m_items(), - m_delay(4.0f), - m_fade_delay(1.0f), - m_current_text(0), - m_status(Status::NOT_STARTED), - m_timer(), - m_anchor(AnchorPoint::ANCHOR_MIDDLE) -{ - m_col.m_bbox.set_pos(pos); - m_col.m_bbox.set_size(32, 32); } void @@ -113,7 +91,7 @@ TextArea::event(Player& player, EventType type) void TextArea::update(float dt_sec) { - TriggerBase::update(dt_sec); + Trigger::update(dt_sec); if (m_timer.check()) { @@ -158,7 +136,7 @@ TextArea::update(float dt_sec) ObjectSettings TextArea::get_settings() { - ObjectSettings settings = TriggerBase::get_settings(); + ObjectSettings settings = Trigger::get_settings(); settings.add_bool(_("Once"), &m_once, "once"); settings.add_float(_("Text change time"), &m_delay, "delay"); diff --git a/src/trigger/text_area.hpp b/src/trigger/text_area.hpp index ae0a6061165..0c0b3d9df31 100644 --- a/src/trigger/text_area.hpp +++ b/src/trigger/text_area.hpp @@ -19,9 +19,10 @@ #define HEADER_SUPERTUX_TRIGGER_TEXT_AREA_HPP #include "trigger/trigger_base.hpp" + #include "supertux/timer.hpp" -class TextArea final : public TriggerBase +class TextArea final : public Trigger { private: enum class Status @@ -35,7 +36,6 @@ class TextArea final : public TriggerBase public: TextArea(const ReaderMapping& mapping); - TextArea(const Vector& pos); virtual void draw(DrawingContext& context) override; virtual void event(Player& player, EventType type) override; diff --git a/src/trigger/trigger_base.cpp b/src/trigger/trigger_base.cpp index 034b66274f0..47fc4ebdc1e 100644 --- a/src/trigger/trigger_base.cpp +++ b/src/trigger/trigger_base.cpp @@ -19,21 +19,10 @@ #include "object/player.hpp" #include "sprite/sprite.hpp" -TriggerBase::TriggerBase(const ReaderMapping& mapping) : - MovingObject(mapping), - m_sprite(), - m_hit(), - m_losetouch_listeners() -{ - set_group(COLGROUP_TOUCHABLE); -} - TriggerBase::TriggerBase() : - m_sprite(), m_hit(), m_losetouch_listeners() { - set_group(COLGROUP_TOUCHABLE); } TriggerBase::~TriggerBase() @@ -46,7 +35,7 @@ TriggerBase::~TriggerBase() } void -TriggerBase::update(float ) +TriggerBase::update() { for (unsigned i = 0; i < m_losetouch_listeners.size(); i++) { @@ -62,15 +51,6 @@ TriggerBase::update(float ) m_hit.clear(); } -void -TriggerBase::draw(DrawingContext& context) -{ - if (!m_sprite.get()) - return; - - m_sprite->draw(context.color(), get_pos(), LAYER_TILES+1); -} - HitResponse TriggerBase::collision(GameObject& other, const CollisionHit& ) { @@ -96,4 +76,23 @@ TriggerBase::object_removed(GameObject* object) m_losetouch_listeners.end()); } + +Trigger::Trigger(const ReaderMapping& reader) : + MovingObject(reader) +{ + set_group(COLGROUP_TOUCHABLE); + + if (m_col.m_bbox.get_width() == 0.f) + m_col.m_bbox.set_width(32.f); + + if (m_col.m_bbox.get_height() == 0.f) + m_col.m_bbox.set_height(32.f); +} + + +SpritedTrigger::SpritedTrigger(const ReaderMapping& reader, const std::string& sprite_name) : + MovingSprite(reader, sprite_name, TriggerBase::get_layer(), COLGROUP_TOUCHABLE) +{ +} + /* EOF */ diff --git a/src/trigger/trigger_base.hpp b/src/trigger/trigger_base.hpp index 176e8426cf2..ec43a773567 100644 --- a/src/trigger/trigger_base.hpp +++ b/src/trigger/trigger_base.hpp @@ -17,21 +17,18 @@ #ifndef HEADER_SUPERTUX_TRIGGER_TRIGGER_BASE_HPP #define HEADER_SUPERTUX_TRIGGER_TRIGGER_BASE_HPP -#include - +#include "object/moving_sprite.hpp" #include "supertux/moving_object.hpp" #include "supertux/object_remove_listener.hpp" -#include "sprite/sprite_ptr.hpp" -#include "video/layer.hpp" +#include class Player; /** This class is the base class for all objects you can interact with in some way. There are several interaction types defined like touch and activate */ -class TriggerBase : public MovingObject, - public ObjectRemoveListener +class TriggerBase : public ObjectRemoveListener { public: enum EventType { @@ -41,24 +38,22 @@ class TriggerBase : public MovingObject, }; public: - TriggerBase(const ReaderMapping& mapping); TriggerBase(); ~TriggerBase() override; - virtual void update(float dt_sec) override; - virtual void draw(DrawingContext& context) override; - virtual HitResponse collision(GameObject& other, const CollisionHit& hit) override; - /** Receive trigger events */ virtual void event(Player& player, EventType type) = 0; /** Called by GameObject destructor of an object in losetouch_listeners */ virtual void object_removed(GameObject* object) override; - virtual int get_layer() const override { return LAYER_TILES + 1; } +protected: + void update(); + HitResponse collision(GameObject& other, const CollisionHit& hit); + + int get_layer() const { return LAYER_TILES + 1; } private: - SpritePtr m_sprite; std::vector m_hit; /** Players that will be informed when we lose touch with them */ @@ -69,6 +64,56 @@ class TriggerBase : public MovingObject, TriggerBase& operator=(const TriggerBase&) = delete; }; + +class Trigger : public MovingObject, + public TriggerBase +{ +public: + Trigger(const ReaderMapping& reader); + + virtual void update(float) override + { + TriggerBase::update(); + } + virtual HitResponse collision(GameObject& other, const CollisionHit& hit) override + { + return TriggerBase::collision(other, hit); + } + int get_layer() const override + { + return TriggerBase::get_layer(); + } + +private: + Trigger(const Trigger&) = delete; + Trigger& operator=(const Trigger&) = delete; +}; + + +class SpritedTrigger : public MovingSprite, + public TriggerBase +{ +public: + SpritedTrigger(const ReaderMapping& reader, const std::string& sprite_name); + + virtual void update(float) override + { + TriggerBase::update(); + } + virtual HitResponse collision(GameObject& other, const CollisionHit& hit) override + { + return TriggerBase::collision(other, hit); + } + int get_layer() const override + { + return TriggerBase::get_layer(); + } + +private: + SpritedTrigger(const SpritedTrigger&) = delete; + SpritedTrigger& operator=(const SpritedTrigger&) = delete; +}; + #endif /* EOF */