Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
skhaz committed Dec 14, 2024
1 parent f58c5df commit eef616e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 10 deletions.
27 changes: 24 additions & 3 deletions src/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ void entity::update(float_t delta) noexcept {
}
}

_props.angle = cpBodyGetAngle(_props.body.get());
const auto body = _props.body.get();
_props.angle = cpBodyGetAngle(body);

const auto position = cpBodyGetPosition(_props.body.get());
const auto position = cpBodyGetPosition(body);
fmt::println("update: Chipmunk position ({}, {}), entity position ({}, {})", position.x, position.y, _props.position.x(), _props.position.y());
_props.position.set(
static_cast<int32_t>(std::round(position.x)),
static_cast<int32_t>(std::round(position.y))
Expand Down Expand Up @@ -118,7 +120,13 @@ void entity::set_props(entityprops props) noexcept {
}

void entity::set_placement(int32_t x, int32_t y) noexcept {
cpBodySetPosition(_props.body.get(), {static_cast<cpFloat>(x), static_cast<cpFloat>(y)});
const auto body = _props.body.get();
cpBodySetVelocity(body, cpvzero);
cpBodySetForce(body, cpvzero);
cpBodySetPosition(body, {static_cast<cpFloat>(x), static_cast<cpFloat>(y)});

auto pos = cpBodyGetPosition(body);
fmt::println("set_placement: ({}, {}), internal Chipmunk position: ({}, {})", x, y, pos.x, pos.y);
}

void entity::set_onupdate(const std::function<void(std::shared_ptr<entity>)> &fn) noexcept {
Expand Down Expand Up @@ -163,6 +171,19 @@ bool entity::visible() const noexcept {
return _props.visible;
}

void entity::set_kv(const std::string &key, const std::variant<std::string, int64_t, double_t, float_t> &value) noexcept {
_kv[key] = value;
}

std::optional<std::variant<std::string, int64_t, double_t, float_t>> entity::get_kv(const std::string &key) const noexcept {
auto it = _kv.find(key);
if (it == _kv.end()) {
return std::nullopt;
}

return it->second;
}

void entity::on_email(const std::string &message) {
if (_onmail) {
_onmail(shared_from_this(), message);
Expand Down
4 changes: 4 additions & 0 deletions src/entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,16 @@ class entity : public std::enable_shared_from_this<entity> {
geometry::size size() const noexcept;
bool visible() const noexcept;

void set_kv(const std::string &key, const std::variant<std::string, int64_t, double_t, float_t> &value) noexcept;
std::optional<std::variant<std::string, int64_t, double_t, float_t>> get_kv(const std::string &key) const noexcept;

void on_email(const std::string &message);

private:
friend class entitymanager;

entityprops _props;
std::unordered_map<std::string, std::variant<std::string, int64_t, double_t, float_t>> _kv;
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;
Expand Down
45 changes: 38 additions & 7 deletions src/scriptengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,37 @@ void scriptengine::run() {
"none", anchor::none
);

struct kvproxy {
entity &e;

sol::object get(const std::string &key, sol::this_state lua_state) {
auto result = e.get_kv(key);
if (result.has_value()) {
return std::visit(
[&lua_state](auto &&arg) -> sol::object {
return sol::make_object(lua_state, arg);
},
result.value()
);
}
return sol::nil;
}

void set(const std::string &key, sol::object value) {
if (value.is<std::string>()) {
e.set_kv(key, value.as<std::string>());
} else if (value.is<int64_t>()) {
e.set_kv(key, value.as<int64_t>());
} else if (value.is<double>()) {
e.set_kv(key, value.as<double>());
} else if (value.is<float>()) {
e.set_kv(key, value.as<float>());
} else {
throw std::runtime_error(fmt::format("Unsupported type for key: {}", key));
}
}
};

lua.new_usertype<entity>(
"Entity",
"id", sol::property(&entity::id),
Expand All @@ -168,9 +199,12 @@ void scriptengine::run() {
"set_flip", &entity::set_flip,
"set_action", &entity::set_action,
"unset_action", &entity::unset_action,
"set_placement", &entity::set_placement
"set_placement", &entity::set_placement,
"kv", sol::property([](entity &e) { return kvproxy{e}; })
);

lua.new_usertype<kvproxy>("KvProxy", sol::meta_function::index, &kvproxy::get, sol::meta_function::new_index, &kvproxy::set);

lua.new_usertype<entitymanager>(
"EntityManager",
"spawn", &entitymanager::spawn,
Expand Down Expand Up @@ -362,13 +396,10 @@ void scriptengine::run() {

lua.new_usertype<label>(
"Label",
sol::factories([] {
return std::make_shared<label>();
}),
sol::constructors<label()>(),
sol::base_classes, sol::bases<widget>(),
"set_font", &label::set_font,
"set_placement", &label::set_placement,
"set", sol::overload(&label::set, &label::set_with_placement)
"font", sol::property(&label::set_font),
"set", sol::overload([](label &self, std::string_view text) { self.set(text); }, [](label &self, std::string_view text, int32_t x, int32_t y) { self.set_with_placement(text, x, y); })
);

lua.new_usertype<widget>(
Expand Down

0 comments on commit eef616e

Please sign in to comment.