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

@@ -20,6 +20,8 @@ public:
void receive_entity_create(S2CEntityCreate& s2c);
const entt::registry& get_registry() const;
private:
struct EntityCreateElement {
EntityID id;
@@ -41,7 +43,14 @@ private:
void handle_task();
template <typename... Args> void add_entity(EntityID id, Args&&... args) {
template <typename... Args>
void create_entity_in_registry(EntityID id, Args&&... args) {
{
cacc a;
if (m_entities.find(a, id)) {
return;
}
}
auto entity = m_registry.create();
((m_registry.emplace<std::remove_cvref_t<Args>>(

View File

@@ -4,30 +4,45 @@
#include <entt/entt.hpp>
#include <tbb/concurrent_hash_map.h>
#include <tbb/concurrent_queue.h>
namespace Cubed {
class ServerWorld;
class Session;
class ServerEntityManager {
public:
ServerEntityManager(ServerWorld& world);
void init();
void update();
// not thread safe
void add_entity(std::string_view name, const glm::vec3& pos);
void handle_player_login(std::shared_ptr<Session> session);
private:
enum class Command { CREATE, SEND_ALL_ENTITIES };
struct EntityCreateElement {
std::string name;
glm::vec3 pos;
};
using EntityMap = tbb::concurrent_hash_map<EntityID, entt::entity>;
using acc = EntityMap::accessor;
using cacc = EntityMap::const_accessor;
using CreateFunc = std::function<EntityID()>;
using TaskElement =
std::variant<std::shared_ptr<Session>, EntityCreateElement>;
using TaskPair = std::pair<Command, TaskElement>;
ServerWorld& m_world;
tbb::concurrent_queue<TaskPair> m_tasks;
entt::registry m_registry;
EntityID m_next = 0;
EntityMap m_entities;
std::unordered_map<std::string_view, CreateFunc> m_factories;
void create_entity(std::string_view name, const glm::vec3& pos);
void send_entity_create(EntityID id, std::string_view name,
const glm::vec3& pos);
void handle_task();
void send_all_entities(std::shared_ptr<Session>& session);
template <typename... Args> EntityID add_entity(Args&&... args) {
auto entity = m_registry.create();