From 9bc58dca5ee10a54250dc67bd650b890cdd11491 Mon Sep 17 00:00:00 2001
From: Claudio Hoffmann <hoffmanncl72341@th-nuernberg.de>
Date: Mon, 18 Jan 2021 16:34:32 +0100
Subject: [PATCH] Player: Visualize reload time

---
 player.cpp | 52 ++++++++++++++++++++++++++++++++++++++--------------
 player.hpp | 13 +++++++++++++
 2 files changed, 51 insertions(+), 14 deletions(-)

diff --git a/player.cpp b/player.cpp
index cf393d3..9fde6ab 100644
--- a/player.cpp
+++ b/player.cpp
@@ -1,7 +1,8 @@
 #include "player.hpp"
 #include "Level.hpp"
 #include "playerprojectile.hpp"
-Player::Player(float PosX, float PosY)
+Player::Player(float PosX, float PosY) :
+    shader(*this)
 {
     hitpoints = 5;
     topleft.X=PosX;
@@ -125,23 +126,46 @@ static StaticPixelBuffer<PLAYER_W, PLAYER_H> constexpr left[2] = {
      }}}
     };
 
-PixelBuffer const& Player::getSprite()
+Player::Shader::Shader(Player const &player) :
+    PixelBuffer(PLAYER_W, (PLAYER_H + 2)), player(player)
 {
-    switch ( dir ) {
+}
 
-    case direction::BACK:
-        return back[animationstep];
-        break;
-    case direction::LEFT:
-        return left[animationstep];
-        break;
-    case direction::RIGHT:
-        return right[animationstep];
-        break;
+Color Player::Shader::peekUnchecked(PixelPoint const &pos) const
+{
+    auto const &basesprite = [this]() {
+        switch (player.dir) {
+        case direction::BACK:
+            return back[player.animationstep];
+            break;
+        case direction::LEFT:
+            return left[player.animationstep];
+            break;
+        case direction::RIGHT:
+            return right[player.animationstep];
+            break;
 
-    default:
-        return front[animationstep];
+        default:
+            return front[player.animationstep];
+        }
+    }();
+    if(player.timetillreload == 0.0f) {
+        return basesprite.peek(pos);
     }
+
+    auto reloadpercent = (player.timetillreload / RELOAD_TIME);
+    auto reloadwidth = static_cast<int>(reloadpercent * PLAYER_W);
+	int constexpr BAR_H = 1;
+
+	if((pos.y >= (h() - BAR_H)) && (pos.x <= reloadwidth)) {
+		return PLAYER_PROJECTILE_COLOR;
+	}
+	return basesprite.peek(pos);
+}
+
+PixelBuffer const& Player::getSprite()
+{
+    return shader;
 }
 void Player::move(Platform &p, FloatSeconds const & Frametime, Level &level)
 {
diff --git a/player.hpp b/player.hpp
index f90864f..aaa14cd 100644
--- a/player.hpp
+++ b/player.hpp
@@ -8,15 +8,28 @@
 
 int constexpr PLAYER_W = 17;
 int constexpr PLAYER_H = 11;
+float constexpr RELOAD_TIME = 1.0f;
 
 class Player : public Entity
 {
 protected:
+    // Turned out to be way overkill given that we only draw a bar below the
+    // sprite...
+    class Shader : public PixelBuffer {
+    protected:
+        virtual Color peekUnchecked(PixelPoint const &pos) const;
+    public:
+        Player const &player;
+
+        Shader(Player const &player);
+    };
+
     int speed=48;
     direction dir=direction::BACK;
     int animationstep=0;
     float timetillreload=0.0f;
     float animationtimer=0.0f;
+    Shader shader;
 public:
     Player(float x, float y);
     PixelBuffer const& getSprite() override;
-- 
GitLab