Compare commits

..

No commits in common. "a8567e26666fb30f721ddf6dca2f507090bd9627" and "cf3894995617535f53d820ec240409788f202961" have entirely different histories.

8 changed files with 76 additions and 212 deletions

View File

@ -4,7 +4,7 @@
#include <stdint.h> #include <stdint.h>
// TODO: Look at sc to use macros to auto generate functions // TODO: Look at sc to use macros to auto generate functions
#define N_COMPONENTS 9 #define N_COMPONENTS 7
enum ComponentEnum enum ComponentEnum
{ {
CBBOX_COMP_T, CBBOX_COMP_T,
@ -14,8 +14,6 @@ enum ComponentEnum
CJUMP_COMP_T, CJUMP_COMP_T,
CPLAYERSTATE_T, CPLAYERSTATE_T,
CCONTAINER_T, CCONTAINER_T,
CHITBOX_T,
CHURTBOX_T,
}; };
typedef enum ComponentEnum ComponentEnum_t; typedef enum ComponentEnum ComponentEnum_t;
@ -97,20 +95,6 @@ typedef struct _CContainer_t
ContainerItem_t item; ContainerItem_t item;
}CContainer_t; }CContainer_t;
typedef struct _CHitBox_t
{
Vector2 offset;
Vector2 size;
bool strong;
}CHitBox_t;
typedef struct _CHurtbox_t
{
Vector2 offset;
Vector2 size;
bool fragile;
}CHurtbox_t;
static inline void set_bbox(CBBox_t* p_bbox, unsigned int x, unsigned int y) static inline void set_bbox(CBBox_t* p_bbox, unsigned int x, unsigned int y)
{ {
p_bbox->size.x = x; p_bbox->size.x = x;

View File

@ -31,10 +31,9 @@ void update_entity_manager(EntityManager_t *p_manager)
sc_map_foreach (&p_entity->components, comp_type_idx, comp_idx) sc_map_foreach (&p_entity->components, comp_type_idx, comp_idx)
{ {
free_component_to_mempool((ComponentEnum_t)comp_type_idx, comp_idx); free_component_to_mempool((ComponentEnum_t)comp_type_idx, comp_idx);
sc_map_del_64v(&p_manager->component_map[comp_type_idx], e_idx); sc_map_del_64v(&p_manager->component_map[comp_type_idx], comp_idx);
sc_map_del_64v(&p_manager->entities_map[p_entity->m_tag], e_idx); sc_map_del_64v(&p_manager->entities_map[p_entity->m_tag], e_idx);
} }
sc_map_clear_64(&p_entity->components);
free_entity_to_mempool(e_idx); free_entity_to_mempool(e_idx);
sc_map_del_64v(&p_manager->entities, e_idx); sc_map_del_64v(&p_manager->entities, e_idx);
} }
@ -94,12 +93,9 @@ void remove_entity(EntityManager_t *p_manager, unsigned long id)
if(!sc_map_found(&p_manager->entities)) return; if(!sc_map_found(&p_manager->entities)) return;
// This only marks the entity for deletion // This only marks the entity for deletion
// Does not free entity. This is done during the update // Does not free entity. This is done during the update
if (p_entity->m_alive)
{
p_entity->m_alive = false; p_entity->m_alive = false;
sc_queue_add_last(&p_manager->to_remove, id); sc_queue_add_last(&p_manager->to_remove, id);
} }
}
Entity_t *get_entity(EntityManager_t *p_manager, unsigned long id) Entity_t *get_entity(EntityManager_t *p_manager, unsigned long id)
{ {

View File

@ -7,9 +7,9 @@
typedef struct EntityManager typedef struct EntityManager
{ {
// All fields are Read-Only // All fields are Read-Only
struct sc_map_64v entities; // ent id : entity struct sc_map_64v entities; // id : entity
struct sc_map_64v entities_map[N_TAGS]; // [{ent id: ent}] struct sc_map_64v entities_map[N_TAGS]; // [{id: ent}]
struct sc_map_64v component_map[N_COMPONENTS]; // [{ent id: comp}, ...] struct sc_map_64v component_map[N_COMPONENTS]; // [{id: comp}, ...]
struct sc_queue_uint to_add; struct sc_queue_uint to_add;
struct sc_queue_uint to_remove; struct sc_queue_uint to_remove;
}EntityManager_t; }EntityManager_t;

View File

@ -1,7 +1,6 @@
#include "mempool.h" #include "mempool.h"
#include "sc/queue/sc_queue.h" #include "sc/queue/sc_queue.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
// Static allocate buffers // Static allocate buffers
static Entity_t entity_buffer[MAX_COMP_POOL_SIZE]; static Entity_t entity_buffer[MAX_COMP_POOL_SIZE];
@ -12,8 +11,6 @@ static CMovementState_t cmstate_buffer[MAX_COMP_POOL_SIZE];
static CJump_t cjump_buffer[1]; // Only player is expected to have this 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 static CPlayerState_t cplayerstate_buffer[1]; // Only player is expected to have this
static CContainer_t ccontainer_buffer[MAX_COMP_POOL_SIZE]; static CContainer_t ccontainer_buffer[MAX_COMP_POOL_SIZE];
static CHitBox_t chitbox_buffer[MAX_COMP_POOL_SIZE];
static CHurtbox_t churtbox_buffer[MAX_COMP_POOL_SIZE];
// Use hashmap as a Set // Use hashmap as a Set
// Use list will be used to check if an object exist // Use list will be used to check if an object exist
@ -40,8 +37,6 @@ static MemPool_t comp_mempools[N_COMPONENTS] =
{cjump_buffer, 1, sizeof(CJump_t), NULL, {0}}, {cjump_buffer, 1, sizeof(CJump_t), NULL, {0}},
{cplayerstate_buffer, 1, sizeof(CPlayerState_t), NULL, {0}}, {cplayerstate_buffer, 1, sizeof(CPlayerState_t), NULL, {0}},
{ccontainer_buffer, MAX_COMP_POOL_SIZE, sizeof(CContainer_t), NULL, {0}}, {ccontainer_buffer, MAX_COMP_POOL_SIZE, sizeof(CContainer_t), NULL, {0}},
{chitbox_buffer, MAX_COMP_POOL_SIZE, sizeof(CHitBox_t), NULL, {0}},
{churtbox_buffer, MAX_COMP_POOL_SIZE, sizeof(CHurtbox_t), NULL, {0}},
}; };
static MemPool_t ent_mempool = {entity_buffer, MAX_COMP_POOL_SIZE, sizeof(Entity_t), NULL, {0}}; static MemPool_t ent_mempool = {entity_buffer, MAX_COMP_POOL_SIZE, sizeof(Entity_t), NULL, {0}};
@ -155,15 +150,6 @@ void free_component_to_mempool(ComponentEnum_t comp_type, unsigned long idx)
if (comp_mempools[comp_type].use_list[idx]) if (comp_mempools[comp_type].use_list[idx])
{ {
comp_mempools[comp_type].use_list[idx] = false; comp_mempools[comp_type].use_list[idx] = false;
sc_queue_add_last(&comp_mempools[comp_type].free_list, idx); sc_queue_add_first(&comp_mempools[comp_type].free_list, idx);
}
}
void print_mempool_stats(char* buffer)
{
int written = 0;
for (size_t i=0; i<N_COMPONENTS; ++i)
{
written += sprintf(buffer+written, "%lu: %lu/%lu\n", i, sc_queue_size(&comp_mempools[i].free_list), comp_mempools[i].max_size);
} }
} }

