feat(gameplay): spawn creatures during chunk generation

Add creature spawning to chunk generation with a configurable
SpawnConfig, including a default pig spawn. Spawning occurs in
the final generation phase and registers entities through the
server world's entity manager.

Also destroy entities when their chunk is unloaded to prevent
orphaned entities and expose the entity manager from ServerWorld.
This commit is contained in:
2026-08-05 16:56:02 +08:00
parent 322fb2fea0
commit 8ff789c576
8 changed files with 62 additions and 4 deletions

View File

@@ -50,6 +50,8 @@ public:
void generate_cave();
void generate_river();
void spawn_creature();
private:
static inline std::atomic<bool> is_init{false};
static inline unsigned m_generator_seed{0};

View File

@@ -0,0 +1,20 @@
#pragma once
#include "Cubed/gameplay/biome.hpp"
#include <array>
#include <span>
#include <string_view>
namespace Cubed {
struct SpawnConfig {
std::string_view name; // factory key, e.g. "cubed:pig"
std::span<const BiomeType> biomes; // allowed biomes
float probability = 0.0f; // per chunk spawn probability
unsigned max_spawn_count = 0; // per chunk_max_spawn_sum
};
namespace SpawnDefaults {
constexpr std::array<BiomeType, 2> PIG_BIOMES{BiomeType::PLAIN,
BiomeType::FOREST};
constexpr SpawnConfig PIG{"cubed:pig", PIG_BIOMES, 0.02f, 3};
} // namespace SpawnDefaults
} // namespace Cubed

View File

@@ -15,7 +15,7 @@ public:
void init();
void update();
// not thread safe
void add_entity(std::string_view name, const glm::vec3& pos);
void add_entity(std::string_view name, const glm::vec3& world_pos);
void destory(EntityID id);
void handle_player_login(std::shared_ptr<Session> session);

View File

@@ -95,6 +95,7 @@ public:
std::vector<std::shared_ptr<Session>> get_all_session() const;
uint32_t get_chunk_ref_count(const glm::vec3& pos) const;
ServerEntityManager& entity_manager();
int get_block(const glm::ivec3& block_pos) const override;
bool is_solid(const glm::ivec3& block_pos) const override;