Compare commits

...

4 Commits

Author SHA1 Message Date
En Yi 0d58ffd79a Tweak tile collision function logic
Changelog:
- Remove check for same tag, seems to be the cause for unaligned crates
- Remove dead code: collision event collection
    - Will rethink this if necessary
2023-06-04 14:54:53 +08:00
En Yi 0e94e64a6a Add solid entity check to boulder movement
Changelog:
- Prevent Boulder going into grid movement if there is an entity in
  the movement when pushed
2023-06-04 14:28:08 +08:00
En Yi 20ec3f6395 Improve on the boulder pushing mechan
Changelog:
- Implement point to AABB check function
- Change moveable check to use point-AABB check
- Allow boulder pushing from standing still
2023-06-03 14:01:10 +08:00
En Yi 62dc51d45e Implement simple systems for pushing boulder 2023-05-30 21:32:04 +08:00
6 changed files with 137 additions and 18 deletions

View File

@ -421,8 +421,10 @@ void init_level_scene(LevelScene_t* scene)
sc_array_add(&scene->scene.systems, &player_bbox_update_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, &player_pushing_system);
sc_array_add(&scene->scene.systems, &tile_collision_system);
sc_array_add(&scene->scene.systems, &hitbox_update_system);
//sc_array_add(&scene->scene.systems, &update_tilemap_system);
@ -430,6 +432,7 @@ void init_level_scene(LevelScene_t* scene)
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

View File

@ -44,3 +44,13 @@ bool find_AABB_overlap(const Vector2 tl1, const Vector2 sz1, const Vector2 tl2,
return 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
);
}

View File

@ -4,4 +4,5 @@
#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);
bool point_in_AABB(Vector2 point, Rectangle box);
#endif // __AABB_H

View File

@ -178,8 +178,8 @@ static bool check_collision_and_move(
)
{
p_ct->position = Vector2Add(p_ct->position, offset);
return true;
}
return true;
}
return false;
}
@ -298,6 +298,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);
@ -466,8 +477,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;
}
}
@ -587,8 +596,7 @@ void tile_collision_system(Scene_t* scene)
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;
@ -625,18 +633,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)
@ -800,6 +796,111 @@ 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);
if (p_moveable->gridmove)
{
memset(&p_ctransform->velocity, 0, sizeof(p_ctransform->velocity));
memset(&p_ctransform->accel, 0, sizeof(p_ctransform->velocity));
p_ctransform->position = p_moveable->target_pos;
p_moveable->gridmove = false;
}
}
}
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)) 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;
unsigned int other_ent_idx;
sc_map_foreach_key(&tilemap.tiles[tile_idx].entities_set, other_ent_idx)
{
if (other_ent_idx == p_player->m_id) continue;
Entity_t *p_other_ent = get_entity(&scene->ent_manager, other_ent_idx);
if (!p_other_ent->m_alive) continue; // To only allow one way collision check
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_64(&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)
{
// Update movement

View File

@ -6,8 +6,10 @@ 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 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 +18,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

View File

@ -31,7 +31,8 @@ Entity_t* create_boulder(EntityManager_t* ent_manager, Assets_t* assets)
add_component(p_boulder, CTRANSFORM_COMP_T);
add_component(p_boulder, CMOVEMENTSTATE_T);
add_component(p_boulder, CTILECOORD_COMP_T);
add_component(p_boulder, CMOVEABLE_T);
CMoveable_t* p_cmove = add_component(p_boulder, CMOVEABLE_T);
p_cmove->move_speed = 4;
CHurtbox_t* p_hurtbox = add_component(p_boulder, CHURTBOX_T);
p_hurtbox->size = p_bbox->size;
p_hurtbox->fragile = false;