/
Ninja.cpp
267 lines (226 loc) · 6.96 KB
/
Ninja.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "Ninja.h"
/*
void setupAnimations();
void setBaseAnimation(AnimID id, bool reset = false);
void setTopAnimation(AnimID id, bool reset = false);
void fadeAnimations(Ogre::Real deltaTime);
void updateAnimations(Ogre::Real deltaTime);
*/
Ninja::Ninja(bool isNinja, std::string name, std::string filename, float height, float angleOffset) :
Agent(name, filename, 0, angleOffset)
{
using namespace Ogre;
this->mBodyNode->scale(Ogre::Vector3(.06f, .06f, .06f));
if (sceneMgr == NULL)
return;
this->isNinja = isNinja;
//this->height = 0;
this->angleOffset = 270;
setupAnimations();
mWalkSpeed = 25.0f;
mDirection = Ogre::Vector3::ZERO;
}
void Ninja::setupAnimations()
{
this->mTimer = 0;
this->mVerticalVelocity = 0;
// this is very important due to the nature of the exported animations
mBodyEntity->getSkeleton()->setBlendMode(Ogre::ANIMBLEND_CUMULATIVE);
Ogre::String animNames[] =
{"Attack1", "Attack2", "Attack3", "Backflip", "Block", "Climb", "Crouch",
"Death1", "Death2", "HighJump", "Idle1", "Idle2", "Idle3", "Jump", "JumpNoHeight", "Kick", "SideKick", "Spin", "Stealth", "Walk"};
// populate our animation list
for (int i = 0; i < NINJA_NUM_ANIMS; i++)
{
mAnims[i] = mBodyEntity->getAnimationState(animNames[i]);
mAnims[i]->setLoop(true);
mFadingIn[i] = false;
mFadingOut[i] = false;
}
// start off in the idle state (top and bottom together)
setBaseAnimation(ANIM_IDLE_1);
//setTopAnimation(ANIM_IDLE_1);
// relax the hands since we're not holding anything
//mAnims[ANIM_HANDS_RELAXED]->setEnabled(true);
}
void Ninja::setBaseAnimation(AnimID id, bool reset)
{
if (mBaseAnimID >= 0 && mBaseAnimID < NINJA_NUM_ANIMS)
{
// if we have an old animation, fade it out
mFadingIn[mBaseAnimID] = false;
mFadingOut[mBaseAnimID] = true;
}
mBaseAnimID = id;
if (id != ANIM_NONE)
{
// if we have a new animation, enable it and fade it in
mAnims[id]->setEnabled(true);
mAnims[id]->setWeight(0);
mFadingOut[id] = false;
mFadingIn[id] = true;
if (reset) mAnims[id]->setTimePosition(0);
}
}
/*
void Ninja::setTopAnimation(AnimID id, bool reset)
{
if (Ninja::mTopAnimID >= 0 && Ninja::mTopAnimID < NINJA_NUM_ANIMS)
{
// if we have an old animation, fade it out
mFadingIn[mTopAnimID] = false;
mFadingOut[mTopAnimID] = true;
}
Ninja::mTopAnimID = id;
if (id != ANIM_NONE)
{
// if we have a new animation, enable it and fade it in
mAnims[id]->setEnabled(true);
mAnims[id]->setWeight(0);
mFadingOut[id] = false;
mFadingIn[id] = true;
if (reset) mAnims[id]->setTimePosition(0);
}
}*/
void Ninja::fadeAnimations(Ogre::Real deltaTime)
{
using namespace Ogre;
for (int i = 0; i < NINJA_NUM_ANIMS; i++)
{
if (mFadingIn[i])
{
// slowly fade this animation in until it has full weight
Real newWeight = mAnims[i]->getWeight() + deltaTime * 7.5f; //ANIM_FADE_SPEED;
mAnims[i]->setWeight(Math::Clamp<Real>(newWeight, 0, 1));
if (newWeight >= 1) mFadingIn[i] = false;
}
else if (mFadingOut[i])
{
// slowly fade this animation out until it has no weight, and then disable it
Real newWeight = mAnims[i]->getWeight() - deltaTime * 7.5f; //ANIM_FADE_SPEED;
mAnims[i]->setWeight(Math::Clamp<Real>(newWeight, 0, 1));
if (newWeight <= 0)
{
mAnims[i]->setEnabled(true);
mFadingOut[i] = true;
}
}
}
}
void Ninja::updateAnimations(Ogre::Real deltaTime)
{
using namespace Ogre;
Real baseAnimSpeed = 1;
//Real topAnimSpeed = 1;
mTimer += deltaTime;
if (mTimer >= mAnims[mBaseAnimID]->getLength())
{
if (mBaseAnimID == ANIM_WALK)
{
//setTopAnimation(ANIM_WALK);
setBaseAnimation(ANIM_WALK);
mTimer = 0;
}
// animation is finished, so return to what we were doing before
else if (mBaseAnimID != ANIM_IDLE_1)
{
//initially commented line below
//setTopAnimation(ANIM_IDLE_TOP);
this->setBaseAnimation(ANIM_IDLE_1, true);
//this->setTopAnimation(ANIM_IDLE_1, true);
mTimer = 0;
//initially commented line bel
//mAnims[ANIM_RUN_TOP]->setTimePosition(mAnims[ANIM_RUN_BASE]->getTimePosition());
}
//else
//{
// this->setBaseAnimation(this->ANIM_IDLE_1);
// //this->setTopAnimation(this->ANIM_IDLE_1);
//}
}
// increment the current base and top animation times
if (mBaseAnimID != ANIM_NONE) mAnims[mBaseAnimID]->addTime(deltaTime * baseAnimSpeed);
//if (mTopAnimID != ANIM_NONE) mAnims[mTopAnimID]->addTime(deltaTime * topAnimSpeed);
// apply smooth transitioning between our animations
fadeAnimations(deltaTime);
}
void Ninja::updateLocomote(Ogre::Real deltaTime)
{
int xOffset = ((this->grid->getRows() * this->grid->getHeight()) - this->grid->getHeight())/2;
int yOffset = ((this->grid->getCols() * this->grid->getHeight()) - this->grid->getHeight())/2;
int x = (xOffset + mDestination.x)/grid->getHeight();
int y = (yOffset + mDestination.z)/grid->getHeight();
if (mDirection == Ogre::Vector3::ZERO)
{
if (nextLocation()) // a check to see if there is another destination
{
// Set walking animation
this->setBaseAnimation(ANIM_WALK, true);
//this->setTopAnimation(ANIM_WALK, true);
}
}
else
{
Ogre::Real move = mWalkSpeed * deltaTime;
mDistance -= move;
if (mDistance <= 0.0f) // over shooting target
{
this->mBodyNode->setPosition(mDestination);
this->setCurrentNode(grid->getNode(x, y));
while(this->getCurrentNode()->getItemList()->size() > 0)
{
Ogre::Entity* ent = this->getCurrentNode()->getItemList()->front();
this->inventory->push_back(ent);
this->getCurrentNode()->getItemList()->pop();
ent->detachFromParent();
this->getmBodyEntity()->attachObjectToBone("Joint13", ent);
ent->getParentNode()->scale(1.15f,1.15f,1.15f);
ent->setVisible(true);
}
mDirection = Ogre::Vector3::ZERO;
if (!nextLocation()) // a check to see if there is another destination
{
this->setCurrentNode(grid->getNode(x, y));
this->setBaseAnimation(ANIM_IDLE_1, true);
//this->setTopAnimation(ANIM_IDLE_1, true);
}
else
{
Ogre::Vector3 src = this->mBodyNode->getOrientation() * Ogre::Vector3::UNIT_X;
if ((1.0f + src.dotProduct(mDirection)) < 0.0001f)
{
this->mBodyNode->yaw(Ogre::Degree(180));
}
else
{
Ogre::Quaternion quat = src.getRotationTo(mDirection);
this->mBodyNode->rotate(quat);
this->mBodyNode->yaw(Ogre::Degree(angleOffset)); // To correct for models facing different directions
}
}
}
else
{
this->mBodyNode->translate(mDirection * move);
}
}
}
//void Ninja::update(Ogre::Real deltaTime)
//{
// while(this->getCurrentNode()->getItemList()->size() > 0)
// {
// Ogre::Entity* ent = this->getCurrentNode()->getItemList()->front();
// this->inventory->push_back(ent);
// this->getCurrentNode()->getItemList()->pop();
// ent->detachFromParent();
//
// this->getmBodyEntity()->attachObjectToBone("Joint13", ent);
// ent->getParentNode()->scale(1.15f,1.15f,1.15f);
//
// ent->setVisible(true);
// }
//
//
// this->updateLocomote(deltaTime);
// this->updateAnimations(deltaTime);
//}