From e420699080f8e6d13315dc93867d213cf7b62f54 Mon Sep 17 00:00:00 2001
From: Claudio Hoffmann <hoffmanncl72341@th-nuernberg.de>
Date: Sat, 16 Jan 2021 02:40:09 +0100
Subject: [PATCH] Entity: Use the player/enemy projectile colors for the hit
 animation

---
 Enemyprojectile.cpp  |  8 ++++----
 Enemyprojectile.hpp  |  2 ++
 Entity.cpp           |  7 +++++--
 Entity.hpp           |  6 ++++--
 Level.cpp            | 10 +++++-----
 Level.hpp            |  2 +-
 PixelShaders.cpp     |  2 +-
 PixelShaders.hpp     | 10 ++++++++--
 playerprojectile.cpp |  8 ++++----
 playerprojectile.hpp |  2 ++
 10 files changed, 36 insertions(+), 21 deletions(-)

diff --git a/Enemyprojectile.cpp b/Enemyprojectile.cpp
index 8fc99db..88108a6 100644
--- a/Enemyprojectile.cpp
+++ b/Enemyprojectile.cpp
@@ -27,7 +27,7 @@ bool EnemyProjectile::move(FloatSeconds const & Frametime, Level &level)
             {
                this->y -= speed*Frametime.count();
                 if(level.HitEnemy(this->x, this->y, sprite.w(), sprite.h())
-                        || level.HitObstacle(this->x, this->y, sprite.w(), sprite.h()))
+                        || level.HitObstacle(this->x, this->y, ENEMY_PROJECTILE_COLOR, sprite.w(), sprite.h()))
                 {
                     //Player hit
                     return false;
@@ -45,7 +45,7 @@ bool EnemyProjectile::move(FloatSeconds const & Frametime, Level &level)
         {
            this->x -= speed*Frametime.count();
             if(level.HitEnemy(this->x, this->y, sprite.w(), sprite.h())
-                    || level.HitObstacle(this->x, this->y, sprite.w(), sprite.h()))
+                    || level.HitObstacle(this->x, this->y, ENEMY_PROJECTILE_COLOR, sprite.w(), sprite.h()))
             {
                 //Player hit
                 return false;
@@ -63,7 +63,7 @@ bool EnemyProjectile::move(FloatSeconds const & Frametime, Level &level)
             {
                this->x += speed*Frametime.count();
                 if(level.HitEnemy(this->x, this->y, sprite.w(), sprite.h())
-                        || level.HitObstacle(this->x, this->y, sprite.w(), sprite.h()))
+                        || level.HitObstacle(this->x, this->y, ENEMY_PROJECTILE_COLOR, sprite.w(), sprite.h()))
                 {
                     //Player hit
                     return false;
@@ -81,7 +81,7 @@ bool EnemyProjectile::move(FloatSeconds const & Frametime, Level &level)
             {
                this->y += speed*Frametime.count();
                if(level.HitEnemy(this->x, this->y, sprite.w(), sprite.h())
-                       || level.HitObstacle(this->x, this->y, sprite.w(), sprite.h()))
+                       || level.HitObstacle(this->x, this->y, ENEMY_PROJECTILE_COLOR, sprite.w(), sprite.h()))
                {
                    //Player hit
                    return false;
diff --git a/Enemyprojectile.hpp b/Enemyprojectile.hpp
index da75429..87a5372 100644
--- a/Enemyprojectile.hpp
+++ b/Enemyprojectile.hpp
@@ -1,6 +1,8 @@
 #ifndef ENEMYPROJECTILE_HPP
 #define ENEMYPROJECTILE_HPP
 #include "Projectile.hpp"
+
+Color constexpr ENEMY_PROJECTILE_COLOR = _2;
 class EnemyProjectile: public Projectile
 {
 public:
diff --git a/Entity.cpp b/Entity.cpp
index da56f29..df138d4 100644
--- a/Entity.cpp
+++ b/Entity.cpp
@@ -55,17 +55,20 @@ bool Entity::step(Position const &PlayerPos, FloatSeconds const & Frametime, Lev
 	return ((hitpoints > 0) || (hittime > 0.0f));
 };
 
-int Entity::takeDamage()
+int Entity::takeDamage(Color color)
 {
 	hitpoints = std::max((hitpoints - 1), 0);
 	hittime = HIT_TIME;
+	hitcolor = color;
 	return hitpoints;
 }
 
 void Entity::blit(DynamicPixelBuffer &buf)
 {
 	if(hittime > 0.0f) {
-		HitShader hs{ getSprite(), (hittime / HIT_TIME), (hitpoints == 0) };
+		HitShader hs{
+			getSprite(), (hittime / HIT_TIME), hitcolor, (hitpoints == 0)
+		};
 		buf.blit_topleft(topleft, hs);
 	} else if(hitpoints > 0) {
 		buf.blit_topleft(topleft, getSprite());
diff --git a/Entity.hpp b/Entity.hpp
index 5949607..64b1a72 100644
--- a/Entity.hpp
+++ b/Entity.hpp
@@ -16,6 +16,7 @@ protected:
 	Position topleft;
 	int hitpoints = 1;
 	float hittime = 0.0f;
+	Color hitcolor;
 
 	// Entity-specific step method.
 	virtual void doStep(
@@ -50,8 +51,9 @@ public:
 		Position const &PlayerPos, FloatSeconds const & Frametime, Level &level
 	);
 
-	// Removes a single hit point. Returns the new number of hit points.
-	int takeDamage();
+	// Removes a single hit point and sets the hit animation color to [color].
+	// Returns the new number of hit points.
+	int takeDamage(Color color);
 	/// -------------
 
 	/// Rendering
diff --git a/Level.cpp b/Level.cpp
index f1f29f6..6e379de 100644
--- a/Level.cpp
+++ b/Level.cpp
@@ -221,7 +221,7 @@ bool Level::HitEnemy(float posX, float posY, int bufferWidth, int bufferHeigth)
 {
     if(collHelper(player.getTopLeft(),player.getSprite(),posX,posY,bufferWidth,bufferHeigth))
     {
-        player.takeDamage();
+        player.takeDamage(ENEMY_PROJECTILE_COLOR);
         return true;
     }
     return false;
@@ -234,7 +234,7 @@ bool Level::HitPlayer(float posX, float posY, int bufferWidth, int bufferHeigth)
         {
             //HauerCh71498
             //{
-            if(i->takeDamage() == 0)
+            if(i->takeDamage(PLAYER_PROJECTILE_COLOR) == 0)
             {
                 score += i->getScore();
             }
@@ -247,19 +247,19 @@ bool Level::HitPlayer(float posX, float posY, int bufferWidth, int bufferHeigth)
 /*For projectiles, Gameover*/
 
 // <kruegerzo72182
-bool Level::HitObstacle(float posX, float posY, int bufferWidth, int bufferHeigth)
+bool Level::HitObstacle(float posX, float posY, Color hitColor, int bufferWidth, int bufferHeigth)
 {
     for(auto &i : wallVector){
         if(collHelper(i.getTopLeft(), i.getSprite(), posX, posY, bufferWidth, bufferHeigth))
         {
-            i.takeDamage();
+            i.takeDamage(hitColor);
             return true;
         }
     }
     for(auto &i : sceneryVector) {
         if(collHelper(i.getTopLeft(), i.getSprite(), posX, posY, bufferWidth, bufferHeigth))
         {
-            i.takeDamage();
+            i.takeDamage(hitColor);
             return true;
         }
     }
diff --git a/Level.hpp b/Level.hpp
index cf47afa..7c0524b 100644
--- a/Level.hpp
+++ b/Level.hpp
@@ -139,7 +139,7 @@ public:
     bool HitEnemy(float posX, float posY, int bufferWidth, int bufferHeigth);
     // Did a player-controlled entity hit an Enemy?
     bool HitPlayer(float posX, float posY, int bufferWidth, int bufferHeigth);
-    bool HitObstacle(float posX, float posY, int bufferWidth, int bufferHeigth);
+    bool HitObstacle(float posX, float posY, Color hitColor, int bufferWidth, int bufferHeigth);
     void enemyVector_PushBack(std::unique_ptr<Enemy> enemy_);
     void doDoSteps(FloatSeconds const &frame_time);
     std::vector<std::unique_ptr<Projectile>>& getProjectileVector();
diff --git a/PixelShaders.cpp b/PixelShaders.cpp
index 2552824..99000fa 100644
--- a/PixelShaders.cpp
+++ b/PixelShaders.cpp
@@ -22,5 +22,5 @@ Color HitShader::peekUnchecked(PixelPoint const &pos) const
 	if(src == _t) {
 		return _t;
 	}
-	return (dist(rng.rng) > 0.95f) ? _3 : (is_final ? _t : src);
+	return (dist(rng.rng) > 0.95f) ? color : (is_final ? _t : src);
 }
diff --git a/PixelShaders.hpp b/PixelShaders.hpp
index cb83bc9..7dd2d05 100644
--- a/PixelShaders.hpp
+++ b/PixelShaders.hpp
@@ -6,10 +6,16 @@
 class HitShader : public ShadedPixelBuffer {
 public:
 	float percent;
+	Color color;
 	bool is_final;
 
-	HitShader(PixelBuffer const &input, float percent, bool is_final)
-		: ShadedPixelBuffer(input), percent(percent), is_final(is_final) {
+	HitShader(
+		PixelBuffer const &input, float percent, Color color, bool is_final
+	) :
+		ShadedPixelBuffer(input),
+		percent(percent),
+		color(color),
+		is_final(is_final) {
 	}
 
 	virtual Color peekUnchecked(PixelPoint const &pos) const;
diff --git a/playerprojectile.cpp b/playerprojectile.cpp
index 6c56438..49ea513 100644
--- a/playerprojectile.cpp
+++ b/playerprojectile.cpp
@@ -27,7 +27,7 @@ bool PlayerProjectile::move(FloatSeconds const & Frametime, Level &level)
             {
                this->y -= speed*Frametime.count();
                 if(level.HitPlayer(this->x, this->y, sprite.w(), sprite.h())
-                        || level.HitObstacle(this->x, this->y, sprite.w(), sprite.h()))
+                        || level.HitObstacle(this->x, this->y, PLAYER_PROJECTILE_COLOR, sprite.w(), sprite.h()))
                 {
                     //Player hit
                     return false;
@@ -45,7 +45,7 @@ bool PlayerProjectile::move(FloatSeconds const & Frametime, Level &level)
         {
            this->x -= speed*Frametime.count();
             if(level.HitPlayer(this->x, this->y, sprite.w(), sprite.h())
-                    || level.HitObstacle(this->x, this->y, sprite.w(), sprite.h()))
+                    || level.HitObstacle(this->x, this->y, PLAYER_PROJECTILE_COLOR, sprite.w(), sprite.h()))
             {
                 //Player hit
                 return false;
@@ -63,7 +63,7 @@ bool PlayerProjectile::move(FloatSeconds const & Frametime, Level &level)
             {
                this->x += speed*Frametime.count();
                 if(level.HitPlayer(this->x, this->y, sprite.w(), sprite.h())
-                        || level.HitObstacle(this->x, this->y, sprite.w(), sprite.h()))
+                        || level.HitObstacle(this->x, this->y, PLAYER_PROJECTILE_COLOR, sprite.w(), sprite.h()))
                 {
                     //Player hit
                     return false;
@@ -81,7 +81,7 @@ bool PlayerProjectile::move(FloatSeconds const & Frametime, Level &level)
             {
                this->y += speed*Frametime.count();
                if(level.HitPlayer(this->x, this->y, sprite.w(), sprite.h())
-                       || level.HitObstacle(this->x, this->y, sprite.w(), sprite.h()))
+                       || level.HitObstacle(this->x, this->y, PLAYER_PROJECTILE_COLOR, sprite.w(), sprite.h()))
                {
                    //Player hit
                    return false;
diff --git a/playerprojectile.hpp b/playerprojectile.hpp
index 425aaae..fdcb29b 100644
--- a/playerprojectile.hpp
+++ b/playerprojectile.hpp
@@ -1,6 +1,8 @@
 #ifndef PLAYERPROJECTILE_HPP
 #define PLAYERPROJECTILE_HPP
 #include "Projectile.hpp"
+
+Color constexpr PLAYER_PROJECTILE_COLOR = _6;
 class PlayerProjectile: public Projectile
 {
 public:
-- 
GitLab