SpelunkyRemake / Hud.cpp
Hud.cpp
Raw
#include "pch.h"
#include "Hud.h"
#include <sstream>
#include <iomanip>

Hud::Hud(const Point2f& topLeft, int totalPowerUps)
	: m_Texture{ "Resources/Sprites/playerhudPRO.png" }
	, m_ItemsBarPos{ 96, 636 }
	, m_ItemsBarIconsPos{ 96, 638 }
	, m_ScoreBarPos{ 767, 638 }
	, m_TimerBarPos{ 927, 636 }
	, m_LevelBarPos{ 1078, 605 }
	, m_ItemsBarSrc{ 0, 30, 256, 30 }
	, m_ItemsIconsSrc{ 0, 156, 256, 30 }
	, m_ScoreBarSrc{ 0, 93, 160, 30 }
	, m_TimerBarSrc{ 0, 63, 256, 30 }
	, m_LevelBarSrc{ 0, 192, 100, 30 }
	, m_LivesTextPos{ 131, 635 }
	, m_BombsTextPos{ 196, 635 }
	, m_RopesTextPos{ 274, 635 }
	, m_ScoreTextPos{ 800, 635 }
	, m_TimerTextPos{ 961, 635 }
	, m_LevelTextPos{ 1124, 605 }
	, m_Score{ }
	, m_FontTextures
	{
		{'0', new Texture{ "0", "Resources/Fonts/Subscribe.ttf", 27, Color4f{ 1, 1, 1, 1 } } },
		{'1', new Texture{ "1", "Resources/Fonts/Subscribe.ttf", 27, Color4f{ 1, 1, 1, 1 } } },
		{'2', new Texture{ "2", "Resources/Fonts/Subscribe.ttf", 27, Color4f{ 1, 1, 1, 1 } } },
		{'3', new Texture{ "3", "Resources/Fonts/Subscribe.ttf", 27, Color4f{ 1, 1, 1, 1 } } },
		{'4', new Texture{ "4", "Resources/Fonts/Subscribe.ttf", 27, Color4f{ 1, 1, 1, 1 } } },
		{'5', new Texture{ "5", "Resources/Fonts/Subscribe.ttf", 27, Color4f{ 1, 1, 1, 1 } } },
		{'6', new Texture{ "6", "Resources/Fonts/Subscribe.ttf", 27, Color4f{ 1, 1, 1, 1 } } },
		{'7', new Texture{ "7", "Resources/Fonts/Subscribe.ttf", 27, Color4f{ 1, 1, 1, 1 } } },
		{'8', new Texture{ "8", "Resources/Fonts/Subscribe.ttf", 27, Color4f{ 1, 1, 1, 1 } } },
		{'9', new Texture{ "9", "Resources/Fonts/Subscribe.ttf", 27, Color4f{ 1, 1, 1, 1 } } },
		{'/', new Texture{ "/", "Resources/Fonts/Subscribe.ttf", 27, Color4f{ 1, 1, 1, 1 } } },
		{':', new Texture{ ":", "Resources/Fonts/Subscribe.ttf", 27, Color4f{ 1, 1, 1, 1 } } },
		{'.', new Texture{ ".", "Resources/Fonts/Subscribe.ttf", 27, Color4f{ 1, 1, 1, 1 } } },
		{'-', new Texture{ "-", "Resources/Fonts/Subscribe.ttf", 27, Color4f{ 1, 1, 1, 1 } } }
	}
	, m_FontOutlineTextures
	{
	{'0', new Texture{ "0", "Resources/Fonts/Subscribe.ttf", 27, Color4f{ 0, 0, 0, 1 }, Texture::USE_OUTLINE } },
	{'1', new Texture{ "1", "Resources/Fonts/Subscribe.ttf", 27, Color4f{ 0, 0, 0, 1 }, Texture::USE_OUTLINE } },
	{'2', new Texture{ "2", "Resources/Fonts/Subscribe.ttf", 27, Color4f{ 0, 0, 0, 1 }, Texture::USE_OUTLINE } },
	{'3', new Texture{ "3", "Resources/Fonts/Subscribe.ttf", 27, Color4f{ 0, 0, 0, 1 }, Texture::USE_OUTLINE } },
	{'4', new Texture{ "4", "Resources/Fonts/Subscribe.ttf", 27, Color4f{ 0, 0, 0, 1 }, Texture::USE_OUTLINE } },
	{'5', new Texture{ "5", "Resources/Fonts/Subscribe.ttf", 27, Color4f{ 0, 0, 0, 1 }, Texture::USE_OUTLINE } },
	{'6', new Texture{ "6", "Resources/Fonts/Subscribe.ttf", 27, Color4f{ 0, 0, 0, 1 }, Texture::USE_OUTLINE } },
	{'7', new Texture{ "7", "Resources/Fonts/Subscribe.ttf", 27, Color4f{ 0, 0, 0, 1 }, Texture::USE_OUTLINE } },
	{'8', new Texture{ "8", "Resources/Fonts/Subscribe.ttf", 27, Color4f{ 0, 0, 0, 1 }, Texture::USE_OUTLINE } },
	{'9', new Texture{ "9", "Resources/Fonts/Subscribe.ttf", 27, Color4f{ 0, 0, 0, 1 }, Texture::USE_OUTLINE } },
	{'/', new Texture{ "/", "Resources/Fonts/Subscribe.ttf", 27, Color4f{ 0, 0, 0, 1 }, Texture::USE_OUTLINE } },
	{':', new Texture{ ":", "Resources/Fonts/Subscribe.ttf", 27, Color4f{ 0, 0, 0, 1 }, Texture::USE_OUTLINE } },
	{'.', new Texture{ ".", "Resources/Fonts/Subscribe.ttf", 27, Color4f{ 0, 0, 0, 1 }, Texture::USE_OUTLINE } },
	{'-', new Texture{ "-", "Resources/Fonts/Subscribe.ttf", 27, Color4f{ 0, 0, 0, 1 }, Texture::USE_OUTLINE } }
}
{

}

