From 915dd2a9acda9c505ad82bd2dd8df282670ed5fd Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 20 Apr 2022 01:56:47 +0200 Subject: Added another generator and enemy for testing purposes --- CMakeLists.txt | 1 + EnemyFactory.cpp | 17 ++++++++++++++++- EnemyFactory.h | 1 + TestStage.cpp | 2 +- WaveGenerator.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ WaveGenerator.h | 26 ++++++++++++++++++++++++++ 6 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 WaveGenerator.cpp create mode 100644 WaveGenerator.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f4c3228..f47ec87 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,5 +28,6 @@ add_executable( Player.cpp TestStage.cpp TitleScreen.cpp + WaveGenerator.cpp ) target_link_libraries(${PROJECT_NAME} raylib) diff --git a/EnemyFactory.cpp b/EnemyFactory.cpp index 8a51c3f..a154ae5 100644 --- a/EnemyFactory.cpp +++ b/EnemyFactory.cpp @@ -8,6 +8,7 @@ #include "Enemy.h" #include "ExampleGenerator.h" #include "FallingAndOscillating.h" +#include "WaveGenerator.h" Enemy @@ -20,7 +21,6 @@ EnemyFactory::make_example( auto position = std::make_shared(Vector2{x, y}); auto generator = std::make_shared(position, bullets); auto behaviour = std::make_shared(position, generator); - generator->m_position = position; if (mirror) { generator->m_color = Color{100, 0, 255, 255}; generator->m_direction *= -1; @@ -30,3 +30,18 @@ EnemyFactory::make_example( enemy.m_hold = hold; return enemy; } + + +Enemy +EnemyFactory::make_waver( + ConstantVelocityBullet::Vector& bullets, + const float x, const float y, + const float hold) +{ + auto position = std::make_shared(Vector2{x, y}); + auto generator = std::make_shared(position, bullets); + auto behaviour = std::make_shared(position, generator); + Enemy enemy(position, generator, behaviour); + enemy.m_hold = hold; + return enemy; +} diff --git a/EnemyFactory.h b/EnemyFactory.h index 0475a25..6134ae9 100644 --- a/EnemyFactory.h +++ b/EnemyFactory.h @@ -7,4 +7,5 @@ struct EnemyFactory { static Enemy make_example(ConstantVelocityBullet::Vector& bullets, float x, float y, float hold, bool mirror=false); + static Enemy make_waver(ConstantVelocityBullet::Vector& bullets, float x, float y, float hold); }; diff --git a/TestStage.cpp b/TestStage.cpp index 00c27e1..fdd833d 100644 --- a/TestStage.cpp +++ b/TestStage.cpp @@ -10,7 +10,7 @@ TestStage::TestStage() : m_enemies.reserve(5); m_enemies.push_back(EnemyFactory::make_example(m_const.m_bullets, 300, -10, 0)); m_enemies.push_back(EnemyFactory::make_example(m_const.m_bullets, 500, -10, 0, true)); - m_enemies.push_back(EnemyFactory::make_example(m_const.m_bullets, 400, -10, 10)); + m_enemies.push_back(EnemyFactory::make_waver(m_const.m_bullets, 400, -10, 10)); m_enemies.push_back(EnemyFactory::make_example(m_const.m_bullets, 550, -10, 20)); m_enemies.push_back(EnemyFactory::make_example(m_const.m_bullets, 250, -10, 20, true)); } diff --git a/WaveGenerator.cpp b/WaveGenerator.cpp new file mode 100644 index 0000000..5d8dd9f --- /dev/null +++ b/WaveGenerator.cpp @@ -0,0 +1,47 @@ +#include "WaveGenerator.h" + +#include +#include +#include + +#include + +#include "ConstantVelocity.h" + + +WaveGenerator::WaveGenerator(std::shared_ptr position, ConstantVelocityBullet::Vector& bullets) : + m_position {position}, + m_bullets {bullets}, + m_delay {0}, + m_interval {0.22f}, + m_speed {100}, + m_shift {10}, + m_segments {30}, + m_color {100, 100, 240, 255} +{ +} + + +void +WaveGenerator::update(const float dt) +{ + if (!m_enabled) + return; + m_delay += dt; + if (m_delay > m_interval) { + m_delay -= m_interval; + for (float i = 0; i < m_segments; ++i) { + const float angle = (2.f / m_segments * i) * M_PI; + const float cos = std::cos(angle); + const float sin = std::sin(angle); + ConstantVelocityBullet bullet; + bullet.color = m_color; + bullet.radius = 3; + bullet.velocity.x = cos * m_speed; + bullet.velocity.y = sin * m_speed; + 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)); + } + } +} diff --git a/WaveGenerator.h b/WaveGenerator.h new file mode 100644 index 0000000..c3d52f5 --- /dev/null +++ b/WaveGenerator.h @@ -0,0 +1,26 @@ +#pragma once + +#include + +#include + +#include "ConstantVelocity.h" +#include "Generator.h" + + +class WaveGenerator : public Generator +{ +public: + friend class EnemyFactory; + WaveGenerator(std::shared_ptr position, ConstantVelocityBullet::Vector& bullets); + void update(float dt) override; +private: + std::shared_ptr m_position; + ConstantVelocityBullet::Vector& m_bullets; + float m_delay; + float m_interval; + int m_speed; + int m_shift; + int m_segments; + Color m_color; +}; -- cgit v1.1