From 7c9c492f1555bb84a43fc68a56f69c25b54e7346 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 18 Apr 2022 11:35:25 +0200 Subject: Separated Stage from GameScreen --- CMakeLists.txt | 1 + GameScreen.cpp | 24 +++++++++++++----------- GameScreen.h | 12 +++++------- Stage.h | 10 ++++++++++ TestStage.cpp | 35 +++++++++++++++++++++++++++++++++++ TestStage.h | 20 ++++++++++++++++++++ TitleScreen.cpp | 3 ++- 7 files changed, 86 insertions(+), 19 deletions(-) create mode 100644 Stage.h create mode 100644 TestStage.cpp create mode 100644 TestStage.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 32c3787..5b2d655 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ add_executable( GameScreen.cpp main.cpp Player.cpp + TestStage.cpp TitleScreen.cpp ) target_link_libraries(${PROJECT_NAME} raylib) diff --git a/GameScreen.cpp b/GameScreen.cpp index 4315060..02aab00 100644 --- a/GameScreen.cpp +++ b/GameScreen.cpp @@ -1,11 +1,15 @@ #include "GameScreen.h" +#include +#include + #include +#include "Stage.h" + -GameScreen::GameScreen() : - m_const {}, - m_generator {m_const.m_bullets} +GameScreen::GameScreen(std::unique_ptr stage) : + m_stage {std::move(stage)} { } @@ -13,19 +17,17 @@ GameScreen::GameScreen() : void GameScreen::update(const float dt) { - m_player.update(dt); - m_generator.update(dt); - m_const.update(dt); - bool collided = m_player.collide(m_const.m_bullets); - (void) collided; + if (m_stage) + m_stage->update(dt); } void GameScreen::draw() { - m_const.draw(); - m_player.draw(); + if (m_stage) { + m_stage->draw(); + DrawText(TextFormat("%d", m_stage->total_bullets()), 5, 25, 20, DARKGRAY); + } DrawFPS(5, 5); - DrawText(TextFormat("%d", m_const.m_bullets.size()), 5, 25, 20, DARKGRAY); } diff --git a/GameScreen.h b/GameScreen.h index 11d5b73..5b43512 100644 --- a/GameScreen.h +++ b/GameScreen.h @@ -1,21 +1,19 @@ #pragma once +#include + #include -#include "ConstantVelocity.h" -#include "ExampleGenerator.h" -#include "Player.h" #include "Screen.h" +#include "Stage.h" class GameScreen : public Screen { public: - GameScreen(); + explicit GameScreen(std::unique_ptr stage); void update(float dt) override; void draw() override; private: - Player m_player; - ConstantVelocitySystem m_const; - ExampleGenerator m_generator; + std::unique_ptr m_stage; }; diff --git a/Stage.h b/Stage.h new file mode 100644 index 0000000..0a0cb46 --- /dev/null +++ b/Stage.h @@ -0,0 +1,10 @@ +#pragma once + + +struct Stage +{ + virtual ~Stage() = default; + virtual void update(float dt) = 0; + virtual void draw() = 0; + virtual int total_bullets() const = 0; +}; diff --git a/TestStage.cpp b/TestStage.cpp new file mode 100644 index 0000000..b257cf6 --- /dev/null +++ b/TestStage.cpp @@ -0,0 +1,35 @@ +#include "TestStage.h" + + +TestStage::TestStage() : + m_player {}, + m_const {}, + m_generator {m_const.m_bullets} +{ +} + + +void +TestStage::update(const float dt) +{ + m_player.update(dt); + m_generator.update(dt); + m_const.update(dt); + bool collided = m_player.collide(m_const.m_bullets); + (void) collided; +} + + +void +TestStage::draw() +{ + m_const.draw(); + m_player.draw(); +} + + +int +TestStage::total_bullets() const +{ + return m_const.m_bullets.size(); +} diff --git a/TestStage.h b/TestStage.h new file mode 100644 index 0000000..10164cd --- /dev/null +++ b/TestStage.h @@ -0,0 +1,20 @@ +#pragma once + +#include "ConstantVelocity.h" +#include "ExampleGenerator.h" +#include "Player.h" +#include "Stage.h" + + +class TestStage : public Stage +{ +public: + TestStage(); + void update(float dt) override; + void draw() override; + int total_bullets() const override; +private: + Player m_player; + ConstantVelocitySystem m_const; + ExampleGenerator m_generator; +}; diff --git a/TitleScreen.cpp b/TitleScreen.cpp index 3994fe9..0bf52f8 100644 --- a/TitleScreen.cpp +++ b/TitleScreen.cpp @@ -7,6 +7,7 @@ #include "Game.h" #include "GameScreen.h" #include "Globals.h" +#include "TestStage.h" static constexpr const char* TITLE {"Generic Bullet HELL"}; @@ -31,7 +32,7 @@ TitleScreen::update(const float dt) if (m_blink > 2 * INTERVAL) m_blink -= 2 * INTERVAL; if (IsKeyPressed(KEY_SPACE)) - g_game.set(std::make_unique()); + g_game.set(std::make_unique(std::make_unique())); } -- cgit v1.1