-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacter.cpp
More file actions
68 lines (55 loc) · 1.42 KB
/
Character.cpp
File metadata and controls
68 lines (55 loc) · 1.42 KB
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
63
64
65
66
67
68
#include "Character.h"
#include "Entity.h"
#include "Weapon.h"
#include <iostream>
Character::Character() : Character(0, sf::Vector2(0.f, 0.f), sf::Color::Cyan, 0, false, 0, nullptr) {}
Character::Character(int r, sf::Vector2f position, sf::Color color, float speed,
bool isPlayer, int health, Weapon *weapon)
: Entity(r, position, color, speed,
position, isPlayer),
weapon(weapon),
health(health)
{
this->color = color;
this->lastHit = -1000.0f;
}
Weapon *Character::getWeapon()
{
return weapon;
}
void Character::takeDamage(int damage)
{
this->health -= damage;
if (health <= 0)
{
this->isDestroyed = true;
}
}
int Character::getHealth()
{
return this->health;
}
std::optional<Projectile> Character::attack(sf::Vector2f destination)
{
return this->weapon->attack(this->getPosition(), destination, this->isPlayer);
}
Character::~Character()
{
delete weapon;
}
void Character::handleCollision(Projectile *projectile, sf::Clock hitTime)
{
if (Entity::checkCollision(projectile))
{
lastHit = hitTime.getElapsedTime().asSeconds();
takeDamage(projectile->getDamage());
}
}
void Character::updateFlash(sf::Clock clock)
{
float elapsed = clock.getElapsedTime().asSeconds();
if (elapsed - lastHit < 0.2f)
body->setFillColor(sf::Color::Red);
else
body->setFillColor(color);
}