-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealthBar.cpp
62 lines (59 loc) · 1.82 KB
/
healthBar.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "healthBar.h"
#include <iostream>
healthBar::healthBar() {
healthBar(nullptr, SDL_Rect(), 0, 0);
}
healthBar::healthBar(SDL_Renderer * renderer, SDL_Rect rect, int maxHealth, bool leftAlign) {
bar = TextureManager::loadTexture("./assets/healthBar.png", renderer);
healthBar::rect = rect;
healthBar::leftAlign = leftAlign;
healthBar::maxHealth = maxHealth;
healthBar::damageEffectValue = maxHealth;
healthBar::currentHealth = maxHealth;
healthBar::fadeDamageEffect = 0.33;
healthBar::fadeDamageSpeed = 25;
}
void healthBar::setHealth(int health) {
if (health == currentHealth) return;
currentHealth = health;
lastHealthChangeTime = 0;
}
void healthBar::update(double deltaTime) {
lastHealthChangeTime += (double)deltaTime;
if (lastHealthChangeTime > fadeDamageEffect && damageEffectValue > currentHealth) {
damageEffectValue -= deltaTime * fadeDamageSpeed;
}
}
void healthBar::render(SDL_Renderer * renderer) {
double delta2 = damageEffectValue / maxHealth;
double delta1 = (double)currentHealth / maxHealth;
SDL_Rect barRect = SDL_Rect();
barRect.x = rect.x;
barRect.y = rect.y;
barRect.w = rect.w * delta1;
barRect.h = rect.h;
SDL_Rect srcRect = getBarRect(0);
SDL_RenderCopy(renderer, bar, &srcRect, &barRect);
barRect = SDL_Rect();
barRect.x = rect.x + rect.w * delta1;
barRect.y = rect.y;
barRect.w = rect.w * (delta2 - delta1);
barRect.h = rect.h;
srcRect = getBarRect(1);
SDL_RenderCopy(renderer, bar, &srcRect, &barRect);
barRect = SDL_Rect();
barRect.x = rect.x + rect.w * delta2;
barRect.y = rect.y;
barRect.w = rect.w - rect.w * delta2;
barRect.h = rect.h;
srcRect = getBarRect(2);
SDL_RenderCopy(renderer, bar, &srcRect, &barRect);
}
SDL_Rect healthBar::getBarRect(int i) {
SDL_Rect barRect = SDL_Rect();
barRect.x = 0;
barRect.y = 16 * i;
barRect.w = 64;
barRect.h = 16;
return barRect;
}