-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeapon.cpp
92 lines (69 loc) · 1.82 KB
/
Weapon.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
#include "Weapon.h"
Weapon::Weapon()
{
damage = 10.0f;
delayTime = 0.25f;
}
float RayIntersectKensler(glm::vec3 origin, glm::vec3 direction, BoundingBox* box) {
float tmin = 0.0f;
float tmax = 10000.0f;
glm::vec3 bmin = box->getMin();
glm::vec3 bmax = box->getMax();
for (int i = 0; i < 3; i++) {
float invD = 1.0f / direction[i];
float t0 = (bmin[i] - origin[i]) * invD;
float t1 = (bmax[i] - origin[i]) * invD;
if (invD < 0.0f) {
float temp = t1;
t1 = t0;
t0 = temp;
}
tmin = t0 > tmin ? t0 : tmin;
tmax = t1 < tmax ? t1 : tmax;
if (tmax <= tmin)
return -1;
}
return tmin;
}
BoundingBox* closestObject(std::vector<BoundingBox *> boundingBoxList, glm::vec3 origin, glm::vec3 direction) {
BoundingBox* minBox = NULL;
//std::cerr << "Considering " << boundingBoxList.size() << "Objects" << std::endl;
float mindist = -1;
for (int i = 0; i < boundingBoxList.size(); i++) {
if (!boundingBoxList[i]->getActive())
{
continue;
}
float dist = RayIntersectKensler(origin, direction, boundingBoxList[i]);
if (dist == -1) {
continue;
}
else if (dist > 0 & (mindist == -1 || (dist < mindist))) {
mindist = dist;
//std::cerr << "MinDist " << mindist << std::endl;
minBox = boundingBoxList[i];
}
}
if (mindist == -1) {
return NULL;
}
else {
return minBox;
}
}
BoundingBox * Weapon::Shoot(std::vector<BoundingBox *> objects, glm::vec3 origin, glm::vec3 direction)
{
//std::cerr << "Origin " << origin.x << " " << origin.y << " " << origin.z << std::endl;
//std::cerr << "Direction " << direction.x << " " << direction.y << " " << direction.z << std::endl;
BoundingBox * minBox = closestObject(objects, origin, direction);
if (minBox != NULL) {
return minBox;
}
return NULL;
}
void Weapon::Reload()
{
reloading = true;
currentAmmo = maxAmmo;
reloading = false;
}