Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Health #40

Merged
merged 16 commits into from
Jan 25, 2025
Binary file added assets/sprites/enemies/D64_Cacodemon(1).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sprites/enemies/D64_Cacodemon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
371 changes: 371 additions & 0 deletions assets/sprites/enemies/cacodemon00.xpm

Large diffs are not rendered by default.

Binary file added cub3D
Binary file not shown.
42 changes: 23 additions & 19 deletions includes/cub3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: hluiz-ma <hluiz-ma@student.42porto.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/11 13:49:34 by hluiz-ma #+# #+# */
/* Updated: 2025/01/11 19:06:50 by marsoare ### ########.fr */
/* Updated: 2025/01/25 10:59:13 by marsoare ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -36,6 +36,8 @@
# include <unistd.h>
# include <validations.h>
# include <checker.h>
# include <health.h>
# include <enemy.h>

# ifdef MAC_OS
# include <mlx.h>
Expand Down Expand Up @@ -101,6 +103,7 @@ typedef struct s_player
double last_fire;
int gun_width;
int gun_height;
int health;
} t_player;

typedef struct s_map
Expand All @@ -116,24 +119,25 @@ typedef struct s_map

typedef struct s_game
{
void *mlx;
void *win;
void *img;
char *addr;
int bits_per_pixel;
int line_length;
int endian;
int fd_map;
char *map_path;
t_map map;
t_player p1;
t_texture north;
t_texture south;
t_texture east;
t_texture west;
t_vector last_mouse;
double mouse_sensi;
double fps;
void *mlx;
void *win;
void *img;
char *addr;
int bits_per_pixel;
int line_length;
int endian;
int fd_map;
char *map_path;
t_map map;
t_player p1;
t_texture north;
t_texture south;
t_texture east;
t_texture west;
t_vector last_mouse;
double mouse_sensi;
double fps;
t_enemy_list *enemies;
} t_game;

void cleanup_game(t_game *game);
Expand Down
79 changes: 79 additions & 0 deletions includes/enemy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* enemy.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marsoare <marsoare@student.42porto.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/22 17:56:23 by marsoare #+# #+# */
/* Updated: 2025/01/25 10:58:02 by marsoare ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef ENEMY_H
# define ENEMY_H

typedef struct s_sprite_data
{
int height;
int width;
double screen_x;
int start_y;
int end_y;
int start_x;
int end_x;
} t_sprite_data;

typedef struct s_ray_data
{
t_vector pos;
t_vector delta_dist;
t_vector side_dist;
t_vector step;
int map_x;
int map_y;
} t_ray_data;

typedef struct s_enemy
{
t_vector pos;
t_vector dir;
int health;
bool alive;
double dist_to_player;
t_texture *texture;
} t_enemy;

typedef struct s_draw_params
{
t_game *game;
t_enemy *enemy;
t_vector pos;
t_vector tex;
} t_draw_params;

typedef struct s_enemy_list
{
t_enemy enemy;
struct s_enemy_list *next;
} t_enemy_list;

double vector_length(t_vector v);
void init_enemies(t_game *game);
void update_enemies(t_game *game);
void draw_enemies(t_game *game);
void add_enemy(t_game *game, t_vector pos);
void draw_stripe_color(t_draw_params *p);
void draw_enemy_stripe(t_game *game, t_enemy *enemy,
t_sprite_data *s, int stripe);
void draw_enemy_sprite(t_game *game, t_enemy *enemy,
double screen_x, int sprite_height);
void calculate_ray_step(t_ray_data *ray, t_vector ray_dir);
t_ray_data init_ray_data(t_game *game, t_vector enemy_pos);
bool is_enemy_visible(t_game *game, t_vector enemy_pos);
void draw_enemy(t_game *game, t_enemy_list *current, double fov);
void draw_enemies(t_game *game);
void spawn_enemies_from_map(t_game *game);
void init_enemies(t_game *game);

#endif
27 changes: 27 additions & 0 deletions includes/health.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* health.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marsoare <marsoare@student.42porto.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/22 17:35:41 by marsoare #+# #+# */
/* Updated: 2025/01/25 10:48:54 by marsoare ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef HEALTH_H
# define HEALTH_H

typedef struct s_bar
{
int start_x;
int start_y;
int width;
int height;
int color;
} t_bar;

void draw_health_bar(t_game *game);

#endif
4 changes: 2 additions & 2 deletions includes/minimap.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
/* By: marsoare <marsoare@student.42porto.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/16 14:20:40 by marsoare #+# #+# */
/* Updated: 2025/01/16 14:20:40 by marsoare ### ########.fr */
/* Updated: 2025/01/22 17:41:47 by marsoare ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef MINIMAP_H
# define MINIMAP_H

# define MINIMAP_CELL_SIZE 12
# define MINIMAP_PADDING 20
# define MINIMAP_PADDING 40
# define PLAYER_DOT_SIZE 6
# define MINIMAP_VIEW_SIZE 11

Expand Down
27 changes: 27 additions & 0 deletions maps/valid/abbandoned_lab.cub
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
F 82,80,85
C 140,138,142

NO assets/texture/lab1.xpm
SO assets/texture/lab2.xpm
WE assets/texture/lab3.xpm
EA assets/texture/lab4.xpm


111111111111111 11111
1000S0000001111 1100011
111000011111001 100000001
1000000000000011111 100000000011
11111111111000001111100111111000000000001
10000000011000001110000000101000000000011111
1111D111111111011100000100D0D000000000000001
11110111111111011101000111111111111111111D11
110000001101010111000001 101
100000000000000011000N01111 101
100000000000000011010000001 101
110000011100010111110111111 101
1111D111111D1011111001 101
1000000000000000000001 101
1000000111010000000001111111111111111111101
100000100000100000000D000000000000000000001
1111111100011111111111111111111111111111111
11111
8 changes: 4 additions & 4 deletions maps/valid/test.cub
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ WE assets/texture/west.xpm
EA assets/texture/east.xpm

111111111111
1000000000001111
10100S0000000001
1000010000001111
100E00E000001111
1000000000000001
1000100000001111
100000001111
10010001
10000001
100W0001
11111111
16 changes: 8 additions & 8 deletions srcs/checker/flood_fill.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@
/* By: marsoare <marsoare@student.42porto.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/12 14:01:02 by marsoare #+# #+# */
/* Updated: 2025/01/12 17:56:15 by marsoare ### ########.fr */
/* Updated: 2025/01/25 10:52:21 by marsoare ### ########.fr */
/* */
/* ************************************************************************** */

