feat(gameplay): implement entity update packets

Add S2CEntityUpdate to sync entity positions from server to client, including update handling in the client entity manager and server-side AI updates.
This commit is contained in:
2026-07-31 17:44:33 +08:00
parent d6f304e6ca
commit 8451921161
7 changed files with 63 additions and 5 deletions

View File

@@ -10,14 +10,15 @@ namespace Cubed {
class ClientWorld;
class ClientEntityManager {
public:
enum class Command { CREATE, DESTORY };
enum class Command { CREATE, DESTORY, UPDATE };
ClientEntityManager(ClientWorld& world);
void update();
void init();
void receive_entity_create(S2CEntityCreate& s2c);
void receive_entity_create(S2CEntityCreate& msg);
void receive_entity_destory(EntityID id);
void receive_entity_update(S2CEntityUpdate& msg);
void destory(EntityID id);
void create(std::string_view name, const glm::vec3& pos);
@@ -30,11 +31,17 @@ private:
std::string name;
glm::vec3 pos;
};
struct UpdateInfo {
EntityID id;
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<void(EntityID id)>;
using TaskElement = std::variant<EntityCreateElement, EntityID>;
using TaskElement = std::variant<EntityCreateElement, EntityID, UpdateInfo>;
using TaskPair = std::pair<Command, TaskElement>;
ClientWorld& m_world;
@@ -48,6 +55,7 @@ private:
// not thread safe
void handle_entity_create(EntityID id, std::string_view name,
const glm::vec3& pos);
void handle_entity_update(UpdateInfo& info);
template <typename... Args>
void create_entity_in_registry(EntityID id, Args&&... args) {
{

View File

@@ -51,10 +51,12 @@ enum class PacketEnum : uint16_t {
LOGIN_RSP = 1002,
LOGOUT_REQ = 1003,
LOGOUT_RSP = 1004,
PLAYER_INFO = 2001,
C2S_PLAYER_INFO = 2002,
PLAYER_INFO_RSP = 2003,
PLAYER_WATER_SOUND = 2004,
CHUNK_DATA_REQ = 3001,
CHUNK_DATA_RSP = 3002,
BLOCK_CHANGE_REQ = 3003,
@@ -65,8 +67,11 @@ enum class PacketEnum : uint16_t {
S2C_ENTITY_DESTORY = 3008,
C2S_ENTITY_CREATE_REQUEST = 3009,
C2S_ENTITY_DESTORY_REQUEST = 3010,
S2C_ENTITY_UPDATE = 3011,
CHAT_MSG = 4001,
VOICE_MSG = 4002,
PING = 9001,
PONG = 9002
@@ -145,6 +150,9 @@ template <> constexpr uint16_t get_packet_id<ChatMsg>() {
template <> constexpr uint16_t get_packet_id<VoiceMsg>() {
return std::to_underlying(PacketEnum::VOICE_MSG);
}
template <> constexpr uint16_t get_packet_id<S2CEntityUpdate>() {
return std::to_underlying(PacketEnum::S2C_ENTITY_UPDATE);
}
template <typename T>
requires std::derived_from<T, google::protobuf::Message>

View File

@@ -44,6 +44,7 @@ private:
void handle_entity_destory(EntityID id);
void handle_task();
void send_all_entities(std::shared_ptr<Session>& session);
void update_ai();
template <typename... Args>
EntityID create_entity_in_factory(Args&&... args) {
auto entity = m_registry.create();