-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBow.cpp
More file actions
30 lines (26 loc) · 1.02 KB
/
Copy pathBow.cpp
File metadata and controls
30 lines (26 loc) · 1.02 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
#include "Bow.h"
Bow::Bow() : Bow(0, 0, 0, 0, 0) {}
Bow::Bow(int damage, int fireRate, int projSize, int projSpeed, int range)
: Weapon(damage, fireRate, projSize, projSpeed),
range(range) {}
sf::Vector2f Bow::calculateRange(sf::Vector2f position, sf::Vector2f destination, int range)
{
sf::Vector2f direction = destination - position;
float length = std::sqrt(direction.x * direction.x + direction.y * direction.y);
if (length != 0)
{
direction /= length;
}
return position + direction * static_cast<float>(range);
}
std::optional<Projectile> Bow::attack(sf::Vector2f position, sf::Vector2f destination, bool isPlayer)
{
if (((float)(clock() - lastAttack) / CLOCKS_PER_SEC) > fireRate)
{
this->lastAttack = clock();
sf::Vector2f endpoint = calculateRange(position, destination, range);
// compared to Bow the endpoint = destination
return Projectile(projSize, position, sf::Color::Red, projSpeed, endpoint, isPlayer, damage);
}
return std::nullopt;
}