00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "SceneComponent.h"
00018 #include "../../core/GameManager.h"
00019
00020
00021
00022
00023 SceneComponent::SceneComponent(Entity *parent)
00024 : EntityComponent(parent)
00025 {
00026
00027 ISceneManager *pScnMgr = GameManager::Instance()->getSceneManager();
00028 EventManager *pEventMgr = GameManager::Instance()->getEventManager();
00029
00030
00031 mSceneNode = pScnMgr->addEmptySceneNode(0, parent->getID());
00032 mSceneNode->setPosition(parent->getPosition());
00033
00034
00035 mTriSelector = pScnMgr->createTriangleSelectorFromBoundingBox(mSceneNode);
00036 mMetaSelector =pScnMgr->createMetaTriangleSelector();
00037
00038
00039 mName = "SceneComponent";
00040 mCanAffectParent = false;
00041 mEvokedParentChange = false;
00042
00043
00044 pEventMgr->connectEventSignal(std::string("ent#") + parent->getName(), "onPositionChange",
00045 this, &SceneComponent::onPositionChange);
00046 pEventMgr->connectEventSignal(std::string("ent#") + parent->getName(), "onUpdate",
00047 this, &SceneComponent::onUpdate);
00048 pEventMgr->connectEventSignal(std::string("ent#") + parent->getName(), "onPause",
00049 this, &SceneComponent::onPause);
00050 pEventMgr->connectEventSignal(std::string("ent#") + parent->getName(), "onUnPause",
00051 this, &SceneComponent::onUnPause);
00052 }
00053
00054
00055 SceneComponent::SceneComponent(Entity *parent, bool isDerived)
00056 : EntityComponent(parent)
00057 {
00058
00059 EventManager *pEventMgr = GameManager::Instance()->getEventManager();
00060
00061
00062 mName = "SceneComponent";
00063
00064
00065 pEventMgr->connectEventSignal(std::string("ent#") + parent->getName(), "onPositionChange",
00066 this, &SceneComponent::onPositionChange);
00067 }
00068
00069
00070 SceneComponent::~SceneComponent()
00071 {
00072 if(mSceneNode != NULL) mSceneNode->remove();
00073 if(mTriSelector != NULL) mTriSelector->drop();
00074 if(mMetaSelector != NULL) mMetaSelector->drop();
00075 }
00076
00077
00078 SceneComponent* SceneComponent::refFactory(Entity* parent)
00079 {
00080 return new SceneComponent(parent);
00081 }
00082
00083
00084
00085 ISceneNode* SceneComponent::getSceneNode()
00086 {
00087 return mSceneNode;
00088 }
00089
00090
00091 IMetaTriangleSelector* SceneComponent::getMetaSelector() const
00092 {
00093 return mMetaSelector;
00094 }
00095
00096
00097 ITriangleSelector* SceneComponent::getTriangleSelector() const
00098 {
00099 return mTriSelector;
00100 }
00101
00102
00103 void SceneComponent::addAnimator(ISceneNodeAnimator *animator)
00104 {
00105 mSceneNode->addAnimator(animator);
00106 }
00107
00108
00109 void SceneComponent::addCollisionResponseAnimator(const vector3df &ellipsoidRadius,
00110 const vector3df &gravityPerSecond,
00111 const vector3df &ellipsoidTranslation,
00112 f32 slidingValue)
00113 {
00114 ISceneNodeAnimator *anim = GameManager::Instance()->getSceneManager()->
00115 createCollisionResponseAnimator(mMetaSelector, mSceneNode,
00116 ellipsoidRadius, gravityPerSecond, ellipsoidTranslation,
00117 slidingValue);
00118
00119 mSceneNode->addAnimator(anim);
00120 anim->drop();
00121 }
00122
00123
00124 void SceneComponent::addFlyCircleAnimator(const vector3df ¢er, f32 radius, f32 speed,
00125 const vector3df &direction)
00126 {
00127 ISceneNodeAnimator *anim = GameManager::Instance()->getSceneManager()->
00128 createFlyCircleAnimator(center, radius, speed, direction);
00129
00130 mSceneNode->addAnimator(anim);
00131 anim->drop();
00132 }
00133
00134
00135 void SceneComponent::addFlyStraightAnimator(const vector3df &startPoint, const vector3df &endPoint,
00136 u32 timeForWay, bool loop)
00137 {
00138 ISceneNodeAnimator *anim = GameManager::Instance()->getSceneManager()->
00139 createFlyStraightAnimator(startPoint, endPoint, timeForWay, loop);
00140
00141 mSceneNode->addAnimator(anim);
00142 anim->drop();
00143 }
00144
00145
00146
00147 void SceneComponent::addToMetaSelector(Entity *entity)
00148 {
00149 if(entity != NULL && mMetaSelector != NULL)
00150 {
00151 SceneComponent *component = static_cast<SceneComponent*>(entity->
00152 getComponent("SceneComponent"));
00153
00154 if(component != NULL)
00155 {
00156 ITriangleSelector *selector = component->getTriangleSelector();
00157
00158 if(selector != NULL)
00159 mMetaSelector->addTriangleSelector(selector);
00160 }
00161 }
00162 }
00163
00164
00165 void SceneComponent::addToMetaSelector(SceneComponent *component)
00166 {
00167 if(component != NULL && mMetaSelector != NULL)
00168 {
00169 ITriangleSelector *selector = component->getTriangleSelector();
00170
00171 if(selector != NULL)
00172 mMetaSelector->addTriangleSelector(selector);
00173 }
00174 }
00175
00176
00177 void SceneComponent::addToMetaSelector(ITriangleSelector *selector)
00178 {
00179 if(selector != NULL && mMetaSelector != NULL)
00180 mMetaSelector->addTriangleSelector(selector);
00181 }
00182
00183
00184 vector3df SceneComponent::getAbsolutePosition() const
00185 {
00186 return mSceneNode->getAbsolutePosition();
00187 }
00188
00189
00190 E_CULLING_TYPE SceneComponent::getAutomaticCulling() const
00191 {
00192 return mSceneNode->getAutomaticCulling();
00193 }
00194
00195
00196 const aabbox3df& SceneComponent::getBoundingBox() const
00197 {
00198 return mSceneNode->getBoundingBox();
00199 }
00200
00201
00202 u32 SceneComponent::getMaterialCount() const
00203 {
00204 return mSceneNode->getMaterialCount();
00205 }
00206
00207
00208 matrix4 SceneComponent::getRelativeTransformation() const
00209 {
00210 return mSceneNode->getRelativeTransformation();
00211 }
00212
00213
00214 const vector3df SceneComponent::getPosition() const
00215 {
00216 return mSceneNode->getPosition() - pParent->getPosition();
00217 }
00218
00219
00220 const vector3df& SceneComponent::getRotation() const
00221 {
00222 return mSceneNode->getRotation();
00223 }
00224
00225
00226 const vector3df& SceneComponent::getScale() const
00227 {
00228 return mSceneNode->getScale();
00229 }
00230
00231
00232 void SceneComponent::removeAnimators()
00233 {
00234 mSceneNode->removeAnimators();
00235 }
00236
00237
00238 void SceneComponent::setAbsolutePosition(const vector3df &position)
00239 {
00240 mSceneNode->setPosition(position);
00241 }
00242
00243
00244 void SceneComponent::setAutomaticCulling(E_CULLING_TYPE state)
00245 {
00246 mSceneNode->setAutomaticCulling(state);
00247 }
00248
00249
00250 void SceneComponent::setCanAffectParent(bool value)
00251 {
00252 mCanAffectParent = value;
00253 }
00254
00255
00256 void SceneComponent::setDebugDataVisible(s32 state)
00257 {
00258 mSceneNode->setDebugDataVisible(state);
00259 }
00260
00261
00262 void SceneComponent::setMaterialFlag(E_MATERIAL_FLAG flag, bool value)
00263 {
00264 mSceneNode->setMaterialFlag(flag, value);
00265 }
00266
00267
00268
00269 void SceneComponent::setMaterialTexture(u32 layer, const std::string &fileName)
00270 {
00271 ITexture *texture = GameManager::Instance()->getDriver()->getTexture(fileName.c_str());
00272 if(texture)
00273 mSceneNode->setMaterialTexture(layer, texture);
00274 }
00275
00276
00277 void SceneComponent::setMaterialTexture(u32 layer, ITexture *texture)
00278 {
00279 mSceneNode->setMaterialTexture(layer, texture);
00280 }
00281
00282
00283 void SceneComponent::setMaterialType(E_MATERIAL_TYPE type)
00284 {
00285 mSceneNode->setMaterialType(type);
00286 }
00287
00288
00289 void SceneComponent::setPosition(const vector3df &position)
00290 {
00291 mSceneNode->setPosition(pParent->getPosition() + position);
00292 }
00293
00294
00295 void SceneComponent::setRotation(const vector3df &rotation)
00296 {
00297 mSceneNode->setRotation(rotation);
00298 }
00299
00300
00301 void SceneComponent::setScale(const vector3df &scale)
00302 {
00303 mSceneNode->setScale(scale);
00304 }
00305
00306
00307 void SceneComponent::setVisible(bool value)
00308 {
00309 mSceneNode->setVisible(value);
00310 }
00311
00312
00313
00314 void SceneComponent::onPositionChange(void *p)
00315 {
00316 if(!mEvokedParentChange)
00317 {
00318 vector3df diff = static_cast<vector3df*>(p)[0];
00319 mSceneNode->setPosition(mSceneNode->getPosition() + diff);
00320 }
00321 }
00322
00323
00324 void SceneComponent::onUpdate(void *p)
00325 {
00326 if(mCanAffectParent)
00327 {
00328
00329
00330 vector3df diff = mSceneNode->getPosition() - mLastPos;
00331 mLastPos = mSceneNode->getPosition();
00332
00333 if(diff == vector3df(0))
00334 {
00335 mEvokedParentChange = true;
00336 pParent->setPosition(pParent->getPosition() + diff);
00337 mEvokedParentChange = false;
00338 }
00339 }
00340 }
00341
00342
00343 void SceneComponent::onPause(void *p)
00344 {
00345 if(mSceneNode)
00346 {
00347 mWasVisible = mSceneNode->isVisible();
00348 mSceneNode->setVisible(false);
00349 }
00350 }
00351
00352
00353 void SceneComponent::onUnPause(void *p)
00354 {
00355 if(mSceneNode && mWasVisible)
00356 mSceneNode->setVisible(true);
00357 }
00358
00359
00360
00361 void bindSceneComponent(asIScriptEngine *engine)
00362 {
00363
00364 int r;
00365
00366
00367 r = engine->RegisterObjectType("SceneComponent", sizeof(SceneComponent), asOBJ_REF); assert(r >= 0);
00368
00369
00370 bindSceneComponentBase<SceneComponent>(engine, "SceneComponent");
00371
00372
00373 r = engine->RegisterObjectBehaviour("SceneComponent", asBEHAVE_FACTORY, "SceneComponent@ f(Entity @)",
00374 asFUNCTIONPR(SceneComponent::refFactory, (Entity*), SceneComponent*),
00375 asCALL_CDECL); assert(r >= 0);
00376 }
00377
00378