enemy collisions

This commit is contained in:
whaffman 2025-05-29 11:47:51 +02:00
parent d8bef01b32
commit d8b93222c0
10 changed files with 91 additions and 67 deletions

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* collision.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/22 14:40:59 by qmennen #+# #+# */
/* Updated: 2025/05/28 17:13:24 by qmennen ### ########.fr */
/* :::::::: */
/* collision.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* 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++;
}

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* game.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */
/* Updated: 2025/05/28 17:39:24 by qmennen ### ########.fr */
/* :::::::: */
/* game.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* 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)

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* game_hud.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/28 14:26:29 by qmennen #+# #+# */
/* Updated: 2025/05/28 17:01:42 by qmennen ### ########.fr */
/* :::::::: */
/* game_hud.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* 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);
}

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* game_manager.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/27 13:48:18 by qmennen #+# #+# */
/* Updated: 2025/05/28 15:07:38 by qmennen ### ########.fr */
/* :::::::: */
/* game_manager.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/05/27 13:48:18 by qmennen #+# #+# */
/* Updated: 2025/05/29 11:06:33 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* monster.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/28 17:15:52 by qmennen #+# #+# */
/* Updated: 2025/05/28 17:45:37 by qmennen ### ########.fr */
/* :::::::: */
/* monster.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* 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;

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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;
}
}

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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);

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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++;
}

View File

@ -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