Compare commits

...

3 Commits

Author SHA1 Message Date
En Yi a1101cd951 Add player respawn system 2023-06-14 20:36:50 +08:00
En Yi 9e60d0fb89 Improve crushing logic
Changelog:
- Do edge checking in both direction on each axis. If either axis has a
  collision on both directions, crush the player
2023-06-14 20:29:23 +08:00
En Yi ae67ba4710 Improve ladder mechanics
Changelog:
- Ladder transition occurs only at falling
- Add tile snapping when going up and down
- Going down ladder from ground state no longer shift player's y
  position significantly
2023-06-14 19:09:24 +08:00
3 changed files with 65 additions and 23 deletions

View File

@ -435,6 +435,7 @@ void init_level_scene(LevelScene_t* scene)
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, &player_respawn_system);
sc_array_add(&scene->scene.systems, &toggle_block_system);
// This avoid graphical glitch, not essential

View File

@ -96,7 +96,7 @@ static uint8_t check_collision(const CollideEntity_t* ent, TileGrid_t* grid, boo
}
// TODO: This should be a point collision check, not an AABB check
static bool check_collision_at(
static bool check_collision_offset(
Entity_t* p_ent, Vector2 pos, Vector2 bbox_sz,
TileGrid_t* grid, Vector2 offset
)
@ -202,7 +202,7 @@ static bool check_collision_and_move(
Vector2 point_to_test = {0};
point_to_test.x = p_ct->position.x;
point_to_test.y = other_pos->y - p_bbox->size.y + 1;
if (!check_collision_at(ent, point_to_test, p_bbox->size, tilemap, (Vector2){0}))
if (!check_collision_offset(ent, point_to_test, p_bbox->size, tilemap, (Vector2){0}))
{
p_ct->position = point_to_test;
goto collision_end;
@ -210,7 +210,7 @@ static bool check_collision_and_move(
point_to_test.x = other_pos->x - p_bbox->size.x + 1;
point_to_test.y = p_ct->position.y;
if (!check_collision_at(ent, point_to_test, p_bbox->size, tilemap, (Vector2){0}))
if (!check_collision_offset(ent, point_to_test, p_bbox->size, tilemap, (Vector2){0}))
{
p_ct->position = point_to_test;
goto collision_end;
@ -218,7 +218,7 @@ static bool check_collision_and_move(
point_to_test.x = other_pos->x + other_bbox.x - 1;
point_to_test.y = p_ct->position.y;
if (!check_collision_at(ent, point_to_test, p_bbox->size, tilemap, (Vector2){0}))
if (!check_collision_offset(ent, point_to_test, p_bbox->size, tilemap, (Vector2){0}))
{
p_ct->position = point_to_test;
goto collision_end;
@ -226,7 +226,7 @@ static bool check_collision_and_move(
point_to_test.x = p_ct->position.x;
point_to_test.y = other_pos->y + other_bbox.y - 1;
if (!check_collision_at(ent, point_to_test, p_bbox->size, tilemap, (Vector2){0}))
if (!check_collision_offset(ent, point_to_test, p_bbox->size, tilemap, (Vector2){0}))
{
p_ct->position = point_to_test;
goto collision_end;
@ -353,6 +353,22 @@ static Vector2 shift_bbox(Vector2 bbox, Vector2 new_bbox, AnchorPoint_t anchor)
return offset;
}
void player_respawn_system(Scene_t* scene)
{
Entity_t* p_player;
sc_map_foreach_value(&scene->ent_manager.entities_map[PLAYER_ENT_TAG], p_player)
{
if (!p_player->m_alive)
{
p_player->m_alive = true;
CTransform_t* p_ctransform = get_component(p_player, CTRANSFORM_COMP_T);
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));
}
}
}
void player_dir_reset_system(Scene_t* scene)
{
CPlayerState_t* p_pstate;
@ -390,15 +406,10 @@ void player_movement_input_system(Scene_t* scene)
p_ctransform->position.y + p_bbox->half_size.y,
data->tilemap.width
);
if (tilemap.tiles[tile_idx].tile_type == LADDER)
if (tilemap.tiles[tile_idx].tile_type == LADDER && p_ctransform->velocity.y >= 0)
{
p_pstate->ladder_state = true;
//p_ctransform->position.x = (tile_idx % tilemap.width) * TILE_SIZE;
if (p_mstate->ground_state & 1)
{
p_ctransform->position.y--;
}
p_ctransform->position.y--;
}
}
else if (p_pstate->player_dir.y > 0)
@ -424,11 +435,7 @@ void player_movement_input_system(Scene_t* scene)
if (tile_idx < tilemap.n_tiles && tilemap.tiles[tile_idx].tile_type == LADDER)
{
p_pstate->ladder_state = true;
//p_ctransform->position.x = (tile_idx % tilemap.width) * TILE_SIZE;
if (p_mstate->ground_state & 1)
{
p_ctransform->position.y += TILE_SIZE / 2;
}
p_ctransform->position.y++;
}
}
}
@ -451,6 +458,10 @@ void player_movement_input_system(Scene_t* scene)
{
p_ctransform->velocity.y = p_pstate->player_dir.y * 150;
p_ctransform->velocity.x = p_pstate->player_dir.x * 40;
if (p_pstate->player_dir.y != 0)
{
p_ctransform->position.x = tile_x * TILE_SIZE + 1;
}
}
}
@ -582,7 +593,7 @@ void player_bbox_update_system(Scene_t* scene)
}
if (
!check_collision_at(
!check_collision_offset(
p_player, p_ctransform->position, new_bbox,
&tilemap, offset
)
@ -617,15 +628,44 @@ void player_crushing_system(Scene_t* scene)
.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_x2 = (p_ctransform->position.x) / TILE_SIZE,
.tile_y2 = (p_ctransform->position.y + p_bbox->size.y - 1) / TILE_SIZE,
},
};
if (check_collision(&ent, &tilemap, false) == 1)
// Mostly identical to edge check function
// Except we want collision instead of just touching
uint8_t detected = 0;
// Left
detected |= (check_collision(&ent, &tilemap, false) ? 1 : 0);
//Right
ent.area.tile_x1 = (p_ctransform->position.x + p_bbox->size.x - 1) / TILE_SIZE;
ent.area.tile_x2 = ent.area.tile_x1;
detected |= (check_collision(&ent, &tilemap, false) ? 1 : 0) << 1;
if (detected == 0b11)
{
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));
p_player->m_alive = false;
return;
}
detected = 0;
// Up
ent.area.tile_x1 = (p_ctransform->position.x) / TILE_SIZE,
ent.area.tile_y1 = (p_ctransform->position.y - 1) / TILE_SIZE,
ent.area.tile_y2 = ent.area.tile_y1;
detected |= (check_collision(&ent, &tilemap, false) ? 1 : 0) << 1;
// Down
ent.area.tile_y1 = (p_ctransform->position.y + p_bbox->size.y - 1) / TILE_SIZE,
ent.area.tile_y2 = ent.area.tile_y1;
detected |= (check_collision(&ent, &tilemap, true) ? 1 : 0);
//if (check_collision(&ent, &tilemap, false) == 1)
if (detected == 0b11)
{
p_player->m_alive = false;
return;
}
}

View File

@ -20,4 +20,5 @@ 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);
void player_respawn_system(Scene_t* scene);
#endif // __GAME_SYSTEMS_H