Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
skhaz committed Oct 7, 2024
1 parent 310e525 commit e6f7f04
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
22 changes: 14 additions & 8 deletions src/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 11 in src/entity.cpp

View workflow job for this annotation

GitHub Actions / lint

src/entity.cpp:11:1 [cppcoreguidelines-pro-type-member-init]

constructor does not initialize these fields: _id, _onupdate, _onanimationfinished, _onmail
: _props(std::move(props)), _fn(nullptr) {
: _props(std::move(props)) {
}

entity::~entity() {
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -151,11 +153,15 @@ void entity::set_velocity(const vector2d &velocity) noexcept {
}

void entity::set_onupdate(const std::function<void(std::shared_ptr<entity>)> &fn) {
_fn = fn;
_onupdate = std::move(fn);
}

void entity::set_onanimationfinished(const std::function<void(std::shared_ptr<entity>)> &fn) {
_onanimationfinished = std::move(fn);
}

void entity::set_onmail(const std::function<void(std::shared_ptr<entity>, const std::string &)> &fn) {
_onmail = fn;
_onmail = std::move(fn);
}

void entity::set_flip(graphics::flip flip) noexcept {
Expand Down
5 changes: 4 additions & 1 deletion src/entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class entity : public std::enable_shared_from_this<entity> {

void set_onupdate(const std::function<void(std::shared_ptr<entity>)> &fn);

void set_onanimationfinished(const std::function<void(std::shared_ptr<entity>)> &fn);

void set_onmail(const std::function<void(std::shared_ptr<entity>, const std::string &)> &fn);

void set_velocity(const vector2d &velocity) noexcept;
Expand Down Expand Up @@ -71,7 +73,8 @@ class entity : public std::enable_shared_from_this<entity> {
entityprops _props;
std::shared_ptr<entitymanager> _entitymanager;
std::shared_ptr<resourcemanager> _resourcemanager;
std::function<void(std::shared_ptr<entity>)> _fn;
std::function<void(std::shared_ptr<entity>)> _onupdate;
std::function<void(std::shared_ptr<entity>)> _onanimationfinished;
std::function<void(std::shared_ptr<entity>, const std::string &)> _onmail;
};
}
1 change: 1 addition & 0 deletions src/scriptengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e6f7f04

Please sign in to comment.