From ad60e242e2d432b436de79ffeb76637e62b057d8 Mon Sep 17 00:00:00 2001 From: Arnold <27396817+arngo@users.noreply.github.com> Date: Sun, 28 May 2023 12:56:19 -0400 Subject: implement random enemy spawning --- src/enemies.c | 32 ++++++++++++++++++++++++++++++++ src/enemies.h | 10 ++++++++++ src/main.c | 7 ++++--- src/wall.c | 15 +++++++++++++++ src/wall.h | 6 ++++++ 5 files changed, 67 insertions(+), 3 deletions(-) diff --git a/src/enemies.c b/src/enemies.c index 19e1c3e..ad09e68 100644 --- a/src/enemies.c +++ b/src/enemies.c @@ -3,10 +3,12 @@ #include "cglm/vec2.h" #include "player.h" #include "bullet.h" +#include "wall.h" static rodeo_collision_2d_world_t collision_enemies_world = {0}; static rodeo_texture_2d_t enemy_texture; static cvec_enemy_t enemies = {0}; +static float spawn_cooldown = 0; void init_enemies(void) @@ -253,3 +255,33 @@ group_follow_target(rodeo_collision_2d_world_item_t *target) enemy->dy = result[1]; } } + +enemy_t* +random_enemy_create( + rodeo_rectangle_t bounds +) +{ + float spawn_coords[2]; + for (int i = 0; i < 100; ++i) { + spawn_coords[0] = (float)rodeo_random_double_get() * bounds.width + bounds.x; + spawn_coords[1] = (float)rodeo_random_double_get() * bounds.height + bounds.y; + if (!coords_inside_wall(spawn_coords[0], spawn_coords[1])) { + return spawn_enemy(spawn_coords[0], spawn_coords[1]); + } + } + rodeo_log(rodeo_logLevel_info, "failed to spawn enemy"); + return NULL; +} + +enemy_t* +attempt_random_enemy_spawn( + rodeo_rectangle_t bounds +) +{ + spawn_cooldown -= rodeo_frame_time_get(); + if (spawn_cooldown <= 0) { + spawn_cooldown += (float)rodeo_random_double_get() * 5000.0f + 1500.0f; + return random_enemy_create(bounds); + } + return NULL; +} diff --git a/src/enemies.h b/src/enemies.h index 29a147a..dc6f721 100644 --- a/src/enemies.h +++ b/src/enemies.h @@ -82,3 +82,13 @@ detect_bullet_enemy_collisions(void); void group_follow_target(rodeo_collision_2d_world_item_t *target); + +enemy_t* +random_enemy_create( + rodeo_rectangle_t bounds +); + +enemy_t* +attempt_random_enemy_spawn( + rodeo_rectangle_t bounds +); diff --git a/src/main.c b/src/main.c index ec0d3a5..a8783f5 100644 --- a/src/main.c +++ b/src/main.c @@ -190,6 +190,7 @@ main_loop(void) */ enemies_attempt_weapon_fire(); + attempt_random_enemy_spawn((rodeo_rectangle_t){ 0, 0, window_width, window_height }); move_bullets(); move_enemies(); @@ -276,10 +277,10 @@ main(void) init_player(); init_enemies(); init_wall(); - spawn_enemy(240, 240); + //spawn_enemy(240, 240); //spawn_enemy(100, 100); - spawn_enemy(300, 100); - spawn_enemy(200, 330); + //spawn_enemy(300, 100); + //spawn_enemy(200, 330); new_wall(0, -10, window_width, 10); new_wall(0, window_height, window_width, 10); new_wall(-10, 0, 10, window_height); diff --git a/src/wall.c b/src/wall.c index d2b1319..5a2d6df 100644 --- a/src/wall.c +++ b/src/wall.c @@ -38,3 +38,18 @@ new_wall( .height = height }); } + +bool +coords_inside_wall( + float x, + float y +) +{ + c_foreach(i, cvec_collision_2d_world_item, collision_wall_world) { + if (x >= i.ref->x && x <= i.ref->x + i.ref->width && + y >= i.ref->y && y <= i.ref->y + i.ref->height) { + return true; + } + } + return false; +} diff --git a/src/wall.h b/src/wall.h index 7b0f9fe..6e3dc48 100644 --- a/src/wall.h +++ b/src/wall.h @@ -16,3 +16,9 @@ new_wall( float width, float height ); + +bool +coords_inside_wall( + float x, + float y +); -- cgit v1.2.3