From fd9232b3d3a3aee28a5965a5ebc4077f8db7c652 Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 20 Apr 2022 00:28:48 +0200 Subject: Streamlined enemy composition --- Behaviour.h | 6 +----- Enemy.cpp | 15 +++++++-------- Enemy.h | 8 ++++---- EnemyFactory.cpp | 10 ++++++---- ExampleGenerator.cpp | 9 +++++---- ExampleGenerator.h | 10 ++++++++-- Falling.cpp | 16 ++++++++++------ Falling.h | 8 ++++++-- FallingAndOscillating.cpp | 15 ++++++++++++--- FallingAndOscillating.h | 5 ++++- Generator.cpp | 39 +-------------------------------------- Generator.h | 13 +------------ NullBehaviour.h | 5 +---- Oscillating.cpp | 20 ++++++-------------- Oscillating.h | 10 ++++++---- TestStage.cpp | 3 --- 16 files changed, 78 insertions(+), 114 deletions(-) diff --git a/Behaviour.h b/Behaviour.h index 07751ce..a06ed50 100644 --- a/Behaviour.h +++ b/Behaviour.h @@ -1,12 +1,8 @@ #pragma once -#include - -#include "Generator.h" - struct Behaviour { virtual ~Behaviour() = default; - virtual void update(float dt, Vector2& position, Generator& generator) = 0; + virtual void update(float dt) = 0; }; diff --git a/Enemy.cpp b/Enemy.cpp index a8e87a3..083c890 100644 --- a/Enemy.cpp +++ b/Enemy.cpp @@ -14,22 +14,21 @@ Enemy::Enemy() : m_hold {0}, m_position {std::make_shared(Vector2{400.f, 300.f})}, - m_generator {std::make_unique()}, - m_behaviour {std::make_unique()} + m_generator {std::make_shared()}, + m_behaviour {std::make_shared()} { } Enemy::Enemy( std::shared_ptr position, - std::unique_ptr generator, - std::unique_ptr behaviour) : + std::shared_ptr generator, + std::shared_ptr behaviour) : m_hold {0}, m_position {position}, - m_generator {std::move(generator)}, - m_behaviour {std::move(behaviour)} + m_generator {generator}, + m_behaviour {behaviour} { - m_generator->attach(m_position); } @@ -40,7 +39,7 @@ Enemy::update(const float dt) m_hold -= dt; return; } - m_behaviour->update(dt, *m_position, *m_generator); + m_behaviour->update(dt); m_generator->update(dt); } diff --git a/Enemy.h b/Enemy.h index 22fc4f7..4305026 100644 --- a/Enemy.h +++ b/Enemy.h @@ -15,13 +15,13 @@ public: Enemy(); Enemy( std::shared_ptr position, - std::unique_ptr generator, - std::unique_ptr behaviour); + std::shared_ptr generator, + std::shared_ptr behaviour); void update(float dt); void draw(); private: float m_hold; std::shared_ptr m_position; - std::unique_ptr m_generator; - std::unique_ptr m_behaviour; + std::shared_ptr m_generator; + std::shared_ptr m_behaviour; }; diff --git a/EnemyFactory.cpp b/EnemyFactory.cpp index b573228..8a51c3f 100644 --- a/EnemyFactory.cpp +++ b/EnemyFactory.cpp @@ -18,13 +18,15 @@ EnemyFactory::make_example( const bool mirror) { auto position = std::make_shared(Vector2{x, y}); - auto generator = std::make_unique(bullets); - auto behaviour = std::make_unique(); + auto generator = std::make_shared(position, bullets); + auto behaviour = std::make_shared(position, generator); + generator->m_position = position; if (mirror) { + generator->m_color = Color{100, 0, 255, 255}; generator->m_direction *= -1; - behaviour->set_phase(1.f); + behaviour->m_phase = 1.f; } - Enemy enemy(position, std::move(generator), std::move(behaviour)); + Enemy enemy(position, generator, behaviour); enemy.m_hold = hold; return enemy; } diff --git a/ExampleGenerator.cpp b/ExampleGenerator.cpp index 5bdade8..f405f58 100644 --- a/ExampleGenerator.cpp +++ b/ExampleGenerator.cpp @@ -1,6 +1,7 @@ #include "ExampleGenerator.h" #include +#include #include #include @@ -8,7 +9,8 @@ #include "ConstantVelocity.h" -ExampleGenerator::ExampleGenerator(ConstantVelocityBullet::Vector& bullets) : +ExampleGenerator::ExampleGenerator(std::shared_ptr position, ConstantVelocityBullet::Vector& bullets) : + m_position {position}, m_bullets {bullets}, m_delay {0}, m_interval {0.08f}, @@ -30,7 +32,6 @@ ExampleGenerator::update(const float dt) return; m_delay += dt; if (m_delay > m_interval) { - const auto pos = position(); m_delay -= m_interval; m_color.g = 0; for (float i = 0; i <= m_segments; ++i) { @@ -42,8 +43,8 @@ ExampleGenerator::update(const float dt) bullet.radius = 3; bullet.velocity.x = cos * m_speed; bullet.velocity.y = sin * m_speed; - bullet.position.x = pos.x + cos * m_shift; - bullet.position.y = pos.y + sin * m_shift; + bullet.position.x = m_position->x + cos * m_shift; + bullet.position.y = m_position->y + sin * m_shift; m_bullets.push_back(std::move(bullet)); m_color.g += 20; } diff --git a/ExampleGenerator.h b/ExampleGenerator.h index 2710d03..6db4151 100644 --- a/ExampleGenerator.h +++ b/ExampleGenerator.h @@ -1,15 +1,21 @@ #pragma once +#include + #include #include "ConstantVelocity.h" #include "Generator.h" -struct ExampleGenerator : public Generator +class ExampleGenerator : public Generator { - explicit ExampleGenerator(ConstantVelocityBullet::Vector& bullets); +public: + friend class EnemyFactory; + ExampleGenerator(std::shared_ptr position, ConstantVelocityBullet::Vector& bullets); void update(float dt) override; +private: + std::shared_ptr m_position; ConstantVelocityBullet::Vector& m_bullets; float m_delay; float m_interval; diff --git a/Falling.cpp b/Falling.cpp index deb81a4..38c05d4 100644 --- a/Falling.cpp +++ b/Falling.cpp @@ -1,20 +1,24 @@ #include "Falling.h" +#include + #include #include "Generator.h" -Falling::Falling() : - m_speed {40} +Falling::Falling(std::shared_ptr position, std::shared_ptr generator) : + m_speed {40}, + m_position {position}, + m_generator {generator} { } void -Falling::update(const float dt, Vector2& position, Generator& generator) +Falling::update(const float dt) { - position.y += dt * m_speed; - if (position.y > 600) - generator.toggle(false); + m_position->y += dt * m_speed; + if (m_generator->m_enabled && m_position->y > 620) + m_generator->m_enabled = false; } diff --git a/Falling.h b/Falling.h index b629d6d..b50228a 100644 --- a/Falling.h +++ b/Falling.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include "Behaviour.h" @@ -9,8 +11,10 @@ class Falling : virtual public Behaviour { public: - Falling(); - void update(float dt, Vector2& position, Generator& generator) override; + Falling(std::shared_ptr position, std::shared_ptr generator); + void update(float dt) override; private: float m_speed; + std::shared_ptr m_position; + std::shared_ptr m_generator; }; diff --git a/FallingAndOscillating.cpp b/FallingAndOscillating.cpp index d3de3c5..366b26b 100644 --- a/FallingAndOscillating.cpp +++ b/FallingAndOscillating.cpp @@ -1,5 +1,7 @@ #include "FallingAndOscillating.h" +#include + #include #include "Falling.h" @@ -7,9 +9,16 @@ #include "Oscillating.h" +FallingAndOscillating::FallingAndOscillating(std::shared_ptr position, std::shared_ptr generator) : + Falling {position, generator}, + Oscillating {position} +{ +} + + void -FallingAndOscillating::update(const float dt, Vector2& position, Generator& generator) +FallingAndOscillating::update(const float dt) { - Falling::update(dt, position, generator); - Oscillating::update(dt, position, generator); + Falling::update(dt); + Oscillating::update(dt); } diff --git a/FallingAndOscillating.h b/FallingAndOscillating.h index 7a32c27..73fd602 100644 --- a/FallingAndOscillating.h +++ b/FallingAndOscillating.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include "Falling.h" @@ -9,5 +11,6 @@ struct FallingAndOscillating : public Falling, Oscillating { - void update(float dt, Vector2& position, Generator& generator) override; + FallingAndOscillating(std::shared_ptr position, std::shared_ptr generator); + void update(float dt) override; }; diff --git a/Generator.cpp b/Generator.cpp index 448872d..7ca3210 100644 --- a/Generator.cpp +++ b/Generator.cpp @@ -1,44 +1,7 @@ #include "Generator.h" -#include -#include - -#include - Generator::Generator() : - m_enabled {true}, - m_origin {} -{ -} - - -void -Generator::toggle(const bool enabled) -{ - m_enabled = enabled; -} - - -void -Generator::attach(std::shared_ptr origin) -{ - m_origin = std::move(origin); -} - - -void -Generator::detach() -{ - m_origin = {}; -} - - -Vector2 -Generator::position() const + m_enabled {true} { - if (m_origin) - return *m_origin; - else - return {400, 40}; } diff --git a/Generator.h b/Generator.h index de3812a..aeff98b 100644 --- a/Generator.h +++ b/Generator.h @@ -1,21 +1,10 @@ #pragma once -#include -#include - - -class Generator +struct Generator { -public: Generator(); virtual ~Generator() = default; virtual void update(float dt) = 0; - void toggle(bool enabled); - void attach(std::shared_ptr origin); - void detach(); - Vector2 position() const; -protected: bool m_enabled; - std::shared_ptr m_origin; }; diff --git a/NullBehaviour.h b/NullBehaviour.h index c645a7e..fe2b49d 100644 --- a/NullBehaviour.h +++ b/NullBehaviour.h @@ -1,12 +1,9 @@ #pragma once -#include - #include "Behaviour.h" -#include "Generator.h" struct NullBehaviour : virtual public Behaviour { - void update(float, Vector2&, Generator&) override {} + void update(float) override {} }; diff --git a/Oscillating.cpp b/Oscillating.cpp index 4839282..eef33df 100644 --- a/Oscillating.cpp +++ b/Oscillating.cpp @@ -1,33 +1,25 @@ #include "Oscillating.h" #include +#include #include -#include "Generator.h" - -Oscillating::Oscillating() : +Oscillating::Oscillating(std::shared_ptr position) : m_phase {0}, - m_shift {1.6f} + m_shift {1.6f}, + m_position {position} { } void -Oscillating::update(const float dt, Vector2& position, Generator& generator) +Oscillating::update(const float dt) { - (void) generator; m_phase += dt * 0.8f; if (m_phase > 2.f) m_phase -= 2.f; const float cos = std::cos(m_phase * M_PI); - position.x += cos * m_shift; -} - - -void -Oscillating::set_phase(const float phase) -{ - m_phase = phase; + m_position->x += cos * m_shift; } diff --git a/Oscillating.h b/Oscillating.h index d596e0f..82b5641 100644 --- a/Oscillating.h +++ b/Oscillating.h @@ -1,18 +1,20 @@ #pragma once +#include + #include #include "Behaviour.h" -#include "Generator.h" class Oscillating : virtual public Behaviour { public: - Oscillating(); - void update(float dt, Vector2& position, Generator& generator) override; - void set_phase(float phase); + friend class EnemyFactory; + explicit Oscillating(std::shared_ptr position); + void update(float dt) override; private: float m_phase; float m_shift; + std::shared_ptr m_position; }; diff --git a/TestStage.cpp b/TestStage.cpp index d858fcb..13cfb65 100644 --- a/TestStage.cpp +++ b/TestStage.cpp @@ -1,10 +1,7 @@ #include "TestStage.h" -#include #include -#include - #include "EnemyFactory.h" -- cgit v1.1