summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-04-18 20:14:53 +0200
committerAki <please@ignore.pl>2022-04-18 20:15:05 +0200
commitbbec07ab3de333649d14a64fa01f7e8ad6d56c58 (patch)
tree06a4cb6e677388da4865075229e4db4090418b83
parenta5f91f4d56106112565239c283441432c443c4d2 (diff)
downloadbullethell2022-bbec07ab3de333649d14a64fa01f7e8ad6d56c58.zip
bullethell2022-bbec07ab3de333649d14a64fa01f7e8ad6d56c58.tar.gz
bullethell2022-bbec07ab3de333649d14a64fa01f7e8ad6d56c58.tar.bz2
Added naive enemy
This reminds me ECS a bit too much but I don't think I want to implement one right now...
-rw-r--r--CMakeLists.txt2
-rw-r--r--Enemy.cpp40
-rw-r--r--Enemy.h20
-rw-r--r--ExampleGenerator.cpp6
-rw-r--r--ExampleGenerator.h1
-rw-r--r--Generator.cpp30
-rw-r--r--Generator.h12
-rw-r--r--TestStage.cpp7
-rw-r--r--TestStage.h4
9 files changed, 113 insertions, 9 deletions
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 <memory>
+#include <utility>
+
+#include <raylib.h>
+
+#include "Generator.h"
+
+
+Enemy::Enemy(std::unique_ptr<Generator> generator) :
+ m_hold {2},
+ m_position {std::make_shared<Vector2>()},
+ 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 <memory>
+
+#include <raylib.h>
+
+#include "Generator.h"
+
+
+class Enemy
+{
+public:
+ explicit Enemy(std::unique_ptr<Generator> generator = {});
+ void update(float dt);
+ void draw();
+private:
+ float m_hold;
+ std::shared_ptr<Vector2> m_position;
+ std::unique_ptr<Generator> 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 <memory>
+#include <utility>
+
+#include <raylib.h>
+
+
+void
+Generator::attach(std::shared_ptr<Vector2> 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 <memory>
-struct Generator
+#include <raylib.h>
+
+
+class Generator
{
+public:
virtual ~Generator() = default;
virtual void update(float dt) = 0;
+ void attach(std::shared_ptr<Vector2> origin);
+ void detach();
+ Vector2 position() const;
+protected:
+ std::shared_ptr<Vector2> 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<ExampleGenerator>(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;
};