summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-04-19 23:48:20 +0200
committerAki <please@ignore.pl>2022-04-19 23:48:20 +0200
commit5f0c15b2d3299ea210a78d54e9b10c3cb4266139 (patch)
tree137190dde6dcab1951301f1a2ac1db8481878f5a
parent78097b5496209b56cef9d7fc8d6c13e4c87e0eb1 (diff)
downloadbullethell2022-5f0c15b2d3299ea210a78d54e9b10c3cb4266139.zip
bullethell2022-5f0c15b2d3299ea210a78d54e9b10c3cb4266139.tar.gz
bullethell2022-5f0c15b2d3299ea210a78d54e9b10c3cb4266139.tar.bz2
Created factory to handle enemy creation
-rw-r--r--CMakeLists.txt2
-rw-r--r--Enemy.cpp30
-rw-r--r--Enemy.h5
-rw-r--r--EnemyFactory.cpp30
-rw-r--r--EnemyFactory.h10
-rw-r--r--Falling.cpp5
-rw-r--r--NullBehaviour.h12
-rw-r--r--NullGenerator.h9
-rw-r--r--Static.cpp14
-rw-r--r--Static.h12
-rw-r--r--TestStage.cpp23
11 files changed, 81 insertions, 71 deletions
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> generator) :
- Enemy(
- std::make_shared<Vector2>(),
- std::move(generator),
- std::make_unique<Static>())
+Enemy::Enemy() :
+ m_hold {0},
+ m_position {std::make_shared<Vector2>(Vector2{400.f, 300.f})},
+ m_generator {std::make_unique<NullGenerator>()},
+ m_behaviour {std::make_unique<NullBehaviour>()}
{
}
@@ -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> generator);
+ friend class EnemyFactory;
+ Enemy();
Enemy(
std::shared_ptr<Vector2> position,
std::unique_ptr<Generator> generator,
std::unique_ptr<Behaviour> 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<Vector2> 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 <memory>
+
+#include <raylib.h>
+
+#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>(Vector2{x, y});
+ auto generator = std::make_unique<ExampleGenerator>(bullets);
+ auto behaviour = std::make_unique<FallingAndOscillating>();
+ 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 <raylib.h>
+
+#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 <raylib.h>
-
-#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 <raylib.h>
-
-#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 <raylib.h>
-#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>(Vector2{x, 100.f});
- auto generator = std::make_unique<ExampleGenerator>(m_const.m_bullets);
- auto behaviour = std::make_unique<FallingAndOscillating>();
- 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)));
}