feat(game): 添加基础游戏引擎和渲染模块
- 新增游戏引擎核心模块,包括初始化和运行逻辑 - 实现基本的渲染功能,支持控制台输出 - 添加物理引擎基础,包括碰撞检测 - 集成日志系统,用于调试和信息输出 - 创建窗口和输入管理模块
This commit is contained in:
20
game_engine/physics/ge_collision_box.h
Normal file
20
game_engine/physics/ge_collision_box.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef __GE_COLLISION_H__
|
||||
#define __GE_COLLISION_H__
|
||||
|
||||
#include "ge_physics.h"
|
||||
|
||||
typedef struct ge_collision {
|
||||
ge_phy_layers_t layers;
|
||||
ge_phy_layers_t mask;
|
||||
ge_vector2i_t position;
|
||||
ge_vector2i_t size;
|
||||
} ge_phy_box_t;
|
||||
|
||||
static inline int check_box_collision(ge_phy_box_t* a, ge_phy_box_t* b) {
|
||||
return (a->position.x < b->position.x + b->size.x) &&
|
||||
(a->position.x + a->size.x > b->position.x) &&
|
||||
(a->position.y < b->position.y + b->size.y) &&
|
||||
(a->position.y + a->size.y > b->position.y);
|
||||
}
|
||||
|
||||
#endif // __GE_COLLISION_H__
|
||||
58
game_engine/physics/ge_collision_tilemap.h
Normal file
58
game_engine/physics/ge_collision_tilemap.h
Normal file
@@ -0,0 +1,58 @@
|
||||
|
||||
#include "ge_physics.h"
|
||||
#include "ge_collision_box.h"
|
||||
|
||||
typedef struct {
|
||||
ge_vector2i_t position;
|
||||
ge_vector2i_t size;
|
||||
ge_int_t tile_size;
|
||||
ge_phy_layers_t* layers; // 二维layer数组
|
||||
} ge_phy_tilemap_t;
|
||||
|
||||
static inline int check_tilemap_collision(ge_phy_tilemap_t* tilemap, ge_phy_box_t* entity_collision) {
|
||||
Assert(tilemap->tile_size > 0);
|
||||
const int tile_size_int = tilemap->tile_size;
|
||||
|
||||
// 计算瓦片地图的网格尺寸(列数和行数)
|
||||
const int grid_cols = tilemap->size.x / tile_size_int;
|
||||
const int grid_rows = tilemap->size.y / tile_size_int;
|
||||
|
||||
// 计算实体碰撞盒在瓦片地图局部坐标系中的位置
|
||||
const ge_vector2i_t local_entity_pos = GE_VEC2I_SUB(
|
||||
entity_collision->position,
|
||||
tilemap->position
|
||||
);
|
||||
|
||||
// 计算实体边界(使用闭区间)
|
||||
const int entity_left = local_entity_pos.x;
|
||||
const int entity_top = local_entity_pos.y;
|
||||
const int entity_right = entity_left + entity_collision->size.x - 1; // 闭区间右边界
|
||||
const int entity_bottom = entity_top + entity_collision->size.y - 1; // 闭区间下边界
|
||||
|
||||
// 计算覆盖的瓦片范围
|
||||
const int start_tile_x = GE_MAX(0, entity_left / tile_size_int);
|
||||
const int start_tile_y = GE_MAX(0, entity_top / tile_size_int);
|
||||
const int end_tile_x = GE_MIN(grid_cols - 1, entity_right / tile_size_int);
|
||||
const int end_tile_y = GE_MIN(grid_rows - 1, entity_bottom / tile_size_int);
|
||||
|
||||
// 遍历实体覆盖的瓦片区域
|
||||
for (int y = start_tile_y; y <= end_tile_y; y++) {
|
||||
for (int x = start_tile_x; x <= end_tile_x; x++) {
|
||||
// 计算当前瓦片的边界(开区间)
|
||||
const int tile_left = x * tile_size_int;
|
||||
const int tile_top = y * tile_size_int;
|
||||
const int tile_right = tile_left + tile_size_int; // 开区间右边界
|
||||
const int tile_bottom = tile_top + tile_size_int; // 开区间下边界
|
||||
|
||||
// 检查实体是否实际接触到瓦片内部
|
||||
const int overlap_x = entity_left < tile_right && entity_right > tile_left;
|
||||
const int overlap_y = entity_top < tile_bottom && entity_bottom > tile_top;
|
||||
|
||||
if (overlap_x && overlap_y && tilemap->layers[y * grid_cols + x] != 0) {
|
||||
return 1; // 检测到碰撞
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0; // 无碰撞
|
||||
}
|
||||
27
game_engine/physics/ge_collison.h
Normal file
27
game_engine/physics/ge_collison.h
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
|
||||
// // 在 ge_common.h 中定义通用碰撞类型
|
||||
// typedef enum ge_collision_type {
|
||||
// GE_COLLISION_TYPE_BOX, // 实体间碰撞
|
||||
// GE_COLLISION_TYPE_TILEMAP // 实体与瓦片地图碰撞
|
||||
// } ge_collision_type_t;
|
||||
|
||||
// // 通用碰撞数据结构
|
||||
// typedef struct {
|
||||
// ge_collision_type_t type;
|
||||
// void* entityA; // 主要实体(通常是被检测的实体)
|
||||
// void* entityB; // 对于BOX碰撞,这是另一个实体;对于TILEMAP,这是瓦片地图
|
||||
// ge_vector2i_t collision_point;
|
||||
// union {
|
||||
// struct {
|
||||
// int tile_x;
|
||||
// int tile_y;
|
||||
// } tilemap_data; // 瓦片地图碰撞特有数据
|
||||
// struct {
|
||||
// // 可以添加实体间碰撞特有数据
|
||||
// } box_data;
|
||||
// } specific;
|
||||
// } ge_collision_event_t;
|
||||
|
||||
// // 碰撞回调函数类型
|
||||
// typedef void (*ge_collision_callback_t)(ge_collision_event_t* event);
|
||||
8
game_engine/physics/ge_physics.h
Normal file
8
game_engine/physics/ge_physics.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#ifndef __GE_PHYSICS_H__
|
||||
#define __GE_PHYSICS_H__
|
||||
|
||||
#include <ge_common.h>
|
||||
|
||||
typedef ge_u8_t ge_phy_layers_t;
|
||||
|
||||
#endif // __GE_PHYSICS_H__
|
||||
Reference in New Issue
Block a user