From 29c9b4eec76807fcd820c58b8b13eb3cf39cb360 Mon Sep 17 00:00:00 2001 From: En Yi Date: Mon, 29 May 2023 21:36:06 +0800 Subject: [PATCH] Add boulder entity and moveable component --- scenes/editor_scene.c | 30 ++++++++++++++++++++++++++++-- scenes/engine/EC/EC.h | 13 +++++++++++-- scenes/engine/EC/mempool.c | 2 ++ scenes/ent_impl.h | 1 + scenes/items_ent.c | 19 +++++++++++++++++++ 5 files changed, 61 insertions(+), 4 deletions(-) diff --git a/scenes/editor_scene.c b/scenes/editor_scene.c index 2b276a3..0bec5c3 100644 --- a/scenes/editor_scene.c +++ b/scenes/editor_scene.c @@ -16,9 +16,10 @@ enum EntitySpawnSelection { TOGGLE_LADDER, SPAWN_CRATE, SPAWN_METAL_CRATE, + SPAWN_BOULDER, }; -#define MAX_SPAWN_TYPE 5 +#define MAX_SPAWN_TYPE 6 static unsigned int current_spawn_selection = 0; static inline unsigned int get_tile_idx(int x, int y, const TileGrid_t* tilemap) @@ -90,10 +91,22 @@ static void level_scene_render_func(Scene_t* scene) case CRATES_ENT_TAG: colour = p_bbox->fragile? BROWN : GRAY; break; + case BOULDER_ENT_TAG: + colour = GRAY; + break; default: colour = BLACK; + break; + } + + if (p_ent->m_tag == BOULDER_ENT_TAG) + { + DrawCircleV(Vector2Add(p_ct->position, p_bbox->half_size), p_bbox->half_size.x, colour); + } + else + { + DrawRectangle(p_ct->position.x, p_ct->position.y, p_bbox->size.x, p_bbox->size.y, colour); } - DrawRectangle(p_ct->position.x, p_ct->position.y, p_bbox->size.x, p_bbox->size.y, colour); CHurtbox_t* p_hurtbox = get_component(p_ent, CHURTBOX_T); CHitBoxes_t* p_hitbox = get_component(p_ent, CHITBOXES_T); if (p_hitbox != NULL) @@ -220,6 +233,16 @@ static void spawn_crate(Scene_t* scene, unsigned int tile_idx, bool metal) p_ctransform->position.y = (tile_idx / data->tilemap.width) * TILE_SIZE; } +static void spawn_boulder(Scene_t* scene, unsigned int tile_idx) +{ + LevelSceneData_t* data = &(CONTAINER_OF(scene, LevelScene_t, scene)->data); + Entity_t* p_boulder = create_boulder(&scene->ent_manager, &scene->engine->assets); + + CTransform_t* p_ctransform = get_component(p_boulder, CTRANSFORM_COMP_T); + p_ctransform->position.x = (tile_idx % data->tilemap.width) * TILE_SIZE; + p_ctransform->position.y = (tile_idx / data->tilemap.width) * TILE_SIZE; +} + static void toggle_block_system(Scene_t* scene) { // TODO: This system is not good as the interface between raw input and actions is broken @@ -300,6 +323,9 @@ static void toggle_block_system(Scene_t* scene) case SPAWN_METAL_CRATE: spawn_crate(scene, tile_idx, true); break; + case SPAWN_BOULDER: + spawn_boulder(scene, tile_idx); + break; } last_tile_idx = tile_idx; } diff --git a/scenes/engine/EC/EC.h b/scenes/engine/EC/EC.h index badc166..257410e 100644 --- a/scenes/engine/EC/EC.h +++ b/scenes/engine/EC/EC.h @@ -6,8 +6,8 @@ #include "sc/map/sc_map.h" #include "sc/queue/sc_queue.h" -#define N_TAGS 4 -#define N_COMPONENTS 10 +#define N_TAGS 5 +#define N_COMPONENTS 11 #define MAX_COMP_POOL_SIZE 1024 typedef struct EntityManager EntityManager_t; typedef struct Entity Entity_t; @@ -23,6 +23,7 @@ typedef enum ComponentEnum { CHITBOXES_T, CHURTBOX_T, CSPRITE_T, + CMOVEABLE_T, } ComponentEnum_t; typedef struct _CBBox_t { @@ -135,6 +136,13 @@ typedef struct _CSprite_t { bool pause; } CSprite_t; +typedef struct _CMoveable_t { + uint16_t move_speed; + Vector2 prev_pos; + Vector2 target_pos; + bool gridmove; +} CMoveable_t; + static inline void set_bbox(CBBox_t* p_bbox, unsigned int x, unsigned int y) { p_bbox->size.x = x; @@ -148,6 +156,7 @@ typedef enum EntityTag { PLAYER_ENT_TAG, ENEMY_ENT_TAG, CRATES_ENT_TAG, + BOULDER_ENT_TAG, } EntityTag_t; struct Entity { diff --git a/scenes/engine/EC/mempool.c b/scenes/engine/EC/mempool.c index 815281c..ede8697 100644 --- a/scenes/engine/EC/mempool.c +++ b/scenes/engine/EC/mempool.c @@ -17,6 +17,7 @@ static CContainer_t ccontainer_buffer[MAX_COMP_POOL_SIZE]; static CHitBoxes_t chitboxes_buffer[MAX_COMP_POOL_SIZE]; static CHurtbox_t churtbox_buffer[MAX_COMP_POOL_SIZE]; static CSprite_t csprite_buffer[MAX_COMP_POOL_SIZE]; +static CMoveable_t cmoveable_buffer[MAX_COMP_POOL_SIZE]; typedef struct ULongCircBuffer { unsigned long* buffer; // data buffer @@ -86,6 +87,7 @@ static MemPool_t comp_mempools[N_COMPONENTS] = { {chitboxes_buffer, MAX_COMP_POOL_SIZE, sizeof(CHitBoxes_t), NULL, {0}}, {churtbox_buffer, MAX_COMP_POOL_SIZE, sizeof(CHurtbox_t), NULL, {0}}, {csprite_buffer, MAX_COMP_POOL_SIZE, sizeof(CSprite_t), NULL, {0}}, + {cmoveable_buffer, MAX_COMP_POOL_SIZE, sizeof(CMoveable_t), NULL, {0}}, }; static MemPool_t ent_mempool = { .buffer = entity_buffer, diff --git a/scenes/ent_impl.h b/scenes/ent_impl.h index 40674e7..7a2bee0 100644 --- a/scenes/ent_impl.h +++ b/scenes/ent_impl.h @@ -5,5 +5,6 @@ bool init_player_creation(const char* info_file, Assets_t* assets); Entity_t* create_player(EntityManager_t* ent_manager, Assets_t* assets); Entity_t* create_crate(EntityManager_t* ent_manager, Assets_t* assets, bool metal); +Entity_t* create_boulder(EntityManager_t* ent_manager, Assets_t* assets); #endif // __ENT_IMPL_H diff --git a/scenes/items_ent.c b/scenes/items_ent.c index aa5a2d5..ab215b1 100644 --- a/scenes/items_ent.c +++ b/scenes/items_ent.c @@ -18,3 +18,22 @@ Entity_t* create_crate(EntityManager_t* ent_manager, Assets_t* assets, bool meta p_hurtbox->fragile = !metal; return p_crate; } + +Entity_t* create_boulder(EntityManager_t* ent_manager, Assets_t* assets) +{ + Entity_t* p_boulder = add_entity(ent_manager, BOULDER_ENT_TAG); + CBBox_t* p_bbox = add_component(p_boulder, CBBOX_COMP_T); + + set_bbox(p_bbox, TILE_SIZE, TILE_SIZE); + p_bbox->solid = true; + p_bbox->fragile = false; + + add_component(p_boulder, CTRANSFORM_COMP_T); + add_component(p_boulder, CMOVEMENTSTATE_T); + add_component(p_boulder, CTILECOORD_COMP_T); + add_component(p_boulder, CMOVEABLE_T); + CHurtbox_t* p_hurtbox = add_component(p_boulder, CHURTBOX_T); + p_hurtbox->size = p_bbox->size; + p_hurtbox->fragile = false; + return p_boulder; +}