Skip to content

Commit 3163aa6

Browse files
committed
implemented the Health bar
1 parent 000bcee commit 3163aa6

File tree

4 files changed

+35
-22
lines changed

4 files changed

+35
-22
lines changed

Engine/Classes/Player/Player.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class Player : public GameObject
110110
hand = new Hand(model);
111111
crosshair = new Crosshair(GetScreenWidth(), GetScreenHeight(), BLACK);
112112

113-
113+
healthComp = new PlayerHealthComp(m_max);
114114

115115
};
116116

@@ -223,8 +223,9 @@ class Player : public GameObject
223223
bool isRunning;
224224

225225
static inline Hand *hand;
226-
227226
Crosshair *crosshair;
227+
PlayerHealthComp *healthComp;
228+
228229
private:
229230

230231
bool startDriving = false;

Engine/Classes/Player/PlayerHealthBar.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@
44
class HealthBar
55
{
66
public:
7-
float CalculatePercentage(int hp, int max_hp)
7+
float CalculatePercentage(float hp, float max_hp)
88
{
99
float percentage = (float)hp / (float)max_hp;
1010
return percentage;
1111
}
1212

13-
void Draw(Vector2 position, int hp, int max_hp)
13+
void Draw(Vector2 position)
1414
{
15-
int percentage = CalculatePercentage(hp, max_hp);
15+
float percentage = CalculatePercentage(hp, maxHP);
1616

17-
DrawRectangle(position.x, position.y, 100, 20, BLACK);
18-
DrawRectangleLines(position.x, position.y, 100, 20, RED);
19-
DrawRectangle(position.x, position.y, 100 * percentage, 20, GREEN);
17+
DrawRectangle((int)position.x, (int)position.y, 100, 20, BLACK);
18+
DrawRectangleLines((int)position.x, (int)position.y, 100, 20, RED);
19+
DrawRectangle((int)position.x, (int)position.y, 100 * percentage, 20, GREEN);
2020
};
21+
22+
float hp;
23+
float maxHP;
2124
};

Engine/Classes/Player/PlayerHealthComp.h

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,41 @@
22
#include "../../../lib/raylib.h"
33
#include "PlayerHealthBar.h"
44

5-
struct PlayerHealthCompData
6-
{
7-
HealthBar healthBar = HealthBar();
8-
int hp;
9-
const int max_hp = 100;
10-
};
11-
125
class PlayerHealthComp
136
{
147

158
public:
16-
PlayerHealthCompData data;
17-
PlayerHealthComp()
9+
HealthBar *healthBar;
10+
11+
PlayerHealthComp(int maxHP)
1812
{
13+
healthBar = new HealthBar();
14+
15+
healthBar->hp = 100;
16+
healthBar->maxHP = maxHP;
17+
}
18+
19+
~PlayerHealthComp() {
20+
delete this;
1921
}
2022

21-
void DamagePlayer(int damage)
23+
void DamagePlayer(float damage)
2224
{
23-
data.hp -= damage;
25+
healthBar->hp -= damage;
2426
}
2527

2628
void HealPlayer(int heal)
2729
{
28-
data.hp += heal;
30+
healthBar->hp += heal;
2931
}
3032

3133
int GetMaxHealth()
3234
{
33-
return data.max_hp;
35+
return healthBar->maxHP;
3436
}
3537

3638
int GetHealth()
3739
{
38-
return data.hp;
40+
return healthBar->hp;
3941
}
4042
};

main.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,10 @@ DefaultBlockModel.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture = Global::Mesh
557557
} else {
558558
player->isRunning = false;
559559
}
560+
561+
if (IsKeyDown(KEY_Y)) {
562+
player->healthComp->DamagePlayer(0.1f);
563+
}
560564
// TODO : day night affect
561565

562566
// float h = Vector3Distance(player->getPosition(), Sun[0].position);
@@ -652,11 +656,14 @@ DefaultBlockModel.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture = Global::Mesh
652656
//BeginMode2D(menucamera->camera);
653657

654658
// Crosshair
659+
player->healthComp->healthBar->Draw({0.0f, (float)screenWidth + 500});
660+
655661
DrawCircle(game->windowWidth/2, game->windowHeight/2, 3, GRAY);
656662
//EndMode2D();
657663

658664
game->renderer->EndDraw();
659665

666+
660667
}
661668

662669

0 commit comments

Comments
 (0)