From 5f0c15b2d3299ea210a78d54e9b10c3cb4266139 Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 19 Apr 2022 23:48:20 +0200 Subject: Created factory to handle enemy creation --- CMakeLists.txt | 2 +- Enemy.cpp | 30 +++++++----------------------- Enemy.h | 5 ++--- EnemyFactory.cpp | 30 ++++++++++++++++++++++++++++++ EnemyFactory.h | 10 ++++++++++ Falling.cpp | 5 +++-- NullBehaviour.h | 12 ++++++++++++ NullGenerator.h | 9 +++++++++ Static.cpp | 14 -------------- Static.h | 12 ------------ TestStage.cpp | 23 +++++++---------------- 11 files changed, 81 insertions(+), 71 deletions(-) create mode 100644 EnemyFactory.cpp create mode 100644 EnemyFactory.h create mode 100644 NullBehaviour.h create mode 100644 NullGenerator.h delete mode 100644 Static.cpp delete mode 100644 Static.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d81491..f4c3228 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ add_executable( ${PROJECT_NAME} ConstantVelocity.cpp Enemy.cpp + EnemyFactory.cpp ExampleGenerator.cpp Falling.cpp FallingAndOscillating.cpp @@ -25,7 +26,6 @@ add_executable( main.cpp Oscillating.cpp Player.cpp - Static.cpp TestStage.cpp TitleScreen.cpp ) diff --git a/Enemy.cpp b/Enemy.cpp index 1be3c76..a8e87a3 100644 --- a/Enemy.cpp +++ b/Enemy.cpp @@ -7,14 +7,15 @@ #include "Behaviour.h" #include "Generator.h" -#include "Static.h" +#include "NullBehaviour.h" +#include "NullGenerator.h" -Enemy::Enemy(std::unique_ptr generator) : - Enemy( - std::make_shared(), - std::move(generator), - std::make_unique()) +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()} { } @@ -28,8 +29,6 @@ Enemy::Enemy( m_generator {std::move(generator)}, m_behaviour {std::move(behaviour)} { - m_position->x = 400; - m_position->y = 40; m_generator->attach(m_position); } @@ -53,18 +52,3 @@ Enemy::draw() return; DrawCircle(m_position->x, m_position->y, 6, DARKGRAY); } - - -void -Enemy::set_position(const float x, const float y) -{ - m_position->x = x; - m_position->y = y; -} - - -void -Enemy::set_hold(const float seconds) -{ - m_hold = seconds; -} diff --git a/Enemy.h b/Enemy.h index 30e4140..22fc4f7 100644 --- a/Enemy.h +++ b/Enemy.h @@ -11,15 +11,14 @@ class Enemy { public: - explicit Enemy(std::unique_ptr generator); + friend class EnemyFactory; + Enemy(); Enemy( std::shared_ptr position, std::unique_ptr generator, std::unique_ptr behaviour); void update(float dt); void draw(); - void set_position(float x, float y); - void set_hold(float seconds); private: float m_hold; std::shared_ptr m_position; diff --git a/EnemyFactory.cpp b/EnemyFactory.cpp new file mode 100644 index 0000000..b573228 --- /dev/null +++ b/EnemyFactory.cpp @@ -0,0 +1,30 @@ +#include "EnemyFactory.h" + +#include + +#include + +#include "ConstantVelocity.h" +#include "Enemy.h" +#include "ExampleGenerator.h" +#include "FallingAndOscillating.h" + + +Enemy +EnemyFactory::make_example( + ConstantVelocityBullet::Vector& bullets, + const float x, const float y, + const float hold, + const bool mirror) +{ + auto position = std::make_shared(Vector2{x, y}); + auto generator = std::make_unique(bullets); + auto behaviour = std::make_unique(); + if (mirror) { + generator->m_direction *= -1; + behaviour->set_phase(1.f); + } + Enemy enemy(position, std::move(generator), std::move(behaviour)); + enemy.m_hold = hold; + return enemy; +} diff --git a/EnemyFactory.h b/EnemyFactory.h new file mode 100644 index 0000000..0475a25 --- /dev/null +++ b/EnemyFactory.h @@ -0,0 +1,10 @@ +#pragma once + +#include "ConstantVelocity.h" +#include "Enemy.h" + + +struct EnemyFactory +{ + static Enemy make_example(ConstantVelocityBullet::Vector& bullets, float x, float y, float hold, bool mirror=false); +}; diff --git a/Falling.cpp b/Falling.cpp index 8547ee2..deb81a4 100644 --- a/Falling.cpp +++ b/Falling.cpp @@ -6,7 +6,7 @@ Falling::Falling() : - m_speed {30} + m_speed {40} { } @@ -14,6 +14,7 @@ Falling::Falling() : void Falling::update(const float dt, Vector2& position, Generator& generator) { - (void) generator; position.y += dt * m_speed; + if (position.y > 600) + generator.toggle(false); } diff --git a/NullBehaviour.h b/NullBehaviour.h new file mode 100644 index 0000000..c645a7e --- /dev/null +++ b/NullBehaviour.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +#include "Behaviour.h" +#include "Generator.h" + + +struct NullBehaviour : virtual public Behaviour +{ + void update(float, Vector2&, Generator&) override {} +}; diff --git a/NullGenerator.h b/NullGenerator.h new file mode 100644 index 0000000..7e92a1c --- /dev/null +++ b/NullGenerator.h @@ -0,0 +1,9 @@ +#pragma once + +#include "Generator.h" + + +struct NullGenerator : public Generator +{ + void update(float) override {} +}; diff --git a/Static.cpp b/Static.cpp deleted file mode 100644 index 28cadda..0000000 --- a/Static.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include "Static.h" - -#include - -#include "Generator.h" - - -void -Static::update(const float dt, Vector2& position, Generator& generator) -{ - (void) dt; - (void) position; - (void) generator; -} diff --git a/Static.h b/Static.h deleted file mode 100644 index a4e7489..0000000 --- a/Static.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include - -#include "Behaviour.h" -#include "Generator.h" - - -struct Static : virtual public Behaviour -{ - void update(float dt, Vector2& position, Generator& generator) override; -}; diff --git a/TestStage.cpp b/TestStage.cpp index e6406f8..d858fcb 100644 --- a/TestStage.cpp +++ b/TestStage.cpp @@ -5,28 +5,19 @@ #include -#include "ExampleGenerator.h" -#include "FallingAndOscillating.h" +#include "EnemyFactory.h" TestStage::TestStage() : m_player {}, m_const {} { - m_enemies.reserve(2); - for (const float x : {350.f, 450.f, 400.f}) { - auto position = std::make_shared(Vector2{x, 100.f}); - auto generator = std::make_unique(m_const.m_bullets); - auto behaviour = std::make_unique(); - if (x > 410.f) { - generator->m_direction *= -1; - behaviour->set_phase(1.f); - } - Enemy enemy(position, std::move(generator), std::move(behaviour)); - if (x > 390.f && x < 410.f) - enemy.set_hold(20.f); - m_enemies.push_back(std::move(enemy)); - } + m_enemies.reserve(5); + m_enemies.push_back(std::move(EnemyFactory::make_example(m_const.m_bullets, 300, -10, 0))); + m_enemies.push_back(std::move(EnemyFactory::make_example(m_const.m_bullets, 500, -10, 0, true))); + m_enemies.push_back(std::move(EnemyFactory::make_example(m_const.m_bullets, 400, -10, 10))); + m_enemies.push_back(std::move(EnemyFactory::make_example(m_const.m_bullets, 550, -10, 20))); + m_enemies.push_back(std::move(EnemyFactory::make_example(m_const.m_bullets, 250, -10, 20, true))); } -- cgit v1.1