feat(gameplay): implement entity system with concurrent task handling and fix model loading

This commit is contained in:
2026-07-30 15:05:56 +08:00
parent efa49a98b7
commit b166bab4f3
10 changed files with 109 additions and 30 deletions

View File

@@ -1,5 +1,6 @@
#include "Cubed/gameplay/client_entity_manager.hpp"
#include "Cubed/gameplay/client_world.hpp"
#include "Cubed/gameplay/creatures/pig.hpp"
#include "Cubed/gameplay/ecs/client_entity.hpp"
#include "Cubed/gameplay/ecs/identity.hpp"
@@ -9,24 +10,25 @@
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{});
create_entity_in_registry(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) {
ASSERT(m_factories.contains(name));
m_factories[name](id);
acc a;
ASSERT(m_entities.find(a, id));
auto* c = m_registry.try_get<Transform>(a->second);
auto* c = m_registry.try_get<BaseClientCreature>(a->second);
ASSERT(c);
c->position.value = pos;
c->transform.position.value = pos;
}
void ClientEntityManager::receive_entity_create(S2CEntityCreate& s2c) {
@@ -44,10 +46,13 @@ void ClientEntityManager::handle_task() {
case Command::CREATE:
auto* p = std::get_if<EntityCreateElement>(&pair.second);
ASSERT(p);
add_entity(p->id, p->name, p->pos);
break;
}
}
}
const entt::registry& ClientEntityManager::get_registry() const {
return m_registry;
}
} // namespace Cubed