summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-04-20 00:28:48 +0200
committerAki <please@ignore.pl>2022-04-20 00:28:48 +0200
commitfd9232b3d3a3aee28a5965a5ebc4077f8db7c652 (patch)
treee9bdb832ed15704f5640ffc9543131f02e6e6e32
parent5f0c15b2d3299ea210a78d54e9b10c3cb4266139 (diff)
downloadbullethell2022-fd9232b3d3a3aee28a5965a5ebc4077f8db7c652.zip
bullethell2022-fd9232b3d3a3aee28a5965a5ebc4077f8db7c652.tar.gz
bullethell2022-fd9232b3d3a3aee28a5965a5ebc4077f8db7c652.tar.bz2
Streamlined enemy composition
-rw-r--r--Behaviour.h6
-rw-r--r--Enemy.cpp15
-rw-r--r--Enemy.h8
-rw-r--r--EnemyFactory.cpp10
-rw-r--r--ExampleGenerator.cpp9
-rw-r--r--ExampleGenerator.h10
-rw-r--r--Falling.cpp16
-rw-r--r--Falling.h8
-rw-r--r--FallingAndOscillating.cpp15
-rw-r--r--FallingAndOscillating.h5
-rw-r--r--Generator.cpp39
-rw-r--r--Generator.h13
-rw-r--r--NullBehaviour.h5
-rw-r--r--Oscillating.cpp20
-rw-r--r--Oscillating.h10
-rw-r--r--TestStage.cpp3
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 <raylib.h>
-
-#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>(Vector2{400.f, 300.f})},
- m_generator {std::make_unique<NullGenerator>()},
- m_behaviour {std::make_unique<NullBehaviour>()}
+ m_generator {std::make_shared<NullGenerator>()},
+ m_behaviour {std::make_shared<NullBehaviour>()}
{
}
Enemy::Enemy(
std::shared_ptr<Vector2> position,
- std::unique_ptr<Generator> generator,
- std::unique_ptr<Behaviour> behaviour) :
+ std::shared_ptr<Generator> generator,
+ std::shared_ptr<Behaviour> 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<Vector2> position,
- std::unique_ptr<Generator> generator,
- std::unique_ptr<Behaviour> behaviour);
+ std::shared_ptr<Generator> generator,
+ std::shared_ptr<Behaviour> behaviour);
void update(float dt);
void draw();
private:
float m_hold;
std::shared_ptr<Vector2> m_position;
- std::unique_ptr<Generator> m_generator;
- std::unique_ptr<Behaviour> m_behaviour;
+ std::shared_ptr<Generator> m_generator;
+ std::shared_ptr<Behaviour> 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>(Vector2{x, y});
- auto generator = std::make_unique<ExampleGenerator>(bullets);
- auto behaviour = std::make_unique<FallingAndOscillating>();
+ auto generator = std::make_shared<ExampleGenerator>(position, bullets);
+ auto behaviour = std::make_shared<FallingAndOscillating>(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 <cmath>
+#include <memory>
#include <utility>
#include <raylib.h>
@@ -8,7 +9,8 @@
#include "ConstantVelocity.h"
-ExampleGenerator::ExampleGenerator(ConstantVelocityBullet::Vector& bullets) :
+ExampleGenerator::ExampleGenerator(std::shared_ptr<Vector2> 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 <memory>
+
#include <raylib.h>
#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<Vector2> position, ConstantVelocityBullet::Vector& bullets);
void update(float dt) override;
+private:
+ std::shared_ptr<Vector2> 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 <memory>
+
#include <raylib.h>
#include "Generator.h"
-Falling::Falling() :
- m_speed {40}
+Falling::Falling(std::shared_ptr<Vector2> position, std::shared_ptr<Generator> 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 <memory>
+
#include <raylib.h>
#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<Vector2> position, std::shared_ptr<Generator> generator);
+ void update(float dt) override;
private:
float m_speed;
+ std::shared_ptr<Vector2> m_position;
+ std::shared_ptr<Generator> 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 <memory>
+
#include <raylib.h>
#include "Falling.h"
@@ -7,9 +9,16 @@
#include "Oscillating.h"
+FallingAndOscillating::FallingAndOscillating(std::shared_ptr<Vector2> position, std::shared_ptr<Generator> 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 <memory>
+
#include <raylib.h>
#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<Vector2> position, std::shared_ptr<Generator> 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 <memory>
-#include <utility>
-
-#include <raylib.h>
-
Generator::Generator() :
- m_enabled {true},
- m_origin {}
-{
-}
-
-
-void
-Generator::toggle(const bool enabled)
-{
- m_enabled = enabled;
-}
-
-
-void
-Generator::attach(std::shared_ptr<Vector2> 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 <memory>
-#include <raylib.h>
-
-
-class Generator
+struct Generator
{
-public:
Generator();
virtual ~Generator() = default;
virtual void update(float dt) = 0;
- void toggle(bool enabled);
- void attach(std::shared_ptr<Vector2> origin);
- void detach();
- Vector2 position() const;
-protected:
bool m_enabled;
- std::shared_ptr<Vector2> 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 <raylib.h>
-
#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 <cmath>
+#include <memory>
#include <raylib.h>
-#include "Generator.h"
-
-Oscillating::Oscillating() :
+Oscillating::Oscillating(std::shared_ptr<Vector2> 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 <memory>
+
#include <raylib.h>
#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<Vector2> position);
+ void update(float dt) override;
private:
float m_phase;
float m_shift;
+ std::shared_ptr<Vector2> 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 <memory>
#include <utility>
-#include <raylib.h>
-
#include "EnemyFactory.h"