Compare commits

...

6 Commits

Author SHA1 Message Date
En Yi 917fdeba9b Fix incorrect entity deletion process
Changelog:
- Components of an entity are now freed on the spot
2023-07-19 19:15:16 +08:00
En Yi b52b662da9 Add water runner entity into game loop
Changelog:
- water runner needs ctransform + ctilecoord at least to be in the tile
  update system
    - Update the tilemap update system as well
- Update rendering function for water runner
- Update water runner creation to handle failure to create
- Add a tag for water runner (pending proper tag)
2023-07-19 11:15:34 +08:00
En Yi cf6dbcf481 Fix incorrect update of entity manager
Changelog:
- Clear to_update queue correctly
- Move entity deletion after adding
    - This is to allow deletion immediately after adding
    - Should be okay
- Fix incorrect null checking when adding entity
- Queue deletion when removing entity even if not found at removal time
    - The update function should handle such case
2023-07-19 10:55:16 +08:00
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
En Yi eaf75ec8ae Create test scene for water flow
Changelog:
- Nothing here yet
2023-07-08 21:05:00 +08:00
18 changed files with 858 additions and 325 deletions

View File

@ -71,6 +71,20 @@ target_link_libraries(scene_test_mem
${GAME_LIBS}
)
add_executable(water_test
water_test.c
)
target_include_directories(water_test
PRIVATE
${CMAKE_CURRENT_LIST_DIR}
)
target_link_libraries(water_test
${GAME_LIBS}
)
target_compile_options(water_test PRIVATE -fsanitize=address -gdwarf-4)
target_link_options(water_test PRIVATE -fsanitize=address -gdwarf-4)
add_executable(menu_test
menu_test.c
)

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

@ -23,20 +23,6 @@ void update_entity_manager(EntityManager_t* p_manager)
// New entities are assigned during add_entity
unsigned long e_idx;
sc_queue_foreach (&p_manager->to_remove, e_idx)
{
Entity_t *p_entity = (Entity_t *)sc_map_get_64v(&p_manager->entities, e_idx);
if (!p_entity) continue;
for (size_t i = 0; i < N_COMPONENTS; ++i)
{
remove_component(p_entity, i);
}
sc_map_del_64v(&p_manager->entities_map[p_entity->m_tag], e_idx);
free_entity_to_mempool(e_idx);
sc_map_del_64v(&p_manager->entities, e_idx);
}
sc_queue_clear(&p_manager->to_remove);
sc_queue_foreach (&p_manager->to_add, e_idx)
{
Entity_t *p_entity = get_entity_wtih_id(e_idx);
@ -45,9 +31,28 @@ void update_entity_manager(EntityManager_t* p_manager)
}
sc_queue_clear(&p_manager->to_add);
sc_queue_foreach (&p_manager->to_remove, e_idx)
{
Entity_t *p_entity = (Entity_t *)sc_map_get_64v(&p_manager->entities, e_idx);
if (!p_entity) continue;
for (size_t i = 0; i < N_COMPONENTS; ++i)
{
if (p_entity->components[i] == MAX_COMP_POOL_SIZE) continue;
free_component_to_mempool(i, p_entity->components[i]);
sc_map_del_64v(&p_manager->component_map[i], e_idx);
p_entity->components[i] = MAX_COMP_POOL_SIZE;
}
sc_map_del_64v(&p_manager->entities_map[p_entity->m_tag], e_idx);
free_entity_to_mempool(e_idx);
sc_map_del_64v(&p_manager->entities, e_idx);
}
sc_queue_clear(&p_manager->to_remove);
struct EntityUpdateEventInfo evt;
sc_queue_foreach (&p_manager->to_update, evt)
{
sc_map_get_64v(&p_manager->entities, evt.e_id);
if(!sc_map_found(&p_manager->entities)) continue;
switch(evt.evt_type)
{
case COMP_ADDTION:
@ -59,7 +64,7 @@ void update_entity_manager(EntityManager_t* p_manager)
break;
}
}
sc_queue_clear(&p_manager->to_add);
sc_queue_clear(&p_manager->to_update);
}
void clear_entity_manager(EntityManager_t* p_manager)
@ -94,11 +99,10 @@ Entity_t *add_entity(EntityManager_t* p_manager, unsigned int tag)
{
unsigned long e_idx = 0;
Entity_t* p_ent = new_entity_from_mempool(&e_idx);
if (p_ent == NULL) return NULL;
p_ent->m_tag = tag;
if (p_ent)
{
sc_queue_add_last(&p_manager->to_add, e_idx);
}
sc_queue_add_last(&p_manager->to_add, e_idx);
p_ent->manager = p_manager;
return p_ent;
}
@ -106,14 +110,17 @@ Entity_t *add_entity(EntityManager_t* p_manager, unsigned int tag)
void remove_entity(EntityManager_t* p_manager, unsigned long id)
{
Entity_t* p_entity = sc_map_get_64v(&p_manager->entities, id);
if(!sc_map_found(&p_manager->entities)) return;
// This only marks the entity for deletion
// Does not free entity. This is done during the update
if (p_entity->m_alive)
if(sc_map_found(&p_manager->entities))
{
p_entity->m_alive = false;
sc_queue_add_last(&p_manager->to_remove, id);
// This only marks the entity for deletion
// Does not free entity. This is done during the update
if (p_entity->m_alive)
{
p_entity->m_alive = false;
}
}
// Queue anyways because added entity may be in update queue
sc_queue_add_last(&p_manager->to_remove, id);
}
Entity_t* get_entity(EntityManager_t* p_manager, unsigned long id)

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

