-
Notifications
You must be signed in to change notification settings - Fork 0
/
health-component.cpp
46 lines (41 loc) · 1.38 KB
/
health-component.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
#include <iostream>
#include <SDL2/SDL.h>
#include "game.hpp"
#include "defs.hpp"
#include "health-component.hpp"
#include "graphics-component.hpp"
HealthComponent::HealthComponent(int h, int i) {
health_points = h;
health_capacity = h;
iframe_capacity = i;
}
void HealthComponent::receiveDamage(Entity* entity, int d) {
if (iframes == 0 && !dead) {
health_points -= d;
dead = health_points <= 0;
iframes = iframe_capacity;
}
}
void HealthComponent::restoreHealth(int r) {
health_points += r;
health_points = fmax(health_points, health_capacity);
}
void HealthComponent::update(Entity* entity) {
isHurt = false;
if (iframes > 0) {
isHurt = true;
iframes--;
}
}
void HealthComponent::draw(Entity* entity) {
SDL_Rect health_point_rect = { Game::camera->xpos + entity->xpos, Game::camera->ypos + entity->ypos - 30, health_points, 20};
SDL_Rect health_capacity_rect = { Game::camera->xpos + entity->xpos, Game::camera->ypos + entity->ypos - 30, health_capacity, 20};
SDL_SetRenderDrawColor(Game::renderer, 100, 0, 0, 255);
SDL_RenderFillRect(Game::renderer, &health_capacity_rect);
if (iframes > 0) {
SDL_SetRenderDrawColor(Game::renderer, 0, 220, 0, 255);
} else {
SDL_SetRenderDrawColor(Game::renderer, 220, 0, 0, 255);
}
SDL_RenderFillRect(Game::renderer, &health_point_rect);
}