summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorArnold <[email protected]>2023-05-28 12:56:19 -0400
committerGitHub <[email protected]>2023-05-28 12:56:19 -0400
commitad60e242e2d432b436de79ffeb76637e62b057d8 (patch)
treedb7e76c4e12c21c85de45e2e948419c5d2d8851c /src
parentfed9c87269db74d70cd5262804873bc875543abe (diff)
downloadTOJam2023-ad60e242e2d432b436de79ffeb76637e62b057d8.tar.gz
TOJam2023-ad60e242e2d432b436de79ffeb76637e62b057d8.zip
implement random enemy spawning
Diffstat (limited to 'src')
-rw-r--r--src/enemies.c32
-rw-r--r--src/enemies.h10
-rw-r--r--src/main.c7
-rw-r--r--src/wall.c15
-rw-r--r--src/wall.h6
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
+);