-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProjectile.cpp
More file actions
223 lines (166 loc) · 5.12 KB
/
Projectile.cpp
File metadata and controls
223 lines (166 loc) · 5.12 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//================================================//
#include "Projectile.hpp"
//================================================//
Projectile::~Projectile(void)
{
}
//================================================//
void Projectile::init(Ogre::SceneManager* mgr, PROJECTILE_DATA& data, NPCManager* npcManager)
{
// Set NPCManager pointer
m_pNPCManager = npcManager;
// Set physics pointer
m_physics = npcManager->getPhysics();
// Set particle data
m_origin = data.origin;
m_direction = data.direction;
m_speed = data.speed;
m_damage = data.damage;
m_range = data.range;
m_colour = data.colour;
// Set scene manager pointer
m_pSceneMgr = mgr;
// Create the light and billboard
m_pLight = m_pSceneMgr->createLight();
m_pLight->setDiffuseColour(m_colour);
m_pLight->setSpecularColour(m_colour);
m_pLight->setAttenuation(1000, 1, 0.0005, 0);
m_pBbSet = m_pSceneMgr->createBillboardSet(1);
// TODO: Use switch statement here to get the right texture/model from projectile data
m_pBbSet->setMaterialName("Examples/Flare3");
m_pBb = m_pBbSet->createBillboard(0, 0, 0, m_colour);
// Create the scene node
m_pSceneNode = m_pSceneMgr->getRootSceneNode()->createChildSceneNode();
m_pSceneNode->attachObject(m_pLight);
m_pSceneNode->attachObject(m_pBbSet);
m_pSceneNode->setPosition(m_origin);
m_alive = true;
m_distanceMoved = 0.0;
m_lastPosition = m_origin;
}
//================================================//
void Projectile::cleanup(void)
{
m_alive = false;
m_pSceneNode->detachAllObjects();
m_pBbSet->removeBillboard(m_pBb);
m_pSceneMgr->destroyBillboardSet(m_pBbSet);
m_pSceneMgr->destroyLight(m_pLight);
}
//================================================//
bool Projectile::isColliding(void)
{
btVector3 from(m_lastPosition.x, m_lastPosition.y, m_lastPosition.z);
btVector3 to(m_pSceneNode->getPosition().x, m_pSceneNode->getPosition().y, m_pSceneNode->getPosition().z);
btCollisionWorld::ClosestRayResultCallback res(from, to);
m_physics->getWorld()->rayTest(from, to, res);
// set the new "last" position
m_lastPosition = m_pSceneNode->getPosition();
if(res.hasHit()){
Ogre::SceneNode* node = static_cast<Ogre::SceneNode*>(res.m_collisionObject->getUserPointer());
// Impact effect
// ...
if(node){
Ogre::String name = node->getName();
Ogre::StringUtil strUtil;
// Test if projectile hit an NPC
if(strUtil.startsWith(name, "NPC_", false)){
std::vector<NPC*> NPCs = m_pNPCManager->getNPCs();
if(NPCs.size() > 0){
for(std::vector<NPC*>::iterator itr = NPCs.begin();
itr != NPCs.end();
++itr){
Ogre::String npcName = static_cast<Ogre::String>((*itr)->getSceneNode()->getName());
if(strUtil.match(name, npcName, true)){
printf("Projectile hit %s\n", npcName.c_str());
// Send damage to NPC
(*itr)->takeDamage(m_damage);
}
}
}
}
}
return true;
}
return false;
}
//================================================//
void Projectile::update(double timeSinceLastFrame)
{
Ogre::Vector3 translate = m_direction * m_speed * timeSinceLastFrame;
// Translate the projectile node
m_pSceneNode->translate(translate);
// Test for collision
if(isColliding()){
cleanup();
}
// Test for range
m_distanceMoved += translate.length();
if(m_distanceMoved > m_range){
cleanup();
}
}
//================================================//
// *** //
//================================================//
ProjectileManager::ProjectileManager(Ogre::SceneManager* mgr, Physics* physics)
{
m_pSceneMgr = mgr;
m_physics = physics;
}
//================================================//
ProjectileManager::~ProjectileManager(void)
{
}
//================================================//
void ProjectileManager::addProjectile(Projectile::PROJECTILE_DATA& data)
{
m_projectiles.push_back(new Projectile()); // memory leak?
m_projectiles.back()->init(m_pSceneMgr, data, m_pNPCManager);
}
//================================================//
void ProjectileManager::clearAll(void)
{
for(std::vector<Projectile*>::iterator itr = m_projectiles.begin();
itr != m_projectiles.end();){
if((*itr)->isAlive()){
(*itr)->cleanup();
}
delete *itr;
itr = m_projectiles.erase(itr);
}
}
//================================================//
void ProjectileManager::update(double timeSinceLastFrame)
{
// Update all the projectiles and remove dead ones
for(std::vector<Projectile*>::iterator itr = m_projectiles.begin();
itr != m_projectiles.end();){
if((*itr)->isAlive()){
(*itr)->update(timeSinceLastFrame);
++itr;
}
else{
delete *itr;
itr = m_projectiles.erase(itr);
}
}
// Update the player projectiles
//for(std::vector<Projectile*>::iterator itr = m_projectiles.begin();
// itr != m_projectiles.end();
// ++itr){
// // prevent the iterator from going out of bounds
// if(m_projectiles.empty())
// break;
//
// // update it if it's alive
// if((*itr)->isAlive()){
// (*itr)->update(timeSinceLastFrame);
// numProjectiles++;
// }
//}
//// Clear vector when player is not firing
//if(numProjectiles == 0)
// m_projectiles.clear();
}
//================================================//