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 22, 2024
1 parent a615c09 commit e83cc3d
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 91 deletions.
10 changes: 0 additions & 10 deletions src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ std::shared_ptr<graphics::renderer> engine::renderer() const noexcept {
return _renderer;
}

std::shared_ptr<framework::world> engine::world() const noexcept {
return _world;
}

int32_t engine::height() const noexcept {
return _window->height();
}
Expand Down Expand Up @@ -114,10 +110,6 @@ void engine::set_renderer(std::shared_ptr<graphics::renderer> renderer) noexcept
_renderer = std::move(renderer);
}

void engine::set_world(std::shared_ptr<framework::world> world) noexcept {
_world = std::move(world);
}

void engine::add_loopable(std::shared_ptr<loopable> loopable) noexcept {
_loopables.emplace_back(std::move(loopable));
}
Expand Down Expand Up @@ -161,7 +153,6 @@ void engine::_loop() noexcept {
_resourcemanager->update(delta);
_scenemanager->update(delta);
_eventmanager->update(delta);
_world->update(delta);
_overlay->update(delta);
_entitymanager->update(delta);

Expand All @@ -172,7 +163,6 @@ void engine::_loop() noexcept {
_renderer->begin();
_scenemanager->draw();
_entitymanager->draw();
_world->draw();
_overlay->draw();
_renderer->end();
}
Expand Down
3 changes: 0 additions & 3 deletions src/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class engine : public input::eventreceiver {
std::shared_ptr<audio::soundmanager> soundmanager() const noexcept;
std::shared_ptr<graphics::window> window() const noexcept;
std::shared_ptr<graphics::renderer> renderer() const noexcept;
std::shared_ptr<framework::world> world() const noexcept;

int32_t height() const noexcept;
int32_t width() const noexcept;
Expand All @@ -36,7 +35,6 @@ class engine : public input::eventreceiver {
void set_statemanager(std::shared_ptr<framework::statemanager> statemanager) noexcept;
void set_window(std::shared_ptr<graphics::window> window) noexcept;
void set_renderer(std::shared_ptr<graphics::renderer> renderer) noexcept;
void set_world(std::shared_ptr<framework::world> world) noexcept;

void add_loopable(std::shared_ptr<loopable> loopable) noexcept;
void flush() const noexcept;
Expand All @@ -63,7 +61,6 @@ class engine : public input::eventreceiver {
std::shared_ptr<framework::statemanager> _statemanager;
std::shared_ptr<graphics::renderer> _renderer;
std::shared_ptr<graphics::window> _window;
std::shared_ptr<framework::world> _world;
std::shared_ptr<graphics::fontfactory> _fontfactory;
};
}
4 changes: 1 addition & 3 deletions src/enginefactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ std::shared_ptr<engine> enginefactory::create() const noexcept {
const auto overlay = std::make_shared<graphics::overlay>(renderer);
const auto scenemanager = std::make_shared<framework::scenemanager>(resourcemanager->pixmappool());
const auto statemanager = std::make_shared<framework::statemanager>();
const auto world = std::make_shared<framework::world>(_gravity, renderer);
const auto entitymanager = std::make_shared<framework::entitymanager>(world, resourcemanager);
const auto entitymanager = std::make_shared<framework::entitymanager>(resourcemanager);
const auto fontfactory = std::make_shared<graphics::fontfactory>(renderer);

engine->set_audiodevice(std::move(audiodevice));
Expand All @@ -65,7 +64,6 @@ std::shared_ptr<engine> enginefactory::create() const noexcept {
engine->set_scenemanager(std::move(scenemanager));
engine->set_statemanager(std::move(statemanager));
engine->set_window(std::move(window));
engine->set_world(std::move(world));
engine->set_fontfactory(fontfactory);

engine->eventmanager()->add_receiver(engine->entitymanager());
Expand Down
21 changes: 13 additions & 8 deletions src/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ const entityprops &entity::props() const noexcept {
return _props;
}

geometry::point entity::position() const noexcept {
return _props.position;
}

int32_t entity::x() const noexcept {
return _props.position.x();
}
Expand Down Expand Up @@ -99,7 +103,8 @@ void entity::draw() const noexcept {
const auto &source = animation.frame;
const auto &offset = animation.offset;
geometry::rect destination{_props.position + offset, source.size()};
destination.scale(_props.size.scale()); // TODO calc this on spawn

destination.scale(_props.size.scale());

_props.spritesheet->draw(
source,
Expand All @@ -123,15 +128,19 @@ void entity::set_placement(int32_t x, int32_t y) noexcept {
}

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

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

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

void entity::set_oncollision(const std::string &kind, const std::function<void(std::shared_ptr<entity>, uint64_t)> &fn) noexcept {
_collisionmapping[kind] = std::move(fn);
}

void entity::set_reflection(graphics::reflection reflection) noexcept {
Expand Down Expand Up @@ -173,7 +182,3 @@ void entity::on_email(const std::string &message) {
memory::kv &entity::kv() noexcept {
return _kv;
}

// memory::kv &entity::kv() noexcept {
// return _kv;
// }
3 changes: 3 additions & 0 deletions src/entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class entity : public std::enable_shared_from_this<entity> {
const entityprops &props() const noexcept;
void set_props(entityprops props) noexcept;

geometry::point position() const noexcept;
int32_t x() const noexcept;
int32_t y() const noexcept;

Expand All @@ -37,6 +38,7 @@ class entity : public std::enable_shared_from_this<entity> {
void set_onupdate(const std::function<void(std::shared_ptr<entity>)> &fn) noexcept;
void set_onanimationfinished(const std::function<void(std::shared_ptr<entity>)> &fn) noexcept;
void set_onmail(const std::function<void(std::shared_ptr<entity>, const std::string &)> &fn) noexcept;
void set_oncollision(const std::string &kind, const std::function<void(std::shared_ptr<entity>, uint64_t)> &fn) noexcept;

void set_reflection(graphics::reflection reflection) noexcept;
void set_action(const std::string &action);
Expand All @@ -58,5 +60,6 @@ class entity : public std::enable_shared_from_this<entity> {
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;
std::unordered_map<std::string, std::function<void(std::shared_ptr<entity>, uint64_t)>> _collisionmapping;
};
}
54 changes: 39 additions & 15 deletions src/entitymanager.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#include "entitymanager.hpp"
#include "common.hpp"

using namespace framework;

using json = nlohmann::json;

entitymanager::entitymanager(std::shared_ptr<world> world, std::shared_ptr<resourcemanager> resourcemanager) noexcept
: _world(std::move(world)),
_resourcemanager(std::move(resourcemanager)) {}
entitymanager::entitymanager(std::shared_ptr<resourcemanager> resourcemanager) noexcept
: _resourcemanager{std::move(resourcemanager)} {
}

std::shared_ptr<entity> entitymanager::spawn(const std::string &kind) {
const auto buffer = storage::io::read((std::ostringstream() << "entities/" << kind << ".json").str());
Expand Down Expand Up @@ -63,18 +64,6 @@ void entitymanager::destroy(const std::shared_ptr<entity> entity) noexcept {
return;
}

// auto &props = entity->props();

// if (props.body) {
// cpSpaceRemoveBody(_world->space().get(), props.body.get());
// props.body.reset();
// }

// if (props.shape) {
// cpSpaceRemoveShape(_world->space().get(), props.shape.get());
// props.shape.reset();
// }

_entities.remove(entity);
}

Expand All @@ -90,6 +79,41 @@ void entitymanager::update(float_t delta) noexcept {
for (const auto &entity : _entities) {
entity->update(delta);
}

std::unordered_map<std::string, std::vector<std::shared_ptr<entity>>> mapping;
for (const auto &entity : _entities) {
mapping[entity->kind()].emplace_back(entity);
}

for (const auto &entity1 : _entities) {
if (entity1->_collisionmapping.empty()) [[unlikely]]
continue;

const auto &pos1 = entity1->position();
const auto &size1 = entity1->size().resized();

for (const auto &[kind, callback] : entity1->_collisionmapping) {
if (auto it = mapping.find(kind); it != mapping.end()) [[likely]] {
const auto &mapping = it->second;

for (const auto &entity2 : mapping) {
if (entity1 == entity2) [[unlikely]]
continue;

const auto &pos2 = entity2->position();
const auto &size2 = entity2->size().resized();

if (pos1.x() < pos2.x() + size2.width() &&
pos1.x() + size1.width() > pos2.x() &&
pos1.y() < pos2.y() + size2.height() &&
pos1.y() + size1.height() > pos2.y()) [[likely]] {

callback(entity1, entity2->id());
}
}
}
}
}
}

void entitymanager::draw() noexcept {
Expand Down
4 changes: 1 addition & 3 deletions src/entitymanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
#include "common.hpp"
#include "event.hpp"
#include "eventreceiver.hpp"
#include "world.hpp"

namespace framework {
class entitymanager : public input::eventreceiver {
public:
entitymanager(std::shared_ptr<world> world, std::shared_ptr<resourcemanager> resourcemanager) noexcept;
explicit entitymanager(std::shared_ptr<resourcemanager> resourcemanager) noexcept;
~entitymanager() = default;

std::shared_ptr<entity> spawn(const std::string &kind);
Expand All @@ -25,7 +24,6 @@ class entitymanager : public input::eventreceiver {
virtual void on_mail(const input::mailevent &event) noexcept override;

private:
std::shared_ptr<world> _world;
std::shared_ptr<resourcemanager> _resourcemanager;
std::list<std::shared_ptr<entity>> _entities;
std::atomic<uint64_t> _counter{0};
Expand Down
23 changes: 0 additions & 23 deletions src/entityprops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,6 @@

namespace framework {

enum bodytype : int8_t {
stationary = 0,
dynamic = 1,
kinematic = 2,
};

NLOHMANN_JSON_SERIALIZE_ENUM(bodytype, {{stationary, "stationary"}, {dynamic, "dynamic"}, {kinematic, "kinematic"}})

enum collisiontype : uint8_t {
player = 1,
enemy = 2,
playerbullet = 3,
enemybullet = 4,
wall = 5
};

struct collision {
collisiontype type;
std::optional<std::string> from;
};

void from_json(const nlohmann::json &j, collision &c);

struct keyframe {
geometry::rect frame;
geometry::point offset;
Expand Down
1 change: 1 addition & 0 deletions src/scriptengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ void scriptengine::run() {
"on_update", &entity::set_onupdate,
"on_animationfinished", &entity::set_onanimationfinished,
"on_mail", &entity::set_onmail,
"on_collision", &entity::set_oncollision,
"reflection", sol::property([](entity &e) -> reflectionproxy { return reflectionproxy{e}; }),
"action", sol::property([](entity &e) -> actionproxy { return actionproxy{e}; }),
"placement", sol::property([](entity &e) -> placementproxy { return placementproxy{e}; }),
Expand Down
2 changes: 1 addition & 1 deletion src/size.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class size {
friend void from_json(const nlohmann::json &j, size &s) noexcept;

private:
float_t _scale{0.0};
float_t _scale{.0};
int32_t _width{0};
int32_t _height{0};
};
Expand Down
25 changes: 0 additions & 25 deletions src/unmarshalling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,3 @@ void from_json(const nlohmann::json &j, rect &r) noexcept {
r._size = geometry::size{j.at("width").get<int>(), j.at("height").get<int>()};
}
}

namespace framework {
void from_json(const nlohmann::json &j, collision &c) {
const auto type = j.at("type").get<std::string>();

if (type == "bullet") {
const auto from = j.at("from").get<std::string>();
if (from == "player") {
c.type = playerbullet;
} else if (from == "enemy") {
c.type = enemybullet;
} else [[unlikely]] {
throw std::invalid_argument("[collision] Invalid 'from' value for bullet");
}
} else if (type == "player") {
c.type = player;
} else if (type == "enemy") {
c.type = enemy;
} else if (type == "wall") {
c.type = wall;
} else {
throw std::invalid_argument("[collision] Invalid 'type' value");
}
}
}

0 comments on commit e83cc3d

Please sign in to comment.