diff --git a/components.h b/components.h index d9a7622..c300ef3 100644 --- a/components.h +++ b/components.h @@ -3,11 +3,12 @@ #include "raylib.h" // TODO: Look at sc to use macros to auto generate functions -#define N_COMPONENTS 4 +#define N_COMPONENTS 5 enum ComponentEnum { CBBOX_COMP_T, CTRANSFORM_COMP_T, + CTILECOORD_COMP_T, CJUMP_COMP_T, CPLAYERSTATE_T }; diff --git a/mempool.c b/mempool.c index 0c747f2..e94d478 100644 --- a/mempool.c +++ b/mempool.c @@ -6,6 +6,7 @@ static Entity_t entity_buffer[MAX_COMP_POOL_SIZE]; static CBBox_t bbox_buffer[MAX_COMP_POOL_SIZE]; static CTransform_t ctransform_buffer[MAX_COMP_POOL_SIZE]; +static CTileCoord_t ctilecoord_buffer[MAX_COMP_POOL_SIZE]; static CJump_t cjump_buffer[1]; // Only player is expected to have this static CPlayerState_t cplayerstate_buffer[1]; // Only player is expected to have this @@ -29,6 +30,7 @@ static MemPool_t comp_mempools[N_COMPONENTS] = { {bbox_buffer, MAX_COMP_POOL_SIZE, sizeof(CBBox_t), NULL, {0}}, {ctransform_buffer, MAX_COMP_POOL_SIZE, sizeof(CTransform_t), NULL, {0}}, + {ctilecoord_buffer, MAX_COMP_POOL_SIZE, sizeof(CTileCoord_t), NULL, {0}}, {cjump_buffer, 1, sizeof(CJump_t), NULL, {0}}, {cplayerstate_buffer, 1, sizeof(CPlayerState_t), NULL, {0}}, }; diff --git a/scene_impl.c b/scene_impl.c index ee4a8ce..85018b5 100644 --- a/scene_impl.c +++ b/scene_impl.c @@ -9,7 +9,7 @@ static const Vector2 TILE_SZ = {TILE_SIZE, TILE_SIZE}; static Tile_t all_tiles[MAX_N_TILES] = {0}; static const Vector2 GRAVITY = {0, GRAV_ACCEL}; -static const Vector2 UPTHRUST = {0, -GRAV_ACCEL * 0.7}; +static const Vector2 UPTHRUST = {0, -GRAV_ACCEL * 1.1}; static inline unsigned int get_tile_idx(int x, int y, unsigned int tilemap_width) { @@ -638,6 +638,49 @@ static void player_collision_system(Scene_t *scene) } } +static void update_tilemap_system(Scene_t *scene) +{ + LevelSceneData_t *data = (LevelSceneData_t *)scene->scene_data; + TileGrid_t tilemap = data->tilemap; + + Entity_t *p_ent; + sc_map_foreach_value(&scene->ent_manager.entities, p_ent) + { + CTileCoord_t * p_tilecoord = get_component(&scene->ent_manager, p_ent, CTILECOORD_COMP_T); + if (p_tilecoord == NULL) continue; + CTransform_t * p_ctransform = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T); + if (p_ctransform == NULL) continue; + CBBox_t * p_bbox = get_component(&scene->ent_manager, p_ent, CBBOX_COMP_T); + if (p_bbox == NULL) continue; + + // Update tilemap position + for (size_t i=0;in_tiles;++i) + { + // Use previously store tile position + // Clear from those positions + unsigned int tile_idx = p_tilecoord->tiles[i]; + sc_map_del_64(&(tilemap.tiles[tile_idx].entities_set), p_ent->m_id); + } + p_tilecoord->n_tiles = 0; + + // Compute new occupied tile positions and add + // Extend the check by a little to avoid missing + unsigned int tile_x1 = (p_ctransform->position.x) / TILE_SIZE; + unsigned int tile_y1 = (p_ctransform->position.y) / TILE_SIZE; + unsigned int tile_x2 = (p_ctransform->position.x + p_bbox->size.x) / TILE_SIZE; + unsigned int tile_y2 = (p_ctransform->position.y + p_bbox->size.y) / TILE_SIZE; + + for (unsigned int tile_y=tile_y1; tile_y <= tile_y2; tile_y++) + { + for (unsigned int tile_x=tile_x1; tile_x <= tile_x2; tile_x++) + { + unsigned int tile_idx = tile_y * tilemap.width + tile_x; + p_tilecoord->tiles[p_tilecoord->n_tiles++] = tile_idx; + sc_map_put_64(&(tilemap.tiles[tile_idx].entities_set), p_ent->m_id, 0); + } + } + } +} static void toggle_block_system(Scene_t *scene) { @@ -714,7 +757,7 @@ void init_level_scene(LevelScene_t *scene) sc_array_add(&scene->scene.systems, &player_movement_input_system); sc_array_add(&scene->scene.systems, &player_bbox_update_system); sc_array_add(&scene->scene.systems, &movement_update_system); - //sc_array_add(&scene->scene.systems, &update_tilemap_system); + sc_array_add(&scene->scene.systems, &update_tilemap_system); sc_array_add(&scene->scene.systems, &player_collision_system); sc_array_add(&scene->scene.systems, &player_ground_air_transition_system); sc_array_add(&scene->scene.systems, &toggle_block_system); diff --git a/scene_test.c b/scene_test.c index 9b1a636..7949978 100644 --- a/scene_test.c +++ b/scene_test.c @@ -24,6 +24,7 @@ int main(void) p_cjump->jumps = 1; p_cjump->max_jumps = 1; add_component(&scene.scene.ent_manager, p_ent, CPLAYERSTATE_T); + add_component(&scene.scene.ent_manager, p_ent, CTILECOORD_COMP_T); update_entity_manager(&scene.scene.ent_manager); //for (size_t step = 0; step < 6000; step++) while(true)