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

82 lines
2.9 KiB
C

#include "tilemap.h"
#include <ecs/ge_render_component.h>
#include <ecs/ge_collision_component.h>
static ge_render_component_t* tilemap_render[TILEMAP_Y_HEIGHT][TILEMAP_X_WIDTH];
static ge_layers_t tilemap_layers[TILEMAP_Y_HEIGHT][TILEMAP_X_WIDTH];
static int real_map[TILEMAP_Y_HEIGHT][TILEMAP_X_WIDTH] = {
{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}, // 第0行: 顶部边界
{2,0,0,1,1,0,0,0,0,0,0,1,1,0,0,2}, // 第1行: 敌方出生点预留
{2,0,0,1,1,0,0,0,0,0,0,1,1,0,0,2}, // 第2行
{2,0,0,0,0,0,0,1,1,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,1,1,0,0,0,0,0,0,2},
{2,0,1,1,0,0,0,0,0,0,0,0,1,1,0,2},
{2,0,1,1,0,0,0,0,0,0,0,0,1,1,0,2},
{2,0,0,0,0,0,1,1,1,1,0,0,0,0,0,2}, // 中央障碍
{2,0,0,0,0,0,1,0,0,1,0,0,0,0,0,2}, // 中央基地区域
{2,0,1,1,0,0,1,0,0,1,0,0,1,1,0,2},
{2,0,1,1,0,0,0,0,0,0,0,0,1,1,0,2},
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,1,1,0,0,0,0,0,0,1,1,0,0,2}, // 第12行: 玩家出生点预留
{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}, // 第13行: 底部边界
};
static inline void fill_render(void) {
// FIXME you can using ecs to store the entity
static ge_render_component_t render_wall = {
.type = GE_RENDER_COMP_TYPE_RECT,
.data.rect.size.x = TILEMAP_SIZE,
.data.rect.size.y = TILEMAP_SIZE,
.data.rect.color = GE_COLOR_GRAY,
};
for (int y = 0; y < TILEMAP_Y_HEIGHT; y++) {
for (int x = 0; x < TILEMAP_X_WIDTH; x++) {
if (real_map[y][x] == 0) {
continue;
}
tilemap_render[y][x] = &render_wall;
tilemap_layers[y][x] = LAYER_WALL;
/**
* the global variable default value is NULL
* tilemap_render[y][x] = NULL;
*/
}
}
}
void init_tilemap(ge_ecs_t* ecs, ge_entity_t **_tilemap) {
Assert(ecs != NULL && _tilemap != NULL);
ge_ecs_add_entity(ecs, _tilemap);
ge_entity_t* tilemap = *_tilemap;
Assert(tilemap != NULL);
tilemap->component_mask = (
GE_COMPONENT_ACVIVE |
GE_COMPONENT_POSITION |
GE_COMPONENT_RENDERABLE |
GE_COMPONENT_COLLIDER
);
/**
* y = 16
*/
tilemap->position = (ge_vector2i_t) {.x = 0, .y = 16};
tilemap->renderable = (ge_render_component_t) {
.type = GE_RENDER_COMP_TYPE_TILEMAP,
.data.tilemap.tile_size = TILEMAP_SIZE,
.data.tilemap.map_size.x = TILEMAP_X_WIDTH,
.data.tilemap.map_size.y = TILEMAP_Y_HEIGHT,
.data.tilemap.components = (ge_render_component_t**)tilemap_render,
};
tilemap->collision = (ge_collision_component_t) {
.type = GE_COLLISION_COMP_TYPE_TILEMAP,
.layers = LAYER_WALL,
.collider.tilemap = {
.tile_size = TILEMAP_SIZE,
.map_size = { TILEMAP_X_WIDTH, TILEMAP_Y_HEIGHT },
.layers = (ge_layers_t*)tilemap_layers,
},
};
fill_render();
}