forked from Barnold1953/GraphicsTutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGun.cpp
More file actions
51 lines (41 loc) · 1.43 KB
/
Copy pathGun.cpp
File metadata and controls
51 lines (41 loc) · 1.43 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
#include "Gun.h"
#include <random>
#include <ctime>
#include <glm/gtx/rotate_vector.hpp>
#include "Grid.h"
Gun::Gun(std::string name, int fireRate, int bulletsPerShot,
float spread, float bulletDamage, float bulletSpeed, Bengine::SoundEffect fireEffect) :
_name(name),
_fireRate(fireRate),
_bulletsPerShot(bulletsPerShot),
_spread(spread),
_bulletDamage(bulletDamage),
_bulletSpeed(bulletSpeed),
_frameCounter(0),
m_fireEffect(fireEffect) {
// Empty
}
Gun::~Gun() {
// Empty
}
void Gun::update(bool isMouseDown, const glm::vec2& position, const glm::vec2& direction, Grid* grid, float deltaTime) {
_frameCounter += 1.0f * deltaTime;
// After a certain number of frames has passed we fire our gun
if (_frameCounter >= _fireRate && isMouseDown) {
fire(direction, position, grid);
_frameCounter = 0;
}
}
void Gun::fire(const glm::vec2& direction, const glm::vec2& position, Grid* grid) {
static std::mt19937 randomEngine(time(nullptr));
// For offsetting the accuracy
std::uniform_real_distribution<float> randRotate(-_spread, _spread);
m_fireEffect.play();
for (int i = 0; i < _bulletsPerShot; i++) {
// Add a new bullet
grid->addBullet(position - glm::vec2(BULLET_RADIUS),
glm::rotate(direction, randRotate(randomEngine)),
_bulletDamage,
_bulletSpeed);
}
}