feat(ecs): add entity type separation and creature limits

Replace generic entity creation with typed creatures and items. Rename `add_entity` to `add_creature`, enforce per-player creature cap, and track entity/creature totals for metrics. Expose run mode to dev panel and show new counts.
This commit is contained in:
2026-08-06 20:13:55 +08:00
parent 33cfea1f36
commit 94d92f2159
10 changed files with 90 additions and 38 deletions

View File

@@ -3,9 +3,12 @@
namespace Cubed {
using EntityID = uint64_t;
enum class EntityType { CREATURE, ITEM };
struct Entity {
EntityID id;
explicit Entity(EntityID id) : id(id) {}
EntityType type;
Entity(EntityID id, EntityType type) : id(id), type(type) {}
};
} // namespace Cubed

View File

@@ -12,15 +12,20 @@ class ServerWorld;
class Session;
class ServerEntityManager {
public:
static constexpr size_t PER_CREATURE_LIMITS = 100;
ServerEntityManager(ServerWorld& world);
void init();
void update();
// not thread safe
void add_entity(std::string_view name, const glm::vec3& world_pos);
void add_creature(std::string_view name, const glm::vec3& world_pos);
void destory(EntityID id);
void handle_player_login(std::shared_ptr<Session> session);
size_t max_creature_sum() const;
size_t creature_sum() const;
size_t entity_sum() const;
private:
enum class Command { CREATE, SEND_ALL_ENTITIES, DESTORY };
struct EntityCreateElement {
@@ -43,6 +48,8 @@ private:
std::variant<std::shared_ptr<Session>, EntityCreateElement, EntityID>;
using TaskPair = std::pair<Command, TaskElement>;
ServerWorld& m_world;
std::atomic<size_t> m_creature_sum{0};
std::atomic<size_t> m_entity_sum{0};
tbb::concurrent_queue<TaskPair> m_tasks;
entt::registry m_registry;
EntityID m_next = 0;

View File

@@ -98,6 +98,8 @@ public:
uint32_t get_chunk_ref_count(const glm::vec3& pos) const;
ServerEntityManager& entity_manager();
std::shared_ptr<ThreadPool> get_compute_pool();
size_t player_sum() const;
int get_block(const glm::ivec3& block_pos) const override;
bool is_solid(const glm::ivec3& block_pos) const override;
bool can_pass_block(const glm::ivec3& block_pos) const override;

View File

@@ -42,6 +42,8 @@ public:
bool is_recording() const;
RunMode runmode() const;
private:
enum class PauseUI { PAUSE_MENU, INVENTORY };
SceneManager& m_scene_manager;
@@ -61,6 +63,7 @@ private:
ErrorUI m_error_ui;
const Argument& m_argument;
VoiceInputType m_input_type;
RunMode m_runmode = RunMode::HYBRID;
bool handle_mouse_move_event(const MouseMoveEvent& e) override;
bool handle_mouse_button_event(const MouseButtonEvent& e) override;
bool handle_window_resize_event(const WindowResizeEvent& e) override;