Enfisk

PlayerHealthBar

Feb 20th, 2014
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. //PlayerHealthBar.h
  2. #pragma once
  3. #include "stdafx.h"
  4. #include "GUIBase.h"
  5.  
  6. class Sprite;
  7. //class SpriteManager;
  8.  
  9. class PlayerHealthBar : public GUIBase {
  10. public:
  11.     PlayerHealthBar(sf::Texture *p_frontTexture, sf::Texture *p_backTexture);
  12.     ~PlayerHealthBar();
  13.  
  14.     void Update(int p_currentHealth, unsigned int p_maxHealth);
  15.  
  16.     void draw(sf::RenderTarget &p_window, sf::RenderStates p_renderStates = sf::RenderStates::Default) const;
  17.  
  18. private:
  19.     int m_backgroundHeight, m_backgroundWidth;
  20. };
  21.  
  22.  
  23. //PlayerHealthBar.cpp
  24. #include "stdafx.h"
  25. #include "PlayerHealthBar.h"
  26. #include "Sprite.h"
  27.  
  28.  
  29. PlayerHealthBar::PlayerHealthBar(sf::Texture *p_frontTexture, sf::Texture *p_backTexture) : GUIBase()
  30. {
  31.     m_sprite = new Sprite[2];
  32.  
  33.     m_sprite[0].setTexture(*p_backTexture);
  34.     m_sprite[1].setTexture(*p_frontTexture);
  35.  
  36.     m_backgroundHeight = m_sprite[0].getTexture()->getSize().y;
  37.     m_backgroundWidth = m_sprite[0].getTexture()->getSize().x;
  38. }
  39.  
  40. PlayerHealthBar::~PlayerHealthBar()
  41. {
  42.     delete [] m_sprite;
  43. }
  44.  
  45. void PlayerHealthBar::Update(int p_currentHealth, unsigned int p_maxHealth)
  46. {
  47.     m_sprite[0].setPosition(m_position);
  48.     m_sprite[1].setPosition(m_position.x - 2, m_position.y - 11);
  49.  
  50.     float percentage = ((float)p_currentHealth / p_maxHealth);
  51.     sf::IntRect size(0,                                                             //X base
  52.                      m_backgroundHeight - (m_backgroundHeight * percentage),        //Y base
  53.                      m_backgroundWidth,                                             //Width
  54.                      m_backgroundHeight * percentage);                              //Height
  55.    
  56.     m_sprite[0].setTextureRect(size);
  57. }
  58.  
  59. void PlayerHealthBar::draw(sf::RenderTarget &p_window, sf::RenderStates p_states) const {
  60.     p_window.draw(m_sprite[0]);
  61.     p_window.draw(m_sprite[1]);
  62. }
Advertisement
Add Comment
Please, Sign In to add comment