Skip to content

Commit ea3f7a3

Browse files
committed
changed health display text to sprites
1 parent 88483a4 commit ea3f7a3

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

Galaga/HealthDisplayComponent.cpp

+32-8
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,61 @@
11
#include "HealthDisplayComponent.h"
2-
#include "Text.h"
2+
#include "Sprite.h"
33
#include "PlayerCharacter.h"
44
#include "GameObject.h"
55
#include "IEventParam.h"
66
#include "CharactersManager.h"
77
#include <sstream>
8+
#include <memory>
9+
#include <format>
810

9-
HealthDisplayComponent::HealthDisplayComponent(Fluffy::GameObject* pOwner, int playerIndex, const std::string& fontPath, int fontSize)
11+
HealthDisplayComponent::HealthDisplayComponent(Fluffy::GameObject* pOwner, int playerIndex)
1012
: Fluffy::Component(pOwner)
1113
, m_PlayerIndex{ playerIndex }
1214
{
13-
if (PlayerCharacter* pPlayer{ CharactersManager::GetInstance().GetPlayer(playerIndex) })
15+
if (PlayerCharacter* pPlayer{ CharactersManager::GetInstance()->GetPlayer(playerIndex) })
1416
{
1517
pPlayer->GetOnDeath().AddListener(this);
16-
}
1718

18-
m_pText = pOwner->AddComponent<Fluffy::Text>(GetLivesString(), fontPath, fontSize);
19+
std::string fileName = std::format("galaga_player{}.png", playerIndex + 1);
20+
const float padding{ 5.0f };
21+
float spriteWidth{ 0.0f };
22+
23+
for (int i{ 0 }; i < pPlayer->GetLivesCount() - 1; ++i)
24+
{
25+
m_Lives.push_back(pOwner->AddComponent<Fluffy::Sprite>(fileName, glm::vec2((spriteWidth + padding) * i, 0.0f)));
26+
27+
if (i == 0)
28+
spriteWidth = m_Lives[0]->GetTextureSize().x;
29+
}
30+
}
1931
}
2032

2133
HealthDisplayComponent::~HealthDisplayComponent()
2234
{
23-
if (PlayerCharacter* pPlayer{ CharactersManager::GetInstance().GetPlayer(m_PlayerIndex) })
35+
if (CharactersManager::GetInstance() == nullptr)
36+
return;
37+
38+
if (PlayerCharacter* pPlayer{ CharactersManager::GetInstance()->GetPlayer(m_PlayerIndex) })
2439
{
2540
pPlayer->GetOnDeath().RemoveListener(this);
2641
}
2742
}
2843

2944
void HealthDisplayComponent::OnNotify(const Fluffy::EventType& eventType, const Fluffy::IEventParam*)
3045
{
46+
int lives{ 0 };
47+
3148
switch (eventType)
3249
{
3350
case Fluffy::EventType::OnCharacterDeath:
34-
m_pText->SetText(GetLivesString());
51+
if (PlayerCharacter * pPlayer{ CharactersManager::GetInstance()->GetPlayer(m_PlayerIndex) })
52+
{
53+
lives = pPlayer->GetLivesCount();
54+
}
55+
56+
if (lives != 0)
57+
m_Lives[lives-1]->SetEnabled(false);
58+
3559
break;
3660
}
3761
}
@@ -43,7 +67,7 @@ std::string HealthDisplayComponent::GetLivesString() const
4367

4468
int lives{ 0 };
4569

46-
if (PlayerCharacter* pPlayer{ CharactersManager::GetInstance().GetPlayer(m_PlayerIndex) })
70+
if (PlayerCharacter* pPlayer{ CharactersManager::GetInstance()->GetPlayer(m_PlayerIndex) })
4771
{
4872
lives = pPlayer->GetLivesCount();
4973
}

Galaga/HealthDisplayComponent.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace Fluffy
77
{
88
class Component;
99
class GameObject;
10+
class Sprite;
1011
class IEventListener;
1112
class Event;
1213
class Text;
@@ -15,14 +16,15 @@ namespace Fluffy
1516
class HealthDisplayComponent final : public Fluffy::Component, Fluffy::IEventListener
1617
{
1718
public:
18-
HealthDisplayComponent(Fluffy::GameObject* pOwner, int playerIndex, const std::string& fontPath, int fontSize);
19+
HealthDisplayComponent(Fluffy::GameObject* pOwner, int playerIndex);
1920
~HealthDisplayComponent();
2021

2122
void OnNotify(const Fluffy::EventType& eventType, const struct Fluffy::IEventParam* param) override;
2223

2324
private:
2425
const int m_PlayerIndex;
25-
Fluffy::Text* m_pText;
26+
27+
std::vector<Fluffy::Sprite*> m_Lives;
2628

2729
std::string GetLivesString() const;
2830
};

0 commit comments

Comments
 (0)