From 7e1081ff8f1297486e177bd716fecebb42c0182a Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 18 Apr 2022 10:36:19 +0200 Subject: Added base class for generators --- Bullets.h | 1 + ExampleGenerator.cpp | 8 ++++---- ExampleGenerator.h | 10 +++++----- GameScreen.cpp | 5 +++-- GameScreen.h | 2 +- Generator.h | 8 ++++++++ 6 files changed, 22 insertions(+), 12 deletions(-) create mode 100644 Generator.h diff --git a/Bullets.h b/Bullets.h index 4a77100..3970aff 100644 --- a/Bullets.h +++ b/Bullets.h @@ -23,6 +23,7 @@ struct ConstantVelocitySystem struct ConstantVelocityBullet { + using Vector = std::vector; Vector2 position; Vector2 velocity; float radius; diff --git a/ExampleGenerator.cpp b/ExampleGenerator.cpp index 7f4d41b..bcead95 100644 --- a/ExampleGenerator.cpp +++ b/ExampleGenerator.cpp @@ -1,7 +1,6 @@ #include "ExampleGenerator.h" #include -#include #include #include @@ -9,7 +8,8 @@ #include "Bullets.h" -ExampleGenerator::ExampleGenerator() : +ExampleGenerator::ExampleGenerator(ConstantVelocityBullet::Vector& bullets) : + m_bullets {bullets}, m_delay {0}, m_interval {0.2f}, m_cone {0.3f}, @@ -22,7 +22,7 @@ ExampleGenerator::ExampleGenerator() : void -ExampleGenerator::update(const float dt, std::vector& bullets) +ExampleGenerator::update(const float dt) { m_delay += dt; if (m_delay > m_interval) { @@ -38,7 +38,7 @@ ExampleGenerator::update(const float dt, std::vector& bu bullet.velocity.y = sin * m_speed; bullet.position.x = m_origin.x + cos * m_shift; bullet.position.y = m_origin.y + sin * m_shift; - bullets.push_back(std::move(bullet)); + m_bullets.push_back(std::move(bullet)); } m_segments += 3; if (m_segments > 20) diff --git a/ExampleGenerator.h b/ExampleGenerator.h index 0e3372b..25c4ce3 100644 --- a/ExampleGenerator.h +++ b/ExampleGenerator.h @@ -1,16 +1,16 @@ #pragma once -#include - #include #include "Bullets.h" +#include "Generator.h" -struct ExampleGenerator +struct ExampleGenerator : public Generator { - ExampleGenerator(); - void update(float dt, std::vector& bullets); + explicit ExampleGenerator(ConstantVelocityBullet::Vector& bullets); + void update(float dt) override; + ConstantVelocityBullet::Vector& m_bullets; float m_delay; float m_interval; float m_cone; diff --git a/GameScreen.cpp b/GameScreen.cpp index 4a2fc22..774956e 100644 --- a/GameScreen.cpp +++ b/GameScreen.cpp @@ -4,7 +4,8 @@ GameScreen::GameScreen() : - m_const {} + m_const {}, + m_generator {m_const.m_bullets} { } @@ -13,7 +14,7 @@ void GameScreen::update(const float dt) { m_player.update(dt); - m_generator.update(dt, m_const.m_bullets); + m_generator.update(dt); m_const.update(dt); bool collided = false; for (const auto& bullet : m_const.m_bullets) { diff --git a/GameScreen.h b/GameScreen.h index 2b5152f..144bd82 100644 --- a/GameScreen.h +++ b/GameScreen.h @@ -16,6 +16,6 @@ public: void draw() override; private: Player m_player; - ExampleGenerator m_generator; ConstantVelocitySystem m_const; + ExampleGenerator m_generator; }; diff --git a/Generator.h b/Generator.h new file mode 100644 index 0000000..5beef2c --- /dev/null +++ b/Generator.h @@ -0,0 +1,8 @@ +#pragma once + + +struct Generator +{ + virtual ~Generator() = default; + virtual void update(float dt) = 0; +}; -- cgit v1.1