Compare commits
14 Commits
a4c9a0d97c
...
bab18dd5e9
Author | SHA1 | Date |
---|---|---|
|
bab18dd5e9 | |
|
57cb3ef07a | |
|
dbf80508ec | |
|
99112e3a7c | |
|
023536f7c0 | |
|
aae8811cc1 | |
|
c84ef5d5ee | |
|
8bc14b17b1 | |
|
0d58ffd79a | |
|
0e94e64a6a | |
|
20ec3f6395 | |
|
62dc51d45e | |
|
b5f026c96b | |
|
29c9b4eec7 |
|
@ -1,4 +1,5 @@
|
|||
#include "mempool.h"
|
||||
#include "ent_impl.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
|
|
|
@ -16,9 +16,10 @@ enum EntitySpawnSelection {
|
|||
TOGGLE_LADDER,
|
||||
SPAWN_CRATE,
|
||||
SPAWN_METAL_CRATE,
|
||||
SPAWN_BOULDER,
|
||||
};
|
||||
|
||||
#define MAX_SPAWN_TYPE 5
|
||||
#define MAX_SPAWN_TYPE 6
|
||||
static unsigned int current_spawn_selection = 0;
|
||||
|
||||
static inline unsigned int get_tile_idx(int x, int y, const TileGrid_t* tilemap)
|
||||
|
@ -49,7 +50,7 @@ static void level_scene_render_func(Scene_t* scene)
|
|||
char buffer[6] = {0};
|
||||
int x = (i % tilemap.width) * TILE_SIZE;
|
||||
int y = (i / tilemap.width) * TILE_SIZE;
|
||||
sprintf(buffer, "%u", sc_map_size_64(&tilemap.tiles[i].entities_set));
|
||||
sprintf(buffer, "%u", sc_map_size_64v(&tilemap.tiles[i].entities_set));
|
||||
|
||||
if (data->tile_sprites[tilemap.tiles[i].tile_type] != NULL)
|
||||
{
|
||||
|
@ -90,10 +91,22 @@ static void level_scene_render_func(Scene_t* scene)
|
|||
case CRATES_ENT_TAG:
|
||||
colour = p_bbox->fragile? BROWN : GRAY;
|
||||
break;
|
||||
case BOULDER_ENT_TAG:
|
||||
colour = GRAY;
|
||||
break;
|
||||
default:
|
||||
colour = BLACK;
|
||||
break;
|
||||
}
|
||||
|
||||
if (p_ent->m_tag == BOULDER_ENT_TAG)
|
||||
{
|
||||
DrawCircleV(Vector2Add(p_ct->position, p_bbox->half_size), p_bbox->half_size.x, colour);
|
||||
}
|
||||
else
|
||||
{
|
||||
DrawRectangle(p_ct->position.x, p_ct->position.y, p_bbox->size.x, p_bbox->size.y, colour);
|
||||
}
|
||||
DrawRectangle(p_ct->position.x, p_ct->position.y, p_bbox->size.x, p_bbox->size.y, colour);
|
||||
CHurtbox_t* p_hurtbox = get_component(p_ent, CHURTBOX_T);
|
||||
CHitBoxes_t* p_hitbox = get_component(p_ent, CHITBOXES_T);
|
||||
if (p_hitbox != NULL)
|
||||
|
@ -135,7 +148,7 @@ static void level_scene_render_func(Scene_t* scene)
|
|||
{
|
||||
int x = (i % tilemap.width) * TILE_SIZE;
|
||||
int y = (i / tilemap.width) * TILE_SIZE;
|
||||
sprintf(buffer, "%u", sc_map_size_64(&tilemap.tiles[i].entities_set));
|
||||
sprintf(buffer, "%u", sc_map_size_64v(&tilemap.tiles[i].entities_set));
|
||||
|
||||
if (tilemap.tiles[i].solid > 0)
|
||||
{
|
||||
|
@ -220,6 +233,16 @@ static void spawn_crate(Scene_t* scene, unsigned int tile_idx, bool metal)
|
|||
p_ctransform->position.y = (tile_idx / data->tilemap.width) * TILE_SIZE;
|
||||
}
|
||||
|
||||
static void spawn_boulder(Scene_t* scene, unsigned int tile_idx)
|
||||
{
|
||||
LevelSceneData_t* data = &(CONTAINER_OF(scene, LevelScene_t, scene)->data);
|
||||
Entity_t* p_boulder = create_boulder(&scene->ent_manager, &scene->engine->assets);
|
||||
|
||||
CTransform_t* p_ctransform = get_component(p_boulder, CTRANSFORM_COMP_T);
|
||||
p_ctransform->position.x = (tile_idx % data->tilemap.width) * TILE_SIZE;
|
||||
p_ctransform->position.y = (tile_idx / data->tilemap.width) * TILE_SIZE;
|
||||
}
|
||||
|
||||
static void toggle_block_system(Scene_t* scene)
|
||||
{
|
||||
// TODO: This system is not good as the interface between raw input and actions is broken
|
||||
|
@ -300,6 +323,9 @@ static void toggle_block_system(Scene_t* scene)
|
|||
case SPAWN_METAL_CRATE:
|
||||
spawn_crate(scene, tile_idx, true);
|
||||
break;
|
||||
case SPAWN_BOULDER:
|
||||
spawn_boulder(scene, tile_idx);
|
||||
break;
|
||||
}
|
||||
last_tile_idx = tile_idx;
|
||||
}
|
||||
|
@ -393,17 +419,22 @@ void init_level_scene(LevelScene_t* scene)
|
|||
// insert level scene systems
|
||||
sc_array_add(&scene->scene.systems, &player_movement_input_system);
|
||||
sc_array_add(&scene->scene.systems, &player_bbox_update_system);
|
||||
sc_array_add(&scene->scene.systems, &player_pushing_system);
|
||||
sc_array_add(&scene->scene.systems, &friction_coefficient_update_system);
|
||||
sc_array_add(&scene->scene.systems, &global_external_forces_system);
|
||||
|
||||
sc_array_add(&scene->scene.systems, &moveable_update_system);
|
||||
sc_array_add(&scene->scene.systems, &movement_update_system);
|
||||
sc_array_add(&scene->scene.systems, &update_tilemap_system);
|
||||
sc_array_add(&scene->scene.systems, &tile_collision_system);
|
||||
sc_array_add(&scene->scene.systems, &hitbox_update_system);
|
||||
sc_array_add(&scene->scene.systems, &player_crushing_system);
|
||||
//sc_array_add(&scene->scene.systems, &update_tilemap_system);
|
||||
sc_array_add(&scene->scene.systems, &state_transition_update_system);
|
||||
sc_array_add(&scene->scene.systems, &player_ground_air_transition_system);
|
||||
sc_array_add(&scene->scene.systems, &sprite_animation_system);
|
||||
sc_array_add(&scene->scene.systems, &camera_update_system);
|
||||
sc_array_add(&scene->scene.systems, &player_dir_reset_system);
|
||||
sc_array_add(&scene->scene.systems, &toggle_block_system);
|
||||
|
||||
// This avoid graphical glitch, not essential
|
||||
|
@ -428,7 +459,7 @@ void init_level_scene(LevelScene_t* scene)
|
|||
{
|
||||
all_tiles[i].solid = NOT_SOLID;
|
||||
all_tiles[i].tile_type = EMPTY_TILE;
|
||||
sc_map_init_64(&all_tiles[i].entities_set, 16, 0);
|
||||
sc_map_init_64v(&all_tiles[i].entities_set, 16, 0);
|
||||
}
|
||||
for (size_t i = 0; i < scene->data.tilemap.width; ++i)
|
||||
{
|
||||
|
@ -447,7 +478,7 @@ void free_level_scene(LevelScene_t* scene)
|
|||
for (size_t i = 0; i < MAX_N_TILES;i++)
|
||||
{
|
||||
all_tiles[i].solid = 0;
|
||||
sc_map_term_64(&all_tiles[i].entities_set);
|
||||
sc_map_term_64v(&all_tiles[i].entities_set);
|
||||
}
|
||||
term_level_scene_data(&scene->data);
|
||||
}
|
||||
|
|
|
@ -1,35 +1,29 @@
|
|||
#include "AABB.h"
|
||||
|
||||
bool find_1D_overlap(Vector2 l1, Vector2 l2, float* overlap)
|
||||
uint8_t find_1D_overlap(Vector2 l1, Vector2 l2, float* overlap)
|
||||
{
|
||||
// No Overlap
|
||||
if (l1.y < l2.x || l2.y < l1.x) return false;
|
||||
if (l1.y < l2.x || l2.y < l1.x) return 0;
|
||||
|
||||
if (l1.x >= l2.x && l1.y <= l2.y)
|
||||
{
|
||||
// Complete Overlap, not sure what to do tbh
|
||||
// Complete Overlap, any direction is possible
|
||||
// Cannot give a singular value, but return something anyways
|
||||
*overlap = l2.y-l2.x + l1.y-l1.x;
|
||||
return 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
//Partial overlap
|
||||
// x is p1, y is p2
|
||||
*overlap = (l2.y >= l1.y)? l2.x - l1.y : l2.y - l1.x;
|
||||
}
|
||||
//if (fabs(*overlap) < 0.01) // Use 2 dp precision
|
||||
//{
|
||||
// *overlap = 0;
|
||||
// return false;
|
||||
//}
|
||||
return true;
|
||||
//Partial overlap
|
||||
// x is p1, y is p2
|
||||
*overlap = (l2.y >= l1.y)? l2.x - l1.y : l2.y - l1.x;
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool find_AABB_overlap(const Vector2 tl1, const Vector2 sz1, const Vector2 tl2, const Vector2 sz2, Vector2* overlap)
|
||||
uint8_t find_AABB_overlap(const Vector2 tl1, const Vector2 sz1, const Vector2 tl2, const Vector2 sz2, Vector2* overlap)
|
||||
{
|
||||
// Note that we include one extra pixel for checking
|
||||
// This avoid overlapping on the border
|
||||
Vector2 l1, l2;
|
||||
bool overlap_x, overlap_y;
|
||||
uint8_t overlap_x, overlap_y;
|
||||
l1.x = tl1.x;
|
||||
l1.y = tl1.x + sz1.x;
|
||||
l2.x = tl2.x;
|
||||
|
@ -42,5 +36,16 @@ bool find_AABB_overlap(const Vector2 tl1, const Vector2 sz1, const Vector2 tl2,
|
|||
l2.y = tl2.y + sz2.y;
|
||||
overlap_y = find_1D_overlap(l1, l2, &overlap->y);
|
||||
|
||||
return overlap_x && overlap_y;
|
||||
if (overlap_x == 2 && overlap_y == 2) return 2;
|
||||
return (overlap_x < overlap_y) ? overlap_x : overlap_y;
|
||||
}
|
||||
|
||||
bool point_in_AABB(Vector2 point, Rectangle box)
|
||||
{
|
||||
return (
|
||||
point.x > box.x
|
||||
&& point.y > box.y
|
||||
&& point.x < box.x + box.width
|
||||
&& point.y < box.y + box.height
|
||||
);
|
||||
}
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
#define __AABB_H
|
||||
#include "raylib.h"
|
||||
#include "raymath.h"
|
||||
bool find_1D_overlap(Vector2 l1, Vector2 l2, float* overlap);
|
||||
bool find_AABB_overlap(const Vector2 tl1, const Vector2 sz1, const Vector2 tl2, const Vector2 sz2, Vector2* overlap);
|
||||
#include <stdint.h>
|
||||
uint8_t find_1D_overlap(Vector2 l1, Vector2 l2, float* overlap);
|
||||
uint8_t find_AABB_overlap(const Vector2 tl1, const Vector2 sz1, const Vector2 tl2, const Vector2 sz2, Vector2* overlap);
|
||||
bool point_in_AABB(Vector2 point, Rectangle box);
|
||||
#endif // __AABB_H
|
||||
|
|
|
@ -6,8 +6,8 @@
|
|||
#include "sc/map/sc_map.h"
|
||||
#include "sc/queue/sc_queue.h"
|
||||
|
||||
#define N_TAGS 4
|
||||
#define N_COMPONENTS 10
|
||||
#define N_TAGS 5
|
||||
#define N_COMPONENTS 11
|
||||
#define MAX_COMP_POOL_SIZE 1024
|
||||
typedef struct EntityManager EntityManager_t;
|
||||
typedef struct Entity Entity_t;
|
||||
|
@ -23,6 +23,7 @@ typedef enum ComponentEnum {
|
|||
CHITBOXES_T,
|
||||
CHURTBOX_T,
|
||||
CSPRITE_T,
|
||||
CMOVEABLE_T,
|
||||
} ComponentEnum_t;
|
||||
|
||||
typedef struct _CBBox_t {
|
||||
|
@ -37,6 +38,7 @@ typedef struct _CTransform_t {
|
|||
Vector2 prev_position;
|
||||
Vector2 position;
|
||||
Vector2 velocity;
|
||||
Vector2 prev_velocity;
|
||||
Vector2 accel;
|
||||
Vector2 fric_coeff;
|
||||
} CTransform_t;
|
||||
|
@ -135,6 +137,13 @@ typedef struct _CSprite_t {
|
|||
bool pause;
|
||||
} CSprite_t;
|
||||
|
||||
typedef struct _CMoveable_t {
|
||||
uint16_t move_speed;
|
||||
Vector2 prev_pos;
|
||||
Vector2 target_pos;
|
||||
bool gridmove;
|
||||
} CMoveable_t;
|
||||
|
||||
static inline void set_bbox(CBBox_t* p_bbox, unsigned int x, unsigned int y)
|
||||
{
|
||||
p_bbox->size.x = x;
|
||||
|
@ -143,16 +152,9 @@ static inline void set_bbox(CBBox_t* p_bbox, unsigned int x, unsigned int y)
|
|||
p_bbox->half_size.y = (unsigned int)(y / 2);
|
||||
}
|
||||
|
||||
typedef enum EntityTag {
|
||||
NO_ENT_TAG,
|
||||
PLAYER_ENT_TAG,
|
||||
ENEMY_ENT_TAG,
|
||||
CRATES_ENT_TAG,
|
||||
} EntityTag_t;
|
||||
|
||||
struct Entity {
|
||||
unsigned long m_id;
|
||||
EntityTag_t m_tag;
|
||||
unsigned int m_tag;
|
||||
bool m_alive;
|
||||
unsigned long components[N_COMPONENTS];
|
||||
EntityManager_t* manager;
|
||||
|
@ -172,7 +174,7 @@ void update_entity_manager(EntityManager_t* p_manager);
|
|||
void clear_entity_manager(EntityManager_t* p_manager);
|
||||
void free_entity_manager(EntityManager_t* p_manager);
|
||||
|
||||
Entity_t* add_entity(EntityManager_t* p_manager, EntityTag_t tag);
|
||||
Entity_t* add_entity(EntityManager_t* p_manager, unsigned int tag);
|
||||
void remove_entity(EntityManager_t* p_manager, unsigned long id);
|
||||
Entity_t *get_entity(EntityManager_t* p_manager, unsigned long id);
|
||||
|
||||
|
|
|
@ -74,7 +74,7 @@ void free_entity_manager(EntityManager_t* p_manager)
|
|||
sc_queue_term(&p_manager->to_remove);
|
||||
}
|
||||
|
||||
Entity_t *add_entity(EntityManager_t* p_manager, EntityTag_t tag)
|
||||
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);
|
||||
|
|
|
@ -17,6 +17,7 @@ static CContainer_t ccontainer_buffer[MAX_COMP_POOL_SIZE];
|
|||
static CHitBoxes_t chitboxes_buffer[MAX_COMP_POOL_SIZE];
|
||||
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];
|
||||
|
||||
typedef struct ULongCircBuffer {
|
||||
unsigned long* buffer; // data buffer
|
||||
|
@ -86,6 +87,7 @@ static MemPool_t comp_mempools[N_COMPONENTS] = {
|
|||
{chitboxes_buffer, MAX_COMP_POOL_SIZE, sizeof(CHitBoxes_t), NULL, {0}},
|
||||
{churtbox_buffer, MAX_COMP_POOL_SIZE, sizeof(CHurtbox_t), NULL, {0}},
|
||||
{csprite_buffer, MAX_COMP_POOL_SIZE, sizeof(CSprite_t), NULL, {0}},
|
||||
{cmoveable_buffer, MAX_COMP_POOL_SIZE, sizeof(CMoveable_t), NULL, {0}},
|
||||
};
|
||||
static MemPool_t ent_mempool = {
|
||||
.buffer = entity_buffer,
|
||||
|
@ -155,7 +157,7 @@ Entity_t* new_entity_from_mempool(unsigned long* e_idx_ptr)
|
|||
ent->components[j] = MAX_COMP_POOL_SIZE;
|
||||
}
|
||||
ent->m_alive = true;
|
||||
ent->m_tag = NO_ENT_TAG;
|
||||
ent->m_tag = 0;
|
||||
return ent;
|
||||
}
|
||||
|
||||
|
|
|
@ -2,8 +2,18 @@
|
|||
#define __ENT_IMPL_H
|
||||
#include "assets.h"
|
||||
|
||||
typedef enum EntityTag {
|
||||
NO_ENT_TAG = 0,
|
||||
PLAYER_ENT_TAG,
|
||||
ENEMY_ENT_TAG,
|
||||
CRATES_ENT_TAG,
|
||||
BOULDER_ENT_TAG,
|
||||
} EntityTag_t;
|
||||
|
||||
|
||||
bool init_player_creation(const char* info_file, Assets_t* assets);
|
||||
Entity_t* create_player(EntityManager_t* ent_manager, Assets_t* assets);
|
||||
Entity_t* create_crate(EntityManager_t* ent_manager, Assets_t* assets, bool metal);
|
||||
Entity_t* create_boulder(EntityManager_t* ent_manager, Assets_t* assets);
|
||||
|
||||
#endif // __ENT_IMPL_H
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include "game_systems.h"
|
||||
#include "ent_impl.h"
|
||||
#include "AABB.h"
|
||||
#include "EC.h"
|
||||
#include "constants.h"
|
||||
|
@ -34,21 +35,24 @@ typedef struct TileArea {
|
|||
} TileArea_t;
|
||||
|
||||
typedef struct CollideEntity {
|
||||
unsigned int idx;
|
||||
Entity_t* p_ent;
|
||||
Rectangle bbox;
|
||||
Rectangle prev_bbox;
|
||||
TileArea_t area;
|
||||
} CollideEntity_t;
|
||||
|
||||
// ------------------------- Collision functions ------------------------------------
|
||||
static bool check_collision(const CollideEntity_t* ent, TileGrid_t* grid, EntityManager_t* p_manager, bool check_oneway)
|
||||
// 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)
|
||||
{
|
||||
for(unsigned int tile_y = ent->area.tile_y1; tile_y <= ent->area.tile_y2; tile_y++)
|
||||
{
|
||||
if (tile_y >= grid->height) return 0;
|
||||
for(unsigned int tile_x = ent->area.tile_x1; tile_x <= ent->area.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) return true;
|
||||
if (grid->tiles[tile_idx].solid == SOLID) return 1;
|
||||
|
||||
Vector2 overlap;
|
||||
if (check_oneway && grid->tiles[tile_idx].solid == ONE_WAY)
|
||||
|
@ -60,15 +64,16 @@ static bool check_collision(const CollideEntity_t* ent, TileGrid_t* grid, Entity
|
|||
);
|
||||
|
||||
//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 true;
|
||||
if (overlap.y != 0 && ent->prev_bbox.y + ent->prev_bbox.height - 1 < tile_y * TILE_SIZE) return 1;
|
||||
}
|
||||
unsigned int ent_idx;
|
||||
sc_map_foreach_value(&grid->tiles[tile_idx].entities_set, ent_idx)
|
||||
|
||||
Entity_t* p_other_ent;
|
||||
sc_map_foreach_value(&grid->tiles[tile_idx].entities_set, p_other_ent)
|
||||
{
|
||||
if (ent->idx == ent_idx) continue;
|
||||
Entity_t * p_ent = get_entity(p_manager, ent_idx);
|
||||
CTransform_t *p_ctransform = get_component(p_ent, CTRANSFORM_COMP_T);
|
||||
CBBox_t *p_bbox = get_component(p_ent, CBBOX_COMP_T);
|
||||
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)
|
||||
|
@ -81,25 +86,24 @@ static bool check_collision(const CollideEntity_t* ent, TileGrid_t* grid, Entity
|
|||
)
|
||||
)
|
||||
{
|
||||
return true;
|
||||
return (p_bbox->fragile) ? 2 : 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// TODO: This should be a point collision check, not an AABB check
|
||||
static bool check_collision_at(
|
||||
unsigned int ent_idx, Vector2 pos, Vector2 bbox_sz,
|
||||
TileGrid_t* grid, Vector2 point, EntityManager_t* p_manager
|
||||
Entity_t* p_ent, Vector2 pos, Vector2 bbox_sz,
|
||||
TileGrid_t* grid, Vector2 offset
|
||||
)
|
||||
{
|
||||
Vector2 new_pos = Vector2Add(pos, point);
|
||||
Vector2 new_pos = Vector2Add(pos, offset);
|
||||
CollideEntity_t ent = {
|
||||
.idx = ent_idx,
|
||||
.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){
|
||||
|
@ -110,18 +114,18 @@ static bool check_collision_at(
|
|||
}
|
||||
};
|
||||
|
||||
return check_collision(&ent, grid, p_manager, false);
|
||||
return check_collision(&ent, grid, false);
|
||||
}
|
||||
|
||||
static inline bool check_on_ground(
|
||||
unsigned int ent_idx, Vector2 pos, Vector2 prev_pos, Vector2 bbox_sz,
|
||||
TileGrid_t* grid, EntityManager_t* p_manager
|
||||
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 = {
|
||||
.idx = ent_idx,
|
||||
.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){
|
||||
|
@ -132,21 +136,23 @@ static inline bool check_on_ground(
|
|||
}
|
||||
};
|
||||
|
||||
return check_collision(&ent, grid, p_manager, true);
|
||||
return check_collision(&ent, grid, true);
|
||||
}
|
||||
|
||||
static bool check_collision_and_move(
|
||||
EntityManager_t* p_manager, TileGrid_t* tilemap,
|
||||
unsigned int ent_idx, CTransform_t* p_ct, Vector2 sz,
|
||||
Vector2 other_pos, Vector2 other_sz, SolidType_t other_solid
|
||||
TileGrid_t* tilemap,
|
||||
Entity_t* ent, Vector2* other_pos, Vector2 other_bbox,
|
||||
SolidType_t other_solid
|
||||
)
|
||||
{
|
||||
CTransform_t* p_ct = get_component(ent, CTRANSFORM_COMP_T);
|
||||
CBBox_t* p_bbox = get_component(ent, CBBOX_COMP_T);
|
||||
Vector2 overlap = {0,0};
|
||||
Vector2 prev_overlap = {0,0};
|
||||
if (find_AABB_overlap(p_ct->position, sz, other_pos, other_sz, &overlap))
|
||||
uint8_t overlap_mode = find_AABB_overlap(p_ct->position, p_bbox->size, *other_pos, other_bbox, &overlap);
|
||||
if (overlap_mode == 1)
|
||||
{
|
||||
// If there is collision, use previous overlap to determine direction
|
||||
find_AABB_overlap(p_ct->prev_position, sz, other_pos, other_sz, &prev_overlap);
|
||||
find_AABB_overlap(p_ct->prev_position, p_bbox->size, *other_pos, other_bbox, &prev_overlap);
|
||||
|
||||
// Store collision event here
|
||||
// Check collision on x or y axis
|
||||
|
@ -173,25 +179,75 @@ static bool check_collision_and_move(
|
|||
// also check for empty to prevent creating new collision. Not fool-proof, but good enough
|
||||
//if (other_solid && !check_collision_at(ent_idx, p_ct->position, sz, tilemap, offset, p_manager))
|
||||
if ( other_solid == SOLID
|
||||
|| (other_solid == ONE_WAY && offset.y < 0 && (p_ct->prev_position.y + sz.y - 1 < other_pos.y))
|
||||
|| (
|
||||
other_solid == ONE_WAY && offset.y < 0
|
||||
&& (p_ct->prev_position.y + p_bbox->size.y - 1 < other_pos->y))
|
||||
)
|
||||
{
|
||||
p_ct->position = Vector2Add(p_ct->position, offset);
|
||||
return true;
|
||||
if (!check_collision_at(ent, p_ct->position, p_bbox->size, tilemap, offset))
|
||||
{
|
||||
p_ct->position = Vector2Add(p_ct->position, offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
//*other_pos = Vector2Subtract(*other_pos, offset);
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
else if (overlap_mode == 2)
|
||||
{
|
||||
if ( other_solid != SOLID ) goto collision_end;
|
||||
// On complete overlap, find a free space in this order: top, left, right, bottom
|
||||
Vector2 point_to_test = {0};
|
||||
point_to_test.x = p_ct->position.x;
|
||||
point_to_test.y = other_pos->y - p_bbox->size.y;
|
||||
if (!check_collision_at(ent, point_to_test, p_bbox->size, tilemap, (Vector2){0}))
|
||||
{
|
||||
p_ct->position = point_to_test;
|
||||
goto collision_end;
|
||||
}
|
||||
|
||||
point_to_test.x = other_pos->x - p_bbox->size.x;
|
||||
point_to_test.y = p_ct->position.y;
|
||||
if (!check_collision_at(ent, point_to_test, p_bbox->size, tilemap, (Vector2){0}))
|
||||
{
|
||||
p_ct->position = point_to_test;
|
||||
goto collision_end;
|
||||
}
|
||||
|
||||
point_to_test.x = other_pos->x + other_bbox.x;
|
||||
point_to_test.y = p_ct->position.y;
|
||||
if (!check_collision_at(ent, point_to_test, p_bbox->size, tilemap, (Vector2){0}))
|
||||
{
|
||||
p_ct->position = point_to_test;
|
||||
goto collision_end;
|
||||
}
|
||||
|
||||
point_to_test.x = p_ct->position.x;
|
||||
point_to_test.y = other_pos->y + other_bbox.y;
|
||||
if (!check_collision_at(ent, point_to_test, p_bbox->size, tilemap, (Vector2){0}))
|
||||
{
|
||||
p_ct->position = point_to_test;
|
||||
goto collision_end;
|
||||
}
|
||||
// If no free space, Move up no matter what
|
||||
p_ct->position.x = p_ct->position.x;
|
||||
p_ct->position.y = other_pos->y - p_bbox->size.y;
|
||||
}
|
||||
collision_end:
|
||||
return overlap_mode > 0;
|
||||
}
|
||||
|
||||
static uint8_t check_bbox_edges(
|
||||
EntityManager_t* p_manager, TileGrid_t* tilemap,
|
||||
unsigned int ent_idx, Vector2 pos, Vector2 prev_pos, Vector2 bbox
|
||||
TileGrid_t* tilemap,
|
||||
Entity_t* p_ent, Vector2 pos, Vector2 prev_pos, Vector2 bbox
|
||||
)
|
||||
{
|
||||
uint8_t detected = 0;
|
||||
CollideEntity_t ent =
|
||||
{
|
||||
.idx = ent_idx,
|
||||
.p_ent = p_ent,
|
||||
.bbox = (Rectangle){pos.x - 1, pos.y, bbox.x, bbox.y},
|
||||
.prev_bbox = (Rectangle){pos.x, pos.y, bbox.x, bbox.y},
|
||||
.area = (TileArea_t){
|
||||
|
@ -205,13 +261,13 @@ static uint8_t check_bbox_edges(
|
|||
|
||||
// TODO: Handle one-way platform
|
||||
// Left
|
||||
detected |= (check_collision(&ent, tilemap, p_manager, false) ? 1 : 0) << 3;
|
||||
detected |= (check_collision(&ent, tilemap, false) ? 1 : 0) << 3;
|
||||
|
||||
//Right
|
||||
ent.bbox.x += 2; // 2 to account for the previous subtraction
|
||||
ent.area.tile_x1 = (pos.x + bbox.x) / TILE_SIZE;
|
||||
ent.area.tile_x2 = ent.area.tile_x1;
|
||||
detected |= (check_collision(&ent, tilemap, p_manager, false) ? 1 : 0) << 2;
|
||||
detected |= (check_collision(&ent, tilemap, false) ? 1 : 0) << 2;
|
||||
|
||||
// Up
|
||||
ent.bbox.x -= 2;
|
||||
|
@ -220,13 +276,13 @@ static uint8_t check_bbox_edges(
|
|||
ent.area.tile_x2 = (pos.x + bbox.x - 1) / TILE_SIZE,
|
||||
ent.area.tile_y1 = (pos.y - 1) / TILE_SIZE,
|
||||
ent.area.tile_y2 = ent.area.tile_y1;
|
||||
detected |= (check_collision(&ent, tilemap, p_manager, false) ? 1 : 0) << 1;
|
||||
detected |= (check_collision(&ent, tilemap, false) ? 1 : 0) << 1;
|
||||
|
||||
// Down
|
||||
ent.bbox.y += 2;
|
||||
ent.area.tile_y1 = (pos.y + bbox.y) / TILE_SIZE,
|
||||
ent.area.tile_y2 = ent.area.tile_y1;
|
||||
detected |= (check_collision(&ent, tilemap, p_manager, true) ? 1 : 0);
|
||||
detected |= (check_collision(&ent, tilemap, true) ? 1 : 0);
|
||||
return detected;
|
||||
}
|
||||
|
||||
|
@ -297,6 +353,17 @@ static Vector2 shift_bbox(Vector2 bbox, Vector2 new_bbox, AnchorPoint_t anchor)
|
|||
return offset;
|
||||
}
|
||||
|
||||
void player_dir_reset_system(Scene_t* scene)
|
||||
{
|
||||
CPlayerState_t* p_pstate;
|
||||
unsigned int ent_idx;
|
||||
sc_map_foreach(&scene->ent_manager.component_map[CPLAYERSTATE_T], ent_idx, p_pstate)
|
||||
{
|
||||
p_pstate->player_dir.x = 0;
|
||||
p_pstate->player_dir.y = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void player_movement_input_system(Scene_t* scene)
|
||||
{
|
||||
LevelSceneData_t* data = &(CONTAINER_OF(scene, LevelScene_t, scene)->data);
|
||||
|
@ -465,8 +532,6 @@ void player_movement_input_system(Scene_t* scene)
|
|||
p_cjump->jump_ready = false;
|
||||
}
|
||||
|
||||
p_pstate->player_dir.x = 0;
|
||||
p_pstate->player_dir.y = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -518,8 +583,8 @@ void player_bbox_update_system(Scene_t* scene)
|
|||
|
||||
if (
|
||||
!check_collision_at(
|
||||
p_player->m_id, p_ctransform->position, new_bbox,
|
||||
&tilemap, offset, &scene->ent_manager
|
||||
p_player, p_ctransform->position, new_bbox,
|
||||
&tilemap, offset
|
||||
)
|
||||
)
|
||||
{
|
||||
|
@ -534,6 +599,38 @@ void player_bbox_update_system(Scene_t* scene)
|
|||
}
|
||||
}
|
||||
|
||||
void player_crushing_system(Scene_t* scene)
|
||||
{
|
||||
LevelSceneData_t* data = &(CONTAINER_OF(scene, LevelScene_t, scene)->data);
|
||||
TileGrid_t tilemap = data->tilemap;
|
||||
|
||||
Entity_t* p_player;
|
||||
sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_player)
|
||||
{
|
||||
CTransform_t* p_ctransform = get_component(p_player, CTRANSFORM_COMP_T);
|
||||
CBBox_t* p_bbox = get_component(p_player, CBBOX_COMP_T);
|
||||
CollideEntity_t ent =
|
||||
{
|
||||
.p_ent = p_player,
|
||||
.bbox = (Rectangle){p_ctransform->position.x, p_ctransform->position.y, p_bbox->size.x, p_bbox->size.y},
|
||||
.prev_bbox = (Rectangle){p_ctransform->prev_velocity.x, p_ctransform->prev_position.y, p_bbox->size.x, p_bbox->size.y},
|
||||
.area = (TileArea_t){
|
||||
.tile_x1 = (p_ctransform->position.x) / TILE_SIZE,
|
||||
.tile_y1 = (p_ctransform->position.y) / TILE_SIZE,
|
||||
.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,
|
||||
},
|
||||
};
|
||||
if (check_collision(&ent, &tilemap, false) == 1)
|
||||
{
|
||||
memset(&p_ctransform->position, 0, sizeof(p_ctransform->position));
|
||||
memset(&p_ctransform->velocity, 0, sizeof(p_ctransform->velocity));
|
||||
memset(&p_ctransform->accel, 0, sizeof(p_ctransform->accel));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void tile_collision_system(Scene_t* scene)
|
||||
{
|
||||
static bool checked_entities[MAX_COMP_POOL_SIZE] = {0};
|
||||
|
@ -554,8 +651,8 @@ void tile_collision_system(Scene_t* scene)
|
|||
// exclude self
|
||||
unsigned int tile_x1 = (p_ctransform->position.x) / TILE_SIZE;
|
||||
unsigned int tile_y1 = (p_ctransform->position.y) / TILE_SIZE;
|
||||
unsigned int tile_x2 = (p_ctransform->position.x + p_bbox->size.x) / TILE_SIZE;
|
||||
unsigned int tile_y2 = (p_ctransform->position.y + p_bbox->size.y) / TILE_SIZE;
|
||||
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;
|
||||
|
||||
for (unsigned int tile_y = tile_y1; tile_y <= tile_y2; tile_y++)
|
||||
{
|
||||
|
@ -569,33 +666,31 @@ void tile_collision_system(Scene_t* scene)
|
|||
other.y = (tile_idx / tilemap.width) * TILE_SIZE; // Precision loss is intentional
|
||||
|
||||
check_collision_and_move(
|
||||
&scene->ent_manager, &tilemap, ent_idx,
|
||||
p_ctransform, p_bbox->size, other,
|
||||
TILE_SZ, tilemap.tiles[tile_idx].solid
|
||||
&tilemap, p_ent,
|
||||
&other, TILE_SZ, tilemap.tiles[tile_idx].solid
|
||||
);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
unsigned int other_ent_idx;
|
||||
Entity_t* p_other_ent;
|
||||
memset(checked_entities, 0, sizeof(checked_entities));
|
||||
sc_map_foreach_key(&tilemap.tiles[tile_idx].entities_set, other_ent_idx)
|
||||
sc_map_foreach(&tilemap.tiles[tile_idx].entities_set, other_ent_idx, p_other_ent)
|
||||
{
|
||||
if (other_ent_idx == ent_idx) continue;
|
||||
if (checked_entities[other_ent_idx]) continue;
|
||||
|
||||
checked_entities[other_ent_idx] = true;
|
||||
Entity_t *p_other_ent = get_entity(&scene->ent_manager, other_ent_idx);
|
||||
if (!p_other_ent->m_alive) continue; // To only allow one way collision check
|
||||
if (p_other_ent->m_tag < p_ent->m_tag) continue; // To only allow one way collision check
|
||||
if (!p_other_ent->m_alive) continue; // No need to move if other is dead
|
||||
CBBox_t *p_other_bbox = get_component(p_other_ent, CBBOX_COMP_T);
|
||||
if (p_other_bbox == NULL) continue;
|
||||
|
||||
CTransform_t *p_other_ct = get_component(p_other_ent, CTRANSFORM_COMP_T);
|
||||
check_collision_and_move(
|
||||
&scene->ent_manager, &tilemap, ent_idx,
|
||||
p_ctransform, p_bbox->size, p_other_ct->position,
|
||||
p_other_bbox->size, p_other_bbox->solid? SOLID : NOT_SOLID
|
||||
&tilemap, p_ent,
|
||||
&p_other_ct->position, p_other_bbox->size,
|
||||
p_other_bbox->solid? SOLID : NOT_SOLID
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -604,7 +699,7 @@ void tile_collision_system(Scene_t* scene)
|
|||
|
||||
// Post movement edge check to zero out velocity
|
||||
uint8_t edges = check_bbox_edges(
|
||||
&scene->ent_manager, &data->tilemap, ent_idx,
|
||||
&data->tilemap, p_ent,
|
||||
p_ctransform->position, p_ctransform->prev_position, p_bbox->size
|
||||
);
|
||||
if (edges & (1<<3))
|
||||
|
@ -624,48 +719,6 @@ void tile_collision_system(Scene_t* scene)
|
|||
if (p_ctransform->velocity.y > 0) p_ctransform->velocity.y = 0;
|
||||
}
|
||||
|
||||
// TODO: Resolve all collision events
|
||||
//uint32_t collision_key;
|
||||
//sc_map_foreach(&data->collision_events, collision_key, collision_value)
|
||||
//{
|
||||
// ent_idx = (collision_key >> 16);
|
||||
// uint other_ent_idx = (collision_key & 0xFFFF);
|
||||
// Entity_t *p_ent = get_entity(&scene->ent_manager, ent_idx);
|
||||
// Entity_t *p_other_ent = get_entity(&scene->ent_manager, other_ent_idx);
|
||||
// if (!p_ent->m_alive || !p_other_ent->m_alive) continue;
|
||||
//}
|
||||
//sc_map_clear_32(&data->collision_events);
|
||||
|
||||
// Level boundary collision
|
||||
unsigned int level_width = tilemap.width * TILE_SIZE;
|
||||
if(p_ctransform->position.x < 0 || p_ctransform->position.x + p_bbox->size.x > level_width)
|
||||
{
|
||||
p_ctransform->position.x = (p_ctransform->position.x < 0) ? 0 : p_ctransform->position.x;
|
||||
if (p_ctransform->position.x + p_bbox->size.x > level_width)
|
||||
{
|
||||
p_ctransform->position.x = level_width - p_bbox->size.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
p_ctransform->position.x = p_ctransform->position.x;
|
||||
}
|
||||
p_ctransform->velocity.x *= -1;
|
||||
}
|
||||
|
||||
unsigned int level_height = tilemap.height * TILE_SIZE;
|
||||
if(p_ctransform->position.y < 0 || p_ctransform->position.y + p_bbox->size.y > level_height)
|
||||
{
|
||||
p_ctransform->position.y = (p_ctransform->position.y < 0) ? 0 : p_ctransform->position.y;
|
||||
if (p_ctransform->position.y + p_bbox->size.y > level_height)
|
||||
{
|
||||
p_ctransform->position.y = level_height - p_bbox->size.y;
|
||||
}
|
||||
else
|
||||
{
|
||||
p_ctransform->position.y = p_ctransform->position.y;
|
||||
}
|
||||
p_ctransform->velocity.y *= -1;
|
||||
}
|
||||
// Deal with float precision, by rounding when it is near to an integer enough by 2 dp
|
||||
float decimal;
|
||||
float fractional = modff(p_ctransform->position.x, &decimal);
|
||||
|
@ -766,7 +819,7 @@ void global_external_forces_system(Scene_t* scene)
|
|||
|
||||
// Zero out acceleration for contacts with sturdy entites and tiles
|
||||
uint8_t edges = check_bbox_edges(
|
||||
&scene->ent_manager, &data->tilemap, ent_idx,
|
||||
&data->tilemap, p_ent,
|
||||
p_ctransform->position, p_ctransform->prev_position, p_bbox->size
|
||||
);
|
||||
if (edges & (1<<3))
|
||||
|
@ -799,13 +852,228 @@ void global_external_forces_system(Scene_t* scene)
|
|||
}
|
||||
}
|
||||
|
||||
void moveable_update_system(Scene_t* scene)
|
||||
{
|
||||
CMoveable_t* p_moveable;
|
||||
unsigned long ent_idx;
|
||||
sc_map_foreach(&scene->ent_manager.component_map[CMOVEABLE_T], ent_idx, p_moveable)
|
||||
{
|
||||
Entity_t* p_ent = get_entity(&scene->ent_manager, ent_idx);
|
||||
CTransform_t* p_ctransform = get_component(p_ent, CTRANSFORM_COMP_T);
|
||||
CBBox_t* p_bbox = get_component(p_ent, CBBOX_COMP_T);
|
||||
|
||||
if (p_moveable->gridmove)
|
||||
{
|
||||
memset(&p_ctransform->velocity, 0, sizeof(p_ctransform->velocity));
|
||||
memset(&p_ctransform->accel, 0, sizeof(p_ctransform->velocity));
|
||||
float remaining_distance = p_moveable->target_pos.x - p_ctransform->position.x;
|
||||
if (fabs(remaining_distance) < 0.1)
|
||||
{
|
||||
p_ctransform->position = p_moveable->target_pos;
|
||||
p_moveable->gridmove = false;
|
||||
p_bbox->solid = true;
|
||||
}
|
||||
else if (remaining_distance > 0.1)
|
||||
{
|
||||
p_ctransform->position.x += (remaining_distance > p_moveable->move_speed) ? p_moveable->move_speed : remaining_distance;
|
||||
}
|
||||
else
|
||||
{
|
||||
p_ctransform->position.x += (remaining_distance < -p_moveable->move_speed) ? -p_moveable->move_speed : remaining_distance;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (p_ctransform->prev_velocity.y <= 0) continue;
|
||||
|
||||
TileGrid_t tilemap = (CONTAINER_OF(scene, LevelScene_t, scene)->data).tilemap;
|
||||
int tile_x = (p_ctransform->position.x + p_bbox->half_size.x) / TILE_SIZE;
|
||||
int tile_y = (p_ctransform->position.y + p_bbox->size.y) / TILE_SIZE;
|
||||
if (tile_y >= tilemap.height) continue;
|
||||
|
||||
int tile_idx = tile_y * tilemap.width + tile_x;
|
||||
unsigned int other_ent_idx;
|
||||
bool can_move = false;
|
||||
int tile_y2 = tile_y - 1;
|
||||
sc_map_foreach_key(&tilemap.tiles[tile_idx].entities_set, other_ent_idx)
|
||||
{
|
||||
if (other_ent_idx == ent_idx) continue;
|
||||
sc_map_get_64v(&scene->ent_manager.component_map[CMOVEABLE_T], other_ent_idx);
|
||||
if (!sc_map_found(&scene->ent_manager.component_map[CMOVEABLE_T])) continue;
|
||||
|
||||
tile_x = (p_ctransform->position.x) / TILE_SIZE - 1;
|
||||
|
||||
if (tile_x >= 0 && tile_x < tilemap.width)
|
||||
{
|
||||
unsigned int tile_idx1 = tile_y * tilemap.width + tile_x;
|
||||
unsigned int tile_idx2 = tile_y2 * tilemap.width + tile_x;
|
||||
if (
|
||||
tilemap.tiles[tile_idx1].tile_type == EMPTY_TILE
|
||||
&& tilemap.tiles[tile_idx2].tile_type == EMPTY_TILE
|
||||
)
|
||||
{
|
||||
bool any_solid = false;
|
||||
unsigned int idx_to_check;
|
||||
sc_map_foreach_key(&tilemap.tiles[tile_idx1].entities_set, idx_to_check)
|
||||
{
|
||||
Entity_t* other_ent = get_entity(&scene->ent_manager, idx_to_check);
|
||||
CBBox_t* p_other_bbox = get_component(other_ent, CBBOX_COMP_T);
|
||||
any_solid |= p_other_bbox->solid;
|
||||
}
|
||||
sc_map_foreach_key(&tilemap.tiles[tile_idx2].entities_set, idx_to_check)
|
||||
{
|
||||
Entity_t* other_ent = get_entity(&scene->ent_manager, idx_to_check);
|
||||
CBBox_t* p_other_bbox = get_component(other_ent, CBBOX_COMP_T);
|
||||
any_solid |= p_other_bbox->solid;
|
||||
}
|
||||
if (!any_solid)
|
||||
{
|
||||
can_move = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tile_x += 2;
|
||||
if (tile_x >= 0 && tile_x < tilemap.width)
|
||||
{
|
||||
unsigned int tile_idx1 = tile_y * tilemap.width + tile_x;
|
||||
unsigned int tile_idx2 = tile_y2 * tilemap.width + tile_x;
|
||||
if (tilemap.tiles[tile_idx1].tile_type == EMPTY_TILE
|
||||
&& tilemap.tiles[tile_idx2].tile_type == EMPTY_TILE
|
||||
)
|
||||
{
|
||||
bool any_solid = false;
|
||||
unsigned int idx_to_check;
|
||||
sc_map_foreach_key(&tilemap.tiles[tile_idx1].entities_set, idx_to_check)
|
||||
{
|
||||
Entity_t* other_ent = get_entity(&scene->ent_manager, idx_to_check);
|
||||
CBBox_t* p_other_bbox = get_component(other_ent, CBBOX_COMP_T);
|
||||
any_solid |= p_other_bbox->solid;
|
||||
}
|
||||
sc_map_foreach_key(&tilemap.tiles[tile_idx2].entities_set, idx_to_check)
|
||||
{
|
||||
Entity_t* other_ent = get_entity(&scene->ent_manager, idx_to_check);
|
||||
CBBox_t* p_other_bbox = get_component(other_ent, CBBOX_COMP_T);
|
||||
any_solid |= p_other_bbox->solid;
|
||||
}
|
||||
if (!any_solid)
|
||||
{
|
||||
can_move = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (can_move)
|
||||
{
|
||||
p_moveable->gridmove = true;
|
||||
p_bbox->solid = false;
|
||||
p_moveable->prev_pos = p_ctransform->position;
|
||||
p_moveable->target_pos = Vector2Scale((Vector2){tile_x,tile_y2}, TILE_SIZE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void player_pushing_system(Scene_t* scene)
|
||||
{
|
||||
LevelSceneData_t* data = &(CONTAINER_OF(scene, LevelScene_t, scene)->data);
|
||||
TileGrid_t tilemap = data->tilemap;
|
||||
|
||||
Entity_t* p_player;
|
||||
sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_player)
|
||||
{
|
||||
CMovementState_t* p_movement = get_component(p_player, CMOVEMENTSTATE_T);
|
||||
CPlayerState_t* p_pstate = get_component(p_player, CPLAYERSTATE_T);
|
||||
if (!(p_movement->ground_state & 1) || p_pstate->player_dir.x == 0) continue;
|
||||
|
||||
CTransform_t* p_ctransform = get_component(p_player, CTRANSFORM_COMP_T);
|
||||
CBBox_t* p_bbox = get_component(p_player, CBBOX_COMP_T);
|
||||
Vector2 point_to_check = p_ctransform->position;
|
||||
point_to_check.y += p_bbox->half_size.y;
|
||||
if (p_pstate->player_dir.x > 0)
|
||||
{
|
||||
point_to_check.x += p_bbox->size.x + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
point_to_check.x -= 1;
|
||||
}
|
||||
//CHitBoxes_t* p_hitbox = get_component(p_player, CHITBOXES_T);
|
||||
// Get the occupied tiles
|
||||
// For each tile, loop through the entities, check collision and move
|
||||
// exclude self
|
||||
unsigned int tile_x = (point_to_check.x) / TILE_SIZE;
|
||||
unsigned int tile_y = (point_to_check.y) / TILE_SIZE;
|
||||
unsigned int tile_idx = tile_y * tilemap.width + tile_x;
|
||||
if(tilemap.tiles[tile_idx].tile_type != EMPTY_TILE) continue;
|
||||
|
||||
Entity_t* p_other_ent;
|
||||
sc_map_foreach_value(&tilemap.tiles[tile_idx].entities_set, p_other_ent)
|
||||
{
|
||||
if (p_other_ent->m_id == p_player->m_id) continue;
|
||||
|
||||
if (!p_other_ent->m_alive) continue;
|
||||
|
||||
CMoveable_t *p_other_moveable = get_component(p_other_ent, CMOVEABLE_T);
|
||||
if (p_other_moveable == NULL) continue;
|
||||
|
||||
CTransform_t *p_other_ct = get_component(p_other_ent, CTRANSFORM_COMP_T);
|
||||
CBBox_t *p_other_bbox = get_component(p_other_ent, CBBOX_COMP_T);
|
||||
Rectangle box = {
|
||||
.x = p_other_ct->position.x,
|
||||
.y = p_other_ct->position.y,
|
||||
.width = p_other_bbox->size.x,
|
||||
.height = p_other_bbox->size.y
|
||||
};
|
||||
if (point_in_AABB(point_to_check, box))
|
||||
{
|
||||
Vector2 target_pos = p_other_ct->position;
|
||||
if (p_ctransform->position.x < p_other_ct->position.x)
|
||||
{
|
||||
target_pos.x += TILE_SIZE;
|
||||
tile_x++;
|
||||
}
|
||||
else
|
||||
{
|
||||
target_pos.x -= TILE_SIZE;
|
||||
tile_x--;
|
||||
}
|
||||
|
||||
if (tile_x >= 0 && tile_x < tilemap.width)
|
||||
{
|
||||
unsigned int target_tile_idx = tile_y * tilemap.width + tile_x;
|
||||
if (
|
||||
tilemap.tiles[target_tile_idx].tile_type == EMPTY_TILE
|
||||
&& sc_map_size_64v(&tilemap.tiles[target_tile_idx].entities_set) == 0
|
||||
)
|
||||
{
|
||||
p_other_moveable->gridmove = true;
|
||||
p_other_moveable->prev_pos = p_other_ct->position;
|
||||
p_other_moveable->target_pos = target_pos;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void movement_update_system(Scene_t* scene)
|
||||
{
|
||||
LevelSceneData_t* data = &(CONTAINER_OF(scene, LevelScene_t, scene)->data);
|
||||
TileGrid_t tilemap = data->tilemap;
|
||||
// Update movement
|
||||
float delta_time = 0.017; // TODO: Will need to think about delta time handling
|
||||
CTransform_t * p_ctransform;
|
||||
sc_map_foreach_value(&scene->ent_manager.component_map[CTRANSFORM_COMP_T], p_ctransform)
|
||||
unsigned long ent_idx;
|
||||
sc_map_foreach(&scene->ent_manager.component_map[CTRANSFORM_COMP_T], ent_idx, p_ctransform)
|
||||
{
|
||||
p_ctransform->prev_velocity = p_ctransform->velocity;
|
||||
p_ctransform->velocity =
|
||||
Vector2Add(
|
||||
p_ctransform->velocity,
|
||||
|
@ -823,14 +1091,46 @@ void movement_update_system(Scene_t* scene)
|
|||
if (fabs(p_ctransform->velocity.y) < 1e-3) p_ctransform->velocity.y = 0;
|
||||
|
||||
// Store previous position before update
|
||||
p_ctransform->prev_position.x = p_ctransform->position.x;
|
||||
p_ctransform->prev_position.y = p_ctransform->position.y;
|
||||
p_ctransform->prev_position = p_ctransform->position;
|
||||
p_ctransform->position = Vector2Add(
|
||||
p_ctransform->position,
|
||||
Vector2Scale(p_ctransform->velocity, delta_time)
|
||||
);
|
||||
p_ctransform->accel.x = 0;
|
||||
p_ctransform->accel.y = 0;
|
||||
|
||||
// Level boundary collision
|
||||
Entity_t* p_ent = get_entity(&scene->ent_manager, ent_idx);
|
||||
CBBox_t* p_bbox = get_component(p_ent, CBBOX_COMP_T);
|
||||
unsigned int level_width = tilemap.width * TILE_SIZE;
|
||||
if(p_ctransform->position.x < 0 || p_ctransform->position.x + p_bbox->size.x > level_width)
|
||||
{
|
||||
p_ctransform->position.x = (p_ctransform->position.x < 0) ? 0 : p_ctransform->position.x;
|
||||
if (p_ctransform->position.x + p_bbox->size.x > level_width)
|
||||
{
|
||||
p_ctransform->position.x = level_width - p_bbox->size.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
p_ctransform->position.x = p_ctransform->position.x;
|
||||
}
|
||||
p_ctransform->velocity.x = 0;
|
||||
}
|
||||
|
||||
unsigned int level_height = tilemap.height * TILE_SIZE;
|
||||
if(p_ctransform->position.y < 0 || p_ctransform->position.y + p_bbox->size.y > level_height)
|
||||
{
|
||||
p_ctransform->position.y = (p_ctransform->position.y < 0) ? 0 : p_ctransform->position.y;
|
||||
if (p_ctransform->position.y + p_bbox->size.y > level_height)
|
||||
{
|
||||
p_ctransform->position.y = level_height - p_bbox->size.y;
|
||||
}
|
||||
else
|
||||
{
|
||||
p_ctransform->position.y = p_ctransform->position.y;
|
||||
}
|
||||
p_ctransform->velocity.y = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -880,8 +1180,8 @@ void state_transition_update_system(Scene_t* scene)
|
|||
if (p_ctransform == NULL || p_bbox == NULL) continue;
|
||||
|
||||
bool on_ground = check_on_ground(
|
||||
ent_idx, p_ctransform->position, p_ctransform->prev_position, p_bbox->size,
|
||||
&data->tilemap, &scene->ent_manager
|
||||
p_ent, p_ctransform->position, p_ctransform->prev_position, p_bbox->size,
|
||||
&data->tilemap
|
||||
);
|
||||
bool in_water = false;
|
||||
if (!(p_mstate->water_state & 1))
|
||||
|
@ -891,7 +1191,6 @@ void state_transition_update_system(Scene_t* scene)
|
|||
p_ctransform->position.y + p_bbox->half_size.y,
|
||||
data->tilemap.width
|
||||
);
|
||||
|
||||
in_water = (data->tilemap.tiles[tile_idx].water_level > 0);
|
||||
}
|
||||
else
|
||||
|
@ -939,7 +1238,7 @@ void update_tilemap_system(Scene_t* scene)
|
|||
// Use previously store tile position
|
||||
// Clear from those positions
|
||||
unsigned int tile_idx = p_tilecoord->tiles[i];
|
||||
sc_map_del_64(&(tilemap.tiles[tile_idx].entities_set), p_ent->m_id);
|
||||
sc_map_del_64v(&(tilemap.tiles[tile_idx].entities_set), p_ent->m_id);
|
||||
}
|
||||
p_tilecoord->n_tiles = 0;
|
||||
|
||||
|
@ -956,7 +1255,7 @@ void update_tilemap_system(Scene_t* scene)
|
|||
{
|
||||
unsigned int tile_idx = tile_y * tilemap.width + tile_x;
|
||||
p_tilecoord->tiles[p_tilecoord->n_tiles++] = tile_idx;
|
||||
sc_map_put_64(&(tilemap.tiles[tile_idx].entities_set), p_ent->m_id, p_ent->m_id);
|
||||
sc_map_put_64v(&(tilemap.tiles[tile_idx].entities_set), p_ent->m_id, (void *)p_ent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -994,8 +1293,9 @@ void hitbox_update_system(Scene_t* scene)
|
|||
{
|
||||
unsigned int tile_idx = tile_y * tilemap.width + tile_x;
|
||||
unsigned int other_ent_idx;
|
||||
Entity_t* p_other_ent;
|
||||
memset(checked_entities, 0, sizeof(checked_entities));
|
||||
sc_map_foreach_key(&tilemap.tiles[tile_idx].entities_set, other_ent_idx)
|
||||
sc_map_foreach(&tilemap.tiles[tile_idx].entities_set, other_ent_idx, p_other_ent)
|
||||
{
|
||||
if (other_ent_idx == ent_idx) continue;
|
||||
if (checked_entities[other_ent_idx]) continue;
|
||||
|
@ -1018,7 +1318,7 @@ void hitbox_update_system(Scene_t* scene)
|
|||
)
|
||||
{
|
||||
if (!p_other_hurtbox->fragile) continue;
|
||||
if (p_other_ent->m_tag == CRATES_ENT_TAG)
|
||||
//if (p_other_ent->m_tag == CRATES_ENT_TAG)
|
||||
{
|
||||
|
||||
CBBox_t* p_bbox = get_component(p_ent, CBBOX_COMP_T);
|
||||
|
@ -1047,7 +1347,7 @@ void hitbox_update_system(Scene_t* scene)
|
|||
// Use previously store tile position
|
||||
// Clear from those positions
|
||||
unsigned int tile_idx = p_tilecoord->tiles[i];
|
||||
sc_map_del_64(&(tilemap.tiles[tile_idx].entities_set), other_ent_idx);
|
||||
sc_map_del_64v(&(tilemap.tiles[tile_idx].entities_set), other_ent_idx);
|
||||
}
|
||||
remove_entity(&scene->ent_manager, other_ent_idx);
|
||||
}
|
||||
|
|
|
@ -6,8 +6,11 @@ void term_level_scene_data(LevelSceneData_t* data);
|
|||
|
||||
void player_movement_input_system(Scene_t* scene);
|
||||
void player_bbox_update_system(Scene_t* scene);
|
||||
void player_pushing_system(Scene_t* scene);
|
||||
void player_crushing_system(Scene_t* scene);
|
||||
void tile_collision_system(Scene_t* scene);
|
||||
void friction_coefficient_update_system(Scene_t* scene);
|
||||
void moveable_update_system(Scene_t* scene);
|
||||
void global_external_forces_system(Scene_t* scene);
|
||||
void movement_update_system(Scene_t* scene);
|
||||
void player_ground_air_transition_system(Scene_t* scene);
|
||||
|
@ -16,4 +19,5 @@ void update_tilemap_system(Scene_t* scene);
|
|||
void hitbox_update_system(Scene_t* scene);
|
||||
void sprite_animation_system(Scene_t* scene);
|
||||
void camera_update_system(Scene_t* scene);
|
||||
void player_dir_reset_system(Scene_t* scene);
|
||||
#endif // __GAME_SYSTEMS_H
|
||||
|
|
|
@ -18,3 +18,23 @@ Entity_t* create_crate(EntityManager_t* ent_manager, Assets_t* assets, bool meta
|
|||
p_hurtbox->fragile = !metal;
|
||||
return p_crate;
|
||||
}
|
||||
|
||||
Entity_t* create_boulder(EntityManager_t* ent_manager, Assets_t* assets)
|
||||
{
|
||||
Entity_t* p_boulder = add_entity(ent_manager, BOULDER_ENT_TAG);
|
||||
CBBox_t* p_bbox = add_component(p_boulder, CBBOX_COMP_T);
|
||||
|
||||
set_bbox(p_bbox, TILE_SIZE, TILE_SIZE);
|
||||
p_bbox->solid = true;
|
||||
p_bbox->fragile = false;
|
||||
|
||||
add_component(p_boulder, CTRANSFORM_COMP_T);
|
||||
add_component(p_boulder, CMOVEMENTSTATE_T);
|
||||
add_component(p_boulder, CTILECOORD_COMP_T);
|
||||
CMoveable_t* p_cmove = add_component(p_boulder, CMOVEABLE_T);
|
||||
p_cmove->move_speed = 8;
|
||||
CHurtbox_t* p_hurtbox = add_component(p_boulder, CHURTBOX_T);
|
||||
p_hurtbox->size = p_bbox->size;
|
||||
p_hurtbox->fragile = false;
|
||||
return p_boulder;
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ typedef struct Tile {
|
|||
TileType_t tile_type;
|
||||
SolidType_t solid;
|
||||
unsigned int water_level;
|
||||
struct sc_map_64 entities_set;
|
||||
struct sc_map_64v entities_set;
|
||||
}Tile_t;
|
||||
|
||||
typedef struct TileGrid {
|
||||
|
|
Loading…
Reference in New Issue