-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnight.cpp
More file actions
62 lines (57 loc) · 2.54 KB
/
Knight.cpp
File metadata and controls
62 lines (57 loc) · 2.54 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
#include "Knight.h"
//constructor parametrizat cu apel la constructorul din clasa parinte
Knight::Knight(sf::Vector2f position)
: Entity(200.f, 200.f, sf::Vector2f(280.f, 280.f), position, "knightR.png")
{}
//functie de clonare/copiere a unei entitati knight
std::unique_ptr<Entity> Knight::clone() const
{
return std::make_unique<Knight>(*this);
}
//functie de scriere in consola a unor mesaje depinzand de stadiul jocului
std::string Knight::getLevelMessage(int gameStage) const
{
if (gameStage == 1) return "One more to go!";
if (gameStage == 2) return "What is happening? Oh, no...the King arises!";
return "";
}
//functie suprascrisa pt miscarea cavalerilor automat
void Knight::moveEntity(const sf::ConvexShape& walls, float delta_t, Entity& knight, Entity* player)
{
float speed = 250.f;
sf::Vector2f enemyPosition = knight.getEntity().getPosition();
sf::Vector2f playerPosition = player->getEntity().getPosition();
sf::Vector2f direction = playerPosition - enemyPosition;
float dist = std::sqrt(std::pow(direction.x, 2) + std::pow(direction.y, 2));
if (dist > 5.f)
{
direction /= dist; //normalizez directia
float angleRad = std::atan2(direction.y, direction.x);
float angleDeg = angleRad * 180.f / 3.14159265f;
knight.getEntity().setRotation(sf::degrees(angleDeg + 0.f)); //pentru o miscare mai smooth pe directie
sf::RectangleShape newPlayer = player->getEntity();
newPlayer.move(direction * speed * delta_t);
if (isPlayerWithinWalls(walls, newPlayer))
{//simulez o miscare pentru a verifica daca am parte de collisions sau nu
knight.getEntity().move(direction * speed * delta_t);
}
}
else
{
float angleRad = std::atan2(playerPosition.y - enemyPosition.y, playerPosition.x - enemyPosition.x);
float angleDeg = angleRad * 180.f / 3.14159265f;
knight.getEntity().setRotation(sf::degrees(angleDeg + 0.f));
}
}
//functie suprascrisa de atac asupra eroului, determinata de o distanta intre cele doua pozitii
void Knight::attack(sf::RectangleShape& player, sf::RectangleShape& enemy, float attackRange, float& playerHP, sf::Clock& attackTimer)
{
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() >= 1.5f)
{
playerHP -= 10.f;
attackTimer.restart();
}
}