From 4481a4688964318727567278b601a1b6e1c5fb85 Mon Sep 17 00:00:00 2001
From: Tamir Atias <engine.games@gmail.com>
Date: Mon, 16 Jan 2012 06:54:17 +0200
Subject: [PATCH] [Fix] Added missing header and source file for Bar. [Add]
 Health bars for characters.

---
 src/Unuk/Game.cpp                |  2 +-
 src/Unuk/Player.cpp              |  2 +
 src/libUnuk/Engine/Character.cpp | 11 +++++
 src/libUnuk/Engine/Character.h   |  3 ++
 src/libUnuk/Engine/NPC.cpp       |  2 +
 src/libUnuk/UI/Bar.cpp           | 73 ++++++++++++++++++++++++++++++++
 src/libUnuk/UI/Bar.h             | 42 ++++++++++++++++++
 7 files changed, 134 insertions(+), 1 deletion(-)
 create mode 100644 src/libUnuk/UI/Bar.cpp
 create mode 100644 src/libUnuk/UI/Bar.h

diff --git a/src/Unuk/Game.cpp b/src/Unuk/Game.cpp
index bf3fba5..07fcf2e 100644
--- a/src/Unuk/Game.cpp
+++ b/src/Unuk/Game.cpp
@@ -196,7 +196,7 @@ void Game::UpdateGame(void) {
 }
 
 void Game::Render(void) {
-	//SDL_FillRect(screen, NULL, 0); //  You might want to clear the buffer! --konom
+	SDL_FillRect(screen, NULL, 0); //  You might want to clear the buffer! --konom
   if(_ingameMenu.GetStatus() == false) {
     _map.Render();
     _player->Render();
diff --git a/src/Unuk/Player.cpp b/src/Unuk/Player.cpp
index f489cd1..628bcd7 100644
--- a/src/Unuk/Player.cpp
+++ b/src/Unuk/Player.cpp
@@ -71,6 +71,8 @@ void Player::Update(void) {
 
   // For now The camera will be static.
   //SetCamera();
+
+  _healthBar.SetProgress((float)GetHealth() / 100.0f);
 }
 
 void Player::SetName(string nameArg) {
diff --git a/src/libUnuk/Engine/Character.cpp b/src/libUnuk/Engine/Character.cpp
index 8892a12..f89d1ea 100644
--- a/src/libUnuk/Engine/Character.cpp
+++ b/src/libUnuk/Engine/Character.cpp
@@ -21,6 +21,9 @@ Character::Character(LevelGen* mapArg) {
   _texture = NULL;
   
   collisionList.push_front(this);
+
+  _healthBar.SetBackgroundRGB(0, 0, 0);
+  _healthBar.SetForegroundRGB(255, 0, 0);
 }
 
 Character::~Character(void) {
@@ -50,6 +53,8 @@ void Character::LoadSprites(string filename, int wArg, int hArg) {
       _sprites[m_direction][m_action].h = (Sint16)h;
     }
   }
+
+  _healthBar.SetWidthHeight((int)w, 10);
 }
 
 void Character::AddSpeachBubble(string text) {
@@ -113,6 +118,8 @@ void Character::Render(void) {
     }
     ApplySurface((int)x, (int)y, _texture, screen, &_sprites[directionFacing][_animationStage]);
   }
+
+  _healthBar.Draw();
 }
 
 void Character::Update(void) {
@@ -131,6 +138,8 @@ void Character::Update(void) {
       }
     }
   }
