added enemy spawns and movement

This commit is contained in:
2025-11-30 18:56:55 -05:00
parent 6694d4ece9
commit e191e1b508
2 changed files with 47 additions and 6 deletions

View File

@@ -16,6 +16,7 @@ FetchContent_Declare(
FetchContent_MakeAvailable(raylib) FetchContent_MakeAvailable(raylib)
add_executable(isaac__ main.cpp add_executable(isaac__ main.cpp
types.h) types.h
)
target_link_libraries(${PROJECT_NAME} PUBLIC raylib) target_link_libraries(${PROJECT_NAME} PUBLIC raylib)
target_include_directories(${PROJECT_NAME} PUBLIC ${raylib_public_headers}) target_include_directories(${PROJECT_NAME} PUBLIC ${raylib_public_headers})

View File

@@ -1,4 +1,6 @@
#include <mutex>
#include <print> #include <print>
#include <random>
#include <raylib.h> #include <raylib.h>
#include <thread> #include <thread>
@@ -20,6 +22,12 @@ float tear_radius = 10.0f;
double last_fired = 0; double last_fired = 0;
double fire_rate = .5; double fire_rate = .5;
std::random_device dev;
std::mt19937 rng(dev());
std::mutex enemy_mutex;
float enemy_max_speed = 5.0;
float enemy_radius = 50.0;
Enemy enemies[100] = {}; Enemy enemies[100] = {};
Vector2 Player::center() const noexcept { Vector2 Player::center() const noexcept {
@@ -76,7 +84,34 @@ struct EnemySpawner {
const auto start = std::chrono::steady_clock::now(); const auto start = std::chrono::steady_clock::now();
if (!paused.load()) { if (!paused.load()) {
std::print("Running at {}\n", start.time_since_epoch().count()); std::lock_guard lock(enemy_mutex);
const auto num = static_cast<float>(rng()) / static_cast<float>(std::mt19937::max());
const auto dir = static_cast<float>(rng()) / static_cast<float>(std::mt19937::max());
for (auto &[center, radius, moving, alive]: enemies) {
if (alive) continue;
alive = true;
radius = enemy_radius;
if (num < 0.25) {
center = {.x = static_cast<float>(screen_width) / 2, .y = 0};
moving = {.x = (dir - 1) * 2, .y = 1};
} else if (0.25 <= num && num < 0.5) {
center = {.x = static_cast<float>(screen_width) / 2, .y = static_cast<float>(screen_height)};
moving = {.x = (dir - 1) * 2, .y = -1};
} else if (0.5 <= num && num < 0.75) {
center = {.x = 0, .y = static_cast<float>(screen_height) / 2};
moving = {.x = 1, .y = (dir - 1) * 2};
} else {
center = {.x = static_cast<float>(screen_width), .y = static_cast<float>(screen_height) / 2};
moving = {.x = -1, .y = (dir - 1) * 2};
}
const auto speed_ratio = static_cast<float>(rng()) / static_cast<float>(std::mt19937::max());
const auto speed = speed_ratio * enemy_max_speed;
moving = Vector2Scale(moving, speed);
break;
}
} }
auto next = start + std::chrono::seconds(rate_secs); auto next = start + std::chrono::seconds(rate_secs);
@@ -140,7 +175,7 @@ int main() {
InitWindow(800, 600, "Isaac++"); InitWindow(800, 600, "Isaac++");
SetTargetFPS(60); SetTargetFPS(60);
EnemySpawner spawner{10}; EnemySpawner spawner{3};
spawner.start(); spawner.start();
player.x = 10; player.x = 10;
player.y = 10; player.y = 10;
@@ -184,10 +219,8 @@ int main() {
update_tears(); update_tears();
BeginDrawing(); BeginDrawing();
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
DrawText("Hello", 100, 100, 20, RED);
DrawRectangleRec(player, BLUE); DrawRectangleRec(player, BLUE);
for (const auto wall: walls) { for (const auto wall: walls) {
@@ -196,7 +229,14 @@ int main() {
for (const auto tear: tears) { for (const auto tear: tears) {
if (tear.active) { if (tear.active) {
DrawCircle(static_cast<int>(tear.center.x), static_cast<int>(tear.center.y), tear_radius, SKYBLUE); DrawCircleV(tear.center, tear_radius, SKYBLUE);
}
}
for (auto& enemy: enemies) {
if (enemy.alive) {
enemy.center = Vector2Add(enemy.center, enemy.moving);
DrawCircleV(enemy.center, enemy.radius, RED);
} }
} }