@ -12,5 +12,6 @@ typedef enum ActionType
ACTION_METAL_TOGGLE,
ACTION_CONFIRM,
ACTION_EXIT,
ACTION_RESTART,
}ActionType_t;
#endif // __ACTIONS_H

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

@ -9,6 +9,7 @@ typedef enum EntityTag {
CRATES_ENT_TAG,
BOULDER_ENT_TAG,
DESTRUCTABLE_ENT_TAG,
DYNMEM_ENT_TAG,
} EntityTag_t;

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;
@ -1636,7 +1367,6 @@ void update_tilemap_system(Scene_t* scene)
CTransform_t* p_ctransform = get_component(p_ent, CTRANSFORM_COMP_T);
if (p_ctransform == NULL) continue;
CBBox_t* p_bbox = get_component(p_ent, CBBOX_COMP_T);
if (p_bbox == NULL) continue;
// Update tilemap position
for (size_t i = 0;i < p_tilecoord->n_tiles; ++i)
@ -1652,8 +1382,13 @@ void update_tilemap_system(Scene_t* scene)
// 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 - 1) / TILE_SIZE;
unsigned int tile_y2 = (p_ctransform->position.y + p_bbox->size.y - 1) / TILE_SIZE;
unsigned int tile_x2 = (p_ctransform->position.x) / TILE_SIZE;
unsigned int tile_y2 = (p_ctransform->position.y) / TILE_SIZE;
if (p_bbox != NULL)
{
tile_x2 = (p_ctransform->position.x + p_bbox->size.x - 1) / TILE_SIZE;
tile_y2 = (p_ctransform->position.y + p_bbox->size.y - 1) / TILE_SIZE;
}
for (unsigned int tile_y = tile_y1; tile_y <= tile_y2; tile_y++)
{

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,61 @@
#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, DYNMEM_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;
CTransform_t* p_ct = add_component(p_filler, CTRANSFORM_COMP_T);
p_ct->movement_mode = KINEMATIC_MOVEMENT;
add_component(p_filler, CTILECOORD_COMP_T);
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);
}
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

400
water_test.c 100644
View File

