-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNPC.cpp
More file actions
227 lines (177 loc) · 5.58 KB
/
NPC.cpp
File metadata and controls
227 lines (177 loc) · 5.58 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
224
225
226
227
//================================================//
#include "NPC.hpp"
//================================================//
NPC::NPC(void)
{
m_pHitTimer = new Ogre::Timer();
}
//================================================//
void NPC::takeDamage(Ogre::Real amount)
{
m_settings.health -= amount;
if(m_settings.health <= 0.0){
m_state = STATE_DEAD;
die();
}
// Hit reaction
m_pHitTimer->reset();
m_pEntity->setMaterialName("red");
m_hit = true;
}
//================================================//
void NPC::die(void)
{
m_pSceneNode->detachAllObjects();
//Base::getSingletonPtr()->m_btWorld->removeCollisionObject(m_collisionObject);
}
//================================================//
void NPC::setAnimationState(Ogre::String str)
{
m_pAnimationState = m_pEntity->getAnimationState(str);
m_pAnimationState->setTimePosition(0.0);
m_pAnimationState->setLoop(true);
m_pAnimationState->setEnabled(true);
}
//================================================//
void NPC::update(double timeSinceLastFrame)
{
// Activate/Deactivate as needed
switch(m_settings.activationMethod){
default:
break;
case ACTIVATE_BY_RANGE:
if(m_pSceneNode->_getDerivedPosition().distance(m_pPlayerNode->getPosition()) < m_settings.activationRange){
m_state = STATE_ACTIVE;
}
break;
case ACTIVATE_BY_LOOKAT:
{
Rayhit* rayhit = m_pCamera->getNegativeZRayhit();
if(rayhit->hasHit){
if(rayhit->node == m_pSceneNode){
if(rayhit->distance <= m_settings.activationRange){
m_state = STATE_ACTIVE;
}
}
}
}
break;
}
if(m_settings.animate){
m_pAnimationState->addTime(timeSinceLastFrame * m_settings.animationSpeed);
}
// Update hit reaction
if(m_hit){
if(m_pHitTimer->getMillisecondsCPU() >= 150){
m_pEntity->setMaterialName("blue");
m_hit = false;
}
}
}
//================================================//
//================================================//
Monster::Monster(void) : NPC()
{
m_friendly = false;
m_state = STATE_IDLE;
}
//================================================//
void Monster::init(Ogre::SceneManager* mgr, Ogre::SceneNode* node, btCollisionObject* obj, Sparks::Camera* camera)
{
// Set pointers
m_pSceneMgr = mgr;
m_pSceneNode = node;
m_collisionObject = obj;
m_pCamera = camera;
m_pPlayerNode = m_pCamera->getSceneNode();
m_pSceneNode->setFixedYawAxis(true);
// Fetch user data and load
const Ogre::Any& any = m_pSceneNode->getUserAny();
if(!any.isEmpty()){
NPC::NPC_DATA* data = Ogre::any_cast<NPC::NPC_DATA*>(any);
memcpy(&m_settings, data, sizeof(NPC_DATA));
}
// Determine forwards vector
switch(m_settings.forwards){
case FORWARDS_NEGATIVE_UNIT_Z:
default:
m_forwards = Ogre::Vector3::NEGATIVE_UNIT_Z;
break;
case FORWARDS_NEGATIVE_UNIT_Y:
m_forwards = Ogre::Vector3::NEGATIVE_UNIT_Y;
break;
case FORWARDS_NEGATIVE_UNIT_X:
m_forwards = Ogre::Vector3::NEGATIVE_UNIT_X;
break;
case FORWARDS_UNIT_Z:
m_forwards = Ogre::Vector3::UNIT_Z;
break;
case FORWARDS_UNIT_Y:
m_forwards = Ogre::Vector3::UNIT_Y;
break;
case FORWARDS_UNIT_X:
m_forwards = Ogre::Vector3::UNIT_X;
break;
}
// Setup animation
m_pEntity = static_cast<Ogre::Entity*>(m_pSceneNode->getAttachedObject(0));
if(m_pEntity->hasAnimationState("Idle")){
setAnimationState("Idle");
}
// Create scene node
/*m_pEntity = m_pSceneMgr->createEntity(data.name, "cylinder.mesh");
m_pSceneNode = m_pSceneMgr->getRootSceneNode()->createChildSceneNode(data.name + "_Node");
m_pSceneNode->attachObject(m_pEntity);
m_pSceneNode->setPosition(data.pos);
m_pSceneNode->scale(10.0, 10.0, 10.0);*/
// Create the Bullet Physics rigid body
/*btTransform transform;
transform.setIdentity();
transform.setOrigin(btVector3(data.pos.x, data.pos.y, data.pos.z));
data.shape.radius = m_pEntity->getBoundingBox().getSize().z;
data.shape.height = m_pEntity->getBoundingBox().getSize().y;
btDefaultMotionState* motionState = new btDefaultMotionState(transform);
btCollisionShape* shape = new btCapsuleShape(data.shape.radius, data.shape.height);
btVector3 localInertia;
shape->calculateLocalInertia(data.mass, localInertia);
m_btBody = new btRigidBody(data.mass, motionState, shape, localInertia);
m_btBody->setUserPointer((void*)m_pSceneNode);
m_btBody->setFriction(0.5);
m_btBody->setDamping(0.4, 0.4);
m_btBody->setAngularFactor(btVector3(0, 0, 0));
m_btBody->setGravity(data.gravity);
Base::getSingletonPtr()->m_btWorld->addRigidBody(m_btBody);*/
//Base::getSingletonPtr()->m_btObjects.push_back(m_btBody);
//Base::getSingletonPtr()->m_btObjectCount++;
}
//================================================//
void Monster::update(double timeSinceLastFrame)
{
NPC::update(timeSinceLastFrame);
switch(m_state){
case STATE_IDLE:
default:
return;
case STATE_ACTIVE:
updateAI(timeSinceLastFrame);
return;
}
}
//================================================//
void Monster::updateAI(double timeSinceLastFrame)
{
Ogre::Vector3 pos = m_pSceneNode->_getDerivedPosition();
Ogre::Quaternion rot = m_pSceneNode->_getDerivedOrientation();
// ...add ability for shooting the NPC and making it disappear
m_pSceneNode->lookAt(m_pPlayerNode->getPosition(), Ogre::Node::TS_WORLD, m_forwards);
Ogre::Vector3 dir = m_pPlayerNode->getPosition() - pos;
dir.normalise();
dir /= 250.0;
m_pSceneNode->translate(dir * timeSinceLastFrame);
// Update Bullet transforms
btTransform transform = m_collisionObject->getWorldTransform();
transform.setOrigin(btVector3(pos.x, pos.y, pos.z));
transform.setRotation(btQuaternion(rot.x, rot.y, rot.z, rot.w));
m_collisionObject->setWorldTransform(transform);
}
//================================================//