feat(gameplay): add entity destruction support

This commit is contained in:
2026-07-30 15:24:19 +08:00
parent b166bab4f3
commit 826c6600d0
7 changed files with 74 additions and 14 deletions

View File

@@ -10,7 +10,7 @@ namespace Cubed {
class ClientWorld;
class ClientEntityManager {
public:
enum class Command { CREATE };
enum class Command { CREATE, DESTORY };
ClientEntityManager(ClientWorld& world);
void update();
@@ -19,7 +19,7 @@ public:
void add_entity(EntityID id, std::string_view name, const glm::vec3& pos);
void receive_entity_create(S2CEntityCreate& s2c);
void destory(EntityID id);
const entt::registry& get_registry() const;
private:
@@ -32,7 +32,7 @@ private:
using acc = EntityMap::accessor;
using cacc = EntityMap::const_accessor;
using CreateFunc = std::function<void(EntityID id)>;
using TaskElement = std::variant<EntityCreateElement>;
using TaskElement = std::variant<EntityCreateElement, EntityID>;
using TaskPair = std::pair<Command, TaskElement>;
ClientWorld& m_world;
@@ -42,7 +42,7 @@ private:
tbb::concurrent_queue<TaskPair> m_tasks;
void handle_task();
void handle_entity_destory(EntityID id);
template <typename... Args>
void create_entity_in_registry(EntityID id, Args&&... args) {
{

View File

@@ -62,6 +62,7 @@ enum class PacketEnum : uint16_t {
S2C_CLEAR_ALL_CHUNKS = 3005,
UPDATE_TIME = 3006,
S2C_ENTITY_CREATE = 3007,
S2C_ENTITY_DESTORY = 3008,
CHAT_MSG = 4001,
VOICE_MSG = 4002,
PING = 9001,
@@ -115,6 +116,9 @@ template <> constexpr uint16_t get_packet_id<S2C_ClearAllChunks>() {
template <> constexpr uint16_t get_packet_id<S2CEntityCreate>() {
return std::to_underlying(PacketEnum::S2C_ENTITY_CREATE);
}
template <> constexpr uint16_t get_packet_id<S2CEntityDestory>() {
return std::to_underlying(PacketEnum::S2C_ENTITY_DESTORY);
}
template <> constexpr uint16_t get_packet_id<UpdateTime>() {
return std::to_underlying(PacketEnum::UPDATE_TIME);
}

View File

@@ -16,11 +16,11 @@ public:
void update();
// not thread safe
void add_entity(std::string_view name, const glm::vec3& pos);
void destory(EntityID id);
void handle_player_login(std::shared_ptr<Session> session);
private:
enum class Command { CREATE, SEND_ALL_ENTITIES };
enum class Command { CREATE, SEND_ALL_ENTITIES, DESTORY };
struct EntityCreateElement {
std::string name;
glm::vec3 pos;
@@ -30,7 +30,7 @@ private:
using cacc = EntityMap::const_accessor;
using CreateFunc = std::function<EntityID()>;
using TaskElement =
std::variant<std::shared_ptr<Session>, EntityCreateElement>;
std::variant<std::shared_ptr<Session>, EntityCreateElement, EntityID>;
using TaskPair = std::pair<Command, TaskElement>;
ServerWorld& m_world;
tbb::concurrent_queue<TaskPair> m_tasks;
@@ -39,8 +39,9 @@ private:
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_entity_create(EntityID id, std::string_view name,
const glm::vec3& pos);
void handle_entity_destory(EntityID id);
void handle_task();
void send_all_entities(std::shared_ptr<Session>& session);
template <typename... Args> EntityID add_entity(Args&&... args) {