-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeapon.cpp
More file actions
55 lines (49 loc) · 1.74 KB
/
Weapon.cpp
File metadata and controls
55 lines (49 loc) · 1.74 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
#include <optional>
#include <iostream>
#include "Weapon.h"
Weapon::Weapon() : Weapon(0, 0, 0, 0, 0) {}
sf::SoundBuffer Weapon::sharedBuffer;
bool Weapon::bufferLoaded = false;
Weapon::Weapon(int damage, float fireRate, int projSize, float projSpeed, float range)
: damage(damage),
fireRate(fireRate),
projSize(projSize),
projSpeed(projSpeed),
range(range)
{
this->lastAttack = clock();
if (!bufferLoaded)
{
sharedBuffer.loadFromFile("click.wav");
}
sound.setBuffer(sharedBuffer);
}
sf::Vector2f Weapon::calculateRange(sf::Vector2f position, sf::Vector2f destination)
{
// Calculate the direction vector from position to destination
sf::Vector2f direction = destination - position;
// Normalize the direction vector
// If the length is zero, return the position (no movement)
float length = std::sqrt(direction.x * direction.x + direction.y * direction.y);
if (length != 0)
{
direction /= length;
}
// Scale the direction vector by the range of the weapon
return direction * range + position;
}
std::optional<Projectile> Weapon::attack(sf::Vector2f position, sf::Vector2f destination, bool isPlayer)
{
// Check if enough time has passed since the last attack
if (((float)(clock() - lastAttack) / CLOCKS_PER_SEC) < fireRate)
{
return std::nullopt;
}
sound.play();
// Update the last attack time
this->lastAttack = clock();
// Calculate the endpoint of the projectile based on the range
sf::Vector2f endpoint = calculateRange(position, destination);
// compared to Weapon the endpoint = destination
return std::optional<Projectile>{Projectile(projSize, position, sf::Color::Red, projSpeed, endpoint, isPlayer, damage)};
}