From 1811ec874d235d44a143c39307ac984b87fe3a8a Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 26 Apr 2022 11:37:44 +0200 Subject: Implemented simple scoring system --- Enemy.cpp | 7 +++++++ Enemy.h | 1 + GameScreen.cpp | 1 + OverScreen.cpp | 22 +++++++++++++++++++++- OverScreen.h | 2 ++ TestStage.cpp | 11 +++++++++++ 6 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Enemy.cpp b/Enemy.cpp index 083c890..2406394 100644 --- a/Enemy.cpp +++ b/Enemy.cpp @@ -51,3 +51,10 @@ Enemy::draw() return; DrawCircle(m_position->x, m_position->y, 6, DARKGRAY); } + + +bool +Enemy::gone() const +{ + return m_position->y > 600; +} diff --git a/Enemy.h b/Enemy.h index 4305026..97f3a8e 100644 --- a/Enemy.h +++ b/Enemy.h @@ -19,6 +19,7 @@ public: std::shared_ptr behaviour); void update(float dt); void draw(); + bool gone() const; private: float m_hold; std::shared_ptr m_position; diff --git a/GameScreen.cpp b/GameScreen.cpp index aeda96b..2c8dd06 100644 --- a/GameScreen.cpp +++ b/GameScreen.cpp @@ -28,6 +28,7 @@ void GameScreen::update(const float dt) { m_stage->update(dt); + m_stats->points += 100 * dt; if (m_stats->lifes < 0) g_game.set(std::make_unique(m_stats)); } diff --git a/OverScreen.cpp b/OverScreen.cpp index dcf5866..a465464 100644 --- a/OverScreen.cpp +++ b/OverScreen.cpp @@ -9,7 +9,11 @@ #include "TitleScreen.h" -static constexpr const char* DIED {"The Bone is Gone"}; +static constexpr const char* DIED {"Game Over"}; +static constexpr const char* LABEL {"Your Score: "}; + + +static const char* points_text(std::shared_ptr stats); OverScreen::OverScreen(std::shared_ptr stats) : @@ -17,7 +21,12 @@ OverScreen::OverScreen(std::shared_ptr stats) : { const int screen_width = GetScreenWidth(); const int died_width = MeasureText(DIED, 20); + const int label_width = MeasureText(LABEL, 20); + const int score_width = MeasureText(points_text(m_stats), 20); + const int label_and_score = label_width + score_width; m_died_x = (screen_width - died_width) / 2; + m_label_x = (screen_width - label_and_score) / 2; + m_score_x = m_label_x + label_width; m_flash.start(); } @@ -36,5 +45,16 @@ OverScreen::draw() { ClearBackground(BLACK); DrawText(DIED, m_died_x, 160, 20, RED); + DrawText(LABEL, m_label_x, 180, 20, GRAY); + DrawText(points_text(m_stats), m_score_x, 180, 20, GOLD); m_flash.draw(); } + + +const char* +points_text(std::shared_ptr stats) +{ + if (stats->lifes > 0) + return TextFormat("%d + %d", stats->points, 1000 * stats->lifes); + return TextFormat("%d", stats->points); +} diff --git a/OverScreen.h b/OverScreen.h index dc7ab1a..a0a0130 100644 --- a/OverScreen.h +++ b/OverScreen.h @@ -16,5 +16,7 @@ public: private: std::shared_ptr m_stats; int m_died_x; + int m_label_x; + int m_score_x; Flash m_flash; }; diff --git a/TestStage.cpp b/TestStage.cpp index b851b0b..19703d2 100644 --- a/TestStage.cpp +++ b/TestStage.cpp @@ -5,6 +5,8 @@ #include #include "EnemyFactory.h" +#include "Globals.h" +#include "OverScreen.h" static constexpr Color DEEPSPACE {3, 5, 22, 255}; @@ -48,6 +50,15 @@ TestStage::update(const float dt) m_stats->total_bullets = m_const.m_bullets.size(); if (collided) m_stats->lifes--; } + bool all_done = true; + for (const auto& enemy : m_enemies) { + if (!enemy.gone()) { + all_done = false; + break; + } + } + if (all_done) + g_game.set(std::make_unique(m_stats)); } -- cgit v1.1