diff --git a/inc/csharp/timespan.hpp b/inc/csharp/timespan.hpp index abbd706..fec1bd0 100644 --- a/inc/csharp/timespan.hpp +++ b/inc/csharp/timespan.hpp @@ -300,6 +300,14 @@ namespace xna { return a._ticks <= b._ticks; } + friend bool operator>(TimeSpan const& a, TimeSpan const& b) { + return a._ticks > b._ticks; + } + + friend bool operator>=(TimeSpan const& a, TimeSpan const& b) { + return a._ticks >= b._ticks; + } + private: int64_t _ticks{ 0 }; diff --git a/inc/graphics/viewport.hpp b/inc/graphics/viewport.hpp index 616763a..91280d6 100644 --- a/inc/graphics/viewport.hpp +++ b/inc/graphics/viewport.hpp @@ -1,6 +1,8 @@ #ifndef XNA_GRAPHICS_VIEWPORT #define XNA_GRAPHICS_VIEWPORT +#include "common/numerics.hpp" + namespace xna { struct Viewport { float X{ 0 }; @@ -23,6 +25,15 @@ namespace xna { && MinDetph == other.MinDetph && MaxDepth == other.MaxDepth; } + + constexpr Rectangle Bounds() const { + return { + static_cast(X), + static_cast(Y), + static_cast(Width), + static_cast(Height), + }; + } }; } diff --git a/samples/02_PlatfformerStarterKit/game.cpp b/samples/02_PlatfformerStarterKit/game.cpp index e9bfee1..3412d4b 100644 --- a/samples/02_PlatfformerStarterKit/game.cpp +++ b/samples/02_PlatfformerStarterKit/game.cpp @@ -119,6 +119,56 @@ namespace PlatformerStarterKit { LoadNextLevel(); } + void DrawHud() + { + auto titleSafeArea = graphicsDevice->Viewport().Bounds(); + auto hudLocation = Vector2(titleSafeArea.X, titleSafeArea.Y); + auto center = Vector2(titleSafeArea.X + titleSafeArea.Width / 2.0f, + titleSafeArea.Y + titleSafeArea.Height / 2.0f); + + //string timeString = "TIME: " + to_string(level->TimeRemaining().Minutes()) + ":" + to_string(level->TimeRemaining().Seconds()); + string timeString = "TIME"; + Color timeColor; + if (level->TimeRemaining() > WarningTime || + level->ReachedExit() || + static_cast(level->TimeRemaining().TotalSeconds()) % 2 == 0) + { + timeColor = Colors::Yellow; + } else { + timeColor = Colors::Red; + } + DrawShadowedString(*hudFont, timeString, hudLocation, timeColor); + + + float timeHeight = hudFont->MeasureString(timeString).Y; + DrawShadowedString(*hudFont, "SCORE: " + to_string(level->Score()), hudLocation + Vector2(0.0f, timeHeight * 1.2f), Colors::Yellow); + + PTexture2D status = nullptr; + if (level->TimeRemaining() == TimeSpan::Zero()) + { + if (level->ReachedExit()) { + status = winOverlay; + } else { + status = loseOverlay; + } + } + else if (!level->Player()->IsAlive()) { + status = diedOverlay; + } + + if (status) + { + const auto statusSize = Vector2(status->Width(), status->Height()); + spriteBatch->Draw(status, center - statusSize / 2, Colors::White); + } + } + + void DrawShadowedString(SpriteFont& font, String const& value, Vector2 const& position, Color const& color) + { + spriteBatch->DrawString(font, value, position + Vector2(1.0f, 1.0f), Colors::Black); + spriteBatch->DrawString(font, value, position, color); + } + private: sptr graphics = nullptr; sptr spriteBatch = nullptr; diff --git a/samples/02_PlatfformerStarterKit/level.cpp b/samples/02_PlatfformerStarterKit/level.cpp index 2a02a15..78be97f 100644 --- a/samples/02_PlatfformerStarterKit/level.cpp +++ b/samples/02_PlatfformerStarterKit/level.cpp @@ -10,7 +10,7 @@ namespace PlatformerStarterKit { Level::Level(xna::sptr const& serviceProvider, xna::String const& path) : path(path) { - //srand(354668); + srand(354668); content = xna::snew("Content", serviceProvider); timeRemaining = xna::TimeSpan::FromMinutes(2.0);