#pragma once // ============================================================================ // world.h - 实体组件系统 (ECS) // ============================================================================ #include #include "../include/engine_api.h" #define MAX_ENTITIES 4096 #define MAX_COMPONENTS 32 typedef uint32_t Entity; typedef uint32_t ComponentType; // 组件接口 typedef struct { void (*destroy)(void* component); } ComponentVTable; // 世界 typedef struct { Entity entities[MAX_ENTITIES]; uint32_t entity_count; uint32_t next_entity; void* components[MAX_COMPONENTS][MAX_ENTITIES]; ComponentVTable component_tables[MAX_COMPONENTS]; uint32_t component_sizes[MAX_COMPONENTS]; uint32_t component_count; uint64_t entity_masks[MAX_ENTITIES]; } World; // 创建/销毁 World* world_create(void); void world_destroy(World* world); // 实体 Entity world_create_entity(World* world); void world_destroy_entity(World* world, Entity entity); int world_entity_alive(World* world, Entity entity); // 组件 ComponentType world_register_component(World* world, uint32_t size, ComponentVTable vtable); void* world_add_component(World* world, Entity entity, ComponentType type); void* world_get_component(World* world, Entity entity, ComponentType type); void world_remove_component(World* world, Entity entity, ComponentType type); int world_has_component(World* world, Entity entity, ComponentType type); // 系统遍历 typedef void (*SystemFunc)(World* world, Entity entity, void* data); void world_each(World* world, uint64_t mask, SystemFunc func, void* data);