summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-04-17 14:03:10 +0200
committerAki <please@ignore.pl>2022-04-17 14:03:10 +0200
commit85119293fe4f6814d44e8ee3f182c40876829128 (patch)
tree646c7b0f1be3aebed67a8b019092c72134087368
parenta0705fcd7b51401b45ff9960a81c08520f142230 (diff)
downloadbullethell2022-85119293fe4f6814d44e8ee3f182c40876829128.zip
bullethell2022-85119293fe4f6814d44e8ee3f182c40876829128.tar.gz
bullethell2022-85119293fe4f6814d44e8ee3f182c40876829128.tar.bz2
Added sample bullet and some generation
-rw-r--r--Bullets.cpp21
-rw-r--r--Bullets.h18
-rw-r--r--CMakeLists.txt1
-rw-r--r--GameScreen.cpp37
-rw-r--r--GameScreen.h4
5 files changed, 79 insertions, 2 deletions
diff --git a/Bullets.cpp b/Bullets.cpp
new file mode 100644
index 0000000..312e12e
--- /dev/null
+++ b/Bullets.cpp
@@ -0,0 +1,21 @@
+#include "Bullets.h"
+
+#include <vector>
+
+#include <raylib.h>
+
+
+void
+update(const float dt, std::vector<ConstantVelocityBullet>& bullets)
+{
+ auto it = bullets.begin();
+ while (it != bullets.end()) {
+ auto& bullet = *it;
+ bullet.position.x += bullet.velocity.x * dt;
+ bullet.position.y += bullet.velocity.y * dt;
+ if (bullet.position.y > 800)
+ it = bullets.erase(it);
+ else
+ ++it;
+ }
+}
diff --git a/Bullets.h b/Bullets.h
new file mode 100644
index 0000000..7778a9c
--- /dev/null
+++ b/Bullets.h
@@ -0,0 +1,18 @@
+#pragma once
+
+#include <vector>
+
+#include <raylib.h>
+
+
+struct ConstantVelocityBullet;
+
+
+void update(float dt, std::vector<ConstantVelocityBullet>& bullets);
+
+
+struct ConstantVelocityBullet
+{
+ Vector2 position;
+ Vector2 velocity;
+};
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3ae25bd..e550575 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -11,6 +11,7 @@ endif()
find_package(raylib 3 REQUIRED)
add_executable(
${PROJECT_NAME}
+ Bullets.cpp
Game.cpp
GameScreen.cpp
main.cpp
diff --git a/GameScreen.cpp b/GameScreen.cpp
index 714ccf5..dcae176 100644
--- a/GameScreen.cpp
+++ b/GameScreen.cpp
@@ -1,11 +1,16 @@
#include "GameScreen.h"
+#include <cmath>
+
#include <raylib.h>
+#include "Bullets.h"
+
GameScreen::GameScreen() :
m_pos {400, 300}
{
+ m_const_bullets.reserve(10000);
}
@@ -13,9 +18,33 @@ void
GameScreen::update(const float dt)
{
if (IsKeyDown(KEY_LEFT))
- m_pos.x -= dt * 30;
+ m_pos.x -= dt * 80;
if (IsKeyDown(KEY_RIGHT))
- m_pos.x += dt * 30;
+ m_pos.x += dt * 80;
+ m_delay += dt;
+ if (m_delay > TEST_INTERVAL) {
+ m_delay -= TEST_INTERVAL;
+ const int speed = 120;
+ const int divisions = 17;
+ for (double i = 0; i <= divisions; ++i) {
+ const double angle = (0.1 + 0.8 * i / divisions) * M_PI;
+ const double cos = std::cos(angle);
+ const double sin = std::sin(angle);
+ ConstantVelocityBullet bullet;
+ bullet.position.x = 400;
+ bullet.position.y = 20;
+ bullet.velocity.x = cos * speed;
+ bullet.velocity.y = sin * speed;
+ m_const_bullets.push_back(std::move(bullet));
+ }
+ }
+ ::update(dt, m_const_bullets);
+ bool collided = false;
+ for (const auto& bullet : m_const_bullets) {
+ if (CheckCollisionCircles(m_pos, 10, bullet.position, 6))
+ collided = true;
+ }
+ (void) collided;
}
@@ -23,4 +52,8 @@ void
GameScreen::draw()
{
DrawCircle(m_pos.x, m_pos.y, 10, LIGHTGRAY);
+ for (const auto& bullet : m_const_bullets)
+ DrawCircle(bullet.position.x, bullet.position.y, 6, RED);
+ DrawFPS(5, 5);
+ DrawText(TextFormat("%d", m_const_bullets.size()), 5, 25, 20, DARKGRAY);
}
diff --git a/GameScreen.h b/GameScreen.h
index 47a80d1..3555a6f 100644
--- a/GameScreen.h
+++ b/GameScreen.h
@@ -2,6 +2,7 @@
#include <raylib.h>
+#include "Bullets.h"
#include "Screen.h"
@@ -12,5 +13,8 @@ public:
void update(float dt) override;
void draw() override;
private:
+ static constexpr float TEST_INTERVAL {0.24f};
+ float m_delay;
Vector2 m_pos;
+ std::vector<ConstantVelocityBullet> m_const_bullets;
};