From d8b93222c0487e8f145fd8d9e083ddb9d310714f Mon Sep 17 00:00:00 2001 From: whaffman Date: Thu, 29 May 2025 11:47:51 +0200 Subject: [PATCH] enemy collisions --- inc/types.h | 8 ++++++-- src/collision.c | 29 +++++++++++++++++------------ src/game/game.c | 15 ++++++++------- src/game/game_hud.c | 30 ++++++++++-------------------- src/manager/game_manager.c | 14 +++++++------- src/monster/monster.c | 16 ++++++++-------- src/moves.c | 9 ++++++--- src/parser/parse_config_line.c | 4 +++- src/render/render_sprite.c | 14 ++++++++------ test.log | 19 ++++++++++++++++++- 10 files changed, 91 insertions(+), 67 deletions(-) diff --git a/inc/types.h b/inc/types.h index 53db6d1..92bcbea 100644 --- a/inc/types.h +++ b/inc/types.h @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/15 15:52:44 by qmennen #+# #+# */ -/* Updated: 2025/05/29 09:35:12 by whaffman ######## odam.nl */ +/* Updated: 2025/05/29 11:06:24 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -30,6 +30,7 @@ typedef enum SPRITE_TYPE SPRITE_TYPE_COLLECTIBLE = 1, SPRITE_TYPE_COLLECTED = 2, SPRITE_TYPE_ENEMY = 3, + SPRITE_TYPE_DISABLED = 4, } t_sprite_type; typedef struct s_vec2 @@ -149,15 +150,18 @@ typedef struct s_sprite_column int x; } t_sprite_column; + typedef struct s_game { t_map *map; t_player *player; t_screen *screen; t_keyboard *keyboard; + double elapsed_time; int fps; } t_game; + typedef struct s_game_manager { t_game_state state; @@ -184,7 +188,7 @@ typedef struct s_menu // mlx_image_t *option_images[MAX_MENU_OPTIONS]; mlx_image_t *selector; mlx_image_t *background; - + } t_menu; #endif diff --git a/src/collision.c b/src/collision.c index 69e2b04..4b3d66c 100644 --- a/src/collision.c +++ b/src/collision.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ -/* ::: :::::::: */ -/* collision.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: qmennen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/04/22 14:40:59 by qmennen #+# #+# */ -/* Updated: 2025/05/28 17:13:24 by qmennen ### ########.fr */ +/* :::::::: */ +/* collision.c :+: :+: */ +/* +:+ */ +/* By: qmennen +#+ */ +/* +#+ */ +/* Created: 2025/04/22 14:40:59 by qmennen #+# #+# */ +/* Updated: 2025/05/29 11:17:56 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -28,16 +28,21 @@ int collision_sprite(t_map *map, t_player *player, double xa, double ya) i = 0; while (i < map->n_sprites) { - if (sprites[i].type == SPRITE_TYPE_COLLECTIBLE) + if ((sprites[i].type == SPRITE_TYPE_COLLECTIBLE || sprites[i].type == SPRITE_TYPE_ENEMY) + && (fabs(player->pos.x + xa - sprites[i].pos.x) < 0.5 + && fabs(player->pos.y + ya - sprites[i].pos.y) < 0.5)) { - - if (fabs(player->pos.x + xa - sprites[i].pos.x) < 0.5 - && fabs(player->pos.y + ya - sprites[i].pos.y) < 0.5) + if (sprites[i].type == SPRITE_TYPE_ENEMY) + { + player->battery -= 0.0001; + printf("You were attacked by an enemy! Battery: %.2f\n", player->battery); + } + else { sprites[i].type = SPRITE_TYPE_COLLECTED; collect(player); - return (1); } + return (1); } i++; } diff --git a/src/game/game.c b/src/game/game.c index 8e5b2c6..4a28af2 100644 --- a/src/game/game.c +++ b/src/game/game.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ -/* ::: :::::::: */ -/* game.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: qmennen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */ -/* Updated: 2025/05/28 17:39:24 by qmennen ### ########.fr */ +/* :::::::: */ +/* game.c :+: :+: */ +/* +:+ */ +/* By: qmennen +#+ */ +/* +#+ */ +/* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */ +/* Updated: 2025/05/29 11:14:15 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -56,6 +56,7 @@ void game_run(t_game *game) } handle_battery(game); handle_record(game); + collision_sprite(game->map, game->player, 0, 0); } void game_free(t_game *game) diff --git a/src/game/game_hud.c b/src/game/game_hud.c index 888c1d2..4f23d03 100644 --- a/src/game/game_hud.c +++ b/src/game/game_hud.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ -/* ::: :::::::: */ -/* game_hud.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: qmennen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/05/28 14:26:29 by qmennen #+# #+# */ -/* Updated: 2025/05/28 17:01:42 by qmennen ### ########.fr */ +/* :::::::: */ +/* game_hud.c :+: :+: */ +/* +:+ */ +/* By: qmennen +#+ */ +/* +#+ */ +/* Created: 2025/05/28 14:26:29 by qmennen #+# #+# */ +/* Updated: 2025/05/29 11:07:12 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -53,21 +53,11 @@ static void draw_battery(mlx_image_t *img, float battery) void handle_battery(t_game *game) { - static mlx_image_t *bat_img = NULL; - game->player->battery -= game->screen->mlx->delta_time / 50; - if (game->player->battery < 0.20) + if (game->player->battery < 0) { - bat_img = mlx_put_string( - game->screen->mlx, "Battery LOW!", 960, 512); - } - else - { - if (bat_img != NULL) - { - mlx_delete_image(game->screen->mlx, bat_img); - bat_img = NULL; - } + game->player->battery = 0; + game_terminate(game); } draw_battery(game->screen->minimap, game->player->battery); } diff --git a/src/manager/game_manager.c b/src/manager/game_manager.c index 8ca1a9e..9f90906 100644 --- a/src/manager/game_manager.c +++ b/src/manager/game_manager.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ -/* ::: :::::::: */ -/* game_manager.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: qmennen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/05/27 13:48:18 by qmennen #+# #+# */ -/* Updated: 2025/05/28 15:07:38 by qmennen ### ########.fr */ +/* :::::::: */ +/* game_manager.c :+: :+: */ +/* +:+ */ +/* By: qmennen +#+ */ +/* +#+ */ +/* Created: 2025/05/27 13:48:18 by qmennen #+# #+# */ +/* Updated: 2025/05/29 11:06:33 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/monster/monster.c b/src/monster/monster.c index ffc5345..28872b4 100644 --- a/src/monster/monster.c +++ b/src/monster/monster.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ -/* ::: :::::::: */ -/* monster.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: qmennen +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/05/28 17:15:52 by qmennen #+# #+# */ -/* Updated: 2025/05/28 17:45:37 by qmennen ### ########.fr */ +/* :::::::: */ +/* monster.c :+: :+: */ +/* +:+ */ +/* By: qmennen +#+ */ +/* +#+ */ +/* Created: 2025/05/28 17:15:52 by qmennen #+# #+# */ +/* Updated: 2025/05/29 10:47:19 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -24,7 +24,7 @@ void update_monsters(t_game *game) { sprite = &game->map->sprites[i]; if (sprite->type != SPRITE_TYPE_ENEMY) - continue; + continue ; dx = game->player->pos.x - sprite->pos.x; dy = game->player->pos.y - sprite->pos.y; double dist_squared = dx * dx + dy * dy; diff --git a/src/moves.c b/src/moves.c index 17e7d81..77f6409 100644 --- a/src/moves.c +++ b/src/moves.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/05/14 20:08:27 by whaffman #+# #+# */ -/* Updated: 2025/05/29 09:39:04 by whaffman ######## odam.nl */ +/* Updated: 2025/05/29 11:16:13 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -29,7 +29,7 @@ static void move(t_map *map, t_player *player, int dir, double delta) player->pos.y += ya; player->is_moving = 1; } - collision_sprite(map, player, xa, ya); + // collision_sprite(map, player, xa, ya); } static void strave(t_map *map, t_player *player, int dir, double delta) @@ -93,6 +93,9 @@ void player_update(t_game *game, double delta_time) rotate(game->player, -2.5, delta_time); else if (get_key(game, MLX_KEY_RIGHT)) rotate(game->player, 2.5, delta_time); - if (get_key(game, MLX_KEY_SPACE)) + if (get_key(game, MLX_KEY_SPACE)) + { game->screen->flash = 3; + game->player->battery -= 0.1f; } +} diff --git a/src/parser/parse_config_line.c b/src/parser/parse_config_line.c index c8e6f6c..5091217 100644 --- a/src/parser/parse_config_line.c +++ b/src/parser/parse_config_line.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/22 13:10:06 by whaffman #+# #+# */ -/* Updated: 2025/05/28 19:03:24 by whaffman ######## odam.nl */ +/* Updated: 2025/05/29 11:40:47 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -44,6 +44,8 @@ int handle_wall(char *token, t_map *map) { if (ft_strcmp(token, wall_tokens[i]) == 0) { + if (map->textures[i] != NULL) + return (FAILURE); map->textures[i] = load_texture(texture_path); if (map->textures[i] == NULL) return (FAILURE); diff --git a/src/render/render_sprite.c b/src/render/render_sprite.c index 1682f61..b864157 100644 --- a/src/render/render_sprite.c +++ b/src/render/render_sprite.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/05/08 12:23:17 by qmennen #+# #+# */ -/* Updated: 2025/05/29 09:47:41 by whaffman ######## odam.nl */ +/* Updated: 2025/05/29 11:16:51 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -67,10 +67,11 @@ static void draw_sprite_column( } } -void handle_flash(t_sprite *sprite, t_game *game) +void handle_flash(t_sprite *sprite, t_game *game) { - if (sprite->type == SPRITE_TYPE_ENEMY && game->screen->flash > 0) - sprite->type = SPRITE_TYPE_COLLECTED; + if (sprite->type == SPRITE_TYPE_ENEMY && game->screen->flash > 0 + && sprite->dist < 2.0) + sprite->type = SPRITE_TYPE_DISABLED; } void draw_sprite(t_game *game, t_sprite *sprite, t_render *render) @@ -107,12 +108,13 @@ void render_sprites(t_render *render, t_game *game) i = 0; while (i < game->map->n_sprites) { - if (game->map->sprites[i].type == SPRITE_TYPE_COLLECTED) + if (game->map->sprites[i].type == SPRITE_TYPE_DISABLED + || game->map->sprites[i].type == SPRITE_TYPE_COLLECTED) { i++; continue ; } - cam_fraction(game, &game->map->sprites[i]); + cam_fraction(game, &game->map->sprites[i]); draw_sprite(game, &game->map->sprites[i], render); i++; } diff --git a/test.log b/test.log index 9ba3db9..039c675 100644 --- a/test.log +++ b/test.log @@ -1,10 +1,27 @@ KO! maps/invalid/double_floor_color.cub +KO! maps/invalid/double_floor_texture.cub OK! maps/invalid/double_player.cub -KO! maps/invalid/double_wall_texture.cub +OK! maps/invalid/double_sprite_identifier.cub +OK! maps/invalid/double_wall_texture.cub KO! maps/invalid/empty_line_in_map.cub +OK! maps/invalid/extra_floor_token.cub +OK! maps/invalid/extra_sprite_token.cub +OK! maps/invalid/extra_wall_token.cub +OK! maps/invalid/garbage_config_line.cub OK! maps/invalid/invalid_color.cub +OK! maps/invalid/invalid_sprite_character.cub +KO! maps/invalid/invalid_sprite_identifier_token.cub KO! maps/invalid/missing_floor_color.cub +OK! maps/invalid/missing_floor_path.cub +OK! maps/invalid/missing_sprite_path.cub +OK! maps/invalid/missing_wall_path.cub KO! maps/invalid/missing_wall_texture.cub KO! maps/invalid/no_player.cub +OK! maps/invalid/non-existing_floor_texture.cub +OK! maps/invalid/non-existing_sprite_texture.cub +OK! maps/invalid/non-existing_texture.cub KO! maps/invalid/not_enclosed_map.cub +KO! maps/invalid/not_enclosed_map_extra_empty_tile.cub +KO! maps/invalid/sprite_outside_wall.cub OK! maps/invalid/test.cub +OK! maps/invalid/unknown_existing_sprite.cub