mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
feat(gameplay): add entity managers and refactor to ECS components
This commit is contained in:
53
src/gameplay/client_entity_manager.cpp
Normal file
53
src/gameplay/client_entity_manager.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
#include "Cubed/gameplay/client_entity_manager.hpp"
|
||||
|
||||
#include "Cubed/gameplay/creatures/pig.hpp"
|
||||
#include "Cubed/gameplay/ecs/client_entity.hpp"
|
||||
#include "Cubed/gameplay/ecs/identity.hpp"
|
||||
#include "Cubed/gameplay/ecs/transform.hpp"
|
||||
#include "Cubed/render/model_manager.hpp"
|
||||
#include "Cubed/tools/cubed_assert.hpp"
|
||||
namespace Cubed {
|
||||
ClientEntityManager::ClientEntityManager(ClientWorld& world) : m_world(world) {}
|
||||
void ClientEntityManager::update() { handle_task(); }
|
||||
void ClientEntityManager::init() {
|
||||
m_factories.emplace("cubed:pig", [this](EntityID id) {
|
||||
BaseClientCreature c;
|
||||
c.model = ModelManager::instance().get_model_id("cubed:pig");
|
||||
add_entity(id, Entity{id}, EntityInfo{"cubed:pig", ""}, std::move(c),
|
||||
PigTag{});
|
||||
});
|
||||
}
|
||||
// not thread safe
|
||||
void ClientEntityManager::add_entity(EntityID id, std::string_view name,
|
||||
const glm::vec3& pos) {
|
||||
|
||||
m_factories[name](id);
|
||||
acc a;
|
||||
ASSERT(m_entities.find(a, id));
|
||||
auto* c = m_registry.try_get<Transform>(a->second);
|
||||
ASSERT(c);
|
||||
c->position.value = pos;
|
||||
}
|
||||
|
||||
void ClientEntityManager::receive_entity_create(S2CEntityCreate& s2c) {
|
||||
EntityCreateElement c;
|
||||
c.id = s2c.id();
|
||||
c.name = s2c.name();
|
||||
c.pos = {s2c.pos().x(), s2c.pos().y(), s2c.pos().z()};
|
||||
m_tasks.emplace(Command::CREATE, std::move(c));
|
||||
}
|
||||
|
||||
void ClientEntityManager::handle_task() {
|
||||
TaskPair pair;
|
||||
while (m_tasks.try_pop(pair)) {
|
||||
switch (pair.first) {
|
||||
case Command::CREATE:
|
||||
auto* p = std::get_if<EntityCreateElement>(&pair.second);
|
||||
ASSERT(p);
|
||||
|
||||
add_entity(p->id, p->name, p->pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Cubed
|
||||
Reference in New Issue
Block a user