View File

@ -13,6 +13,4 @@ void free_entity_to_mempool(unsigned long idx);
void* new_component_from_mempool(ComponentEnum_t comp_type, unsigned long *idx); void* new_component_from_mempool(ComponentEnum_t comp_type, unsigned long *idx);
void* get_component_wtih_id(ComponentEnum_t comp_type, unsigned long idx); void* get_component_wtih_id(ComponentEnum_t comp_type, unsigned long idx);
void free_component_to_mempool(ComponentEnum_t comp_type, unsigned long idx); void free_component_to_mempool(ComponentEnum_t comp_type, unsigned long idx);
void print_mempool_stats(char* buffer);
#endif //__MEMPOOL_H #endif //__MEMPOOL_H

View File

@ -12,9 +12,8 @@ enum EntitySpawnSelection
{ {
TOGGLE_TILE = 0, TOGGLE_TILE = 0,
SPAWN_CRATE, SPAWN_CRATE,
SPAWN_METAL_CRATE,
}; };
#define MAX_SPAWN_TYPE 3 #define MAX_SPAWN_TYPE 2
static unsigned int current_spawn_selection = 0; static unsigned int current_spawn_selection = 0;
static inline unsigned int get_tile_idx(int x, int y, unsigned int tilemap_width) static inline unsigned int get_tile_idx(int x, int y, unsigned int tilemap_width)
@ -49,7 +48,6 @@ static void level_scene_render_func(Scene_t* scene)
} }
} }
char buffer[64] = {0};
sc_map_foreach_value(&scene->ent_manager.entities, p_ent) sc_map_foreach_value(&scene->ent_manager.entities, p_ent)
{ {
CTransform_t* p_ct = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T); CTransform_t* p_ct = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T);
@ -61,38 +59,17 @@ static void level_scene_render_func(Scene_t* scene)
colour = RED; colour = RED;
break; break;
case CRATES_ENT_TAG: case CRATES_ENT_TAG:
colour = p_bbox->fragile? BROWN : GRAY; colour = BROWN;
break; break;
default: default:
colour = BLACK; colour = BLACK;
} }
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(&scene->ent_manager, p_ent, CHURTBOX_T);
CHitBox_t* p_hitbox = get_component(&scene->ent_manager, p_ent, CHITBOX_T);
if (p_hitbox != NULL)
{
Rectangle rec = {
.x = p_ct->position.x + p_hitbox->offset.x,
.y = p_ct->position.y + p_hitbox->offset.y,
.width = p_hitbox->size.x,
.height = p_hitbox->size.y,
};
DrawRectangleLinesEx(rec, 1.5, ORANGE);
}
if (p_hurtbox != NULL)
{
Rectangle rec = {
.x = p_ct->position.x + p_hurtbox->offset.x,
.y = p_ct->position.y + p_hurtbox->offset.y,
.width = p_hurtbox->size.x,
.height = p_hurtbox->size.y,
};
DrawRectangleLinesEx(rec, 1.5, PURPLE);
}
} }
for (size_t i=0; i<tilemap.n_tiles;++i) for (size_t i=0; i<tilemap.n_tiles;++i)
{ {
char buffer[6] = {0};
int x = (i % tilemap.width) * TILE_SIZE; int x = (i % tilemap.width) * TILE_SIZE;
int y = (i / tilemap.width) * TILE_SIZE; int y = (i / tilemap.width) * TILE_SIZE;
sprintf(buffer, "%u", sc_map_size_64(&tilemap.tiles[i].entities_set)); sprintf(buffer, "%u", sc_map_size_64(&tilemap.tiles[i].entities_set));
@ -121,6 +98,7 @@ static void level_scene_render_func(Scene_t* scene)
} }
// For DEBUG // For DEBUG
char buffer[64];
sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_ent) sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_ent)
{ {
CTransform_t* p_ct = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T); CTransform_t* p_ct = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T);
@ -142,28 +120,21 @@ static void level_scene_render_func(Scene_t* scene)
DrawText(buffer, tilemap.width * TILE_SIZE + 1, 270, 12, BLACK); DrawText(buffer, tilemap.width * TILE_SIZE + 1, 270, 12, BLACK);
sprintf(buffer, "FPS: %u", GetFPS()); sprintf(buffer, "FPS: %u", GetFPS());
DrawText(buffer, tilemap.width * TILE_SIZE + 1, 320, 12, BLACK); DrawText(buffer, tilemap.width * TILE_SIZE + 1, 320, 12, BLACK);
static char mempool_stats[512];
print_mempool_stats(mempool_stats);
DrawText(mempool_stats, tilemap.width * TILE_SIZE + 1, 350, 12, BLACK);
} }
static void spawn_crate(Scene_t *scene, unsigned int tile_idx, bool metal) static void spawn_crate(Scene_t *scene, unsigned int tile_idx)
{ {
LevelSceneData_t *data = (LevelSceneData_t *)scene->scene_data; LevelSceneData_t *data = (LevelSceneData_t *)scene->scene_data;
Entity_t *p_crate = add_entity(&scene->ent_manager, CRATES_ENT_TAG); Entity_t *p_crate = add_entity(&scene->ent_manager, CRATES_ENT_TAG);
CBBox_t *p_bbox = add_component(&scene->ent_manager, p_crate, CBBOX_COMP_T); CBBox_t *p_bbox = add_component(&scene->ent_manager, p_crate, CBBOX_COMP_T);
set_bbox(p_bbox, TILE_SIZE, TILE_SIZE); set_bbox(p_bbox, TILE_SIZE, TILE_SIZE);
p_bbox->solid = true; p_bbox->solid = true;
p_bbox->fragile = !metal; p_bbox->fragile = true;
CTransform_t *p_ctransform = add_component(&scene->ent_manager, p_crate, CTRANSFORM_COMP_T); CTransform_t *p_ctransform = add_component(&scene->ent_manager, p_crate, CTRANSFORM_COMP_T);
p_ctransform->position.x = (tile_idx % data->tilemap.width) * TILE_SIZE; p_ctransform->position.x = (tile_idx % data->tilemap.width) * TILE_SIZE;
p_ctransform->position.y = (tile_idx / data->tilemap.width) * TILE_SIZE; p_ctransform->position.y = (tile_idx / data->tilemap.width) * TILE_SIZE;
add_component(&scene->ent_manager, p_crate, CMOVEMENTSTATE_T); add_component(&scene->ent_manager, p_crate, CMOVEMENTSTATE_T);
add_component(&scene->ent_manager, p_crate, CTILECOORD_COMP_T); add_component(&scene->ent_manager, p_crate, CTILECOORD_COMP_T);
CHurtbox_t* p_hurtbox = add_component(&scene->ent_manager, p_crate, CHURTBOX_T);
p_hurtbox->size = p_bbox->size;
p_hurtbox->fragile = !metal;
} }
static void spawn_player(Scene_t *scene) static void spawn_player(Scene_t *scene)
@ -171,7 +142,7 @@ static void spawn_player(Scene_t *scene)
Entity_t *p_ent = add_entity(&scene->ent_manager, PLAYER_ENT_TAG); Entity_t *p_ent = add_entity(&scene->ent_manager, PLAYER_ENT_TAG);
CBBox_t *p_bbox = add_component(&scene->ent_manager, p_ent, CBBOX_COMP_T); CBBox_t *p_bbox = add_component(&scene->ent_manager, p_ent, CBBOX_COMP_T);
set_bbox(p_bbox, PLAYER_WIDTH, PLAYER_HEIGHT); set_bbox(p_bbox, 30, 45);
add_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T); add_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T);
CJump_t *p_cjump = add_component(&scene->ent_manager, p_ent, CJUMP_COMP_T); CJump_t *p_cjump = add_component(&scene->ent_manager, p_ent, CJUMP_COMP_T);
p_cjump->jump_speed = 680; p_cjump->jump_speed = 680;
@ -181,9 +152,6 @@ static void spawn_player(Scene_t *scene)
add_component(&scene->ent_manager, p_ent, CPLAYERSTATE_T); add_component(&scene->ent_manager, p_ent, CPLAYERSTATE_T);
add_component(&scene->ent_manager, p_ent, CTILECOORD_COMP_T); add_component(&scene->ent_manager, p_ent, CTILECOORD_COMP_T);
add_component(&scene->ent_manager, p_ent, CMOVEMENTSTATE_T); add_component(&scene->ent_manager, p_ent, CMOVEMENTSTATE_T);
CHitBox_t* p_hitbox = add_component(&scene->ent_manager, p_ent, CHITBOX_T);
p_hitbox->size = Vector2Add(p_bbox->size, (Vector2){2,2});
p_hitbox->offset = (Vector2){-1, -1};
} }
static void toggle_block_system(Scene_t *scene) static void toggle_block_system(Scene_t *scene)
@ -206,10 +174,7 @@ static void toggle_block_system(Scene_t *scene)
tilemap.tiles[tile_idx].water_level = 0; tilemap.tiles[tile_idx].water_level = 0;
break; break;
case SPAWN_CRATE: case SPAWN_CRATE:
spawn_crate(scene, tile_idx, false); spawn_crate(scene, tile_idx);
break;
case SPAWN_METAL_CRATE:
spawn_crate(scene, tile_idx, true);
break; break;
} }
last_tile_idx = tile_idx; last_tile_idx = tile_idx;
@ -265,17 +230,14 @@ void level_do_action(Scene_t *scene, ActionType_t action, bool pressed)
if (!pressed) if (!pressed)
{ {
current_spawn_selection++; current_spawn_selection++;
current_spawn_selection %= MAX_SPAWN_TYPE; current_spawn_selection &= 1;
} }
break; break;
case ACTION_PREV_SPAWN: case ACTION_PREV_SPAWN:
if (!pressed) if (!pressed)
{ {
if (current_spawn_selection == 0)
{
current_spawn_selection = MAX_SPAWN_TYPE;
}
current_spawn_selection--; current_spawn_selection--;
current_spawn_selection &= 1;
} }
break; break;
case ACTION_EXIT: case ACTION_EXIT:
@ -303,8 +265,6 @@ void init_level_scene(LevelScene_t *scene)
sc_array_add(&scene->scene.systems, &movement_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, &tile_collision_system); sc_array_add(&scene->scene.systems, &tile_collision_system);
sc_array_add(&scene->scene.systems, &hitbox_update_system);
//sc_array_add(&scene->scene.systems, &update_tilemap_system);
sc_array_add(&scene->scene.systems, &state_transition_update_system); sc_array_add(&scene->scene.systems, &state_transition_update_system);
sc_array_add(&scene->scene.systems, &player_ground_air_transition_system); sc_array_add(&scene->scene.systems, &player_ground_air_transition_system);
sc_array_add(&scene->scene.systems, &toggle_block_system); sc_array_add(&scene->scene.systems, &toggle_block_system);

