QUOTE: Life is a journey, not a destination.

freezo

A retro platform game

commit 732cca38e6985660d53a7520e0cf5636a53346d8
parent c828b946ebb446d3a90149f694b10163d247f68f
Author: Sophie <info@soophie.de>
Date:   Tue, 17 Dec 2024 15:06:49 +0100

fix: Fixed frozen enemy collision & shooting range detection

Diffstat:
Msrc/enemy.c | 56++++++++++++++++++++++++++++++++++++--------------------
Msrc/player.c | 14++++++++++----
2 files changed, 46 insertions(+), 24 deletions(-)

diff --git a/src/enemy.c b/src/enemy.c @@ -79,11 +79,25 @@ void enemy_update(enemy_t *enemy, game_t *game) { game->player->target_entity->enemy == enemy ) { if (enemy->frozen) { - if (game->player->dir > 0) { - enemy->pos.x += 2.0; + bool is_colliding = false; + rect_t enemy_rect = { enemy->pos.x, enemy->pos.y, ENEMY_WIDTH, ENEMY_HEIGHT }; + for (int i = 0; i < game->tiles_len; i++) { + tile_t *tile = game->tiles[i]; + if (tile_wall(tile)) { + rect_t tile_rect = { tile->pos.x, tile->pos.y, TILE_WIDTH, TILE_HEIGHT }; + if (rect_collide(enemy_rect, tile_rect)) { + is_colliding = true; + break; + } + } } - else { - enemy->pos.x -= 2.0; + if (!is_colliding) { + if (game->player->dir > 0) { + enemy->pos.x += 2.0; + } + else { + enemy->pos.x -= 2.0; + } } } else { @@ -127,23 +141,25 @@ void enemy_update(enemy_t *enemy, game_t *game) { enemy->pos.x += enemy->dir * 0.5; } } - // detect wall - for (int i = 0; i < game->tiles_len; i++) { - tile_t *tile = game->tiles[i]; - if (tile_wall(tile)) { - if ( - enemy->pos.y + ENEMY_HEIGHT > tile->pos.y && - enemy->pos.y < tile->pos.y + TILE_HEIGHT - ) { - float tolerance = 4.0; - if (enemy->dir > 0) { - if (fabs((enemy->pos.x + ENEMY_WIDTH - 0.4 * TILE_WIDTH) - tile->pos.x) < tolerance) { - enemy->dir = -enemy->dir; + // detect wall and direction change + if (!enemy->frozen) { + for (int i = 0; i < game->tiles_len; i++) { + tile_t *tile = game->tiles[i]; + if (tile_wall(tile)) { + if ( + enemy->pos.y + ENEMY_HEIGHT > tile->pos.y && + enemy->pos.y < tile->pos.y + TILE_HEIGHT + ) { + float tolerance = 4.0; + if (enemy->dir > 0) { + if (fabs((enemy->pos.x + ENEMY_WIDTH - 0.4 * TILE_WIDTH) - tile->pos.x) < tolerance) { + enemy->dir = -enemy->dir; + } } - } - else { - if (fabs((enemy->pos.x + 0.4 * TILE_WIDTH) - (tile->pos.x + TILE_WIDTH)) < tolerance) { - enemy->dir = -enemy->dir; + else { + if (fabs((enemy->pos.x + 0.4 * TILE_WIDTH) - (tile->pos.x + TILE_WIDTH)) < tolerance) { + enemy->dir = -enemy->dir; + } } } } diff --git a/src/player.c b/src/player.c @@ -285,22 +285,28 @@ void player_update(player_t *player, game_t *game) { rect_t tile_rect = { tile->pos.x, tile->pos.y, TILE_WIDTH, TILE_HEIGHT }; if (rect_collide(shooting_rect, tile_rect)) { if (player->dir > 0) { - player->shooting_distance = tile->pos.x - (player->pos.x + PLAYER_WIDTH); // clear target if it stands behind a wall - if (player->target_entity != NULL) { + if (player->target_entity == NULL) { + player->shooting_distance = tile->pos.x - (player->pos.x + PLAYER_WIDTH); + } + else { rect_t rect = entity_get_rect(player->target_entity); if (rect.x > tile->pos.x) { player->target_entity = NULL; + player->shooting_distance = tile->pos.x - (player->pos.x + PLAYER_WIDTH); } } } else { - player->shooting_distance = player->pos.x - (tile->pos.x + TILE_WIDTH); // clear target if it stands behind a wall - if (player->target_entity != NULL) { + if (player->target_entity == NULL) { + player->shooting_distance = player->pos.x - (tile->pos.x + TILE_WIDTH); + } + else { rect_t rect = entity_get_rect(player->target_entity); if (rect.x + rect.width < tile->pos.x + TILE_WIDTH) { player->target_entity = NULL; + player->shooting_distance = player->pos.x - (tile->pos.x + TILE_WIDTH); } } }