proper collisions for block platforms

This commit is contained in:
Lephenixnoir 2023-08-03 08:29:00 +02:00
parent 87a6d28957
commit 83a508598d
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
3 changed files with 37 additions and 5 deletions

View File

@ -176,12 +176,24 @@ void game_advance(struct game *game)
}
}
static bool standable(struct platform const &p)
{
return p.type != PLATFORM_SEPARATING_WALL
&& p.type != PLATFORM_BLOCK
&& p.type != PLATFORM_BLOCK_TOP;
}
static bool collidable(struct platform const &p)
{
return p.type == PLATFORM_BLOCK_TOP;
}
num game_height_at(struct game *game, num z, int face)
{
num max_h = -KILL_PLANE_RADIUS;
for(auto const &p: game->level.platform_buffer) {
if(p.face == face && z >= p.z && z <= p.z + p.length)
if(standable(p) && p.face == face && z >= p.z && z <= p.z + p.length)
max_h = (max_h >= p.height) ? max_h : p.height;
}
@ -195,7 +207,20 @@ struct platform *game_platform_under_player(struct game *game,
return NULL;
for(auto &p: game->level.platform_buffer) {
if(player->platform == p.face
if(standable(p) && player->platform == p.face
&& player->z >= p.z && player->z <= p.z + p.length) {
return &p;
}
}
return NULL;
}
struct platform *game_block_hitting_player(struct game *game,
struct player const *player)
{
for(auto &p: game->level.platform_buffer) {
if(collidable(p) && player->platform == p.face
&& player->z >= p.z && player->z <= p.z + p.length) {
return &p;
}

View File

@ -134,4 +134,8 @@ num game_height_at(struct game *game, num z, int face);
struct platform *game_platform_under_player(struct game *game,
struct player const *player);
/* Returns the block platform that the player is hitting, if any. */
struct platform *game_block_hitting_player(struct game *game,
struct player const *player);
#endif /* __GAME_H__ */

View File

@ -155,8 +155,10 @@ int play_level(int level_id)
level_update(&game.level, player->z, player->platform);
camera_track(camera, player);
struct platform *standing_on = game_platform_under_player(&game,
&game.player);
struct platform *standing_on =
game_platform_under_player(&game, &game.player);
struct platform *hitting =
game_block_hitting_player(&game, &game.player);
//======= Rendering =======//
@ -297,7 +299,8 @@ int play_level(int level_id)
/* Death condition #1/2: inside a platform (hit it in front) */
bool death_1 = (player->height >= floor - PLAYER_HEIGHT &&
player->height < floor - STEP_HEIGHT);
player->height < floor - STEP_HEIGHT)
|| (hitting != NULL);
/* Death condition #2/2: below the kill plane */
bool death_2 = (player->height < LEVEL_RADIUS - KILL_PLANE_RADIUS);