#include <cub3d.h>

static bool recursive_flood_fill(char **grid, t_vector_i st, int h, int w)
{
int current_line_length;
int cline_length;

if (st.x < 0 || st.y < 0 || st.x >= h || st.y >= w
|| grid[st.x][st.y] == '1' || grid[st.x][st.y] == 'X')
return (true);
current_line_length = 0;
while (grid[st.x][current_line_length] != '\0')
current_line_length++;
if (st.y >= current_line_length)
cline_length = 0;
while (grid[st.x][cline_length] != '\0')
cline_length++;
if (st.y >= cline_length)
return (false);
if (grid[st.x][st.y] == ' ')
return (false);
if (grid[st.x][st.y] == '0' && (st.x == 0 || st.y == 0
|| st.x == h - 1 || st.y >= current_line_length - 1))
if ((grid[st.x][st.y] == '0' || grid[st.x][st.y] == 'E') &&
(st.x == 0 || st.y == 0 || st.x == h - 1 || st.y >= cline_length - 1))
return (false);
grid[st.x][st.y] = 'X';
if (!recursive_flood_fill(grid, (t_vector_i){st.x - 1, st.y}, h, w))
Expand Down
77 changes: 77 additions & 0 deletions srcs/engine/enemy/enemy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* enemy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marsoare <marsoare@student.42porto.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/22 19:40:02 by marsoare #+# #+# */
/* Updated: 2025/01/25 10:21:45 by marsoare ### ########.fr */
/* */
/* ************************************************************************** */

#include "cub3d.h"

double vector_length(t_vector v)
{
return (sqrt(v.x * v.x + v.y * v.y));
}

t_texture *load_enemy_texture(t_game *game)
{
t_texture *texture;
char *path;

path = "./assets/sprites/enemies/cacodemon00.xpm";
texture = texture_create(game, path);
if (!texture)
{
ft_printf("Error: Failed to load enemy texture: %s\n", path);
return (NULL);
}
return (texture);
}

void add_enemy(t_game *game, t_vector pos)
{
t_enemy_list *new;
t_enemy_list *temp;

new = (t_enemy_list *)malloc(sizeof(t_enemy_list));
if (!new)
return ;
new->enemy.pos = pos;
new->enemy.health = 100;
new->enemy.alive = true;
new->enemy.dir = vector_create(0, 0);
new->enemy.texture = load_enemy_texture(game);
new->next = NULL;
if (!game->enemies)
game->enemies = new;
else
{
temp = game->enemies;
while (temp->next)
temp = temp->next;
temp->next = new;
}
}

void calculate_enemy_distance(t_game *game, t_enemy *enemy)
{
enemy->dist_to_player = vector_dist(game->p1.pos, enemy->pos);
enemy->dir = vector_create(1, 0);
}

void update_enemies(t_game *game)
{
t_enemy_list *current;

current = game->enemies;
while (current)
{
if (current->enemy.alive)
calculate_enemy_distance(game, &current->enemy);
current = current->next;
}
}
Loading
Loading