From 162ab6e923cf0c065449cfc248102b5b92fac8a7 Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 17 Apr 2022 15:04:55 +0200 Subject: Separated bullet generator into class and created system for bullets --- Bullets.cpp | 35 ++++++++++++++++++++++++++++------- Bullets.h | 14 +++++++++++++- CMakeLists.txt | 1 + ExampleGenerator.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ ExampleGenerator.h | 21 +++++++++++++++++++++ GameScreen.cpp | 35 ++++++++--------------------------- GameScreen.h | 6 +++--- 7 files changed, 121 insertions(+), 38 deletions(-) create mode 100644 ExampleGenerator.cpp create mode 100644 ExampleGenerator.h diff --git a/Bullets.cpp b/Bullets.cpp index 312e12e..62b6ea3 100644 --- a/Bullets.cpp +++ b/Bullets.cpp @@ -1,21 +1,42 @@ #include "Bullets.h" -#include - #include +ConstantVelocitySystem::ConstantVelocitySystem() : + ConstantVelocitySystem(10000) +{ +} + + +ConstantVelocitySystem::ConstantVelocitySystem(int reserved) +{ + m_bullets.reserve(reserved); +} + + void -update(const float dt, std::vector& bullets) +ConstantVelocitySystem::update(const float dt) { - auto it = bullets.begin(); - while (it != bullets.end()) { + 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.position.x += bullet.velocity.x * dt; bullet.position.y += bullet.velocity.y * dt; - if (bullet.position.y > 800) - it = bullets.erase(it); + if (bullet.position.y > max_height || bullet.position.x < min_width || bullet.position.x > max_width) + it = m_bullets.erase(it); else ++it; } } + + +void +ConstantVelocitySystem::draw() +{ + for (const auto& bullet : m_bullets) + DrawCircle(bullet.position.x, bullet.position.y, bullet.radius, bullet.color); +} diff --git a/Bullets.h b/Bullets.h index 7778a9c..4a77100 100644 --- a/Bullets.h +++ b/Bullets.h @@ -5,14 +5,26 @@ #include +static constexpr float MARGIN {40}; + + struct ConstantVelocityBullet; -void update(float dt, std::vector& bullets); +struct ConstantVelocitySystem +{ + ConstantVelocitySystem(); + explicit ConstantVelocitySystem(int reserved); + void update(float dt); + void draw(); + std::vector m_bullets; +}; struct ConstantVelocityBullet { Vector2 position; Vector2 velocity; + float radius; + Color color; }; diff --git a/CMakeLists.txt b/CMakeLists.txt index e550575..0ca18f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,7 @@ find_package(raylib 3 REQUIRED) add_executable( ${PROJECT_NAME} Bullets.cpp + ExampleGenerator.cpp Game.cpp GameScreen.cpp main.cpp diff --git a/ExampleGenerator.cpp b/ExampleGenerator.cpp new file mode 100644 index 0000000..7f4d41b --- /dev/null +++ b/ExampleGenerator.cpp @@ -0,0 +1,47 @@ +#include "ExampleGenerator.h" + +#include +#include +#include + +#include + +#include "Bullets.h" + + +ExampleGenerator::ExampleGenerator() : + m_delay {0}, + m_interval {0.2f}, + m_cone {0.3f}, + m_speed {120}, + m_shift {10}, + m_segments {3}, + m_origin {400, 20} +{ +} + + +void +ExampleGenerator::update(const float dt, std::vector& bullets) +{ + m_delay += dt; + if (m_delay > m_interval) { + m_delay -= m_interval; + for (float i = 0; i <= m_segments; ++i) { + const float angle = ((1 - m_cone) / 2 + m_cone * i / m_segments) * M_PI; + const float cos = std::cos(angle); + const float sin = std::sin(angle); + ConstantVelocityBullet bullet; + bullet.color = RED; + 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; + bullets.push_back(std::move(bullet)); + } + m_segments += 3; + if (m_segments > 20) + m_segments = 3; + } +} diff --git a/ExampleGenerator.h b/ExampleGenerator.h new file mode 100644 index 0000000..0e3372b --- /dev/null +++ b/ExampleGenerator.h @@ -0,0 +1,21 @@ +#pragma once + +#include + +#include + +#include "Bullets.h" + + +struct ExampleGenerator +{ + ExampleGenerator(); + void update(float dt, std::vector& bullets); + float m_delay; + float m_interval; + float m_cone; + int m_speed; + int m_shift; + int m_segments; + Vector2 m_origin; +}; diff --git a/GameScreen.cpp b/GameScreen.cpp index dcae176..6dc8025 100644 --- a/GameScreen.cpp +++ b/GameScreen.cpp @@ -1,16 +1,14 @@ #include "GameScreen.h" -#include - #include #include "Bullets.h" GameScreen::GameScreen() : - m_pos {400, 300} + m_pos {400, 300}, + m_const {} { - m_const_bullets.reserve(10000); } @@ -21,27 +19,11 @@ GameScreen::update(const float dt) m_pos.x -= dt * 80; if (IsKeyDown(KEY_RIGHT)) 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); + m_generator.update(dt, m_const.m_bullets); + m_const.update(dt); bool collided = false; - for (const auto& bullet : m_const_bullets) { - if (CheckCollisionCircles(m_pos, 10, bullet.position, 6)) + for (const auto& bullet : m_const.m_bullets) { + if (CheckCollisionCircles(m_pos, 9, bullet.position, bullet.radius)) collided = true; } (void) collided; @@ -52,8 +34,7 @@ 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); + m_const.draw(); DrawFPS(5, 5); - DrawText(TextFormat("%d", m_const_bullets.size()), 5, 25, 20, DARKGRAY); + DrawText(TextFormat("%d", m_const.m_bullets.size()), 5, 25, 20, DARKGRAY); } diff --git a/GameScreen.h b/GameScreen.h index 3555a6f..b821d0c 100644 --- a/GameScreen.h +++ b/GameScreen.h @@ -3,6 +3,7 @@ #include #include "Bullets.h" +#include "ExampleGenerator.h" #include "Screen.h" @@ -13,8 +14,7 @@ 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 m_const_bullets; + ExampleGenerator m_generator; + ConstantVelocitySystem m_const; }; -- cgit v1.1