From 78097b5496209b56cef9d7fc8d6c13e4c87e0eb1 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 18 Apr 2022 23:50:31 +0200 Subject: 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. --- Behaviour.h | 12 ++++++++++++ CMakeLists.txt | 4 ++++ Enemy.cpp | 30 ++++++++++++++++++++++++++---- Enemy.h | 9 ++++++++- Falling.cpp | 19 +++++++++++++++++++ Falling.h | 16 ++++++++++++++++ FallingAndOscillating.cpp | 15 +++++++++++++++ FallingAndOscillating.h | 13 +++++++++++++ Oscillating.cpp | 33 +++++++++++++++++++++++++++++++++ Oscillating.h | 18 ++++++++++++++++++ Static.cpp | 14 ++++++++++++++ Static.h | 12 ++++++++++++ TestStage.cpp | 16 ++++++++++++---- 13 files changed, 202 insertions(+), 9 deletions(-) create mode 100644 Behaviour.h create mode 100644 Falling.cpp create mode 100644 Falling.h create mode 100644 FallingAndOscillating.cpp create mode 100644 FallingAndOscillating.h create mode 100644 Oscillating.cpp create mode 100644 Oscillating.h create mode 100644 Static.cpp create mode 100644 Static.h 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 + +#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 ) diff --git a/Enemy.cpp b/Enemy.cpp index b8329a2..1be3c76 100644 --- a/Enemy.cpp +++ b/Enemy.cpp @@ -5,13 +5,28 @@ #include +#include "Behaviour.h" #include "Generator.h" +#include "Static.h" Enemy::Enemy(std::unique_ptr generator) : + Enemy( + std::make_shared(), + std::move(generator), + std::make_unique()) +{ +} + + +Enemy::Enemy( + std::shared_ptr position, + std::unique_ptr generator, + std::unique_ptr behaviour) : m_hold {0}, - m_position {std::make_shared()}, - 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; +} diff --git a/Enemy.h b/Enemy.h index 0a8c429..30e4140 100644 --- a/Enemy.h +++ b/Enemy.h @@ -4,18 +4,25 @@ #include +#include "Behaviour.h" #include "Generator.h" class Enemy { public: - explicit Enemy(std::unique_ptr generator = {}); + explicit Enemy(std::unique_ptr generator); + 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; std::unique_ptr m_generator; + std::unique_ptr 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 + +#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 + +#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 + +#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 + +#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 + +#include + +#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 + +#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 + +#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 + +#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 #include +#include + #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{x, 100.f}); auto generator = std::make_unique(m_const.m_bullets); - if (x > 400.f) + auto behaviour = std::make_unique(); + 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)); } } -- cgit v1.1