Hud::~Hud()
{
	for (auto& it : m_FontTextures)
	{
		delete it.second;
	}

	for (auto& it : m_FontOutlineTextures)
	{
		delete it.second;
	}
}

void Hud::Update(float elapsedSec, const std::vector<Player::Info>& playersInfo, const Level::Info& levelInfo)
{
	if (m_Score < Player::m_Score)
	{
		m_Score = m_Score + (10 * elapsedSec) * (Player::m_Score + 0.9f - m_Score);
	}

	//for (Player::Info pInfo : playersInfo )
	{
		std::string bombs{ std::to_string(playersInfo.front().bombs) };
		m_BombsStr = bombs;

		std::string lives{ std::to_string(playersInfo.front().lives) };
		m_LivesStr = lives;

		std::string ropes{ std::to_string(playersInfo.front().ropes) };
		m_RopesStr = ropes;
	}
	
	int miliSeconds{ int(levelInfo.timer * 10) % 10 };
	int seconds{ int(levelInfo.timer) % 60 };
	int minutes{ int(levelInfo.timer / 60) % 60 };

	std::stringstream timer;
	timer << std::setw(2) << std::setfill('0') << std::to_string(minutes) << ':' << std::setw(2) << std::setfill('0') << std::to_string(seconds) << '.' << std::to_string(miliSeconds);
	m_TimerStr = timer.str();

	std::string level{ std::to_string(levelInfo.world) + '-' + std::to_string(levelInfo.stage) };
	m_LevelStr = level;

	std::string score{ std::to_string(int(m_Score)) };
	m_ScoreStr = score;
}

void Hud::Draw() const
{
	m_Texture.Draw(m_ItemsBarPos, m_ItemsBarSrc);
	m_Texture.Draw(m_ItemsBarIconsPos, m_ItemsIconsSrc);
	
	m_Texture.Draw(m_ScoreBarPos, m_ScoreBarSrc);
	m_Texture.Draw(m_TimerBarPos, m_TimerBarSrc);
	m_Texture.Draw(m_LevelBarPos, m_LevelBarSrc);

	DrawFont(m_ScoreTextPos, m_ScoreStr);

	DrawFont(m_LivesTextPos, m_LivesStr);
	DrawFont(m_BombsTextPos, m_BombsStr);
	DrawFont(m_RopesTextPos, m_RopesStr);

	DrawFont(m_TimerTextPos, m_TimerStr);
	DrawFont(Point2f{ m_TimerTextPos.x + 93, m_TimerTextPos.y + 2 }, "/");
	DrawFont(Point2f{ m_TimerTextPos.x + 119, m_TimerTextPos.y + 2 }, m_TimerStr);

	DrawFont(m_LevelTextPos, m_LevelStr);
}

void Hud::DrawFont(Point2f pos, const std::string& text) const
{
	for (char c : text)
	{
		const Texture* characterOutline{ m_FontOutlineTextures.at(c) };
		const Texture* character{ m_FontTextures.at(c) };

		characterOutline->Draw(Point2f{ pos.x - 2, pos.y + 2 });
		character->Draw(pos);

		pos.x += character->GetWidth();
	}
}