Add boulder entity and moveable component

scene_man
En Yi 2023-05-29 21:36:06 +08:00
parent a4c9a0d97c
commit 29c9b4eec7
5 changed files with 61 additions and 4 deletions

View File

@ -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;
}

View File

@ -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 {

View File

@ -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,

View File

@ -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

View File

@ -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;
}