[Fix] Added missing header and source file for Bar.

[Add] Health bars for characters.
This commit is contained in:
Tamir Atias 2012-01-16 06:54:17 +02:00
parent c69213695a
commit 4481a46889
7 changed files with 134 additions and 1 deletions

View File

@ -196,7 +196,7 @@ void Game::UpdateGame(void) {
} }
void Game::Render(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) { if(_ingameMenu.GetStatus() == false) {
_map.Render(); _map.Render();
_player->Render(); _player->Render();

View File

@ -71,6 +71,8 @@ void Player::Update(void) {
// For now The camera will be static. // For now The camera will be static.
//SetCamera(); //SetCamera();
_healthBar.SetProgress((float)GetHealth() / 100.0f);
} }
void Player::SetName(string nameArg) { void Player::SetName(string nameArg) {

View File

@ -21,6 +21,9 @@ Character::Character(LevelGen* mapArg) {
_texture = NULL; _texture = NULL;
collisionList.push_front(this); collisionList.push_front(this);
_healthBar.SetBackgroundRGB(0, 0, 0);
_healthBar.SetForegroundRGB(255, 0, 0);
} }
Character::~Character(void) { Character::~Character(void) {
@ -50,6 +53,8 @@ void Character::LoadSprites(string filename, int wArg, int hArg) {
_sprites[m_direction][m_action].h = (Sint16)h; _sprites[m_direction][m_action].h = (Sint16)h;
} }
} }
_healthBar.SetWidthHeight((int)w, 10);
} }
void Character::AddSpeachBubble(string text) { void Character::AddSpeachBubble(string text) {
@ -113,6 +118,8 @@ void Character::Render(void) {
} }
ApplySurface((int)x, (int)y, _texture, screen, &_sprites[directionFacing][_animationStage]); ApplySurface((int)x, (int)y, _texture, screen, &_sprites[directionFacing][_animationStage]);
} }
_healthBar.Draw();
} }
void Character::Update(void) { void Character::Update(void) {
@ -131,6 +138,8 @@ void Character::Update(void) {
} }
} }
} }
_healthBar.SetProgress((float)_health / 100.0f);
} }
void Character::Move(void) { void Character::Move(void) {
@ -152,6 +161,8 @@ void Character::Move(void) {
if(CheckTileCollisions()) y -= yVel; if(CheckTileCollisions()) y -= yVel;
if(CheckEntityCollisions()) y -= yVel; if(CheckEntityCollisions()) y -= yVel;
if(CheckCharacterCollisions()) y -= yVel; if(CheckCharacterCollisions()) y -= yVel;
_healthBar.SetXY((int)x, (int)(y - _healthBar.GetHeight() - 5));
} }
/* /*

View File

@ -12,6 +12,7 @@
#include "../LevelGen/LevelGen.h" #include "../LevelGen/LevelGen.h"
#include "../System/Timer.h" #include "../System/Timer.h"
#include "../UI/Text.h" #include "../UI/Text.h"
#include "../UI/Bar.h"
#include "../System/Debug.h" #include "../System/Debug.h"
using namespace std; using namespace std;
@ -99,6 +100,8 @@ protected:
static const int ANIM_RIGHT_FOOT = 2; static const int ANIM_RIGHT_FOOT = 2;
static const int ANIM_ATTACK = 3; static const int ANIM_ATTACK = 3;
Bar _healthBar;
private: private:
static const int ANIMATION_SPEED = 200; static const int ANIMATION_SPEED = 200;
static const int ATTACKING_DISPLAY_LEN = 150; static const int ATTACKING_DISPLAY_LEN = 150;

View File

@ -22,6 +22,8 @@ void NPC::Update(void) {
else if(xVel < 0) directionFacing = FACING_LEFT; else if(xVel < 0) directionFacing = FACING_LEFT;
else if(yVel > 0) directionFacing = FACING_DOWN; else if(yVel > 0) directionFacing = FACING_DOWN;
else if(yVel < 0) directionFacing = FACING_UP; else if(yVel < 0) directionFacing = FACING_UP;
_healthBar.SetProgress((float)GetHealth() / 100.0f);
} }
void NPC::Move(void) { void NPC::Move(void) {

73
src/libUnuk/UI/Bar.cpp Normal file
View File

@ -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);
}

42
src/libUnuk/UI/Bar.h Normal file
View File

@ -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;
};