diff --git a/src/entity.cpp b/src/entity.cpp index 802574b..21405d3 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -3,15 +3,13 @@ #include "anchor.hpp" #include "entitymanager.hpp" #include "pixmap.hpp" -#include "pixmappool.hpp" #include "rect.hpp" #include "resourcemanager.hpp" -#include "soundmanager.hpp" using namespace framework; entity::entity(const entityprops &&props) - : _props(std::move(props)), _fn(nullptr) { + : _props(std::move(props)) { } entity::~entity() { @@ -43,8 +41,8 @@ int32_t entity::y() const noexcept { } void entity::update(double delta) noexcept { - if (_fn) { - _fn(shared_from_this()); + if (_onupdate) { + _onupdate(shared_from_this()); } if (_props.action.empty() || !_props.visible) { @@ -62,8 +60,12 @@ void entity::update(double delta) noexcept { if (_props.frame >= animation.size()) { if (std::any_of(animation.begin(), animation.end(), [](const auto &keyframe) { return keyframe.singleshoot; })) { - _props.visible = false; _props.action.clear(); + + if (_onanimationfinished) { + _onanimationfinished(shared_from_this()); + } + return; } @@ -151,11 +153,15 @@ void entity::set_velocity(const vector2d &velocity) noexcept { } void entity::set_onupdate(const std::function)> &fn) { - _fn = fn; + _onupdate = std::move(fn); +} + +void entity::set_onanimationfinished(const std::function)> &fn) { + _onanimationfinished = std::move(fn); } void entity::set_onmail(const std::function, const std::string &)> &fn) { - _onmail = fn; + _onmail = std::move(fn); } void entity::set_flip(graphics::flip flip) noexcept { diff --git a/src/entity.hpp b/src/entity.hpp index 1537209..9d6d7fc 100644 --- a/src/entity.hpp +++ b/src/entity.hpp @@ -40,6 +40,8 @@ class entity : public std::enable_shared_from_this { void set_onupdate(const std::function)> &fn); + void set_onanimationfinished(const std::function)> &fn); + void set_onmail(const std::function, const std::string &)> &fn); void set_velocity(const vector2d &velocity) noexcept; @@ -71,7 +73,8 @@ class entity : public std::enable_shared_from_this { entityprops _props; std::shared_ptr _entitymanager; std::shared_ptr _resourcemanager; - std::function)> _fn; + std::function)> _onupdate; + std::function)> _onanimationfinished; std::function, const std::string &)> _onmail; }; } diff --git a/src/scriptengine.cpp b/src/scriptengine.cpp index 5601295..412d5fc 100644 --- a/src/scriptengine.cpp +++ b/src/scriptengine.cpp @@ -134,6 +134,7 @@ void scriptengine::run() { "visible", sol::property(&entity::visible), "size", sol::property(&entity::size), "on_update", &entity::set_onupdate, + "on_animationfinished", &entity::set_onanimationfinished, "on_mail", &entity::set_onmail, "set_flip", &entity::set_flip, "set_action", &entity::set_action,