View File

@ -1,7 +1,6 @@
#include "game_systems.h" #include "game_systems.h"
#include "AABB.h" #include "AABB.h"
#include "constants.h" #include "constants.h"
#include <stdio.h>
static const Vector2 TILE_SZ = {TILE_SIZE, TILE_SIZE}; static const Vector2 TILE_SZ = {TILE_SIZE, TILE_SIZE};
static const Vector2 GRAVITY = {0, GRAV_ACCEL}; static const Vector2 GRAVITY = {0, GRAV_ACCEL};
@ -34,7 +33,7 @@ typedef struct TileArea
unsigned int tile_y2; unsigned int tile_y2;
}TileArea_t; }TileArea_t;
static bool check_collision(unsigned int self_idx, Vector2 pos, Vector2 bbox_sz, TileArea_t area_to_check, TileGrid_t* grid, EntityManager_t* p_manager) static bool check_collision(Vector2 pos, Vector2 bbox_sz, TileArea_t area_to_check, TileGrid_t* grid, EntityManager_t* p_manager)
{ {
for(unsigned int tile_y = area_to_check.tile_y1; tile_y <= area_to_check.tile_y2; tile_y++) for(unsigned int tile_y = area_to_check.tile_y1; tile_y <= area_to_check.tile_y2; tile_y++)
{ {
@ -45,20 +44,12 @@ static bool check_collision(unsigned int self_idx, Vector2 pos, Vector2 bbox_sz,
unsigned int ent_idx; unsigned int ent_idx;
sc_map_foreach_value(&grid->tiles[tile_idx].entities_set, ent_idx) sc_map_foreach_value(&grid->tiles[tile_idx].entities_set, ent_idx)
{ {
if (self_idx == ent_idx) continue;
Entity_t * p_ent = get_entity(p_manager, ent_idx); Entity_t * p_ent = get_entity(p_manager, ent_idx);
CTransform_t *p_ctransform = get_component(p_manager, p_ent, CTRANSFORM_COMP_T); CTransform_t *p_ctransform = get_component(p_manager, p_ent, CTRANSFORM_COMP_T);
CBBox_t *p_bbox = get_component(p_manager, p_ent, CBBOX_COMP_T); CBBox_t *p_bbox = get_component(p_manager, p_ent, CBBOX_COMP_T);
Vector2 overlap; Vector2 overlap;
if (p_bbox == NULL || p_ctransform == NULL) continue; if (p_bbox == NULL || p_ctransform == NULL) continue;
//if (p_bbox->solid && !p_bbox->fragile) if (p_bbox->solid && !p_bbox->fragile && find_AABB_overlap(pos, bbox_sz, p_ctransform->position, p_bbox->size, &overlap)) return true;
if (p_bbox->solid)
{
if (find_AABB_overlap(pos, bbox_sz, p_ctransform->position, p_bbox->size, &overlap))
{
return true;
}
}
} }
} }
} }
@ -66,7 +57,7 @@ static bool check_collision(unsigned int self_idx, Vector2 pos, Vector2 bbox_sz,
} }
static bool check_collision_at(unsigned int ent_idx, Vector2 pos, Vector2 bbox_sz, TileGrid_t* grid, Vector2 point, EntityManager_t* p_manager) static bool check_collision_at(Vector2 pos, Vector2 bbox_sz, TileGrid_t* grid, Vector2 point, EntityManager_t* p_manager)
{ {
Vector2 new_pos = Vector2Add(pos, point); Vector2 new_pos = Vector2Add(pos, point);
TileArea_t area = { TileArea_t area = {
@ -76,7 +67,7 @@ static bool check_collision_at(unsigned int ent_idx, Vector2 pos, Vector2 bbox_s
.tile_y2 = (new_pos.y + bbox_sz.y - 1) / TILE_SIZE .tile_y2 = (new_pos.y + bbox_sz.y - 1) / TILE_SIZE
}; };
return check_collision(ent_idx, new_pos, bbox_sz, area, grid, p_manager); return check_collision(new_pos, bbox_sz, area, grid, p_manager);
} }
static inline bool check_on_ground(unsigned int ent_idx, Vector2 pos, Vector2 bbox_sz, TileGrid_t* grid, EntityManager_t* p_manager) static inline bool check_on_ground(unsigned int ent_idx, Vector2 pos, Vector2 bbox_sz, TileGrid_t* grid, EntityManager_t* p_manager)
@ -88,7 +79,7 @@ static inline bool check_on_ground(unsigned int ent_idx, Vector2 pos, Vector2 bb
.tile_x2 = (pos.x + bbox_sz.x - 1) / TILE_SIZE, .tile_x2 = (pos.x + bbox_sz.x - 1) / TILE_SIZE,
.tile_y2 = (pos.y + bbox_sz.y - 1) / TILE_SIZE, .tile_y2 = (pos.y + bbox_sz.y - 1) / TILE_SIZE,
}; };
return check_collision(ent_idx, pos, bbox_sz, area, grid, p_manager); return check_collision(pos, bbox_sz, area, grid, p_manager);
} }
static Vector2 shift_bbox(Vector2 bbox, Vector2 new_bbox, AnchorPoint_t anchor) static Vector2 shift_bbox(Vector2 bbox, Vector2 new_bbox, AnchorPoint_t anchor)
{ {
@ -291,7 +282,7 @@ void player_bbox_update_system(Scene_t *scene)
} }
} }
if (!check_collision_at(p_player->m_id, p_ctransform->position, new_bbox, &tilemap, offset, &scene->ent_manager)) if (!check_collision_at(p_ctransform->position, new_bbox, &tilemap, offset, &scene->ent_manager))
{ {
set_bbox(p_bbox, new_bbox.x, new_bbox.y); set_bbox(p_bbox, new_bbox.x, new_bbox.y);
p_ctransform->position = Vector2Add(p_ctransform->position, offset); p_ctransform->position = Vector2Add(p_ctransform->position, offset);
@ -299,7 +290,7 @@ void player_bbox_update_system(Scene_t *scene)
} }
} }
static bool check_collision_and_move(unsigned int ent_idx, EntityManager_t* p_manager, TileGrid_t* tilemap, CTransform_t *const p_ct, Vector2 sz, Vector2 other_pos, Vector2 other_sz, bool other_solid, uint32_t* collision_value) static bool check_collision_and_move(EntityManager_t* p_manager, TileGrid_t* tilemap, CTransform_t *const p_ct, Vector2 sz, Vector2 other_pos, Vector2 other_sz, bool other_solid, uint32_t* collision_value)
{ {
Vector2 overlap = {0,0}; Vector2 overlap = {0,0};
Vector2 prev_overlap = {0,0}; Vector2 prev_overlap = {0,0};
@ -338,7 +329,7 @@ static bool check_collision_and_move(unsigned int ent_idx, EntityManager_t* p_ma
// Resolve collision via moving player by the overlap amount only if other is solid // Resolve collision via moving player by the overlap amount only if other is solid
// also check for empty to prevent creating new collision. Not fool-proof, but good enough // also check for empty to prevent creating new collision. Not fool-proof, but good enough
if (other_solid && !check_collision_at(ent_idx, p_ct->position, sz, tilemap, offset, p_manager)) if (other_solid && !check_collision_at(p_ct->position, sz, tilemap, offset, p_manager))
{ {
p_ct->position = Vector2Add(p_ct->position, offset); p_ct->position = Vector2Add(p_ct->position, offset);
if (dir_to_move == 0) if (dir_to_move == 0)
@ -391,7 +382,7 @@ void tile_collision_system(Scene_t *scene)
other.x = (tile_idx % tilemap.width) * TILE_SIZE; other.x = (tile_idx % tilemap.width) * TILE_SIZE;
other.y = (tile_idx / tilemap.width) * TILE_SIZE; // Precision loss is intentional other.y = (tile_idx / tilemap.width) * TILE_SIZE; // Precision loss is intentional
check_collision_and_move(ent_idx, &scene->ent_manager, &tilemap, p_ctransform, p_bbox->size, other, TILE_SZ, true, &collision_value); check_collision_and_move(&scene->ent_manager, &tilemap, p_ctransform, p_bbox->size, other, TILE_SZ, true, &collision_value);
} }
else else
@ -405,13 +396,12 @@ void tile_collision_system(Scene_t *scene)
checked_entities[other_ent_idx] = true; checked_entities[other_ent_idx] = true;
Entity_t *p_other_ent = get_entity(&scene->ent_manager, other_ent_idx); Entity_t *p_other_ent = get_entity(&scene->ent_manager, other_ent_idx);
if (!p_other_ent->m_alive) continue; // To only allow one way collision check
if (p_other_ent->m_tag < p_ent->m_tag) continue; // To only allow one way collision check if (p_other_ent->m_tag < p_ent->m_tag) continue; // To only allow one way collision check
CBBox_t *p_other_bbox = get_component(&scene->ent_manager, p_other_ent, CBBOX_COMP_T); CBBox_t *p_other_bbox = get_component(&scene->ent_manager, p_other_ent, CBBOX_COMP_T);
if (p_other_bbox == NULL) continue; if (p_other_bbox == NULL) continue;
CTransform_t *p_other_ct = get_component(&scene->ent_manager, p_other_ent, CTRANSFORM_COMP_T); CTransform_t *p_other_ct = get_component(&scene->ent_manager, p_other_ent, CTRANSFORM_COMP_T);
if (check_collision_and_move(ent_idx, &scene->ent_manager, &tilemap, p_ctransform, p_bbox->size, p_other_ct->position, p_other_bbox->size, p_other_bbox->solid, &collision_value)) if (check_collision_and_move(&scene->ent_manager, &tilemap, p_ctransform, p_bbox->size, p_other_ct->position, p_other_bbox->size, p_other_bbox->solid, &collision_value))
{ {
uint32_t collision_key = ((ent_idx << 16) | other_ent_idx); uint32_t collision_key = ((ent_idx << 16) | other_ent_idx);
sc_map_put_32(&data->collision_events, collision_key, collision_value); sc_map_put_32(&data->collision_events, collision_key, collision_value);
@ -422,15 +412,49 @@ void tile_collision_system(Scene_t *scene)
} }
// TODO: Resolve all collision events // TODO: Resolve all collision events
//uint32_t collision_key; uint32_t collision_key;
//sc_map_foreach(&data->collision_events, collision_key, collision_value) sc_map_foreach(&data->collision_events, collision_key, collision_value)
//{ {
// ent_idx = (collision_key >> 16); ent_idx = (collision_key >> 16);
// uint other_ent_idx = (collision_key & 0xFFFF); uint other_ent_idx = (collision_key & 0xFFFF);
// Entity_t *p_ent = get_entity(&scene->ent_manager, ent_idx); Entity_t *p_ent = get_entity(&scene->ent_manager, ent_idx);
// Entity_t *p_other_ent = get_entity(&scene->ent_manager, other_ent_idx); Entity_t *p_other_ent = get_entity(&scene->ent_manager, other_ent_idx);
// if (!p_ent->m_alive || !p_other_ent->m_alive) continue; if (!p_ent->m_alive || !p_other_ent->m_alive) continue;
//} if (p_ent->m_tag == PLAYER_ENT_TAG && p_other_ent->m_tag == CRATES_ENT_TAG)
{
CTransform_t *p_ctransform = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T);
CBBox_t * p_bbox = get_component(&scene->ent_manager, p_ent, CBBOX_COMP_T);
CPlayerState_t * p_pstate = get_component(&scene->ent_manager, p_ent, CPLAYERSTATE_T);
CTransform_t *p_other_ct = get_component(&scene->ent_manager, p_other_ent, CTRANSFORM_COMP_T);
CBBox_t *p_other_bbox = get_component(&scene->ent_manager, p_other_ent, CBBOX_COMP_T);
if (
// TODO: Check Material of the crates
p_ctransform->position.y + p_bbox->size.y <= p_other_ct->position.y
)
{
p_ctransform->velocity.y = -400;
if (p_pstate->jump_pressed)
{
p_ctransform->velocity.y = -600;
CJump_t * p_cjump = get_component(&scene->ent_manager, p_ent, CJUMP_COMP_T);
p_cjump->short_hop = false;
p_cjump->jumped = true;
}
}
if (p_other_bbox->fragile)
{
CTileCoord_t *p_tilecoord = get_component(&scene->ent_manager, p_other_ent, CTILECOORD_COMP_T);
for (size_t i=0;i<p_tilecoord->n_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), other_ent_idx);
}
remove_entity(&scene->ent_manager, other_ent_idx);
}
}
}
sc_map_clear_32(&data->collision_events); sc_map_clear_32(&data->collision_events);
// Level boundary collision // Level boundary collision
@ -534,12 +558,12 @@ void global_external_forces_system(Scene_t *scene)
.tile_x2 = (p_ctransform->position.x - 1) / TILE_SIZE, .tile_x2 = (p_ctransform->position.x - 1) / TILE_SIZE,
.tile_y2 = (p_ctransform->position.y + p_bbox->size.y - 1) / TILE_SIZE, .tile_y2 = (p_ctransform->position.y + p_bbox->size.y - 1) / TILE_SIZE,
}; };
if (check_collision(ent_idx, new_pos, p_bbox->size, area, &data->tilemap, &scene->ent_manager) && p_ctransform->accel.x < 0) p_ctransform->accel.x = 0; if (check_collision(new_pos, p_bbox->size, area, &data->tilemap, &scene->ent_manager) && p_ctransform->accel.x < 0) p_ctransform->accel.x = 0;
new_pos.x += 2; // 2 to account for the previous subtraction new_pos.x += 2; // 2 to account for the previous subtraction
area.tile_x1 = (p_ctransform->position.x + p_bbox->size.x + 1) / TILE_SIZE; area.tile_x1 = (p_ctransform->position.x + p_bbox->size.x + 1) / TILE_SIZE;
area.tile_x2 = area.tile_x1; area.tile_x2 = area.tile_x1;
if (check_collision(ent_idx, new_pos, p_bbox->size, area, &data->tilemap, &scene->ent_manager) && p_ctransform->accel.x > 0) p_ctransform->accel.x = 0; if (check_collision(new_pos, p_bbox->size, area, &data->tilemap, &scene->ent_manager) && p_ctransform->accel.x > 0) p_ctransform->accel.x = 0;
new_pos.x -= 2; new_pos.x -= 2;
new_pos.y--; new_pos.y--;
@ -547,12 +571,12 @@ void global_external_forces_system(Scene_t *scene)
area.tile_x2 = (p_ctransform->position.x - 1) / TILE_SIZE, area.tile_x2 = (p_ctransform->position.x - 1) / TILE_SIZE,
area.tile_y1 = (p_ctransform->position.y - 1) / TILE_SIZE, area.tile_y1 = (p_ctransform->position.y - 1) / TILE_SIZE,
area.tile_y1 = area.tile_y2; area.tile_y1 = area.tile_y2;
if (check_collision(ent_idx, new_pos, p_bbox->size, area, &data->tilemap, &scene->ent_manager) && p_ctransform->accel.y < 0) p_ctransform->accel.y = 0; if (check_collision(new_pos, p_bbox->size, area, &data->tilemap, &scene->ent_manager) && p_ctransform->accel.y < 0) p_ctransform->accel.y = 0;
new_pos.y += 2; new_pos.y += 2;
area.tile_y1 = (p_ctransform->position.y + p_bbox->size.y + 1) / TILE_SIZE, area.tile_y1 = (p_ctransform->position.y + p_bbox->size.y + 1) / TILE_SIZE,
area.tile_y1 = area.tile_y2; area.tile_y1 = area.tile_y2;
if (check_collision(ent_idx, new_pos, p_bbox->size, area, &data->tilemap, &scene->ent_manager) && p_ctransform->accel.y > 0) p_ctransform->accel.y = 0; if (check_collision(new_pos, p_bbox->size, area, &data->tilemap, &scene->ent_manager) && p_ctransform->accel.y > 0) p_ctransform->accel.y = 0;
} }
} }
@ -708,90 +732,7 @@ void update_tilemap_system(Scene_t *scene)
{ {
unsigned int tile_idx = tile_y * tilemap.width + tile_x; unsigned int tile_idx = tile_y * tilemap.width + tile_x;
p_tilecoord->tiles[p_tilecoord->n_tiles++] = tile_idx; p_tilecoord->tiles[p_tilecoord->n_tiles++] = tile_idx;
sc_map_put_64(&(tilemap.tiles[tile_idx].entities_set), p_ent->m_id, p_ent->m_id); sc_map_put_64(&(tilemap.tiles[tile_idx].entities_set), p_ent->m_id, 0);
}
}
}
}
void hitbox_update_system(Scene_t *scene)
{
static bool checked_entities[MAX_COMP_POOL_SIZE] = {0};
LevelSceneData_t *data = (LevelSceneData_t *)scene->scene_data;
TileGrid_t tilemap = data->tilemap;
unsigned int ent_idx;
CHitBox_t* p_hitbox;
//sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_player)
sc_map_foreach(&scene->ent_manager.component_map[CHITBOX_T], ent_idx, p_hitbox)
{
Entity_t *p_ent = get_entity(&scene->ent_manager, ent_idx);
CTransform_t* p_ctransform = get_component(&scene->ent_manager, p_ent, CTRANSFORM_COMP_T);
Vector2 hitbox_pos = Vector2Add(p_ctransform->position, p_hitbox->offset);
unsigned int tile_x1 = (hitbox_pos.x) / TILE_SIZE;
unsigned int tile_y1 = (hitbox_pos.y) / TILE_SIZE;
unsigned int tile_x2 = (hitbox_pos.x + p_hitbox->size.x - 1) / TILE_SIZE;
unsigned int tile_y2 = (hitbox_pos.y + p_hitbox->size.y - 1) / 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;
unsigned int other_ent_idx;
memset(checked_entities, 0, sizeof(checked_entities));
sc_map_foreach_key(&tilemap.tiles[tile_idx].entities_set, other_ent_idx)
{
if (other_ent_idx == ent_idx) continue;
if (checked_entities[other_ent_idx]) continue;
Entity_t *p_other_ent = get_entity(&scene->ent_manager, other_ent_idx);
if (!p_other_ent->m_alive) continue; // To only allow one way collision check
if (p_other_ent->m_tag < p_ent->m_tag) continue; // To only allow one way collision check
CHurtbox_t *p_other_hurtbox = get_component(&scene->ent_manager, p_other_ent, CHURTBOX_T);
if (p_other_hurtbox == NULL) continue;
CTransform_t *p_other_ct = get_component(&scene->ent_manager, p_other_ent, CTRANSFORM_COMP_T);
Vector2 hurtbox_pos = Vector2Add(p_other_ct->position, p_other_hurtbox->offset);
Vector2 overlap;
if (find_AABB_overlap(hitbox_pos, p_hitbox->size, hurtbox_pos, p_other_hurtbox->size, &overlap))
{
if (!p_other_hurtbox->fragile) continue;
if (p_other_ent->m_tag == CRATES_ENT_TAG)
{
CBBox_t * p_bbox = get_component(&scene->ent_manager, p_ent, CBBOX_COMP_T);
CPlayerState_t * p_pstate = get_component(&scene->ent_manager, p_ent, CPLAYERSTATE_T);
if (
// TODO: Check Material of the crates
p_ctransform->position.y + p_bbox->size.y <= p_other_ct->position.y
)
{
p_ctransform->velocity.y = -400;
if (p_pstate->jump_pressed)
{
p_ctransform->velocity.y = -600;
CJump_t * p_cjump = get_component(&scene->ent_manager, p_ent, CJUMP_COMP_T);
p_cjump->short_hop = false;
p_cjump->jumped = true;
}
}
CTileCoord_t *p_tilecoord = get_component(&scene->ent_manager, p_other_ent, CTILECOORD_COMP_T);
for (size_t i=0;i<p_tilecoord->n_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), other_ent_idx);
}
remove_entity(&scene->ent_manager, other_ent_idx);
}
}
}
} }
} }
} }

View File

@ -12,5 +12,4 @@ void movement_update_system(Scene_t* scene);
void player_ground_air_transition_system(Scene_t* scene); void player_ground_air_transition_system(Scene_t* scene);
void state_transition_update_system(Scene_t *scene); void state_transition_update_system(Scene_t *scene);
void update_tilemap_system(Scene_t *scene); void update_tilemap_system(Scene_t *scene);
void hitbox_update_system(Scene_t *scene);
#endif // __GAME_SYSTEMS_H #endif // __GAME_SYSTEMS_H