From 804e71083e4df85c4f0692553de3084f0ed4cd9a Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 22 Apr 2022 01:11:24 +0200 Subject: Added Stats for lifes points and other kind of gameplay state --- CMakeLists.txt | 1 + Game.h | 1 - GameScreen.cpp | 15 ++++++++------- GameScreen.h | 2 ++ Stage.h | 6 +++++- Stats.cpp | 8 ++++++++ Stats.h | 10 ++++++++++ TestStage.cpp | 12 ++++-------- TestStage.h | 1 - 9 files changed, 38 insertions(+), 18 deletions(-) create mode 100644 Stats.cpp create mode 100644 Stats.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 42eccd4..176b3f8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,7 @@ add_executable( main.cpp Oscillating.cpp Player.cpp + Stats.cpp TestStage.cpp TitleScreen.cpp WaveGenerator.cpp diff --git a/Game.h b/Game.h index b047336..702ed17 100644 --- a/Game.h +++ b/Game.h @@ -11,7 +11,6 @@ public: void set(std::unique_ptr screen); void update(float dt); void draw(); - private: std::unique_ptr m_screen; }; diff --git a/GameScreen.cpp b/GameScreen.cpp index 02aab00..f813a60 100644 --- a/GameScreen.cpp +++ b/GameScreen.cpp @@ -9,25 +9,26 @@ GameScreen::GameScreen(std::unique_ptr stage) : - m_stage {std::move(stage)} + m_stage {std::move(stage)}, + m_stats {std::make_shared()} { + m_stage->m_stats = m_stats; } void GameScreen::update(const float dt) { - if (m_stage) - m_stage->update(dt); + m_stage->update(dt); } void GameScreen::draw() { - if (m_stage) { - m_stage->draw(); - DrawText(TextFormat("%d", m_stage->total_bullets()), 5, 25, 20, DARKGRAY); - } + m_stage->draw(); + DrawText(TextFormat("%d", m_stats->total_bullets), 5, 25, 20, DARKGRAY); + DrawText(TextFormat("%d", m_stats->lifes), 5, 45, 20, DARKGREEN); + DrawText(TextFormat("%d", m_stats->points), 5, 65, 20, GOLD); DrawFPS(5, 5); } diff --git a/GameScreen.h b/GameScreen.h index 5b43512..dd0a615 100644 --- a/GameScreen.h +++ b/GameScreen.h @@ -6,6 +6,7 @@ #include "Screen.h" #include "Stage.h" +#include "Stats.h" class GameScreen : public Screen @@ -16,4 +17,5 @@ public: void draw() override; private: std::unique_ptr m_stage; + std::shared_ptr m_stats; }; diff --git a/Stage.h b/Stage.h index 0a0cb46..fcb69b3 100644 --- a/Stage.h +++ b/Stage.h @@ -1,10 +1,14 @@ #pragma once +#include + +#include "Stats.h" + struct Stage { virtual ~Stage() = default; virtual void update(float dt) = 0; virtual void draw() = 0; - virtual int total_bullets() const = 0; + std::shared_ptr m_stats; }; diff --git a/Stats.cpp b/Stats.cpp new file mode 100644 index 0000000..dd2e581 --- /dev/null +++ b/Stats.cpp @@ -0,0 +1,8 @@ +#include "Stats.h" + + +Stats::Stats() : + lifes {3}, + points {0} +{ +} diff --git a/Stats.h b/Stats.h new file mode 100644 index 0000000..ea46681 --- /dev/null +++ b/Stats.h @@ -0,0 +1,10 @@ +#pragma once + + +struct Stats +{ + Stats(); + int lifes; + int points; + int total_bullets; +}; diff --git a/TestStage.cpp b/TestStage.cpp index fdd833d..e6ff74a 100644 --- a/TestStage.cpp +++ b/TestStage.cpp @@ -24,7 +24,10 @@ TestStage::update(const float dt) enemy.update(dt); m_const.update(dt); bool collided = m_player.collide(m_const.m_bullets); - (void) collided; + if (m_stats) { + m_stats->total_bullets = m_const.m_bullets.size(); + if (collided) m_stats->lifes--; + } } @@ -36,10 +39,3 @@ TestStage::draw() enemy.draw(); m_player.draw(); } - - -int -TestStage::total_bullets() const -{ - return m_const.m_bullets.size(); -} diff --git a/TestStage.h b/TestStage.h index 0e90ab6..7ddbe06 100644 --- a/TestStage.h +++ b/TestStage.h @@ -14,7 +14,6 @@ public: TestStage(); void update(float dt) override; void draw() override; - int total_bullets() const override; private: Player m_player; ConstantVelocitySystem m_const; -- cgit v1.1