Refactor out broad phase grid collision functions
parent
eaf75ec8ae
commit
aae61edda9
|
@ -683,6 +683,7 @@ void init_level_scene(LevelScene_t* scene)
|
||||||
|
|
||||||
scene->data.tilemap.width = DEFAULT_MAP_WIDTH;
|
scene->data.tilemap.width = DEFAULT_MAP_WIDTH;
|
||||||
scene->data.tilemap.height = DEFAULT_MAP_HEIGHT;
|
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;
|
scene->data.tilemap.n_tiles = scene->data.tilemap.width * scene->data.tilemap.height;
|
||||||
assert(scene->data.tilemap.n_tiles <= MAX_N_TILES);
|
assert(scene->data.tilemap.n_tiles <= MAX_N_TILES);
|
||||||
scene->data.tilemap.tiles = all_tiles;
|
scene->data.tilemap.tiles = all_tiles;
|
||||||
|
|
|
@ -4,6 +4,7 @@ add_library(lib_engine STATIC
|
||||||
AABB.c
|
AABB.c
|
||||||
gui.c
|
gui.c
|
||||||
engine.c
|
engine.c
|
||||||
|
collisions.c
|
||||||
)
|
)
|
||||||
target_include_directories(lib_engine
|
target_include_directories(lib_engine
|
||||||
PUBLIC
|
PUBLIC
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
|
@ -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
|
|
@ -1,6 +1,7 @@
|
||||||
#ifndef __ENGINE_H
|
#ifndef __ENGINE_H
|
||||||
#define __ENGINE_H
|
#define __ENGINE_H
|
||||||
#include "actions.h"
|
#include "actions.h"
|
||||||
|
#include "collisions.h"
|
||||||
#include "sc/array/sc_array.h"
|
#include "sc/array/sc_array.h"
|
||||||
#include "assets.h"
|
#include "assets.h"
|
||||||
|
|
||||||
|
|
|
@ -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;
|
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)
|
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;
|
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 ------------------------------------
|
// ------------------------- Collision functions ------------------------------------
|
||||||
// Do not subtract one for the size for any collision check, just pass normally. The extra one is important for AABB test
|
// 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(
|
static bool check_collision_and_move(
|
||||||
TileGrid_t* tilemap,
|
TileGrid_t* tilemap,
|
||||||
Entity_t* ent, Vector2* other_pos, Vector2 other_bbox,
|
Entity_t* ent, Vector2* other_pos, Vector2 other_bbox,
|
||||||
|
@ -437,65 +227,6 @@ collision_end:
|
||||||
return overlap_mode > 0;
|
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)
|
static Vector2 shift_bbox(Vector2 bbox, Vector2 new_bbox, AnchorPoint_t anchor)
|
||||||
{
|
{
|
||||||
Vector2 p1;
|
Vector2 p1;
|
||||||
|
|
|
@ -26,6 +26,5 @@ void player_respawn_system(Scene_t* scene);
|
||||||
void lifetimer_update_system(Scene_t* scene);
|
void lifetimer_update_system(Scene_t* scene);
|
||||||
void spike_collision_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);
|
void change_a_tile(TileGrid_t* tilemap, unsigned int tile_idx, TileType_t new_type);
|
||||||
#endif // __GAME_SYSTEMS_H
|
#endif // __GAME_SYSTEMS_H
|
||||||
|
|
|
@ -18,32 +18,6 @@ typedef enum TileType {
|
||||||
LADDER,
|
LADDER,
|
||||||
SPIKES,
|
SPIKES,
|
||||||
} TileType_t;
|
} 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 {
|
typedef struct LevelSceneData {
|
||||||
TileGrid_t tilemap;
|
TileGrid_t tilemap;
|
||||||
|
|
Loading…
Reference in New Issue