@ -0,0 +1,400 @@
#include "constants.h"
#include "mempool.h"
#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>
#include <unistd.h>
#define MAX_N_TILES 4096
static Tile_t all_tiles[MAX_N_TILES] = {0};
// Maintain own queue to handle key presses
struct sc_queue_32 key_buffer;
Scene_t* scenes[1];
static GameEngine_t engine =
{
.scenes = scenes,
.max_scenes = 1,
.curr_scene = 0,
.assets = {0}
};
static void level_scene_render_func(Scene_t* scene)
{
LevelSceneData_t* data = &(CONTAINER_OF(scene, LevelScene_t, scene)->data);
TileGrid_t tilemap = data->tilemap;
Entity_t* p_ent;
BeginTextureMode(data->game_viewport);
ClearBackground(WHITE);
BeginMode2D(data->cam);
for (size_t i = 0; i < tilemap.n_tiles; ++i)
{
char buffer[6] = {0};
int x = (i % tilemap.width) * TILE_SIZE;
int y = (i / tilemap.width) * TILE_SIZE;
sprintf(buffer, "%u", sc_map_size_64v(&tilemap.tiles[i].entities_set));
if (!tilemap.tiles[i].moveable)
{
// Draw water tile
Color water_colour = ColorAlpha(RED, 0.2);
DrawRectangle(x, y, TILE_SIZE, TILE_SIZE, water_colour);
}
if (tilemap.tiles[i].tile_type == SOLID_TILE)
{
DrawRectangle(x, y, TILE_SIZE, TILE_SIZE, BLACK);
}
if (tilemap.tiles[i].water_level > 0)
{
// Draw water tile
Color water_colour = ColorAlpha(BLUE, 0.5);
DrawRectangle(x, y, TILE_SIZE, TILE_SIZE, water_colour);
}
}
sc_map_foreach_value(&scene->ent_manager.entities, p_ent)
{
CTransform_t* p_ct = get_component(p_ent, CTRANSFORM_COMP_T);
if (p_ct == NULL) continue;
CSprite_t* p_cspr = get_component(p_ent, CSPRITE_T);
if (p_cspr != NULL)
{
const SpriteRenderInfo_t spr = p_cspr->sprites[p_cspr->current_idx];
if (spr.sprite != NULL)
{
Vector2 pos = Vector2Add(p_ct->position, spr.offset);
draw_sprite(spr.sprite, pos, p_cspr->flip_x);
}
}
}
sc_map_foreach_value(&scene->ent_manager.entities_map[DYNMEM_ENT_TAG], p_ent)
{
CWaterRunner_t* p_runner = get_component(p_ent, CWATERRUNNER_T);
unsigned int x = ((p_runner->current_tile) % tilemap.width) * tilemap.tile_size;
unsigned int y = ((p_runner->current_tile) / tilemap.width) * tilemap.tile_size;
DrawCircle(x+16, y+16, 8, ColorAlpha(BLACK, 0.2));
}
char buffer[64] = {0};
for (size_t i = 0; i < tilemap.n_tiles; ++i)
{
int x = (i % tilemap.width) * TILE_SIZE;
int y = (i / tilemap.width) * TILE_SIZE;
sprintf(buffer, "%u", sc_map_size_64v(&tilemap.tiles[i].entities_set));
if (tilemap.tiles[i].solid > 0)
{
DrawText(buffer, x, y, 10, WHITE);
}
else
{
// Draw water tile
DrawText(buffer, x, y, 10, BLACK);
}
}
// Draw tile grid
for (size_t i = 0; i < tilemap.width; ++i)
{
int x = (i+1)*TILE_SIZE;
DrawLine(x, 0, x, tilemap.height * TILE_SIZE, BLACK);
}
for (size_t i = 0; i < tilemap.height;++i)
{
int y = (i+1)*TILE_SIZE;
DrawLine(0, y, tilemap.width * TILE_SIZE, y, BLACK);
}
EndMode2D();
EndTextureMode();
Rectangle draw_rec = data->game_rec;
draw_rec.x = 0;
draw_rec.y = 0;
draw_rec.height *= -1;
BeginDrawing();
ClearBackground(LIGHTGRAY);
DrawTextureRec(
data->game_viewport.texture,
draw_rec,
(Vector2){data->game_rec.x, data->game_rec.y},
WHITE
);
EndDrawing();
}
#define MAX_N_TILES 4096
static inline unsigned int get_tile_idx(int x, int y, const TileGrid_t* tilemap)
{
unsigned int tile_x = x / TILE_SIZE;
unsigned int tile_y = y / TILE_SIZE;
if (tile_x < tilemap->width && tile_y < tilemap->height)
{
return tile_y * tilemap->width + tile_x;
}
return MAX_N_TILES;
}
static void simple_friction_system(Scene_t* scene)
{
CMovementState_t* p_mstate;
unsigned long ent_idx;
sc_map_foreach(&scene->ent_manager.component_map[CMOVEMENTSTATE_T], ent_idx, p_mstate)
{
Entity_t* p_ent = get_entity(&scene->ent_manager, ent_idx);
CTransform_t* p_ctransform = get_component(p_ent, CTRANSFORM_COMP_T);
p_ctransform->fric_coeff = (Vector2){-4.5, -4.5};
p_ctransform->accel = Vector2Add(
p_ctransform->accel,
Vector2Multiply((Vector2){-3.0,-3.0}, p_ctransform->velocity)
);
}
}
static void toggle_block_system(Scene_t* scene)
{
// TODO: This system is not good as the interface between raw input and actions is broken
static unsigned int last_tile_idx = MAX_N_TILES;
LevelSceneData_t* data = &(CONTAINER_OF(scene, LevelScene_t, scene)->data);
TileGrid_t tilemap = data->tilemap;
Vector2 raw_mouse_pos = {GetMouseX(), GetMouseY()};
raw_mouse_pos = Vector2Subtract(raw_mouse_pos, (Vector2){data->game_rec.x, data->game_rec.y});
if (
raw_mouse_pos.x < data->game_rec.width
&& raw_mouse_pos.y < data->game_rec.height
)
{
Vector2 mouse_pos = GetScreenToWorld2D(raw_mouse_pos, data->cam);
unsigned int tile_idx = get_tile_idx(mouse_pos.x, mouse_pos.y, &tilemap);
if (tile_idx >= MAX_N_TILES) return;
if (tile_idx == last_tile_idx) return;
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
{
TileType_t new_type = EMPTY_TILE;
new_type = (tilemap.tiles[tile_idx].tile_type == SOLID_TILE)? EMPTY_TILE : SOLID_TILE;
if (new_type == SOLID_TILE) tilemap.tiles[tile_idx].water_level = 0;
change_a_tile(&tilemap, tile_idx, new_type);
last_tile_idx = tile_idx;
}
else if (IsMouseButtonReleased(MOUSE_RIGHT_BUTTON))
{
if (sc_map_size_64v(&tilemap.tiles[tile_idx].entities_set) == 0)
{
Entity_t* p_ent = create_water_runner(&scene->ent_manager, DEFAULT_MAP_WIDTH, DEFAULT_MAP_HEIGHT, tile_idx);
if (p_ent == NULL) return;
CTransform_t* p_ct = get_component(p_ent, CTRANSFORM_COMP_T);
p_ct->position.x = (tile_idx % tilemap.width) * tilemap.tile_size;
p_ct->position.y = (tile_idx / tilemap.width) * tilemap.tile_size;
}
else
{
Entity_t* ent;
unsigned int m_id;
sc_map_foreach(&tilemap.tiles[tile_idx].entities_set, m_id, ent)
{
if (ent->m_tag == PLAYER_ENT_TAG) continue;
CTileCoord_t* p_tilecoord = get_component(
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), m_id);
}
CWaterRunner_t* p_crunner = get_component(ent, CWATERRUNNER_T);
if (p_crunner == NULL)
{
remove_entity(&scene->ent_manager, m_id);
}
else
{
free_water_runner(ent, &scene->ent_manager);
}
}
}
}
}
}
static void level_do_action(Scene_t* scene, ActionType_t action, bool pressed)
{
CPlayerState_t* p_playerstate;
sc_map_foreach_value(&scene->ent_manager.component_map[CPLAYERSTATE_T], p_playerstate)
{
switch(action)
{
case ACTION_UP:
p_playerstate->player_dir.y = (pressed)? -1 : 0;
break;
case ACTION_DOWN:
p_playerstate->player_dir.y = (pressed)? 1 : 0;
break;
case ACTION_LEFT:
p_playerstate->player_dir.x = (pressed)? -1 : 0;
break;
case ACTION_RIGHT:
p_playerstate->player_dir.x = (pressed)? 1 : 0;
break;
default:
break;
}
}
switch (action)
{
case ACTION_RESTART:
puts("Restarting!");
break;
default:
break;
}
}
static void player_simple_movement_system(Scene_t* scene)
{
// Deal with player acceleration/velocity via inputs first
CPlayerState_t* p_pstate;
unsigned int ent_idx;
sc_map_foreach(&scene->ent_manager.component_map[CPLAYERSTATE_T], ent_idx, p_pstate)
{
Entity_t* p_player = get_entity(&scene->ent_manager, ent_idx);
CTransform_t* p_ctransform = get_component(p_player, CTRANSFORM_COMP_T);
p_ctransform->accel = Vector2Scale(Vector2Normalize(p_pstate->player_dir), MOVE_ACCEL);
}
}
int main(void)
{
sc_queue_init(&key_buffer);
InitWindow(1280, 640, "raylib");
SetTargetFPS(60);
init_memory_pools();
init_assets(&engine.assets);
load_from_infofile("res/assets.info", &engine.assets);
init_player_creation("res/player_spr.info", &engine.assets);
LevelScene_t scene;
scene.scene.engine = &engine;
init_scene(&scene.scene, &level_scene_render_func, &level_do_action);
init_level_scene_data(&scene.data);
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;
memset(scene.data.tile_sprites, 0, sizeof(scene.data.tile_sprites));
for (size_t i = 0; i < scene.data.tilemap.n_tiles;i++)
{
all_tiles[i].solid = NOT_SOLID;
all_tiles[i].tile_type = EMPTY_TILE;
all_tiles[i].moveable = true;
sc_map_init_64v(&all_tiles[i].entities_set, 16, 0);
all_tiles[i].size = (Vector2){TILE_SIZE, TILE_SIZE};
}
for (size_t i = 0; i < scene.data.tilemap.width; ++i)
{
unsigned int tile_idx = (scene.data.tilemap.height - 1) * scene.data.tilemap.width + i;
change_a_tile(&scene.data.tilemap, tile_idx, SOLID_TILE);
}
create_player(&scene.scene.ent_manager, &scene.scene.engine->assets);
update_entity_manager(&scene.scene.ent_manager);
scene.data.tile_sprites[ONEWAY_TILE] = get_sprite(&engine.assets, "tl_owp");
scene.data.tile_sprites[LADDER] = get_sprite(&engine.assets, "tl_ldr");
scenes[0] = &scene.scene;
change_scene(&engine, 0);
sc_array_add(&scene.scene.systems, &player_simple_movement_system);
//sc_array_add(&scene.scene.systems, &player_bbox_update_system);
sc_array_add(&scene.scene.systems, &simple_friction_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, &toggle_block_system);
sc_array_add(&scene.scene.systems, &camera_update_system);
sc_array_add(&scene.scene.systems, &player_dir_reset_system);
sc_map_put_64(&scene.scene.action_map, KEY_R, ACTION_RESTART);
sc_map_put_64(&scene.scene.action_map, KEY_UP, ACTION_UP);
sc_map_put_64(&scene.scene.action_map, KEY_DOWN, ACTION_DOWN);
sc_map_put_64(&scene.scene.action_map, KEY_LEFT, ACTION_LEFT);
sc_map_put_64(&scene.scene.action_map, KEY_RIGHT, ACTION_RIGHT);
while(true)
{
// This entire key processing relies on the assumption that a pressed key will
// appear in the polling of raylib
unsigned int sz = sc_queue_size(&key_buffer);
// Process any existing pressed key
for (size_t i = 0; i < sz; i++)
{
int button = sc_queue_del_first(&key_buffer);
ActionType_t action = sc_map_get_64(&scene.scene.action_map, button);
if (IsKeyReleased(button))
{
do_action(&scene.scene, action, false);
}
else
{
do_action(&scene.scene, action, true);
sc_queue_add_last(&key_buffer, button);
}
}
// Detect new key presses
while(true)
{
int button = GetKeyPressed();
if (button == 0) break;
ActionType_t action = sc_map_get_64(&scene.scene.action_map, button);
if (!sc_map_found(&scene.scene.action_map)) continue;
do_action(&scene.scene, action, true);
sc_queue_add_last(&key_buffer, button);
}
update_scene(&scene.scene);
update_entity_manager(&scene.scene.ent_manager);
// This is needed to advance time delta
render_scene(&scene.scene);
if (WindowShouldClose()) break;
}
unsigned int m_id;
Entity_t* ent;
sc_map_foreach(&scene.scene.ent_manager.entities_map[DYNMEM_ENT_TAG], m_id, ent)
{
free_water_runner(ent, &scene.scene.ent_manager);
}
free_scene(&scene.scene);
for (size_t i = 0; i < scene.data.tilemap.n_tiles;i++)
{
all_tiles[i].solid = 0;
sc_map_term_64v(&all_tiles[i].entities_set);
}
term_level_scene_data(&scene.data);
sc_queue_term(&key_buffer);
term_assets(&engine.assets);
CloseWindow();
}