+
+  _healthBar.SetProgress((float)_health / 100.0f);
 }
 
 void Character::Move(void) {
@@ -152,6 +161,8 @@ void Character::Move(void) {
   if(CheckTileCollisions())                                          y -= yVel;
   if(CheckEntityCollisions())                                        y -= yVel;
   if(CheckCharacterCollisions())                                     y -= yVel;
+
+  _healthBar.SetXY((int)x, (int)(y - _healthBar.GetHeight() - 5));
 }
 
 /*
diff --git a/src/libUnuk/Engine/Character.h b/src/libUnuk/Engine/Character.h
index 965bc6f..36c3824 100644
--- a/src/libUnuk/Engine/Character.h
+++ b/src/libUnuk/Engine/Character.h
@@ -12,6 +12,7 @@
 #include "../LevelGen/LevelGen.h"
 #include "../System/Timer.h"
 #include "../UI/Text.h"
+#include "../UI/Bar.h"
 #include "../System/Debug.h"
 using namespace std;
 
@@ -99,6 +100,8 @@ protected:
   static const int ANIM_RIGHT_FOOT   = 2;
   static const int ANIM_ATTACK       = 3;
 
+  Bar _healthBar;
+
 private:
   static const int ANIMATION_SPEED         = 200;
   static const int ATTACKING_DISPLAY_LEN   = 150;
diff --git a/src/libUnuk/Engine/NPC.cpp b/src/libUnuk/Engine/NPC.cpp
index 9c4a560..c4d4e75 100644
--- a/src/libUnuk/Engine/NPC.cpp
+++ b/src/libUnuk/Engine/NPC.cpp
@@ -22,6 +22,8 @@ void NPC::Update(void) {
   else if(xVel < 0) directionFacing     = FACING_LEFT;
   else if(yVel > 0) directionFacing     = FACING_DOWN;
   else if(yVel < 0) directionFacing     = FACING_UP;
+
+  _healthBar.SetProgress((float)GetHealth() / 100.0f);
 }
 
 void NPC::Move(void) {
diff --git a/src/libUnuk/UI/Bar.cpp b/src/libUnuk/UI/Bar.cpp
new file mode 100644
index 0000000..403c503
--- /dev/null
+++ b/src/libUnuk/UI/Bar.cpp
@@ -0,0 +1,73 @@
+#include "Bar.h"
+
+Bar::Bar(void) {
+  x = 0;
+  y = 0;
+  width = 0;
+  height = 0;
+
+  SetProgress(1.0f);
+}
+
+void Bar::SetXY(int xArg, int yArg) {
+  x = xArg;
+  y = yArg;
+
+  _bgRect.SetXY(x, y);
+  _fgRect.SetXY(x, y);
+}
+
+void Bar::SetWidthHeight(int wArg, int hArg) {
+  if(width == wArg && height == hArg) {
+    return;
+  }
+  
+  width = wArg;
+  height = hArg;
+  
+  _bgRect.SetWidthHeight(width, height);
+
+  SetProgress(_progress);
+}
+
+void Bar::SetBackgroundRGB(Uint8 r, Uint8 g, Uint8 b) {
+  _bgRect.SetRGB(r, g, b);
+}
+
+void Bar::SetBackgroundRGB(SDL_Color colour) {
+  _bgRect.SetRGB(colour);
+}
+
+void Bar::SetForegroundRGB(Uint8 r, Uint8 g, Uint8 b) {
+  _fgRect.SetRGB(r, g, b);
+}
+
+void Bar::SetForegroundRGB(SDL_Color colour) {
+  _fgRect.SetRGB(colour);
+}
+
+void Bar::SetProgress(float progress) {
+  _progress = progress;
+  
+  _fgRect.SetWidthHeight((int)(progress * width), height);
+}
+
+void Bar::Draw(void) {
+  _bgRect.Draw();
+  _fgRect.Draw();
+}
+
+void Bar::Draw(int xArg, int yArg) {
+  _bgRect.Draw(xArg, yArg);
+  _fgRect.Draw(xArg, yArg);
+}
+
+void Bar::DrawLiteral(void) {
+  _bgRect.DrawLiteral();
+  _fgRect.DrawLiteral();
+}
+
+void Bar::DrawLiteral(int xArg, int yArg) {
+  _bgRect.DrawLiteral(xArg, yArg);
+  _fgRect.DrawLiteral(xArg, yArg);
+}
diff --git a/src/libUnuk/UI/Bar.h b/src/libUnuk/UI/Bar.h
new file mode 100644
index 0000000..27072da
--- /dev/null
+++ b/src/libUnuk/UI/Bar.h
@@ -0,0 +1,42 @@
+#pragma once
+#include <SDL/SDL.h>
+
+#include "../System/Rect.h"
+
+class Bar {
+public:
+  Bar(void);
+
+  int GetX(void)      const { return x;       }
+  int GetY(void)      const { return y;       }
+  int GetWidth(void)  const { return width;   }
+  int GetHeight(void) const { return height;  }
+
+  void SetXY(int xArg, int yArg);
+  void SetWidthHeight(int wArg, int hArg);
+
+  void SetBackgroundRGB(Uint8 r, Uint8 g, Uint8 b);
+  void SetBackgroundRGB(SDL_Color colour);
+
+  void SetForegroundRGB(Uint8 r, Uint8 g, Uint8 b);
+  void SetForegroundRGB(SDL_Color colour);
+
+  void  SetProgress(float progress);
+  float GetProgress(void) { return _progress; }
+
+  void Draw(void);
+  void Draw(int xArg, int yArg);
+  void DrawLiteral(void);
+  void DrawLiteral(int xArg, int yArg);
+
+private:
+  Rect _bgRect; // background
+  Rect _fgRect; // foreground
+  
+  float _progress;
+
+  int x;
+  int y;
+  int width;
+  int height;
+};