-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlameTower.cpp
102 lines (86 loc) · 3.07 KB
/
FlameTower.cpp
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
94
95
96
97
98
99
100
101
102
#include "FlameTower.h"
#define _USE_MATH_DEFINES
#include <math.h>
FlameTower::FlameTower(glm::vec3 &entityPos, GLuint entityTexture, GLint entityNumElements, GLuint bulletTex) :
TowerObject (entityPos, entityTexture, entityNumElements, bulletTex)
{
fireRate = 5;
sep = 0.4f;
target = nullptr;
for (int i = 0; i < 4; ++i) {
bullets.push_back(new BulletObject(position, bulletTexture, entityNumElements));
}
for (int i = 0; i < bullets.size(); ++i) {
if (i < 4) {
bullets[i]->setScale(i * 1.5f);
}
else {
bullets[i]->setScale(3 * 1.5f);
}
bullets[i]->setPosition(glm::vec3(i * cos(glm::radians(orientation)) * sep + position.x,
i * sin(glm::radians(orientation)) * sep + position.y, 1.0f));
bullets[i]->setDamage(30);
}
}
FlameTower::~FlameTower()
{
}
void FlameTower::update(float deltaTime, std::vector<EnemyObject*> targets) {
if (target == nullptr ||
(target != nullptr && target->kill) ||
(target != nullptr && glm::length(glm::vec2(target->getPosition().x - position.x, position.y - target->getPosition().y)) > 3.0f)) {
float shortest = -1;
for (int i = 0; i < targets.size(); ++i) {
float tmp = glm::length(glm::vec2(targets[i]->getPosition().x - position.x, position.y - targets[i]->getPosition().y));
if (shortest == -1 || tmp < shortest) {
shortest = tmp;
target = targets[i];
}
}
isShooting = false;
}
// calculate orientation
if (target != nullptr && glm::length(glm::vec2(target->getPosition().x - position.x, position.y - target->getPosition().y)) < 3.0f) {
float dx = target->getPosition().x - position.x;
float dy = position.y - target->getPosition().y;
float radians = atan2f(dy, dx);
if (radians < 0) {
radians = abs(radians);
}
else {
radians = 2 * M_PI - radians;
}
float degrees = glm::degrees(radians);
orientation = degrees;
isShooting = glm::length(glm::vec2(target->getPosition().x - position.x, position.y - target->getPosition().y)) < 2.5f;
}
//updateBullets(deltaTime, target);
if (fireCooldown > 0)
fireCooldown -= deltaTime;
else {
fireCooldown = 1.0f / fireRate;
//spawn bullet here
//bullets.push_back(new BulletObject(position, bulletTexture, numElements));
}
}
void FlameTower::updateBullets(float deltaTime, std::vector<EnemyObject *> targets) {
bool fireFlag = false;
for (int i = 0; i < bullets.size(); ++i) {
bullets[i]->setPosition(glm::vec3(i * cos(glm::radians(orientation)) * sep + position.x,
i * sin(glm::radians(orientation)) * sep + position.y, 1.0f));
}
for (std::vector<BulletObject*>::iterator it = bullets.begin(); it != bullets.end(); ++it) {
for (int j = 0; j < targets.size(); ++j) {
if (fireCooldown == 1.0f / fireRate && (*it)->hitsTarget(targets[j])) {
targets[j]->takeDamage((*it)->getDamage());
float dist = glm::length(glm::vec2(targets[j]->getPosition().x - position.x, position.y - targets[j]->getPosition().y));
if (dist < 1.5f) {
targets[j]->takeDamage((*it)->getDamage());
}
}
}
}
}
void FlameTower::render(Shader &shader) {
GameObject::render(shader);
}