Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //PlayerHealthBar.h
- #pragma once
- #include "stdafx.h"
- #include "GUIBase.h"
- class Sprite;
- //class SpriteManager;
- class PlayerHealthBar : public GUIBase {
- public:
- PlayerHealthBar(sf::Texture *p_frontTexture, sf::Texture *p_backTexture);
- ~PlayerHealthBar();
- void Update(int p_currentHealth, unsigned int p_maxHealth);
- void draw(sf::RenderTarget &p_window, sf::RenderStates p_renderStates = sf::RenderStates::Default) const;
- private:
- int m_backgroundHeight, m_backgroundWidth;
- };
- //PlayerHealthBar.cpp
- #include "stdafx.h"
- #include "PlayerHealthBar.h"
- #include "Sprite.h"
- PlayerHealthBar::PlayerHealthBar(sf::Texture *p_frontTexture, sf::Texture *p_backTexture) : GUIBase()
- {
- m_sprite = new Sprite[2];
- m_sprite[0].setTexture(*p_backTexture);
- m_sprite[1].setTexture(*p_frontTexture);
- m_backgroundHeight = m_sprite[0].getTexture()->getSize().y;
- m_backgroundWidth = m_sprite[0].getTexture()->getSize().x;
- }
- PlayerHealthBar::~PlayerHealthBar()
- {
- delete [] m_sprite;
- }
- void PlayerHealthBar::Update(int p_currentHealth, unsigned int p_maxHealth)
- {
- m_sprite[0].setPosition(m_position);
- m_sprite[1].setPosition(m_position.x - 2, m_position.y - 11);
- float percentage = ((float)p_currentHealth / p_maxHealth);
- sf::IntRect size(0, //X base
- m_backgroundHeight - (m_backgroundHeight * percentage), //Y base
- m_backgroundWidth, //Width
- m_backgroundHeight * percentage); //Height
- m_sprite[0].setTextureRect(size);
- }
- void PlayerHealthBar::draw(sf::RenderTarget &p_window, sf::RenderStates p_states) const {
- p_window.draw(m_sprite[0]);
- p_window.draw(m_sprite[1]);
- }
Advertisement
Add Comment
Please, Sign In to add comment