-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHero.cpp
More file actions
93 lines (86 loc) · 2.96 KB
/
Hero.cpp
File metadata and controls
93 lines (86 loc) · 2.96 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "Hero.h"
Hero::Hero(sf::Vector2f position)
: Entity(200.f, 200.f, sf::Vector2f(280.f, 280.f), position, "heroF-removebg-preview.png")
{
}
std::unique_ptr<Entity> Hero::clone() const
{
return std::make_unique<Hero>(*this);
}
std::string Hero::getLevelMessage(int gameStage) const
{
if (gameStage == 0) return "Let's get the party started!";
return "";
}
void Hero::moveEntity(const sf::ConvexShape& walls, float delta_t, Entity& player, Entity* enemy)
{
const float speed = 400.f;
sf::Vector2f movement(0.f, 0.f);
//ma folosesc de evenimente pe tastatura pentru a misca eroul dupa bunul plac al jucatorului
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W))
{
movement.y -= speed * delta_t;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S))
{
movement.y += speed * delta_t;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A))
{
movement.x -= speed * delta_t;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D))
{
movement.x += speed * delta_t;
}
//aici sintetizez o miscare smooth pentru a crea iluzia de deplasare pe diagonala( eroul sa fie indreptat in sensul de mers)
if (movement.x != 0.f || movement.y != 0.f)
{
float length = std::sqrt(movement.x * movement.x + movement.y * movement.y);
movement.x /= length;
movement.y /= length;
movement.x *= speed * delta_t;
movement.y *= speed * delta_t;
float rotationAngle = std::atan2(movement.y, movement.x);
float rotationDeg = rotationAngle * 180.f / 3.14159265f;
player.getEntity().setRotation(sf::degrees(rotationDeg + 180.f));
}
//foloses exceptia pentru coliziuni( in cazul in care playerul vrea sa depaseasca limitele impuse)
try
{
sf::RectangleShape newPlayer = player.getEntity();
newPlayer.move(movement);
if (!isPlayerWithinWalls(walls, newPlayer))
{
throw InvalidMovementException();
}
player.getEntity().move(movement);
}
catch (const InvalidMovementException& e)
{
std::cerr << e.what() << std::endl;
}
}
void Hero::attack(sf::RectangleShape& player, sf::RectangleShape& enemy, float attackRange, float& enemyHP, sf::Clock& attackTimer)
{
//exceptie pentru attack out of range->pentru a nu se scadea HP-ul in cazul in care un atac a fost considerat invalid
try
{
sf::Vector2f playerPos = player.getPosition();
sf::Vector2f enemyPos = enemy.getPosition();
float distance = std::sqrt(std::pow(playerPos.x - enemyPos.x, 2) + std::pow(playerPos.y - enemyPos.y, 2));
if (distance <= attackRange && attackTimer.getElapsedTime().asSeconds() >= 0.5f)
{
enemyHP -= 10.f;
attackTimer.restart();
}
else
{
throw InvalidAttackException();
}
}
catch (const InvalidAttackException& e)
{
std::cerr << e.what() << std::endl;
}
}