diff options
author | Aki <please@ignore.pl> | 2022-04-18 23:50:31 +0200 |
---|---|---|
committer | Aki <please@ignore.pl> | 2022-04-18 23:50:31 +0200 |
commit | 78097b5496209b56cef9d7fc8d6c13e4c87e0eb1 (patch) | |
tree | 29ccba95bafd04b594938a7f55be61c52bf4f4cc | |
parent | 5bb6b95fe71d33956491b98c29bf464a440ad221 (diff) | |
download | bullethell2022-78097b5496209b56cef9d7fc8d6c13e4c87e0eb1.zip bullethell2022-78097b5496209b56cef9d7fc8d6c13e4c87e0eb1.tar.gz bullethell2022-78097b5496209b56cef9d7fc8d6c13e4c87e0eb1.tar.bz2 |
Added simple behaviours for enemies
I don't quite like FallingAndOscillating approach. Perhaps it will be
better to just make it a list/vector in the enemy to allow for
composition.
-rw-r--r-- | Behaviour.h | 12 | ||||
-rw-r--r-- | CMakeLists.txt | 4 | ||||
-rw-r--r-- | Enemy.cpp | 30 | ||||
-rw-r--r-- | Enemy.h | 9 | ||||
-rw-r--r-- | Falling.cpp | 19 | ||||
-rw-r--r-- | Falling.h | 16 | ||||
-rw-r--r-- | FallingAndOscillating.cpp | 15 | ||||
-rw-r--r-- | FallingAndOscillating.h | 13 | ||||
-rw-r--r-- | Oscillating.cpp | 33 | ||||
-rw-r--r-- | Oscillating.h | 18 | ||||
-rw-r--r-- | Static.cpp | 14 | ||||
-rw-r--r-- | Static.h | 12 | ||||
-rw-r--r-- | TestStage.cpp | 16 |
13 files changed, 202 insertions, 9 deletions
diff --git a/Behaviour.h b/Behaviour.h new file mode 100644 index 0000000..07751ce --- /dev/null +++ b/Behaviour.h @@ -0,0 +1,12 @@ +#pragma once + +#include <raylib.h> + +#include "Generator.h" + + +struct Behaviour +{ + virtual ~Behaviour() = default; + virtual void update(float dt, Vector2& position, Generator& generator) = 0; +}; diff --git a/CMakeLists.txt b/CMakeLists.txt index 11b364e..7d81491 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,12 +16,16 @@ add_executable( ConstantVelocity.cpp Enemy.cpp ExampleGenerator.cpp + Falling.cpp + FallingAndOscillating.cpp Game.cpp GameScreen.cpp Generator.cpp KeyboardController.cpp main.cpp + Oscillating.cpp Player.cpp + Static.cpp TestStage.cpp TitleScreen.cpp ) @@ -5,13 +5,28 @@ #include <raylib.h> +#include "Behaviour.h" #include "Generator.h" +#include "Static.h" Enemy::Enemy(std::unique_ptr<Generator> generator) : + Enemy( + std::make_shared<Vector2>(), + std::move(generator), + std::make_unique<Static>()) +{ +} + + +Enemy::Enemy( + std::shared_ptr<Vector2> position, + std::unique_ptr<Generator> generator, + std::unique_ptr<Behaviour> behaviour) : m_hold {0}, - m_position {std::make_shared<Vector2>()}, - m_generator {std::move(generator)} + m_position {position}, + m_generator {std::move(generator)}, + m_behaviour {std::move(behaviour)} { m_position->x = 400; m_position->y = 40; @@ -26,8 +41,8 @@ Enemy::update(const float dt) m_hold -= dt; return; } - if (m_generator) - m_generator->update(dt); + m_behaviour->update(dt, *m_position, *m_generator); + m_generator->update(dt); } @@ -46,3 +61,10 @@ 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; +} @@ -4,18 +4,25 @@ #include <raylib.h> +#include "Behaviour.h" #include "Generator.h" class Enemy { public: - explicit Enemy(std::unique_ptr<Generator> generator = {}); + explicit Enemy(std::unique_ptr<Generator> generator); + 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; std::unique_ptr<Generator> m_generator; + std::unique_ptr<Behaviour> m_behaviour; }; diff --git a/Falling.cpp b/Falling.cpp new file mode 100644 index 0000000..8547ee2 --- /dev/null +++ b/Falling.cpp @@ -0,0 +1,19 @@ +#include "Falling.h" + +#include <raylib.h> + +#include "Generator.h" + + +Falling::Falling() : + m_speed {30} +{ +} + + +void +Falling::update(const float dt, Vector2& position, Generator& generator) +{ + (void) generator; + position.y += dt * m_speed; +} diff --git a/Falling.h b/Falling.h new file mode 100644 index 0000000..b629d6d --- /dev/null +++ b/Falling.h @@ -0,0 +1,16 @@ +#pragma once + +#include <raylib.h> + +#include "Behaviour.h" +#include "Generator.h" + + +class Falling : virtual public Behaviour +{ +public: + Falling(); + void update(float dt, Vector2& position, Generator& generator) override; +private: + float m_speed; +}; diff --git a/FallingAndOscillating.cpp b/FallingAndOscillating.cpp new file mode 100644 index 0000000..d3de3c5 --- /dev/null +++ b/FallingAndOscillating.cpp @@ -0,0 +1,15 @@ +#include "FallingAndOscillating.h" + +#include <raylib.h> + +#include "Falling.h" +#include "Generator.h" +#include "Oscillating.h" + + +void +FallingAndOscillating::update(const float dt, Vector2& position, Generator& generator) +{ + Falling::update(dt, position, generator); + Oscillating::update(dt, position, generator); +} diff --git a/FallingAndOscillating.h b/FallingAndOscillating.h new file mode 100644 index 0000000..7a32c27 --- /dev/null +++ b/FallingAndOscillating.h @@ -0,0 +1,13 @@ +#pragma once + +#include <raylib.h> + +#include "Falling.h" +#include "Generator.h" +#include "Oscillating.h" + + +struct FallingAndOscillating : public Falling, Oscillating +{ + void update(float dt, Vector2& position, Generator& generator) override; +}; diff --git a/Oscillating.cpp b/Oscillating.cpp new file mode 100644 index 0000000..4839282 --- /dev/null +++ b/Oscillating.cpp @@ -0,0 +1,33 @@ +#include "Oscillating.h" + +#include <cmath> + +#include <raylib.h> + +#include "Generator.h" + + +Oscillating::Oscillating() : + m_phase {0}, + m_shift {1.6f} +{ +} + + +void +Oscillating::update(const float dt, Vector2& position, Generator& generator) +{ + (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; +} diff --git a/Oscillating.h b/Oscillating.h new file mode 100644 index 0000000..d596e0f --- /dev/null +++ b/Oscillating.h @@ -0,0 +1,18 @@ +#pragma once + +#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); +private: + float m_phase; + float m_shift; +}; diff --git a/Static.cpp b/Static.cpp new file mode 100644 index 0000000..28cadda --- /dev/null +++ b/Static.cpp @@ -0,0 +1,14 @@ +#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 new file mode 100644 index 0000000..a4e7489 --- /dev/null +++ b/Static.h @@ -0,0 +1,12 @@ +#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 2ce4118..e6406f8 100644 --- a/TestStage.cpp +++ b/TestStage.cpp @@ -3,7 +3,10 @@ #include <memory> #include <utility> +#include <raylib.h> + #include "ExampleGenerator.h" +#include "FallingAndOscillating.h" TestStage::TestStage() : @@ -11,12 +14,17 @@ TestStage::TestStage() : m_const {} { m_enemies.reserve(2); - for (const float x : {300.f, 500.f}) { + 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); - if (x > 400.f) + auto behaviour = std::make_unique<FallingAndOscillating>(); + if (x > 410.f) { generator->m_direction *= -1; - Enemy enemy(std::move(generator)); - enemy.set_position(x, 100.f); + 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)); } } |