Skip to content

Commit

Permalink
enemies can die, buddy lets gooooooooooo
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkosComK committed Jan 27, 2025
1 parent 4c49987 commit 3cfe114
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 31 deletions.
Binary file modified cub3D
Binary file not shown.
6 changes: 3 additions & 3 deletions maps/valid/test.cub
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ EA assets/texture/east.xpm
1000000000000001
1000000000000001
1000000000000001
10000000000000M1
10000000000010M1
1000000000001111
1000000000000001
10000000000000N1
1000000000000001
1000000000000001
1000000000000001
1000000000000001
100N000000000001
1111111111111111
4 changes: 0 additions & 4 deletions srcs/engine/enemy/enemy_dist.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,10 @@ void update_enemies(t_game *game)
if (current->enemy.dist_to_player <= current->enemy.detection_radius)
{
update_enemy_position(&current->enemy, game, speed);
printf("Updating alive enemy at (%d, %d).\n",
(int)current->enemy.pos.x, (int)current->enemy.pos.y);
}
}
else
{
printf("Skipping dead enemy at (%d, %d).\n",
(int)current->enemy.pos.x, (int)current->enemy.pos.y);
}
current = current->next;
}
Expand Down
63 changes: 44 additions & 19 deletions srcs/engine/enemy/enemy_shoot.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,65 @@

bool is_shot_hitting_enemy(t_game *game, t_enemy *enemy)
{
t_vector shot_pos = game->p1.pos; // Use player position as shot origin
double distance = vector_dist(shot_pos, enemy->pos);
t_vector to_enemy;
double angle;
double distance;
double shoot_range = 8.0; // Reasonable shooting range
double angle_threshold = 0.3; // About 17 degrees field of view for shots

// If the shot is close enough to the enemy, it's a hit
if (distance < 0.5) // HIT_RANGE can be a small value like 0.5
return (true);
return (false);
// Vector from player to enemy
to_enemy = vector_sub(enemy->pos, game->p1.pos);
distance = vector_length(to_enemy);

// Check if enemy is within shooting range
if (distance > shoot_range)
return (false);

// Calculate angle between player's direction and enemy
angle = atan2(to_enemy.y, to_enemy.x) -
atan2(game->p1.dir.y, game->p1.dir.x);

// Normalize angle to [-PI, PI]
while (angle > M_PI)
angle -= 2 * M_PI;
while (angle < -M_PI)
angle += 2 * M_PI;

// Check if enemy is within our shooting angle threshold
if (fabs(angle) > angle_threshold)
return (false);

// Finally, check if there's a wall between player and enemy
return (is_enemy_visible(game, enemy->pos));
}

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

current = game->enemies; // Start at the head of the linked list
current = game->enemies;
while (current != NULL)
{
if (current->enemy.alive &&
is_shot_hitting_enemy(game, &current->enemy)) // Check if shot hits enemy
is_shot_hitting_enemy(game, &current->enemy))
{
current->enemy.health--; // Decrement health
if (current->enemy.health <= 0) // Check if enemy should die

// Print hit message
printf("Enemy at (%d, %d) hit! Health: %d\n",
(int)current->enemy.pos.x, (int)current->enemy.pos.y,
current->enemy.health);

// Check if exactly 3 shots were taken (health = 0)
if (current->enemy.health <= 0)
{
current->enemy.alive = false; // Mark enemy as dead
printf("Enemy at (%d, %d) has been killed!\n",
current->enemy.alive = false;
current->enemy.health = 0; // Ensure health doesn't go negative
printf("Enemy at (%d, %d) killed after 3rd shot!\n",
(int)current->enemy.pos.x, (int)current->enemy.pos.y);
}
else
{
printf("Enemy at (%d, %d) has been hit! Health remaining: %d\n",
(int)current->enemy.pos.x, (int)current->enemy.pos.y,
current->enemy.health);
}
break; // Exit loop after hitting one enemy
return; // Exit after hitting one enemy
}
current = current->next; // Move to the next enemy in the list
current = current->next;
}
}
27 changes: 22 additions & 5 deletions srcs/engine/enemy/enemy_utils2.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: marsoare <marsoare@student.42porto.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/25 10:16:16 by marsoare #+# #+# */
/* Updated: 2025/01/26 17:49:55 by marsoare ### ########.fr */
/* Updated: 2025/01/26 17:56:19 by marsoare ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,6 +16,11 @@ bool is_enemy_visible(t_game *game, t_vector enemy_pos)
{
t_ray_data ray;

// Don't process if position is out of bounds
if (enemy_pos.x < 0 || enemy_pos.x >= game->map.width ||
enemy_pos.y < 0 || enemy_pos.y >= game->map.height)
return (false);

ray = init_ray_data(game, enemy_pos);
while (1)
{
Expand All @@ -29,9 +34,20 @@ bool is_enemy_visible(t_game *game, t_vector enemy_pos)
ray.side_dist.y += ray.delta_dist.y;
ray.map_y += ray.step.y;
}

// Check if we've reached the enemy position
if (ray.map_x == (int)enemy_pos.x && ray.map_y == (int)enemy_pos.y)
return (true);
if (game->map.grid[ray.map_x][ray.map_y] == '1')

// Check for wall collision (fixed array indexing)
if (ray.map_y >= 0 && ray.map_y < game->map.height &&
ray.map_x >= 0 && ray.map_x < game->map.width &&
game->map.grid[ray.map_y][ray.map_x] == '1')
return (false);

// Add bounds checking
if (ray.map_x < 0 || ray.map_x >= game->map.width ||
ray.map_y < 0 || ray.map_y >= game->map.height)
return (false);
}
}
Expand Down Expand Up @@ -61,13 +77,14 @@ void draw_enemies(t_game *game)

current = game->enemies;
fov = 2 * atan2(vector_length(game->p1.plane), 1.0);
while (current) // Iterate through all enemies
while (current)
{
if (current->enemy.alive && is_enemy_visible(game, current->enemy.pos)) // Only render alive and visible enemies
// Only draw if enemy is alive and visible
if (current->enemy.alive && is_enemy_visible(game, current->enemy.pos))
{
draw_enemy(game, current, fov);
}
current = current->next; // Move to the next enemy
current = current->next;
}
}

Expand Down

0 comments on commit 3cfe114

Please # to comment.