From bbec07ab3de333649d14a64fa01f7e8ad6d56c58 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 18 Apr 2022 20:14:53 +0200 Subject: Added naive enemy This reminds me ECS a bit too much but I don't think I want to implement one right now... --- CMakeLists.txt | 2 ++ Enemy.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ Enemy.h | 20 ++++++++++++++++++++ ExampleGenerator.cpp | 6 +++--- ExampleGenerator.h | 1 - Generator.cpp | 30 ++++++++++++++++++++++++++++++ Generator.h | 12 +++++++++++- TestStage.cpp | 7 +++++-- TestStage.h | 4 ++-- 9 files changed, 113 insertions(+), 9 deletions(-) create mode 100644 Enemy.cpp create mode 100644 Enemy.h create mode 100644 Generator.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c22511..b3083f3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,9 +12,11 @@ find_package(raylib 3 REQUIRED) add_executable( ${PROJECT_NAME} ConstantVelocity.cpp + Enemy.cpp ExampleGenerator.cpp Game.cpp GameScreen.cpp + Generator.cpp KeyboardController.cpp main.cpp Player.cpp diff --git a/Enemy.cpp b/Enemy.cpp new file mode 100644 index 0000000..7caea31 --- /dev/null +++ b/Enemy.cpp @@ -0,0 +1,40 @@ +#include "Enemy.h" + +#include +#include + +#include + +#include "Generator.h" + + +Enemy::Enemy(std::unique_ptr generator) : + m_hold {2}, + m_position {std::make_shared()}, + m_generator {std::move(generator)} +{ + m_position->x = 400; + m_position->y = 40; + m_generator->attach(m_position); +} + + +void +Enemy::update(const float dt) +{ + if (m_hold > 0) { + m_hold -= dt; + return; + } + if (m_generator) + m_generator->update(dt); +} + + +void +Enemy::draw() +{ + if (m_hold > 0) + return; + DrawCircle(m_position->x, m_position->y, 6, DARKGRAY); +} diff --git a/Enemy.h b/Enemy.h new file mode 100644 index 0000000..ad0c99d --- /dev/null +++ b/Enemy.h @@ -0,0 +1,20 @@ +#pragma once + +#include + +#include + +#include "Generator.h" + + +class Enemy +{ +public: + explicit Enemy(std::unique_ptr generator = {}); + void update(float dt); + void draw(); +private: + float m_hold; + std::shared_ptr m_position; + std::unique_ptr m_generator; +}; diff --git a/ExampleGenerator.cpp b/ExampleGenerator.cpp index f7af287..4c86d94 100644 --- a/ExampleGenerator.cpp +++ b/ExampleGenerator.cpp @@ -17,7 +17,6 @@ ExampleGenerator::ExampleGenerator(ConstantVelocityBullet::Vector& bullets) : m_speed {100}, m_shift {10}, m_segments {2}, - m_origin {400, 20}, m_color {240, 0, 0, 255} { } @@ -28,6 +27,7 @@ ExampleGenerator::update(const float dt) { m_delay += dt; if (m_delay > m_interval) { + const auto pos = position(); m_delay -= m_interval; for (float i = 0; i <= m_segments; ++i) { const float angle = (m_angle - m_cone / 2 + m_cone * i / m_segments) * M_PI; @@ -38,8 +38,8 @@ ExampleGenerator::update(const float dt) bullet.radius = 4; bullet.velocity.x = cos * m_speed; bullet.velocity.y = sin * m_speed; - bullet.position.x = m_origin.x + cos * m_shift; - bullet.position.y = m_origin.y + sin * m_shift; + bullet.position.x = pos.x + cos * m_shift; + bullet.position.y = pos.y + sin * m_shift; m_bullets.push_back(std::move(bullet)); } m_angle += dt * 1.1f; diff --git a/ExampleGenerator.h b/ExampleGenerator.h index 4bf0030..c74ccfd 100644 --- a/ExampleGenerator.h +++ b/ExampleGenerator.h @@ -18,6 +18,5 @@ struct ExampleGenerator : public Generator int m_speed; int m_shift; int m_segments; - Vector2 m_origin; Color m_color; }; diff --git a/Generator.cpp b/Generator.cpp new file mode 100644 index 0000000..32ad608 --- /dev/null +++ b/Generator.cpp @@ -0,0 +1,30 @@ +#include "Generator.h" + +#include +#include + +#include + + +void +Generator::attach(std::shared_ptr origin) +{ + m_origin = std::move(origin); +} + + +void +Generator::detach() +{ + m_origin = {}; +} + + +Vector2 +Generator::position() const +{ + if (m_origin) + return *m_origin; + else + return {400, 40}; +} diff --git a/Generator.h b/Generator.h index 5beef2c..1679055 100644 --- a/Generator.h +++ b/Generator.h @@ -1,8 +1,18 @@ #pragma once +#include -struct Generator +#include + + +class Generator { +public: virtual ~Generator() = default; virtual void update(float dt) = 0; + void attach(std::shared_ptr origin); + void detach(); + Vector2 position() const; +protected: + std::shared_ptr m_origin; }; diff --git a/TestStage.cpp b/TestStage.cpp index b257cf6..cc90f19 100644 --- a/TestStage.cpp +++ b/TestStage.cpp @@ -1,10 +1,12 @@ #include "TestStage.h" +#include "ExampleGenerator.h" + TestStage::TestStage() : m_player {}, m_const {}, - m_generator {m_const.m_bullets} + m_enemy {std::make_unique(m_const.m_bullets)} { } @@ -13,7 +15,7 @@ void TestStage::update(const float dt) { m_player.update(dt); - m_generator.update(dt); + m_enemy.update(dt); m_const.update(dt); bool collided = m_player.collide(m_const.m_bullets); (void) collided; @@ -24,6 +26,7 @@ void TestStage::draw() { m_const.draw(); + m_enemy.draw(); m_player.draw(); } diff --git a/TestStage.h b/TestStage.h index 10164cd..c5287a5 100644 --- a/TestStage.h +++ b/TestStage.h @@ -1,7 +1,7 @@ #pragma once #include "ConstantVelocity.h" -#include "ExampleGenerator.h" +#include "Enemy.h" #include "Player.h" #include "Stage.h" @@ -16,5 +16,5 @@ public: private: Player m_player; ConstantVelocitySystem m_const; - ExampleGenerator m_generator; + Enemy m_enemy; }; -- cgit v1.1