Compare commits

...

2 Commits

Author SHA1 Message Date
En Yi ebffd48958 Ready a water filler/runner entity
Changelog:
- Add new component: water runner component
    - specific for such an entity. Not general, but i dont really care
- Implement adding and freeing the water filler entity
    - Component involves dynamic memory allocation, so need custom
      function to add and free
    - May look into custom allocation for this part in the future.
2023-07-18 21:49:43 +08:00
En Yi aae61edda9 Refactor out broad phase grid collision functions 2023-07-17 21:14:02 +08:00
14 changed files with 405 additions and 298 deletions

View File

@ -3,6 +3,7 @@ add_library(lib_scenes STATIC
assets_loader.c
player_ent.c
items_ent.c
water_flow.c
editor_scene.c
menu_scene.c
game_systems.c

View File

@ -683,6 +683,7 @@ void init_level_scene(LevelScene_t* scene)
scene->data.tilemap.width = DEFAULT_MAP_WIDTH;
scene->data.tilemap.height = DEFAULT_MAP_HEIGHT;
scene->data.tilemap.tile_size = TILE_SIZE;
scene->data.tilemap.n_tiles = scene->data.tilemap.width * scene->data.tilemap.height;
assert(scene->data.tilemap.n_tiles <= MAX_N_TILES);
scene->data.tilemap.tiles = all_tiles;

View File

@ -4,6 +4,7 @@ add_library(lib_engine STATIC
AABB.c
gui.c
engine.c
collisions.c
)
target_include_directories(lib_engine
PUBLIC

View File

@ -7,7 +7,7 @@
#include "sc/queue/sc_queue.h"
#define N_TAGS 8
#define N_COMPONENTS 12
#define N_COMPONENTS 13
#define MAX_COMP_POOL_SIZE 1024
typedef struct EntityManager EntityManager_t;
typedef struct Entity Entity_t;
@ -25,6 +25,7 @@ typedef enum ComponentEnum {
CSPRITE_T,
CMOVEABLE_T,
CLIFETIMER_T,
CWATERRUNNER_T,
} ComponentEnum_t;
typedef enum MovementMode {
@ -127,6 +128,24 @@ typedef struct _CLifeTimer_t {
uint8_t life_time;
} CLifeTimer_t;
typedef struct _BFSTile {
int32_t to;
int32_t from;
bool reachable;
}BFSTile_t;
typedef struct _BFSTileMap {
BFSTile_t* tilemap;
uint32_t width;
uint32_t height;
uint32_t len;
}BFSTileMap_t;
typedef struct _CWaterRunner {
int32_t current_tile;
BFSTileMap_t bfs_tilemap;
}CWaterRunner_t;
// Credits to bedroomcoders.co.uk for this
typedef struct Sprite {
Texture2D* texture;

View File

@ -19,6 +19,7 @@ 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];
static CLifeTimer_t clifetimer_buffer[MAX_COMP_POOL_SIZE];
static CWaterRunner_t cwaterrunner_buffer[4];
typedef struct ULongCircBuffer {
unsigned long* buffer; // data buffer
@ -90,6 +91,7 @@ static MemPool_t comp_mempools[N_COMPONENTS] = {
{csprite_buffer, MAX_COMP_POOL_SIZE, sizeof(CSprite_t), NULL, {0}},
{cmoveable_buffer, MAX_COMP_POOL_SIZE, sizeof(CMoveable_t), NULL, {0}},
{clifetimer_buffer, MAX_COMP_POOL_SIZE, sizeof(CLifeTimer_t), NULL, {0}},
{cwaterrunner_buffer, 4, sizeof(CWaterRunner_t), NULL, {0}},
};
static MemPool_t ent_mempool = {
.buffer = entity_buffer,

View File

@ -0,0 +1,252 @@
#include "collisions.h"
#include "AABB.h"
void remove_entity_from_tilemap(EntityManager_t *p_manager, TileGrid_t* tilemap, Entity_t* p_ent)
{
CTileCoord_t* p_tilecoord = get_component(p_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_64v(&(tilemap->tiles[tile_idx].entities_set), p_ent->m_id);
}
remove_entity(p_manager, p_ent->m_id);
}
uint8_t check_collision(const CollideEntity_t* ent, TileGrid_t* grid, bool check_oneway)
{
unsigned int tile_x1 = (ent->area.tile_x1 < 0) ? 0 : ent->area.tile_x1;
unsigned int tile_x2 = (ent->area.tile_x2 >= grid->width) ? grid->width - 1 : ent->area.tile_x2;
unsigned int tile_y1 = (ent->area.tile_y1 < 0) ? 0 : ent->area.tile_y1;
unsigned int tile_y2 = (ent->area.tile_y2 >= grid->height) ? grid->height - 1 : ent->area.tile_y2;
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++)
{
if (tile_x >= grid->width) return 0;
unsigned int tile_idx = tile_y*grid->width + tile_x;
Vector2 overlap;
if (grid->tiles[tile_idx].solid == SOLID)
{
if (find_AABB_overlap(
(Vector2){ent->bbox.x, ent->bbox.y},
(Vector2){ent->bbox.width, ent->bbox.height},
(Vector2){tile_x * grid->tile_size + grid->tiles[tile_idx].offset.x, tile_y * grid->tile_size + grid->tiles[tile_idx].offset.y},
grid->tiles[tile_idx].size,
&overlap
))
{
return 1;
}
}
if (check_oneway && grid->tiles[tile_idx].solid == ONE_WAY)
{
find_AABB_overlap(
(Vector2){ent->bbox.x, ent->bbox.y},
(Vector2){ent->bbox.width, ent->bbox.height},
(Vector2){tile_x * grid->tile_size + grid->tiles[tile_idx].offset.x, tile_y * grid->tile_size + grid->tiles[tile_idx].offset.y},
grid->tiles[tile_idx].size,
&overlap
);
//For one-way platform, check for vectical collision, only return true for up direction
if (overlap.y != 0 && ent->prev_bbox.y + ent->prev_bbox.height - 1 < tile_y * grid->tile_size) return 1;
}
Entity_t* p_other_ent;
sc_map_foreach_value(&grid->tiles[tile_idx].entities_set, p_other_ent)
{
if (ent->p_ent->m_id == p_other_ent->m_id) continue;
if (!ent->p_ent->m_alive) continue;
CTransform_t *p_ctransform = get_component(p_other_ent, CTRANSFORM_COMP_T);
CBBox_t *p_bbox = get_component(p_other_ent, CBBOX_COMP_T);
if (p_bbox == NULL || p_ctransform == NULL) continue;
//if (p_bbox->solid && !p_bbox->fragile)
if (p_bbox->solid)
{
if (
find_AABB_overlap(
(Vector2){ent->bbox.x, ent->bbox.y},
(Vector2){ent->bbox.width, ent->bbox.height},
p_ctransform->position, p_bbox->size, &overlap
)
)
{
return (p_bbox->fragile) ? 2 : 1;
}
}
}
}
}
return 0;
}
uint8_t check_collision_line(const CollideEntity_t* ent, TileGrid_t* grid, bool check_oneway)
{
unsigned int tile_x1 = (ent->area.tile_x1 < 0) ? 0 : ent->area.tile_x1;
unsigned int tile_x2 = (ent->area.tile_x2 >= grid->width) ? grid->width - 1 : ent->area.tile_x2;
unsigned int tile_y1 = (ent->area.tile_y1 < 0) ? 0 : ent->area.tile_y1;
unsigned int tile_y2 = (ent->area.tile_y2 >= grid->height) ? grid->height - 1 : ent->area.tile_y2;
Vector2 p1 = {ent->bbox.x, ent->bbox.y};
Vector2 p2 = {ent->bbox.x + ent->bbox.width - 1, ent->bbox.y + ent->bbox.height - 1};
for(unsigned int tile_y = tile_y1; tile_y <= tile_y2; tile_y++)
{
if (tile_y >= grid->height) return 0;
for(unsigned int tile_x = tile_x1; tile_x <= tile_x2; tile_x++)
{
if (tile_x >= grid->width) return 0;
unsigned int tile_idx = tile_y*grid->width + tile_x;
if (grid->tiles[tile_idx].solid == SOLID)
{
Rectangle tile_rec = {
.x = tile_x * grid->tile_size + grid->tiles[tile_idx].offset.x,
.y = tile_y * grid->tile_size + grid->tiles[tile_idx].offset.y,
.width = grid->tiles[tile_idx].size.x,
.height = grid->tiles[tile_idx].size.y
};
if ( line_in_AABB(p1, p2, tile_rec) ) return 1;
}
if (check_oneway && grid->tiles[tile_idx].solid == ONE_WAY)
{
Rectangle tile_rec = {
.x = tile_x * grid->tile_size + grid->tiles[tile_idx].offset.x,
.y = tile_y * grid->tile_size + grid->tiles[tile_idx].offset.y,
.width = grid->tiles[tile_idx].size.x,
.height = grid->tiles[tile_idx].size.y
};
bool collide = line_in_AABB(p1, p2, tile_rec);
//For one-way platform, check for vectical collision, only return true for up direction
if (collide && ent->prev_bbox.y + ent->prev_bbox.height - 1 < tile_y * grid->tile_size) return 1;
}
Entity_t* p_other_ent;
sc_map_foreach_value(&grid->tiles[tile_idx].entities_set, p_other_ent)
{
if (ent->p_ent->m_id == p_other_ent->m_id) continue;
if (!ent->p_ent->m_alive) continue;
CTransform_t *p_ctransform = get_component(p_other_ent, CTRANSFORM_COMP_T);
CBBox_t *p_bbox = get_component(p_other_ent, CBBOX_COMP_T);
if (p_bbox == NULL || p_ctransform == NULL) continue;
//if (p_bbox->solid && !p_bbox->fragile)
if (p_bbox->solid)
{
Rectangle box = {
.x = p_ctransform->position.x,
.y = p_ctransform->position.y,
.width = p_bbox->size.x,
.height = p_bbox->size.y,
};
if ( line_in_AABB(p1, p2, box) )
{
return (p_bbox->fragile) ? 2 : 1;
}
}
}
}
}
return 0;
}
// TODO: This should be a point collision check, not an AABB check
uint8_t check_collision_offset(Entity_t* p_ent, Vector2 pos, Vector2 bbox_sz, TileGrid_t* grid, Vector2 offset)
{
Vector2 new_pos = Vector2Add(pos, offset);
CollideEntity_t ent = {
.p_ent = p_ent,
.bbox = (Rectangle){new_pos.x, new_pos.y, bbox_sz.x, bbox_sz.y},
.prev_bbox = (Rectangle){pos.x, pos.y, bbox_sz.x, bbox_sz.y},
.area = (TileArea_t){
.tile_x1 = (new_pos.x) / grid->tile_size,
.tile_y1 = (new_pos.y) / grid->tile_size,
.tile_x2 = (new_pos.x + bbox_sz.x - 1) / grid->tile_size,
.tile_y2 = (new_pos.y + bbox_sz.y - 1) / grid->tile_size
}
};
return check_collision(&ent, grid, false);
}
bool check_on_ground(Entity_t* p_ent, Vector2 pos, Vector2 prev_pos, Vector2 bbox_sz, TileGrid_t* grid)
{
//return check_collision_at(ent_idx, pos, bbox_sz, grid, (Vector2){0, 1}, p_manager);
Vector2 new_pos = Vector2Add(pos, (Vector2){0, 1});
CollideEntity_t ent = {
.p_ent = p_ent,
.bbox = (Rectangle){new_pos.x, new_pos.y + bbox_sz.y - 1, bbox_sz.x, 1},
.prev_bbox = (Rectangle){prev_pos.x, prev_pos.y, bbox_sz.x, bbox_sz.y},
.area = (TileArea_t){
.tile_x1 = (new_pos.x) / grid->tile_size,
.tile_y1 = (new_pos.y + bbox_sz.y - 1) / grid->tile_size,
.tile_x2 = (new_pos.x + bbox_sz.x - 1) / grid->tile_size,
.tile_y2 = (new_pos.y + bbox_sz.y - 1) / grid->tile_size
}
};
return check_collision(&ent, grid, true);
}
uint8_t check_bbox_edges(
TileGrid_t* tilemap,
Entity_t* p_ent, Vector2 pos, Vector2 prev_pos, Vector2 bbox,
bool ignore_fragile
)
{
uint8_t detected = 0;
// Too lazy to adjust the tile area to check, so just make a big one
CollideEntity_t ent =
{
.p_ent = p_ent,
.bbox = (Rectangle){pos.x - 1, pos.y, 1, bbox.y},
.prev_bbox = (Rectangle){pos.x, pos.y, bbox.x, bbox.y},
.area = (TileArea_t){
.tile_x1 = (pos.x - 1) / tilemap->tile_size,
.tile_y1 = (pos.y - 1) / tilemap->tile_size,
.tile_x2 = (pos.x + bbox.x) / tilemap->tile_size,
.tile_y2 = (pos.y + bbox.y) / tilemap->tile_size,
}
};
// Left
uint8_t collide_type = check_collision_line(&ent, tilemap, false);
if (collide_type == 1 || (collide_type == 2 && !ignore_fragile))
{
detected |= 1 << 3;
}
//Right
ent.bbox.x = pos.x + bbox.x + 1; // 2 to account for the previous subtraction
collide_type = check_collision_line(&ent, tilemap, false);
if (collide_type == 1 || (collide_type == 2 && !ignore_fragile))
{
detected |= 1 << 2;
}
// Up
ent.bbox.x = pos.x;
ent.bbox.y = pos.y - 1;
ent.bbox.width = bbox.x;
ent.bbox.height = 1;
collide_type = check_collision_line(&ent, tilemap, false);
if (collide_type == 1 || (collide_type == 2 && !ignore_fragile))
{
detected |= 1 << 1;
}
// Down
ent.bbox.y = pos.y + bbox.y + 1;
collide_type = check_collision_line(&ent, tilemap, true);
if (collide_type == 1 || (collide_type == 2 && !ignore_fragile))
{
detected |= 1;
}
return detected;
}

View File

@ -0,0 +1,54 @@
#ifndef __COLLISION_FUNCS_H
#define __COLLISION_FUNCS_H
#include "EC.h"
#define MAX_TILE_TYPES 16
typedef enum SolidType
{
NOT_SOLID = 0,
SOLID,
ONE_WAY,
}SolidType_t;
typedef struct Tile {
unsigned int tile_type;
SolidType_t solid;
uint8_t def;
unsigned int water_level;
struct sc_map_64v entities_set;
Vector2 offset;
Vector2 size;
bool moveable;
}Tile_t;
typedef struct TileGrid
{
unsigned int width;
unsigned int height;
unsigned int n_tiles;
unsigned int tile_size;
Tile_t* tiles;
}TileGrid_t;
typedef struct TileArea {
unsigned int tile_x1;
unsigned int tile_y1;
unsigned int tile_x2;
unsigned int tile_y2;
} TileArea_t;
typedef struct CollideEntity {
Entity_t* p_ent;
Rectangle bbox;
Rectangle prev_bbox;
TileArea_t area;
} CollideEntity_t;
void remove_entity_from_tilemap(EntityManager_t *p_manager, TileGrid_t* tilemap, Entity_t* p_ent);
uint8_t check_collision(const CollideEntity_t* ent, TileGrid_t* grid, bool check_oneway);
uint8_t check_collision_line(const CollideEntity_t* ent, TileGrid_t* grid, bool check_oneway);
uint8_t check_collision_offset(Entity_t* p_ent, Vector2 pos, Vector2 bbox_sz, TileGrid_t* grid, Vector2 offset);
bool check_on_ground(Entity_t* p_ent, Vector2 pos, Vector2 prev_pos, Vector2 bbox_sz, TileGrid_t* grid);
uint8_t check_bbox_edges(TileGrid_t* tilemap, Entity_t* p_ent, Vector2 pos, Vector2 prev_pos, Vector2 bbox, bool ignore_fragile);
#endif // __COLLISION_FUNCS_H

View File

@ -1,6 +1,7 @@
#ifndef __ENGINE_H
#define __ENGINE_H
#include "actions.h"
#include "collisions.h"
#include "sc/array/sc_array.h"
#include "assets.h"

View File

@ -26,20 +26,6 @@ static inline unsigned int get_tile_idx(int x, int y, unsigned int tilemap_width
return tile_y * tilemap_width + tile_x;
}
typedef struct TileArea {
unsigned int tile_x1;
unsigned int tile_y1;
unsigned int tile_x2;
unsigned int tile_y2;
} TileArea_t;
typedef struct CollideEntity {
Entity_t* p_ent;
Rectangle bbox;
Rectangle prev_bbox;
TileArea_t area;
} CollideEntity_t;
void change_a_tile(TileGrid_t* tilemap, unsigned int tile_idx, TileType_t new_type)
{
TileType_t last_type = tilemap->tiles[tile_idx].tile_type;
@ -137,205 +123,9 @@ void change_a_tile(TileGrid_t* tilemap, unsigned int tile_idx, TileType_t new_ty
}
static inline void remove_entity_from_tilemap(EntityManager_t *p_manager, TileGrid_t* tilemap, Entity_t* p_ent)
{
CTileCoord_t* p_tilecoord = get_component(p_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_64v(&(tilemap->tiles[tile_idx].entities_set), p_ent->m_id);
}
remove_entity(p_manager, p_ent->m_id);
}
// ------------------------- Collision functions ------------------------------------
// Do not subtract one for the size for any collision check, just pass normally. The extra one is important for AABB test
static uint8_t check_collision(const CollideEntity_t* ent, TileGrid_t* grid, bool check_oneway)
{
unsigned int tile_x1 = (ent->area.tile_x1 < 0) ? 0 : ent->area.tile_x1;
unsigned int tile_x2 = (ent->area.tile_x2 >= grid->width) ? grid->width - 1 : ent->area.tile_x2;
unsigned int tile_y1 = (ent->area.tile_y1 < 0) ? 0 : ent->area.tile_y1;
unsigned int tile_y2 = (ent->area.tile_y2 >= grid->height) ? grid->height - 1 : ent->area.tile_y2;
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++)
{
if (tile_x >= grid->width) return 0;
unsigned int tile_idx = tile_y*grid->width + tile_x;
Vector2 overlap;
if (grid->tiles[tile_idx].solid == SOLID)
{
if (find_AABB_overlap(
(Vector2){ent->bbox.x, ent->bbox.y},
(Vector2){ent->bbox.width, ent->bbox.height},
(Vector2){tile_x * TILE_SIZE + grid->tiles[tile_idx].offset.x, tile_y * TILE_SIZE + grid->tiles[tile_idx].offset.y},
grid->tiles[tile_idx].size,
&overlap
))
{
return 1;
}
}
if (check_oneway && grid->tiles[tile_idx].solid == ONE_WAY)
{
find_AABB_overlap(
(Vector2){ent->bbox.x, ent->bbox.y},
(Vector2){ent->bbox.width, ent->bbox.height},
(Vector2){tile_x * TILE_SIZE + grid->tiles[tile_idx].offset.x, tile_y * TILE_SIZE + grid->tiles[tile_idx].offset.y},
grid->tiles[tile_idx].size,
&overlap
);
//For one-way platform, check for vectical collision, only return true for up direction
if (overlap.y != 0 && ent->prev_bbox.y + ent->prev_bbox.height - 1 < tile_y * TILE_SIZE) return 1;
}
Entity_t* p_other_ent;
sc_map_foreach_value(&grid->tiles[tile_idx].entities_set, p_other_ent)
{
if (ent->p_ent->m_id == p_other_ent->m_id) continue;
if (!ent->p_ent->m_alive) continue;
CTransform_t *p_ctransform = get_component(p_other_ent, CTRANSFORM_COMP_T);
CBBox_t *p_bbox = get_component(p_other_ent, CBBOX_COMP_T);
if (p_bbox == NULL || p_ctransform == NULL) continue;
//if (p_bbox->solid && !p_bbox->fragile)
if (p_bbox->solid)
{
if (
find_AABB_overlap(
(Vector2){ent->bbox.x, ent->bbox.y},
(Vector2){ent->bbox.width, ent->bbox.height},
p_ctransform->position, p_bbox->size, &overlap
)
)
{
return (p_bbox->fragile) ? 2 : 1;
}
}
}
}
}
return 0;
}
static uint8_t check_collision_line(const CollideEntity_t* ent, TileGrid_t* grid, bool check_oneway)
{
unsigned int tile_x1 = (ent->area.tile_x1 < 0) ? 0 : ent->area.tile_x1;
unsigned int tile_x2 = (ent->area.tile_x2 >= grid->width) ? grid->width - 1 : ent->area.tile_x2;
unsigned int tile_y1 = (ent->area.tile_y1 < 0) ? 0 : ent->area.tile_y1;
unsigned int tile_y2 = (ent->area.tile_y2 >= grid->height) ? grid->height - 1 : ent->area.tile_y2;
Vector2 p1 = {ent->bbox.x, ent->bbox.y};
Vector2 p2 = {ent->bbox.x + ent->bbox.width - 1, ent->bbox.y + ent->bbox.height - 1};
for(unsigned int tile_y = tile_y1; tile_y <= tile_y2; tile_y++)
{
if (tile_y >= grid->height) return 0;
for(unsigned int tile_x = tile_x1; tile_x <= tile_x2; tile_x++)
{
if (tile_x >= grid->width) return 0;
unsigned int tile_idx = tile_y*grid->width + tile_x;
if (grid->tiles[tile_idx].solid == SOLID)
{
Rectangle tile_rec = {
.x = tile_x * TILE_SIZE + grid->tiles[tile_idx].offset.x,
.y = tile_y * TILE_SIZE + grid->tiles[tile_idx].offset.y,
.width = grid->tiles[tile_idx].size.x,
.height = grid->tiles[tile_idx].size.y
};
if ( line_in_AABB(p1, p2, tile_rec) ) return 1;
}
if (check_oneway && grid->tiles[tile_idx].solid == ONE_WAY)
{
Rectangle tile_rec = {
.x = tile_x * TILE_SIZE + grid->tiles[tile_idx].offset.x,
.y = tile_y * TILE_SIZE + grid->tiles[tile_idx].offset.y,
.width = grid->tiles[tile_idx].size.x,
.height = grid->tiles[tile_idx].size.y
};
bool collide = line_in_AABB(p1, p2, tile_rec);
//For one-way platform, check for vectical collision, only return true for up direction
if (collide && ent->prev_bbox.y + ent->prev_bbox.height - 1 < tile_y * TILE_SIZE) return 1;
}
Entity_t* p_other_ent;
sc_map_foreach_value(&grid->tiles[tile_idx].entities_set, p_other_ent)
{
if (ent->p_ent->m_id == p_other_ent->m_id) continue;
if (!ent->p_ent->m_alive) continue;
CTransform_t *p_ctransform = get_component(p_other_ent, CTRANSFORM_COMP_T);
CBBox_t *p_bbox = get_component(p_other_ent, CBBOX_COMP_T);
if (p_bbox == NULL || p_ctransform == NULL) continue;
//if (p_bbox->solid && !p_bbox->fragile)
if (p_bbox->solid)
{
Rectangle box = {
.x = p_ctransform->position.x,
.y = p_ctransform->position.y,
.width = p_bbox->size.x,
.height = p_bbox->size.y,
};
if ( line_in_AABB(p1, p2, box) )
{
return (p_bbox->fragile) ? 2 : 1;
}
}
}
}
}
return 0;
}
// TODO: This should be a point collision check, not an AABB check
static uint8_t check_collision_offset(
Entity_t* p_ent, Vector2 pos, Vector2 bbox_sz,
TileGrid_t* grid, Vector2 offset
)
{
Vector2 new_pos = Vector2Add(pos, offset);
CollideEntity_t ent = {
.p_ent = p_ent,
.bbox = (Rectangle){new_pos.x, new_pos.y, bbox_sz.x, bbox_sz.y},
.prev_bbox = (Rectangle){pos.x, pos.y, bbox_sz.x, bbox_sz.y},
.area = (TileArea_t){
.tile_x1 = (new_pos.x) / TILE_SIZE,
.tile_y1 = (new_pos.y) / TILE_SIZE,
.tile_x2 = (new_pos.x + bbox_sz.x - 1) / TILE_SIZE,
.tile_y2 = (new_pos.y + bbox_sz.y - 1) / TILE_SIZE
}
};
return check_collision(&ent, grid, false);
}
static inline bool check_on_ground(
Entity_t* p_ent, Vector2 pos, Vector2 prev_pos, Vector2 bbox_sz,
TileGrid_t* grid
)
{
//return check_collision_at(ent_idx, pos, bbox_sz, grid, (Vector2){0, 1}, p_manager);
Vector2 new_pos = Vector2Add(pos, (Vector2){0, 1});
CollideEntity_t ent = {
.p_ent = p_ent,
.bbox = (Rectangle){new_pos.x, new_pos.y + bbox_sz.y - 1, bbox_sz.x, 1},
.prev_bbox = (Rectangle){prev_pos.x, prev_pos.y, bbox_sz.x, bbox_sz.y},
.area = (TileArea_t){
.tile_x1 = (new_pos.x) / TILE_SIZE,
.tile_y1 = (new_pos.y + bbox_sz.y - 1) / TILE_SIZE,
.tile_x2 = (new_pos.x + bbox_sz.x - 1) / TILE_SIZE,
.tile_y2 = (new_pos.y + bbox_sz.y - 1) / TILE_SIZE
}
};
return check_collision(&ent, grid, true);
}
static bool check_collision_and_move(
TileGrid_t* tilemap,
Entity_t* ent, Vector2* other_pos, Vector2 other_bbox,
@ -437,65 +227,6 @@ collision_end:
return overlap_mode > 0;
}
static uint8_t check_bbox_edges(
TileGrid_t* tilemap,
Entity_t* p_ent, Vector2 pos, Vector2 prev_pos, Vector2 bbox,
bool ignore_fragile
)
{
uint8_t detected = 0;
// Too lazy to adjust the tile area to check, so just make a big one
CollideEntity_t ent =
{
.p_ent = p_ent,
.bbox = (Rectangle){pos.x - 1, pos.y, 1, bbox.y},
.prev_bbox = (Rectangle){pos.x, pos.y, bbox.x, bbox.y},
.area = (TileArea_t){
.tile_x1 = (pos.x - 1) / TILE_SIZE,
.tile_y1 = (pos.y - 1) / TILE_SIZE,
.tile_x2 = (pos.x + bbox.x) / TILE_SIZE,
.tile_y2 = (pos.y + bbox.y) / TILE_SIZE,
}
};
// Left
uint8_t collide_type = check_collision_line(&ent, tilemap, false);
if (collide_type == 1 || (collide_type == 2 && !ignore_fragile))
{
detected |= 1 << 3;
}
//Right
ent.bbox.x = pos.x + bbox.x + 1; // 2 to account for the previous subtraction
collide_type = check_collision_line(&ent, tilemap, false);
if (collide_type == 1 || (collide_type == 2 && !ignore_fragile))
{
detected |= 1 << 2;
}
// Up
ent.bbox.x = pos.x;
ent.bbox.y = pos.y - 1;
ent.bbox.width = bbox.x;
ent.bbox.height = 1;
collide_type = check_collision_line(&ent, tilemap, false);
if (collide_type == 1 || (collide_type == 2 && !ignore_fragile))
{
detected |= 1 << 1;
}
// Down
ent.bbox.y = pos.y + bbox.y + 1;
collide_type = check_collision_line(&ent, tilemap, true);
if (collide_type == 1 || (collide_type == 2 && !ignore_fragile))
{
detected |= 1;
}
return detected;
}
static Vector2 shift_bbox(Vector2 bbox, Vector2 new_bbox, AnchorPoint_t anchor)
{
Vector2 p1;

View File

@ -26,6 +26,5 @@ void player_respawn_system(Scene_t* scene);
void lifetimer_update_system(Scene_t* scene);
void spike_collision_system(Scene_t* scene);
void change_a_tile(TileGrid_t* tilemap, unsigned int tile_idx, TileType_t new_type);
#endif // __GAME_SYSTEMS_H

View File

@ -18,32 +18,6 @@ typedef enum TileType {
LADDER,
SPIKES,
} TileType_t;
#define MAX_TILE_TYPES 5
typedef enum SolidType
{
NOT_SOLID = 0,
SOLID,
ONE_WAY,
}SolidType_t;
typedef struct Tile {
TileType_t tile_type;
SolidType_t solid;
uint8_t def;
unsigned int water_level;
struct sc_map_64v entities_set;
Vector2 offset;
Vector2 size;
bool moveable;
}Tile_t;
typedef struct TileGrid {
unsigned int width;
unsigned int height;
unsigned int n_tiles;
Tile_t * tiles;
}TileGrid_t;
typedef struct LevelSceneData {
TileGrid_t tilemap;

View File

@ -0,0 +1,58 @@
#include "water_flow.h"
Entity_t* create_water_runner(EntityManager_t* ent_manager, int32_t width, int32_t height, int32_t start_tile)
{
Entity_t* p_filler = add_entity(ent_manager, NO_ENT_TAG);
if (p_filler == NULL) return NULL;
CWaterRunner_t* p_crunner = add_component(p_filler, CWATERRUNNER_T);
if (p_crunner == NULL)
{
remove_entity(ent_manager, p_filler->m_id);
return NULL;
}
int32_t total = width * height;
p_crunner->bfs_tilemap.tilemap = calloc(total, sizeof(BFSTile_t));
if (p_crunner->bfs_tilemap.tilemap == NULL)
{
remove_entity(ent_manager, p_filler->m_id);
return NULL;
}
p_crunner->bfs_tilemap.width = width;
p_crunner->bfs_tilemap.height = height;
p_crunner->bfs_tilemap.len = total;
p_crunner->current_tile = start_tile;
return p_filler;
}
void free_water_runner(Entity_t** ent, EntityManager_t* ent_manager)
{
CWaterRunner_t* p_crunner = get_component(*ent, CWATERRUNNER_T);
free(p_crunner->bfs_tilemap.tilemap);
remove_entity(ent_manager, (*ent)->m_id);
*ent = NULL;
}
void update_water_runner_system(Scene_t* scene)
{
// The core of the water runner is to:
// - Reach the lowest possible point in the tilemap
// - Scanline fill
// A runner is given an amount of movement cost
// Within the movement cost, do the following logic
// Perform a modified DFS to find the lowest point:
// - Solid tiles are not reachable
// - If bottom tile is non-solid, that is the only reachable tile,
// - If bottom tile is filled with water fully, down+left+right are reachable
// - If bottom tile is solid, left+right are reachable
// - If bottom tile is OOB, terminate
// Use a LIFO to deal with this.
// On DFS completion, find the path to the lowest point. Keep track of this
// The DFS should have figured out all reachable tiles, start scanline filling at the lowest point.
// On completion, move up update tile reachability by DFS on the current level. (repeat first step)
// - No need to recheck already reachable tiles
// - If current tile is solid, scan left and right for reachable tile and start from there
// Repeat scanline fill
}

View File

@ -0,0 +1,10 @@
#ifndef __WATER_FLOW_H
#define __WATER_FLOW_H
#include "scene_impl.h"
#include "ent_impl.h"
Entity_t* create_water_runner(EntityManager_t* ent_manager, int32_t width, int32_t height, int32_t start_tile);
void free_water_runner(Entity_t** ent, EntityManager_t* ent_manager);
void update_water_runner_system(Scene_t* scene);
#endif // __WATER_FLOW_H

View File

@ -3,6 +3,7 @@
#include "raymath.h"
#include "scene_impl.h"
#include "ent_impl.h"
#include "water_flow.h"
#include "game_systems.h"
#include "assets_loader.h"
#include <stdio.h>
@ -306,6 +307,8 @@ int main(void)
sc_map_put_64(&scene.scene.action_map, KEY_LEFT, ACTION_LEFT);
sc_map_put_64(&scene.scene.action_map, KEY_RIGHT, ACTION_RIGHT);
Entity_t* p_runner = create_water_runner(&scene.scene.ent_manager, DEFAULT_MAP_WIDTH, DEFAULT_MAP_HEIGHT, 0);
while(true)
{
@ -346,6 +349,7 @@ int main(void)
render_scene(&scene.scene);
if (WindowShouldClose()) break;
}
free_water_runner(&p_runner, &scene.scene.ent_manager);
free_scene(&scene.scene);
for (size_t i = 0; i < scene.data.tilemap.n_tiles;i++)
{