Skip to content

Commit

Permalink
added basic health and round HUD (#3)
Browse files Browse the repository at this point in the history
* added basic health and round HUD

* render HUD over entities
  • Loading branch information
MorphewPrime authored Oct 31, 2021
1 parent d3f0847 commit cedcb03
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 1 deletion.
Binary file added fonts/Helvetica.ttf
Binary file not shown.
5 changes: 5 additions & 0 deletions src/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ const int& Entity::getHeight() const
return _height;
}

const int& Entity::getHealth() const
{
return _health;
}

void Entity::update(float deltaTime)
{
// When we update a frame we want to do a few things
Expand Down
8 changes: 8 additions & 0 deletions src/Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ class Entity
*/
const int& getHeight() const;


/**
* @brief Getter for entity health
*
* @return Health of entity
*/
const int& getHealth() const;

/// Main function to update an entity

/**
Expand Down
80 changes: 80 additions & 0 deletions src/GameManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ void GameManager::handleKeyboardEvent(sf::Event &kdbEvent)
if(this->_wave.getEnemy(i)->getIsAlive())
{
this->_wave.getEnemy(i)->kill();
this->_wave.updateAliveEnemyCount();
break;
}
}
Expand Down Expand Up @@ -199,6 +200,10 @@ void GameManager::drawFrame()
}
// TODO: Add other entities

// Draw the HUD over most things
drawHealthHUD();
drawRoundProgressHUD();

// Finally, display the window
_gameWindow.display();
}
Expand Down Expand Up @@ -244,3 +249,78 @@ void GameManager::drawMap()

this->_gameWindow.draw(sprite);
}

void GameManager::drawHealthHUD()
{
const int lineSize = 2;
const sf::Vector2f viewCenter = _gameWindow.getView().getCenter();
const sf::Vector2f &playerLocation = this->_player.getPosition();
const sf::Vector2f &viewSize = _view.getSize();
const sf::Vector2f barOutterSize{100.f, 10.f};
const sf::Vector2f barInnerSize{barOutterSize.x * (_player.getHealth() / 100), barOutterSize.y};
const sf::Vector2i padding{5 + lineSize, 5 + lineSize};
const sf::Vector2f barPosition{viewCenter.x + padding.x - (viewSize.x / 2), viewCenter.y - padding.y - barOutterSize.y + viewSize.y / 2};

// This is the outside grey/black rectangle.
sf::RectangleShape outsideRect(barOutterSize);
outsideRect.setPosition(barPosition);
outsideRect.setFillColor(sf::Color(45, 45, 45, 255));
outsideRect.setOutlineColor(sf::Color::Black);
outsideRect.setOutlineThickness(lineSize);
_gameWindow.draw(outsideRect);

// This is the inside red rectangle.
sf::RectangleShape insideRect(barInnerSize);
insideRect.setPosition(barPosition);
insideRect.setFillColor(sf::Color(255, 0, 0, 255));
_gameWindow.draw(insideRect);
}

void GameManager::drawRoundProgressHUD()
{
float enemiesAlive = (float)this->_wave.getEnemiesAlive();
float totalEnemies = (float)this->_wave.getEnemies();
int currWave = this->_wave.getWave();

const int lineSize = 2;
const sf::Vector2f viewCenter = _gameWindow.getView().getCenter();
const sf::Vector2f &playerLocation = this->_player.getPosition();
const sf::Vector2f &viewSize = _view.getSize();
const sf::Vector2f barOutterSize{100.f, 5.f};
const sf::Vector2f barInnerSize{barOutterSize.x * (enemiesAlive / totalEnemies), barOutterSize.y};
const sf::Vector2i padding{2 + lineSize, 2 + lineSize};
const sf::Vector2f barPosition{viewCenter.x - padding.x - barOutterSize.x / 2, viewCenter.y + padding.y + barOutterSize.y - viewSize.y / 2};

// This is the outside grey/black rectangle.
sf::RectangleShape outsideRect(barOutterSize);
outsideRect.setPosition(barPosition);
outsideRect.setFillColor(sf::Color(45, 45, 45, 255));
outsideRect.setOutlineColor(sf::Color::Black);
outsideRect.setOutlineThickness(lineSize);
_gameWindow.draw(outsideRect);

// This is the inside purple rectangle.
sf::RectangleShape insideRect(barInnerSize);
insideRect.setPosition(barPosition);
insideRect.setFillColor(sf::Color(128, 0, 187, 255));
_gameWindow.draw(insideRect);

sf::Text text;
sf::Font font;

if (!font.loadFromFile("fonts/Helvetica.ttf"))
{
printf("ERROR: font can not be loaded!!");
}

// Current wave number text
text.setFont(font);
text.setString(std::to_string(currWave));
text.setCharacterSize(lineSize * 2 + barOutterSize.y);
text.setFillColor(sf::Color::White);
text.setOutlineColor(sf::Color::Black);
text.setOutlineThickness(1);

text.setPosition(sf::Vector2f{barPosition.x - padding.x - (text.getGlobalBounds().left + text.getGlobalBounds().width), barPosition.y + lineSize - (text.getGlobalBounds().top + text.getGlobalBounds().height) / 2});
_gameWindow.draw(text);
}
14 changes: 13 additions & 1 deletion src/GameManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,21 @@ class GameManager
*/
void updateViewLocked();

/**
/**
* @brief Called from drawFrame(),
* Temporary function to draw a basic background of our map
*/
void drawMap();

/**
* @brief Called from drawFrame(),
* Draw the players health heads up display
*/
void drawHealthHUD();

/**
* @brief Called from drawFrame(),
* Draw a heads up display on the current round information
*/
void drawRoundProgressHUD();
};
6 changes: 6 additions & 0 deletions src/WaveManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ void WaveManager::beginWave(sf::Vector2<float> player)
currentWave++;
// For now, waves will progress linearly for simple demonstration sake
enemyCount = currentWave;
aliveEnemyCount = currentWave;
// Spawn enemies
Enemy* temp = nullptr;
for(int i=0; i<enemyCount; i++)
Expand Down Expand Up @@ -82,6 +83,11 @@ int WaveManager::getEnemies()
return(enemyCount);
}

int WaveManager::getEnemiesAlive()
{
return(aliveEnemyCount);
}

int WaveManager::getEnemiesRemaining()
{
int alive = 0;
Expand Down
1 change: 1 addition & 0 deletions src/WaveManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class WaveManager
void endWave();
int getWave();
int getEnemies();
int getEnemiesAlive();
int getEnemiesRemaining();
void updateWaves(sf::Vector2<float> player);
void updateAliveEnemyCount();
Expand Down

0 comments on commit cedcb03

Please sign in to comment.