Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
skhaz committed Nov 17, 2024
1 parent ca5959a commit 6f2fe54
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 19 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: Lint

on:
push:
branches:
- main
# push:
# branches:
# - main

pull_request:
branches:
Expand Down
5 changes: 5 additions & 0 deletions src/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ void entity::move(float_t x_velocity, float_t y_velocity) {
cpBodySetVelocity(_props.body.get(), {x_velocity, y_velocity});
}

math::vector2d entity::get_velocity() const noexcept {
cpVect velocity = cpBodyGetVelocity(_props.body.get());
return math::vector2d{velocity.x, velocity.y};
}

void entity::update() {
if (_onupdate) {
_onupdate(shared_from_this());
Expand Down
1 change: 1 addition & 0 deletions src/entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class entity : public std::enable_shared_from_this<entity> {
int32_t y() const noexcept;

void move(float_t x_velocity, float_t y_velocity);
math::vector2d get_velocity() const noexcept;

void set_placement(int32_t x, int32_t y) noexcept;
void set_entitymanager(std::shared_ptr<entitymanager> entitymanager) noexcept;
Expand Down
21 changes: 9 additions & 12 deletions src/entitymanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ void entitymanager::set_resourcemanager(std::shared_ptr<resourcemanager> resourc
std::shared_ptr<entity> entitymanager::spawn(const std::string &kind) {
const auto buffer = storage::io::read(fmt::format("entities/{}.json", kind));
const auto j = json::parse(buffer);
const auto spritesheet = _resourcemanager->pixmappool()->get(j["spritesheet"].get<std::string>());
const auto spritesheet = j.contains("spritesheet") ? _resourcemanager->pixmappool()->get(j["spritesheet"].get<std::string>())
: std::shared_ptr<graphics::pixmap>(nullptr);

const auto size = j["size"].get<geometry::size>();

Expand All @@ -45,8 +46,6 @@ std::shared_ptr<entity> entitymanager::spawn(const std::string &kind) {
animations.emplace(key, std::move(keyframes));
}

body_ptr body{nullptr, cpBodyFree};
shape_ptr shape{nullptr, cpShapeFree};
const auto resized = size.resized();
cpVect vertices[] = {
cpv(0, 0),
Expand All @@ -57,31 +56,29 @@ std::shared_ptr<entity> entitymanager::spawn(const std::string &kind) {

const int n = sizeof(vertices) / sizeof(vertices[0]);

body_ptr body{nullptr, cpBodyFree};
std::unordered_map<bodytype, std::function<void()>> mapping = {
{bodytype::stationary, [&]() {
{bodytype::stationary, [&body]() {
body = body_ptr(cpBodyNewStatic(), [](cpBody *body) { cpBodyFree(body); });
shape = shape_ptr(cpPolyShapeNew(body.get(), n, vertices, cpTransformIdentity, 0.0), [](cpShape *shape) { cpShapeFree(shape); });
}},
{bodytype::kinematic, [&]() {
{bodytype::kinematic, [&body]() {
body = body_ptr(cpBodyNewKinematic(), [](cpBody *body) { cpBodyFree(body); });
shape = shape_ptr(cpPolyShapeNew(body.get(), n, vertices, cpTransformIdentity, 0.0), [](cpShape *shape) { cpShapeFree(shape); });
}},
{bodytype::dynamic, [&]() {
{bodytype::dynamic, [&body, &size]() {
body = body_ptr(cpBodyNew(1.0, cpMomentForBox(1.0, size.width(), size.height())), [](cpBody *body) { cpBodyFree(body); });
shape = shape_ptr(cpPolyShapeNew(body.get(), n, vertices, cpTransformIdentity, 0.0), [](cpShape *shape) { cpShapeFree(shape); });
cpShapeSetFriction(shape.get(), 0.7);
cpShapeSetElasticity(shape.get(), 0.3);
cpSpaceAddBody(_world->space().get(), body.get());
}}
};

const auto p = j["physics"];

mapping[p["type"].get<bodytype>()]();

auto shape = shape_ptr(cpPolyShapeNew(body.get(), n, vertices, cpTransformIdentity, 0.0), [](cpShape *shape) { cpShapeFree(shape); });

cpShapeSetFriction(shape.get(), p.value("friction", 0.5f));
cpShapeSetElasticity(shape.get(), p.value("elasticity", 0.3f));
cpSpaceAddShape(_world->space().get(), shape.get());
cpSpaceAddBody(_world->space().get(), body.get());

entityprops props{
_counter++,
Expand Down
1 change: 1 addition & 0 deletions src/scriptengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ void scriptengine::run() {
"visible", sol::property(&entity::visible),
"size", sol::property(&entity::size),
"move", &entity::move,
"velocity", sol::property(&entity::get_velocity),
"on_update", &entity::set_onupdate,
"on_animationfinished", &entity::set_onanimationfinished,
"on_mail", &entity::set_onmail,
Expand Down
8 changes: 4 additions & 4 deletions src/vector2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ class vector2d {

vector2d operator+(const vector2d &other) const noexcept;
vector2d operator-(const vector2d &other) const noexcept;
vector2d operator*(double scalar) const noexcept;
vector2d operator/(double scalar) const noexcept;
vector2d operator*(double_t scalar) const noexcept;
vector2d operator/(double_t scalar) const noexcept;

vector2d &operator+=(const vector2d &other) noexcept;
vector2d &operator-=(const vector2d &other) noexcept;
vector2d &operator*=(double scalar) noexcept;
vector2d &operator/=(double scalar) noexcept;
vector2d &operator*=(double_t scalar) noexcept;
vector2d &operator/=(double_t scalar) noexcept;

bool operator==(const vector2d &other) const noexcept;
bool operator!=(const vector2d &other) const noexcept;
Expand Down

0 comments on commit 6f2fe54

Please sign in to comment.