From 756df27fd6bd58a4938537dcaa8b7a12bef48c63 Mon Sep 17 00:00:00 2001 From: Karn Kaul Date: Wed, 26 Jun 2024 00:32:59 +0530 Subject: [PATCH] Make `SpawnTimer` polymorphic, store vector. --- src/spaced/spaced/scenes/endless.cpp | 9 +++++++-- src/spaced/spaced/scenes/endless.hpp | 13 +++++++++---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/spaced/spaced/scenes/endless.cpp b/src/spaced/spaced/scenes/endless.cpp index 808fda0..3281fcd 100644 --- a/src/spaced/spaced/scenes/endless.cpp +++ b/src/spaced/spaced/scenes/endless.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -9,10 +10,12 @@ using bave::random_in_range; using bave::Seconds; using bave::Services; -EndlessScene::EndlessScene(App& app, Services const& services) : GameScene(app, services), m_creeps{.services = &services} { +EndlessScene::EndlessScene(App& app, Services const& services) : GameScene(app, services) { m_on_player_scored = services.get().player_scored.connect([this](Enemy const& e) { if (random_in_range(0, 10) < 3) { debug_spawn_powerup(e.get_position()); } }); + + m_spawn_timers.push_back(std::make_unique>(&services)); } void EndlessScene::tick(Seconds const dt) { @@ -20,7 +23,9 @@ void EndlessScene::tick(Seconds const dt) { if (is_game_over()) { return; } - if (auto creep = m_creeps.tick(dt)) { push_enemy(std::move(creep)); } + for (auto const& spawn_timer : m_spawn_timers) { + if (auto enemy = spawn_timer->tick(dt)) { push_enemy(std::move(enemy)); } + } } void EndlessScene::debug_spawn_powerup(glm::vec2 const position) { diff --git a/src/spaced/spaced/scenes/endless.hpp b/src/spaced/spaced/scenes/endless.hpp index a262866..e6a63d1 100644 --- a/src/spaced/spaced/scenes/endless.hpp +++ b/src/spaced/spaced/scenes/endless.hpp @@ -1,5 +1,4 @@ #pragma once -#include #include #include @@ -9,13 +8,19 @@ class EndlessScene : public GameScene { EndlessScene(bave::App& app, bave::Services const& services); private: + struct ISpawnTimer : bave::Polymorphic { + virtual auto tick(bave::Seconds dt) -> std::unique_ptr = 0; + }; + template T> - struct SpawnTimer { + struct SpawnTimer : ISpawnTimer { bave::NotNull services; bave::Seconds spawn_rate{1s}; bave::Seconds elapsed{}; - auto tick(bave::Seconds const dt) -> std::unique_ptr { + explicit SpawnTimer(bave::NotNull services) : services(services) {} + + auto tick(bave::Seconds const dt) -> std::unique_ptr final { auto ret = std::unique_ptr{}; elapsed += dt; if (elapsed >= spawn_rate) { @@ -32,6 +37,6 @@ class EndlessScene : public GameScene { SignalHandle m_on_player_scored{}; - SpawnTimer m_creeps; + std::vector> m_spawn_timers{}; }; } // namespace spaced