summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMichal <michrydz@wp.pl>2022-04-23 21:15:11 +0200
committerMichal <michrydz@wp.pl>2022-04-23 21:15:11 +0200
commit22b52bffbe51ade791fa50a995e0b47a9978efed (patch)
tree82af6e1207878baf27e1d71e09383fd6372a4ab1
parent38df088b80cb6c159eb9941cf6d3c0a8492e65ee (diff)
downloadbullethell2022-22b52bffbe51ade791fa50a995e0b47a9978efed.zip
bullethell2022-22b52bffbe51ade791fa50a995e0b47a9978efed.tar.gz
bullethell2022-22b52bffbe51ade791fa50a995e0b47a9978efed.tar.bz2
Add spiral bullet
-rw-r--r--Spiral.cpp50
-rw-r--r--Spiral.h32
2 files changed, 82 insertions, 0 deletions
diff --git a/Spiral.cpp b/Spiral.cpp
new file mode 100644
index 0000000..636a6b2
--- /dev/null
+++ b/Spiral.cpp
@@ -0,0 +1,50 @@
+#include "Spiral.h"
+
+#include <raylib.h>
+#include <cmath>
+
+
+SpiralSystem::SpiralSystem()
+{
+ m_bullets.reserve(RESERVED);
+}
+
+
+void
+SpiralSystem::update(const float dt)
+{
+ const int min_height = 0 - 3 * MARGIN;
+ const int max_height = GetScreenHeight() + MARGIN;
+ const int min_width = 0 - MARGIN;
+ const int max_width = GetScreenWidth() + MARGIN;
+ auto it = m_bullets.begin();
+ while (it != m_bullets.end()) {
+ auto& bullet = *it;
+ bullet.timer += dt;
+ if (bullet.timer > bullet.redirect_time) {
+ bullet.timer -= bullet.redirect_time;
+ bullet.angle = bullet.angle + bullet.redirect;
+ if (bullet.angle > 2.0)
+ bullet.angle -= 2.0;
+ bullet.redirect *= bullet.redirect_decrease;
+ }
+ float velovity_x = std::cos(angle * M_PI);
+ float velovity_y = std::sin(angle * M_PI);
+ bullet.position.x += velocity_x * dt;
+ bullet.position.y += velocity_y * dt;
+ const bool y_exceeded = bullet.position.y < min_height || bullet.position.y > max_height;
+ const bool x_exceeded = bullet.position.x < min_width || bullet.position.x > max_width;
+ if (y_exceeded || x_exceeded)
+ it = m_bullets.erase(it);
+ else
+ ++it;
+ }
+}
+
+
+void
+SpiralSystem::draw()
+{
+ for (const auto& bullet : m_bullets)
+ DrawCircle(bullet.position.x, bullet.position.y, bullet.radius, bullet.color);
+}
diff --git a/Spiral.h b/Spiral.h
new file mode 100644
index 0000000..064e3a7
--- /dev/null
+++ b/Spiral.h
@@ -0,0 +1,32 @@
+#pragma once
+
+#include <vector>
+
+#include <raylib.h>
+
+
+struct SpiralBullet
+{
+ using Vector = std::vector<SpiralBullet>;
+ Vector2 position;
+ float velocity;
+ float angle;
+ float redirect;
+ float timer;
+ float redirect_time;
+ float redirect_decrease;
+ float radius;
+ Color color;
+};
+
+
+struct SpiralSystem
+{
+ static constexpr float MARGIN {40};
+ static constexpr int RESERVED {10000};
+
+ SpiralSystem();
+ void update(float dt);
+ void draw();
+ SpiralBullet::Vector m_bullets;
+};