Files
school_stm32/game_engine/ecs/ge_entity.h
ZZY b5b2c90e75 feat(game_core): 重构游戏引擎并添加新功能
- 重构了游戏引擎的核心逻辑和架构
- 添加了新的实体组件系统(ECS)
- 实现了简单的碰撞检测和响应
- 新增了地图和子弹功能
- 优化了输入处理和渲染逻辑
- 调整了游戏控制方式
2025-07-02 12:14:57 +08:00

68 lines
1.8 KiB
C

#ifndef __GE_ENTIRY_H__
#define __GE_ENTIRY_H__
#include <utils/ge_vector2i.h>
#include "ge_render_component.h"
#include "ge_physics_component.h"
#include "ge_collision_component.h"
#include <stdint.h>
typedef uint16_t ge_ecs_id_t; // 支持65536个实体
#define GE_ECS_MAX 128
typedef enum {
GE_COMPONENT_ACVIVE = 1 << 0,
GE_COMPONENT_POSITION = 1 << 1,
GE_COMPONENT_TRANSFORM = 1 << 2, // TODO not implimented
GE_COMPONENT_RENDERABLE = 1 << 3,
GE_COMPONENT_PHYSICS_BODY = 1 << 4,
GE_COMPONENT_COLLIDER = 1 << 5,
GE_COMPONENT_TIMED_LIFE = 1 << 6, // TODO not implimented
} ge_ecs_mask_t;
#define GE_RENDERABLE_MASK \
(GE_COMPONENT_POSITION | GE_COMPONENT_RENDERABLE)
#define GE_PHYSICS_MASK \
(GE_COMPONENT_POSITION | GE_COMPONENT_PHYSICS_BODY)
#define GE_COLLIDER_MASK \
(GE_COMPONENT_POSITION | GE_COMPONENT_COLLIDER)
typedef struct ge_entity {
int user_type;
ge_ecs_id_t id;
ge_ecs_mask_t component_mask;
ge_vector2i_t position;
ge_render_component_t renderable;
ge_physics_component_t physics_body;
ge_collision_component_t collision;
} ge_entity_t;
typedef struct {
// TODO static storage to dynamic storage
ge_entity_t entities[GE_ECS_MAX];
ge_ecs_id_t count;
} ge_ecs_storage_t;
typedef struct ge_ecs {
ge_ecs_storage_t storage;
} ge_ecs_t;
static inline ge_ecs_id_t ge_ecs_add_entity(ge_ecs_t* ecs, ge_entity_t** entity) {
ge_ecs_id_t id = ++ecs->storage.count;
if (id >= GE_ECS_MAX) {
return 0;
}
// TODO squeze the data
if (entity != NULL) *entity = &ecs->storage.entities[id];
return id;
}
static inline ge_entity_t* ge_ecs_get_entity(ge_ecs_t* ecs, ge_ecs_id_t id) {
if(id >= ecs->storage.count) {
return NULL;
}
return &ecs->storage.entities[id];
}
#endif // __GE_ENTIRY_H__