diff --git a/CMakeLists.txt b/CMakeLists.txt index 2643aae..0f3e533 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,7 @@ FetchContent_Declare( FetchContent_MakeAvailable(raylib) add_executable(isaac__ main.cpp - types.h) + types.h +) target_link_libraries(${PROJECT_NAME} PUBLIC raylib) target_include_directories(${PROJECT_NAME} PUBLIC ${raylib_public_headers}) \ No newline at end of file diff --git a/main.cpp b/main.cpp index 4fe780f..ccd0808 100644 --- a/main.cpp +++ b/main.cpp @@ -1,4 +1,6 @@ +#include #include +#include #include #include @@ -20,6 +22,12 @@ float tear_radius = 10.0f; double last_fired = 0; 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] = {}; Vector2 Player::center() const noexcept { @@ -76,7 +84,34 @@ struct EnemySpawner { const auto start = std::chrono::steady_clock::now(); if (!paused.load()) { - std::print("Running at {}\n", start.time_since_epoch().count()); + std::lock_guard lock(enemy_mutex); + const auto num = static_cast(rng()) / static_cast(std::mt19937::max()); + const auto dir = static_cast(rng()) / static_cast(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(screen_width) / 2, .y = 0}; + moving = {.x = (dir - 1) * 2, .y = 1}; + } else if (0.25 <= num && num < 0.5) { + center = {.x = static_cast(screen_width) / 2, .y = static_cast(screen_height)}; + moving = {.x = (dir - 1) * 2, .y = -1}; + } else if (0.5 <= num && num < 0.75) { + center = {.x = 0, .y = static_cast(screen_height) / 2}; + moving = {.x = 1, .y = (dir - 1) * 2}; + } else { + center = {.x = static_cast(screen_width), .y = static_cast(screen_height) / 2}; + moving = {.x = -1, .y = (dir - 1) * 2}; + } + + const auto speed_ratio = static_cast(rng()) / static_cast(std::mt19937::max()); + const auto speed = speed_ratio * enemy_max_speed; + moving = Vector2Scale(moving, speed); + + break; + } } auto next = start + std::chrono::seconds(rate_secs); @@ -140,7 +175,7 @@ int main() { InitWindow(800, 600, "Isaac++"); SetTargetFPS(60); - EnemySpawner spawner{10}; + EnemySpawner spawner{3}; spawner.start(); player.x = 10; player.y = 10; @@ -184,10 +219,8 @@ int main() { update_tears(); - BeginDrawing(); ClearBackground(RAYWHITE); - DrawText("Hello", 100, 100, 20, RED); DrawRectangleRec(player, BLUE); for (const auto wall: walls) { @@ -196,7 +229,14 @@ int main() { for (const auto tear: tears) { if (tear.active) { - DrawCircle(static_cast(tear.center.x), static_cast(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); } }