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 13, 2024
1 parent dd57c59 commit 468d065
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 133 deletions.
24 changes: 15 additions & 9 deletions src/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@

using namespace framework;

entity::entity(const entityprops &&props)
entity::entity(entityprops &&props)
: _props(std::move(props)) {}

entity::~entity() {
std::cout << "~entity id: " << _props.id << std::endl;
}

std::shared_ptr<entity> entity::create(const entityprops &&props) {
std::shared_ptr<entity> entity::create(entityprops &&props) {

Check warning on line 8 in src/entity.cpp

View workflow job for this annotation

GitHub Actions / lint

src/entity.cpp:8:33 [modernize-use-trailing-return-type]

use a trailing return type for this function
return std::shared_ptr<entity>(new entity(std::move(props)));
}

uint64_t entity::id() const noexcept { return _props.id; }

Check warning on line 12 in src/entity.cpp

View workflow job for this annotation

GitHub Actions / lint

src/entity.cpp:12:18 [modernize-use-trailing-return-type]

use a trailing return type for this function

std::string entity::kind() const { return _props.kind; }

Check warning on line 14 in src/entity.cpp

View workflow job for this annotation

GitHub Actions / lint

src/entity.cpp:14:21 [modernize-use-trailing-return-type]

use a trailing return type for this function

entityprops entity::props() const { return _props; }
const entityprops &entity::props() const { return _props; }

Check warning on line 16 in src/entity.cpp

View workflow job for this annotation

GitHub Actions / lint

src/entity.cpp:16:28 [modernize-use-trailing-return-type]

use a trailing return type for this function

int32_t entity::x() const noexcept { return _props.position.x(); }

Check warning on line 18 in src/entity.cpp

View workflow job for this annotation

GitHub Actions / lint

src/entity.cpp:18:17 [modernize-use-trailing-return-type]

use a trailing return type for this function

Expand Down Expand Up @@ -64,6 +60,16 @@ void entity::update() {
// static_cast<int32_t>(std::round(position.y * ppm))
// );

cpVect position = cpBodyGetPosition(_props.body.get());

Check warning on line 63 in src/entity.cpp

View workflow job for this annotation

GitHub Actions / lint

src/entity.cpp:63:10 [cppcoreguidelines-init-variables]

variable 'position' is not initialized
cpFloat angle = cpBodyGetAngle(_props.body.get());

Check warning on line 64 in src/entity.cpp

View workflow job for this annotation

GitHub Actions / lint

src/entity.cpp:64:11 [cppcoreguidelines-init-variables]

variable 'angle' is not initialized

_props.position.set(
static_cast<int32_t>(std::round(position.x)),
static_cast<int32_t>(std::round(position.y))
);

_props.angle = angle;

// _props.angle = b2Rot_GetAngle(b2Body_GetRotation(_props.body));
}

Expand All @@ -75,7 +81,7 @@ void entity::draw() const {
const auto source = _props.animations.at(_props.action)[_props.frame].frame;
const auto offset = _props.animations.at(_props.action)[_props.frame].offset;
geometry::rect destination{_props.position + offset, source.size()};
destination.scale(_props.scale);
destination.scale(_props.size.scale());

_props.spritesheet->draw(
source,
Expand Down Expand Up @@ -151,7 +157,7 @@ geometry::size entity::size() const noexcept { return _props.size; }

bool entity::visible() const noexcept { return _props.visible; }

void entity::dispatch(const std::string &message) {
void entity::on_email(const std::string &message) {
if (!_onmail) {
return;
}
Expand Down
10 changes: 5 additions & 5 deletions src/entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
namespace framework {
class entity : public std::enable_shared_from_this<entity> {
public:
entity(const entityprops &&props);
virtual ~entity();
explicit entity(entityprops &&props);
~entity() noexcept = default;

static std::shared_ptr<entity> create(const entityprops &&props);
static std::shared_ptr<entity> create(entityprops &&props);

uint64_t id() const noexcept;
std::string kind() const;
Expand All @@ -24,7 +24,7 @@ class entity : public std::enable_shared_from_this<entity> {

bool colliding_with(const entity &other) const noexcept;

entityprops props() const;
const entityprops &props() const;
void set_props(entityprops props) noexcept;

int32_t x() const noexcept;
Expand All @@ -49,7 +49,7 @@ class entity : public std::enable_shared_from_this<entity> {
geometry::size size() const noexcept;
bool visible() const noexcept;

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

private:
friend class entitymanager;
Expand Down
62 changes: 34 additions & 28 deletions src/entitymanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,31 @@ 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 scale = j.value("scale", 1.0);

const auto position = j["position"].get<geometry::point>();
// const auto position = j["position"].get<geometry::point>();
// UNUSED(position);

// const auto position = geometry::point{
// static_cast<int32_t>(j.value("x", 0) * scale),
// static_cast<int32_t>(j.value("y", 0) * scale),
// };

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

UNUSED(size);
// const auto size = geometry::size{
// static_cast<int32_t>(j.value("width", 0) * scale),
// static_cast<int32_t>(j.value("height", 0) * scale)
// };
std::cout << "w " << size.width() << " h " << size.height() << " s " << size.scale() << std::endl;
// UNUSED(size);
// const auto size = geometry::size{
// static_cast<int32_t>(j.value("width", 0) * scale),
// static_cast<int32_t>(j.value("height", 0) * scale)
// };

std::map<std::string, std::vector<keyframe>> animations;
for (const auto &[key, frames] : j["animations"].items()) {
std::vector<keyframe> keyframes;
for (const auto &frame_list : frames) {
for (const auto &f : frame_list) {

// const auto rect = f.get<geometry::size>();

const auto rect = geometry::rect{
{f.at("x").get<int32_t>(), f.at("y").get<int32_t>()},
{f.at("width").get<int32_t>(), f.at("height").get<int32_t>()}
Expand All @@ -71,34 +74,36 @@ std::shared_ptr<entity> entitymanager::spawn(const std::string &kind) {
// {"dynamic", b2_dynamicBody},
// };

std::unordered_map<std::string, std::function<void()>> cases = {
{"static", []() {
auto body = body_ptr(cpBodyNew(10, 10 /* _props.mass, _props.moment */), [](cpBody *body) { cpBodyFree(body); });

}},
{"dynamic", []() {
std::unordered_map<std::string, std::function<void()>>
cases = {
{"static", []() {

}}
};
}},
{"dynamic", []() {

}}
};

const auto p = j["physics"];
const auto width = p["size"]["width"].get<int32_t>();
const auto height = p["size"]["height"].get<int32_t>();
const auto type = p["type"].get<std::string>();
const auto margin = p["margin"].get<geometry::margin>();
// const auto p = j["physics"];
// const auto width = p["size"]["width"].get<int32_t>();
// const auto height = p["size"]["height"].get<int32_t>();
// const auto type = p["type"].get<std::string>();
// const auto margin = p["margin"].get<geometry::margin>();

cases[type]();
// cases[type]();

UNUSED(position);
UNUSED(width);
UNUSED(height);
UNUSED(margin);
// UNUSED(position);
// UNUSED(width);
// UNUSED(height);
// UNUSED(margin);

entityprops props{
_counter++,
0,
SDL_GetTicks(),
0.0,
scale,
255,
true,
{},
Expand All @@ -108,12 +113,13 @@ std::shared_ptr<entity> entitymanager::spawn(const std::string &kind) {
"",
graphics::flip::none,
spritesheet,
std::move(animations)
std::move(animations),
std::move(body)
};

const auto e = entity::create(std::move(props));
std::cout << "[entitymanager] spawn: " << e->id() << std::endl;
_entities.emplace_back(e);
_entities.emplace_back(std::move(e));
return e;
}

Expand Down Expand Up @@ -146,6 +152,6 @@ void entitymanager::draw() noexcept {

void entitymanager::on_mail(const input::mailevent &event) noexcept {
if (const auto entity = find(event.to); entity) {
entity->dispatch(event.body);
entity->on_email(event.body);
}
}
92 changes: 7 additions & 85 deletions src/entityprops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
#include "point.hpp"
#include "rect.hpp"
#include "size.hpp"
#include "vector2d.hpp"

namespace framework {

using body_ptr = std::unique_ptr<cpBody, void (*)(cpBody *)>;

struct keyframe {
geometry::rect frame;
geometry::point offset;
Expand All @@ -26,101 +27,22 @@ struct entityprops {
uint32_t frame{};
uint32_t last_frame{};
double_t angle{};
double_t scale{1.0};
uint8_t alpha{255};
bool visible{true};
geometry::point position{};
geometry::point pivot{};
geometry::size size{};
// math::vector2d velocity;
std::string kind{};
std::string action{};
graphics::flip flip{graphics::flip::none};
std::shared_ptr<graphics::pixmap> spritesheet{};
std::map<std::string, std::vector<keyframe>> animations{};
// b2BodyId body{};

entityprops() noexcept = default;

using attribute_t = std::variant<
uint64_t,
std::string,
std::shared_ptr<graphics::pixmap>,
std::map<std::string, std::vector<keyframe>>,
geometry::point,
geometry::size,
float_t,
double_t,
graphics::flip,
uint8_t,
uint32_t,
math::vector2d,
bool>;

attribute_t get(const std::string &name) const {
if (name == "id")
return id;
else if (name == "angle")
return angle;
else if (name == "scale")
return scale;
else if (name == "alpha")
return alpha;
else if (name == "visible")
return visible;
else if (name == "position")
return position;
else if (name == "pivot")
return pivot;
else if (name == "size")
return size;
else if (name == "kind")
return kind;
else if (name == "action")
return action;
else if (name == "flip")
return flip;
else if (name == "spritesheet")
return spritesheet;
else if (name == "animations")
return animations;
throw std::invalid_argument("Invalid property name");
}
body_ptr body{nullptr, [](cpBody *) {}};

void set(const std::string &name, const attribute_t &value) {
if (name == "id")
id = std::get<uint64_t>(value);
else if (name == "frame")
frame = std::get<uint32_t>(value);
else if (name == "last_frame")
last_frame = std::get<uint32_t>(value);
else if (name == "angle")
angle = std::get<double_t>(value);
else if (name == "scale")
scale = std::get<double_t>(value);
else if (name == "alpha")
alpha = std::get<uint8_t>(value);
else if (name == "visible")
visible = std::get<bool>(value);
else if (name == "position")
position = std::get<geometry::point>(value);
else if (name == "pivot")
pivot = std::get<geometry::point>(value);
else if (name == "size")
size = std::get<geometry::size>(value);
else if (name == "kind")
kind = std::get<std::string>(value);
else if (name == "action")
action = std::get<std::string>(value);
else if (name == "flip")
flip = std::get<graphics::flip>(value);
else if (name == "spritesheet")
spritesheet = std::get<std::shared_ptr<graphics::pixmap>>(value);
else if (name == "animations")
animations = std::get<std::map<std::string, std::vector<keyframe>>>(value);
else
throw std::invalid_argument("Invalid property name");
}
entityprops(const entityprops &) = delete;
entityprops &operator=(const entityprops &) = delete;
entityprops(entityprops &&) noexcept = default;
entityprops &operator=(entityprops &&) noexcept = default;
};

}
16 changes: 13 additions & 3 deletions src/size.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

using namespace geometry;

size::size() noexcept {}

size::size(int32_t width, int32_t height) noexcept : _width(width), _height(height) {}

size::size(const size &other) noexcept : _width(other._width), _height(other._height) {}
size::size(float_t scale, int32_t width, int32_t height) noexcept : _scale(scale), _width(width), _height(height) {}

size::size(const size &other) noexcept : _scale(other._scale), _width(other._width), _height(other._height) {}

void size::set_scale(float_t scale) noexcept { _scale = scale; }
float_t size::scale() const noexcept { return _scale; }

void size::set_width(int32_t width) noexcept { _width = width; }

Expand All @@ -16,6 +19,13 @@ void size::set_height(int32_t height) noexcept { _height = height; }

int32_t size::height() const noexcept { return _height; }

size size::resized() const noexcept {
return {
static_cast<int32_t>(std::round(_width * _scale)),
static_cast<int32_t>(std::round(_height * _scale))
};
}

bool size::operator==(const size &rhs) const noexcept {
return _width == rhs._width && _height == rhs._height;
}
Expand Down
Loading

0 comments on commit 468d065

Please sign in to comment.