| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163 |
- #include "Base.h"
- #include "AudioSource.h"
- #include "Node.h"
- #include "Scene.h"
- #include "Joint.h"
- #include "PhysicsRigidBody.h"
- #include "PhysicsGhostObject.h"
- #include "PhysicsCharacter.h"
- #include "Game.h"
- // Node dirty flags
- #define NODE_DIRTY_WORLD 1
- #define NODE_DIRTY_BOUNDS 2
- #define NODE_DIRTY_ALL (NODE_DIRTY_WORLD | NODE_DIRTY_BOUNDS)
- namespace gameplay
- {
- Node::Node(const char* id)
- : _scene(NULL), _firstChild(NULL), _nextSibling(NULL), _prevSibling(NULL), _parent(NULL), _childCount(0),
- _tags(NULL), _camera(NULL), _light(NULL), _model(NULL), _form(NULL), _audioSource(NULL), _particleEmitter(NULL),
- _collisionObject(NULL), _agent(NULL), _dirtyBits(NODE_DIRTY_ALL), _notifyHierarchyChanged(true), _userData(NULL)
- {
- if (id)
- {
- _id = id;
- }
- }
- Node::~Node()
- {
- removeAllChildren();
- if (_model)
- _model->setNode(NULL);
- if (_audioSource)
- _audioSource->setNode(NULL);
- if (_particleEmitter)
- _particleEmitter->setNode(NULL);
- if (_form)
- _form->setNode(NULL);
- SAFE_RELEASE(_camera);
- SAFE_RELEASE(_light);
- SAFE_RELEASE(_model);
- SAFE_RELEASE(_audioSource);
- SAFE_RELEASE(_particleEmitter);
- SAFE_RELEASE(_form);
- SAFE_DELETE(_collisionObject);
- SAFE_DELETE(_tags);
- setAgent(NULL);
- // Cleanup user data
- if (_userData)
- {
- // Call custom cleanup callback if specified
- if (_userData->cleanupCallback)
- _userData->cleanupCallback(_userData->pointer);
- SAFE_DELETE(_userData);
- }
- }
- Node* Node::create(const char* id)
- {
- return new Node(id);
- }
- const char* Node::getId() const
- {
- return _id.c_str();
- }
- void Node::setId(const char* id)
- {
- if (id)
- {
- _id = id;
- }
- }
- Node::Type Node::getType() const
- {
- return Node::NODE;
- }
- void Node::addChild(Node* child)
- {
- GP_ASSERT(child);
- if (child->_parent == this)
- {
- // This node is already present in our hierarchy
- return;
- }
- child->addRef();
- // If the item belongs to another hierarchy, remove it first.
- if (child->_parent)
- {
- child->_parent->removeChild(child);
- }
- else if (child->_scene)
- {
- child->_scene->removeNode(child);
- }
- // Order is irrelevant, so add to the beginning of the list.
- if (_firstChild)
- {
- _firstChild->_prevSibling = child;
- child->_nextSibling = _firstChild;
- _firstChild = child;
- }
- else
- {
- _firstChild = child;
- }
- child->_parent = this;
- ++_childCount;
- if (_notifyHierarchyChanged)
- {
- hierarchyChanged();
- }
- }
- void Node::removeChild(Node* child)
- {
- if (child == NULL || child->_parent != this)
- {
- // The child is not in our hierarchy.
- return;
- }
- // Call remove on the child.
- child->remove();
- SAFE_RELEASE(child);
- }
- void Node::removeAllChildren()
- {
- _notifyHierarchyChanged = false;
- while (_firstChild)
- {
- removeChild(_firstChild);
- }
- _notifyHierarchyChanged = true;
- hierarchyChanged();
- }
- void Node::remove()
- {
- // Re-link our neighbours.
- if (_prevSibling)
- {
- _prevSibling->_nextSibling = _nextSibling;
- }
- if (_nextSibling)
- {
- _nextSibling->_prevSibling = _prevSibling;
- }
- // Update our parent.
- Node* parent = _parent;
- if (parent)
- {
- if (this == parent->_firstChild)
- {
- parent->_firstChild = _nextSibling;
- }
- --parent->_childCount;
- }
- _nextSibling = NULL;
- _prevSibling = NULL;
- _parent = NULL;
- if (parent && parent->_notifyHierarchyChanged)
- {
- parent->hierarchyChanged();
- }
- }
- Node* Node::getFirstChild() const
- {
- return _firstChild;
- }
- Node* Node::getNextSibling() const
- {
- return _nextSibling;
- }
- Node* Node::getPreviousSibling() const
- {
- return _prevSibling;
- }
- Node* Node::getParent() const
- {
- return _parent;
- }
- bool Node::hasTag(const char* name) const
- {
- GP_ASSERT(name);
- return (_tags ? _tags->find(name) != _tags->end() : false);
- }
- const char* Node::getTag(const char* name) const
- {
- GP_ASSERT(name);
- if (!_tags)
- return NULL;
- std::map<std::string, std::string>::const_iterator itr = _tags->find(name);
- return (itr == _tags->end() ? NULL : itr->second.c_str());
- }
- void Node::setTag(const char* name, const char* value)
- {
- GP_ASSERT(name);
- if (value == NULL)
- {
- // Removing tag
- if (_tags)
- {
- _tags->erase(name);
- if (_tags->size() == 0)
- SAFE_DELETE(_tags);
- }
- }
- else
- {
- // Setting tag
- if (_tags == NULL)
- _tags = new std::map<std::string, std::string>();
- (*_tags)[name] = value;
- }
- }
- void* Node::getUserPointer() const
- {
- return (_userData ? _userData->pointer : NULL);
- }
- void Node::setUserPointer(void* pointer, void (*cleanupCallback)(void*))
- {
- // If existing user pointer is being changed, call cleanup function to free previous pointer
- if (_userData && _userData->pointer && _userData->cleanupCallback && pointer != _userData->pointer)
- {
- _userData->cleanupCallback(_userData->pointer);
- }
- if (pointer)
- {
- // Assign user pointer
- if (_userData == NULL)
- _userData = new UserData();
- _userData->pointer = pointer;
- _userData->cleanupCallback = cleanupCallback;
- }
- else
- {
- // Clear user pointer
- SAFE_DELETE(_userData);
- }
- }
- unsigned int Node::getChildCount() const
- {
- return _childCount;
- }
- Node* Node::findNode(const char* id, bool recursive, bool exactMatch) const
- {
- GP_ASSERT(id);
- // If the node has a model with a mesh skin, search the skin's hierarchy as well.
- Node* rootNode = NULL;
- if (_model != NULL && _model->getSkin() != NULL && (rootNode = _model->getSkin()->_rootNode) != NULL)
- {
- if ((exactMatch && rootNode->_id == id) || (!exactMatch && rootNode->_id.find(id) == 0))
- return rootNode;
-
- Node* match = rootNode->findNode(id, true, exactMatch);
- if (match)
- {
- return match;
- }
- }
-
- // Search immediate children first.
- for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
- {
- // Does this child's ID match?
- if ((exactMatch && child->_id == id) || (!exactMatch && child->_id.find(id) == 0))
- {
- return child;
- }
- }
- // Recurse.
- if (recursive)
- {
- for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
- {
- Node* match = child->findNode(id, true, exactMatch);
- if (match)
- {
- return match;
- }
- }
- }
- return NULL;
- }
- unsigned int Node::findNodes(const char* id, std::vector<Node*>& nodes, bool recursive, bool exactMatch) const
- {
- GP_ASSERT(id);
-
- unsigned int count = 0;
- // Search immediate children first.
- for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
- {
- // Does this child's ID match?
- if ((exactMatch && child->_id == id) || (!exactMatch && child->_id.find(id) == 0))
- {
- nodes.push_back(child);
- ++count;
- }
- }
- // Recurse.
- if (recursive)
- {
- for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
- {
- count += child->findNodes(id, nodes, true, exactMatch);
- }
- }
- return count;
- }
- Scene* Node::getScene() const
- {
- // Search for a scene in our parents.
- for (Node* n = const_cast<Node*>(this); n != NULL; n = n->getParent())
- {
- if (n->_scene)
- {
- return n->_scene;
- }
- }
- return NULL;
- }
- Node* Node::getRootNode() const
- {
- Node* n = const_cast<Node*>(this);
- while (n->getParent())
- {
- n = n->getParent();
- }
- return n;
- }
- const Matrix& Node::getWorldMatrix() const
- {
- if (_dirtyBits & NODE_DIRTY_WORLD)
- {
- // Clear our dirty flag immediately to prevent this block from being entered if our
- // parent calls our getWorldMatrix() method as a result of the following calculations.
- _dirtyBits &= ~NODE_DIRTY_WORLD;
- // If we have a parent, multiply our parent world transform by our local
- // transform to obtain our final resolved world transform.
- Node* parent = getParent();
- if (parent && (!_collisionObject || _collisionObject->isKinematic()))
- {
- Matrix::multiply(parent->getWorldMatrix(), getMatrix(), &_world);
- }
- else
- {
- _world = getMatrix();
- }
- // Our world matrix was just updated, so call getWorldMatrix() on all child nodes
- // to force their resolved world matrices to be updated.
- for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
- {
- child->getWorldMatrix();
- }
- }
- return _world;
- }
- const Matrix& Node::getWorldViewMatrix() const
- {
- static Matrix worldView;
- Matrix::multiply(getViewMatrix(), getWorldMatrix(), &worldView);
- return worldView;
- }
- const Matrix& Node::getInverseTransposeWorldViewMatrix() const
- {
- static Matrix invTransWorldView;
- Matrix::multiply(getViewMatrix(), getWorldMatrix(), &invTransWorldView);
- invTransWorldView.invert();
- invTransWorldView.transpose();
- return invTransWorldView;
- }
- const Matrix& Node::getInverseTransposeWorldMatrix() const
- {
- static Matrix invTransWorld;
- invTransWorld = getWorldMatrix();
- invTransWorld.invert();
- invTransWorld.transpose();
- return invTransWorld;
- }
- const Matrix& Node::getViewMatrix() const
- {
- Scene* scene = getScene();
- Camera* camera = scene ? scene->getActiveCamera() : NULL;
- if (camera)
- {
- return camera->getViewMatrix();
- }
- else
- {
- return Matrix::identity();
- }
- }
- const Matrix& Node::getInverseViewMatrix() const
- {
- Scene* scene = getScene();
- Camera* camera = scene ? scene->getActiveCamera() : NULL;
- if (camera)
- {
- return camera->getInverseViewMatrix();
- }
- else
- {
- return Matrix::identity();
- }
- }
- const Matrix& Node::getProjectionMatrix() const
- {
- Scene* scene = getScene();
- Camera* camera = scene ? scene->getActiveCamera() : NULL;
- if (camera)
- {
- return camera->getProjectionMatrix();
- }
- else
- {
- return Matrix::identity();
- }
- }
- const Matrix& Node::getViewProjectionMatrix() const
- {
- Scene* scene = getScene();
- Camera* camera = scene ? scene->getActiveCamera() : NULL;
- if (camera)
- {
- return camera->getViewProjectionMatrix();
- }
- else
- {
- return Matrix::identity();
- }
- }
- const Matrix& Node::getInverseViewProjectionMatrix() const
- {
- Scene* scene = getScene();
- Camera* camera = scene ? scene->getActiveCamera() : NULL;
- if (camera)
- {
- return camera->getInverseViewProjectionMatrix();
- }
- return Matrix::identity();
- }
- const Matrix& Node::getWorldViewProjectionMatrix() const
- {
- static Matrix worldViewProj;
- // Always re-calculate worldViewProjection matrix since it's extremely difficult
- // to track whether the camera has changed (it may frequently change every frame).
- Matrix::multiply(getViewProjectionMatrix(), getWorldMatrix(), &worldViewProj);
- return worldViewProj;
- }
- Vector3 Node::getTranslationWorld() const
- {
- Vector3 translation;
- getWorldMatrix().getTranslation(&translation);
- return translation;
- }
- Vector3 Node::getTranslationView() const
- {
- Vector3 translation;
- getWorldMatrix().getTranslation(&translation);
- getViewMatrix().transformPoint(&translation);
- return translation;
- }
- Vector3 Node::getForwardVectorWorld() const
- {
- Vector3 vector;
- getWorldMatrix().getForwardVector(&vector);
- return vector;
- }
- Vector3 Node::getForwardVectorView() const
- {
- Vector3 vector;
- getWorldMatrix().getForwardVector(&vector);
- getViewMatrix().transformVector(&vector);
- return vector;
- }
- Vector3 Node::getRightVectorWorld() const
- {
- Vector3 vector;
- getWorldMatrix().getRightVector(&vector);
- return vector;
- }
- Vector3 Node::getUpVectorWorld() const
- {
- Vector3 vector;
- getWorldMatrix().getUpVector(&vector);
- return vector;
- }
- Vector3 Node::getActiveCameraTranslationWorld() const
- {
- Scene* scene = getScene();
- if (scene)
- {
- Camera* camera = scene->getActiveCamera();
- if (camera)
- {
- Node* cameraNode = camera->getNode();
- if (cameraNode)
- {
- return cameraNode->getTranslationWorld();
- }
- }
- }
- return Vector3::zero();
- }
- Vector3 Node::getActiveCameraTranslationView() const
- {
- Scene* scene = getScene();
- if (scene)
- {
- Camera* camera = scene->getActiveCamera();
- if (camera)
- {
- Node* cameraNode = camera->getNode();
- if (cameraNode)
- {
- return cameraNode->getTranslationView();
- }
- }
- }
- return Vector3::zero();
- }
- void Node::hierarchyChanged()
- {
- // When our hierarchy changes our world transform is affected, so we must dirty it.
- transformChanged();
- }
- void Node::transformChanged()
- {
- // Our local transform was changed, so mark our world matrices dirty.
- _dirtyBits |= NODE_DIRTY_WORLD | NODE_DIRTY_BOUNDS;
- // Notify our children that their transform has also changed (since transforms are inherited).
- for (Node* n = getFirstChild(); n != NULL; n = n->getNextSibling())
- {
- if (Transform::isTransformChangedSuspended())
- {
- // If the DIRTY_NOTIFY bit is not set
- if (!n->isDirty(Transform::DIRTY_NOTIFY))
- {
- n->transformChanged();
- suspendTransformChange(n);
- }
- }
- else
- {
- n->transformChanged();
- }
- }
- Transform::transformChanged();
- }
- void Node::setBoundsDirty()
- {
- // Mark ourself and our parent nodes as dirty
- _dirtyBits |= NODE_DIRTY_BOUNDS;
- // Mark our parent bounds as dirty as well
- if (_parent)
- _parent->setBoundsDirty();
- }
- Animation* Node::getAnimation(const char* id) const
- {
- Animation* animation = ((AnimationTarget*)this)->getAnimation(id);
- if (animation)
- return animation;
-
- // See if this node has a model, then drill down.
- Model* model = this->getModel();
- if (model)
- {
- // Check to see if there's any animations with the ID on the joints.
- MeshSkin* skin = model->getSkin();
- if (skin)
- {
- Node* rootNode = skin->_rootNode;
- if (rootNode)
- {
- animation = rootNode->getAnimation(id);
- if (animation)
- return animation;
- }
- }
- // Check to see if any of the model's material parameter's has an animation
- // with the given ID.
- Material* material = model->getMaterial();
- if (material)
- {
- // How to access material parameters? hidden on the Material::RenderState.
- std::vector<MaterialParameter*>::iterator itr = material->_parameters.begin();
- for (; itr != material->_parameters.end(); itr++)
- {
- GP_ASSERT(*itr);
- animation = ((MaterialParameter*)(*itr))->getAnimation(id);
- if (animation)
- return animation;
- }
- }
- }
- // look through form for animations.
- Form* form = this->getForm();
- if (form)
- {
- animation = form->getAnimation(id);
- if (animation)
- return animation;
- }
- // Look through this node's children for an animation with the specified ID.
- for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
- {
- animation = child->getAnimation(id);
- if (animation)
- return animation;
- }
-
- return NULL;
- }
- Camera* Node::getCamera() const
- {
- return _camera;
- }
- void Node::setCamera(Camera* camera)
- {
- if (_camera != camera)
- {
- if (_camera)
- {
- _camera->setNode(NULL);
- SAFE_RELEASE(_camera);
- }
- _camera = camera;
- if (_camera)
- {
- _camera->addRef();
- _camera->setNode(this);
- }
- }
- }
- Light* Node::getLight() const
- {
- return _light;
- }
- void Node::setLight(Light* light)
- {
- if (_light != light)
- {
- if (_light)
- {
- _light->setNode(NULL);
- SAFE_RELEASE(_light);
- }
- _light = light;
- if (_light)
- {
- _light->addRef();
- _light->setNode(this);
- }
- }
- }
- void Node::setModel(Model* model)
- {
- if (_model != model)
- {
- if (_model)
- {
- _model->setNode(NULL);
- SAFE_RELEASE(_model);
- }
- _model = model;
- if (_model)
- {
- _model->addRef();
- _model->setNode(this);
- }
- }
- }
- Model* Node::getModel() const
- {
- return _model;
- }
- void Node::setForm(Form* form)
- {
- if (_form != form)
- {
- if (_form)
- {
- _form->setNode(NULL);
- SAFE_RELEASE(_form);
- }
- _form = form;
- if (_form)
- {
- _form->addRef();
- _form->setNode(this);
- }
- }
- }
- Form* Node::getForm() const
- {
- return _form;
- }
- const BoundingSphere& Node::getBoundingSphere() const
- {
- if (_dirtyBits & NODE_DIRTY_BOUNDS)
- {
- _dirtyBits &= ~NODE_DIRTY_BOUNDS;
- const Matrix& worldMatrix = getWorldMatrix();
- // Start with our local bounding sphere
- // TODO: Incorporate bounds from entities other than mesh (i.e. emitters, audiosource, etc)
- bool empty = true;
- if (_model && _model->getMesh())
- {
- _bounds.set(_model->getMesh()->getBoundingSphere());
- empty = false;
- }
- else
- {
- // Empty bounding sphere, set the world translation with zero radius
- worldMatrix.getTranslation(&_bounds.center);
- _bounds.radius = 0;
- }
- // Transform the sphere (if not empty) into world space.
- if (!empty)
- {
- bool applyWorldTransform = true;
- if (_model && _model->getSkin())
- {
- // Special case: If the root joint of our mesh skin is parented by any nodes,
- // multiply the world matrix of the root joint's parent by this node's
- // world matrix. This computes a final world matrix used for transforming this
- // node's bounding volume. This allows us to store a much smaller bounding
- // volume approximation than would otherwise be possible for skinned meshes,
- // since joint parent nodes that are not in the matrix pallette do not need to
- // be considered as directly transforming vertices on the GPU (they can instead
- // be applied directly to the bounding volume transformation below).
- GP_ASSERT(_model->getSkin()->getRootJoint());
- Node* jointParent = _model->getSkin()->getRootJoint()->getParent();
- if (jointParent)
- {
- // TODO: Should we protect against the case where joints are nested directly
- // in the node hierachy of the model (this is normally not the case)?
- Matrix boundsMatrix;
- Matrix::multiply(getWorldMatrix(), jointParent->getWorldMatrix(), &boundsMatrix);
- _bounds.transform(boundsMatrix);
- applyWorldTransform = false;
- }
- }
- if (applyWorldTransform)
- {
- _bounds.transform(getWorldMatrix());
- }
- }
- // Merge this world-space bounding sphere with our childrens' bounding volumes.
- for (Node* n = getFirstChild(); n != NULL; n = n->getNextSibling())
- {
- const BoundingSphere& childSphere = n->getBoundingSphere();
- if (!childSphere.isEmpty())
- {
- if (empty)
- {
- _bounds.set(childSphere);
- empty = false;
- }
- else
- {
- _bounds.merge(childSphere);
- }
- }
- }
- }
- return _bounds;
- }
- Node* Node::clone() const
- {
- NodeCloneContext context;
- return cloneRecursive(context);
- }
- Node* Node::cloneSingleNode(NodeCloneContext &context) const
- {
- Node* copy = Node::create(getId());
- context.registerClonedNode(this, copy);
- cloneInto(copy, context);
- return copy;
- }
- Node* Node::cloneRecursive(NodeCloneContext &context) const
- {
- Node* copy = cloneSingleNode(context);
- GP_ASSERT(copy);
- Node* lastChild = NULL;
- for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
- {
- lastChild = child;
- }
- // Loop through the nodes backwards because addChild adds the node to the front.
- for (Node* child = lastChild; child != NULL; child = child->getPreviousSibling())
- {
- Node* childCopy = child->cloneRecursive(context);
- GP_ASSERT(childCopy);
- copy->addChild(childCopy);
- childCopy->release();
- }
- return copy;
- }
- void Node::cloneInto(Node* node, NodeCloneContext &context) const
- {
- GP_ASSERT(node);
- Transform::cloneInto(node, context);
- // TODO: Clone the rest of the node data.
- if (Camera* camera = getCamera())
- {
- Camera* cameraClone = camera->clone(context);
- node->setCamera(cameraClone);
- cameraClone->release();
- }
- if (Light* light = getLight())
- {
- Light* lightClone = light->clone(context);
- node->setLight(lightClone);
- lightClone->release();
- }
- if (AudioSource* audio = getAudioSource())
- {
- AudioSource* audioClone = audio->clone(context);
- node->setAudioSource(audioClone);
- audioClone->release();
- }
- if (Model* model = getModel())
- {
- Model* modelClone = model->clone(context);
- node->setModel(modelClone);
- modelClone->release();
- }
- node->_world = _world;
- node->_bounds = _bounds;
- }
- AudioSource* Node::getAudioSource() const
- {
- return _audioSource;
- }
- void Node::setAudioSource(AudioSource* audio)
- {
- if (_audioSource != audio)
- {
- if (_audioSource)
- {
- _audioSource->setNode(NULL);
- SAFE_RELEASE(_audioSource);
- }
-
- _audioSource = audio;
- if (_audioSource)
- {
- _audioSource->addRef();
- _audioSource->setNode(this);
- }
- }
- }
- ParticleEmitter* Node::getParticleEmitter() const
- {
- return _particleEmitter;
- }
- void Node::setParticleEmitter(ParticleEmitter* emitter)
- {
- if (_particleEmitter != emitter)
- {
- if (_particleEmitter)
- {
- _particleEmitter->setNode(NULL);
- SAFE_RELEASE(_particleEmitter);
- }
-
- _particleEmitter = emitter;
- if (_particleEmitter)
- {
- _particleEmitter->addRef();
- _particleEmitter->setNode(this);
- }
- }
- }
- PhysicsCollisionObject* Node::getCollisionObject() const
- {
- return _collisionObject;
- }
- PhysicsCollisionObject* Node::setCollisionObject(PhysicsCollisionObject::Type type, const PhysicsCollisionShape::Definition& shape, PhysicsRigidBody::Parameters* rigidBodyParameters)
- {
- SAFE_DELETE(_collisionObject);
- switch (type)
- {
- case PhysicsCollisionObject::RIGID_BODY:
- {
- _collisionObject = new PhysicsRigidBody(this, shape, rigidBodyParameters ? *rigidBodyParameters : PhysicsRigidBody::Parameters());
- }
- break;
- case PhysicsCollisionObject::GHOST_OBJECT:
- {
- _collisionObject = new PhysicsGhostObject(this, shape);
- }
- break;
- case PhysicsCollisionObject::CHARACTER:
- {
- _collisionObject = new PhysicsCharacter(this, shape, rigidBodyParameters ? rigidBodyParameters->mass : 1.0f);
- }
- break;
- case PhysicsCollisionObject::NONE:
- break; // Already deleted, Just don't add a new collision object back.
- }
- return _collisionObject;
- }
- PhysicsCollisionObject* Node::setCollisionObject(const char* url)
- {
- // Load the collision object properties from file.
- Properties* properties = Properties::create(url);
- if (properties == NULL)
- {
- GP_ERROR("Failed to load collision object file: %s", url);
- return NULL;
- }
- PhysicsCollisionObject* collisionObject = setCollisionObject((strlen(properties->getNamespace()) > 0) ? properties : properties->getNextNamespace());
- SAFE_DELETE(properties);
- return collisionObject;
- }
- PhysicsCollisionObject* Node::setCollisionObject(Properties* properties)
- {
- SAFE_DELETE(_collisionObject);
- // Check if the properties is valid.
- if (!properties || !(strcmp(properties->getNamespace(), "collisionObject") == 0))
- {
- GP_ERROR("Failed to load collision object from properties object: must be non-null object and have namespace equal to 'collisionObject'.");
- return NULL;
- }
- if (const char* type = properties->getString("type"))
- {
- if (strcmp(type, "CHARACTER") == 0)
- {
- _collisionObject = PhysicsCharacter::create(this, properties);
- }
- else if (strcmp(type, "GHOST_OBJECT") == 0)
- {
- _collisionObject = PhysicsGhostObject::create(this, properties);
- }
- else if (strcmp(type, "RIGID_BODY") == 0)
- {
- _collisionObject = PhysicsRigidBody::create(this, properties);
- }
- else
- {
- GP_ERROR("Unsupported collision object type '%s'.", type);
- return NULL;
- }
- }
- else
- {
- GP_ERROR("Failed to load collision object from properties object; required attribute 'type' is missing.");
- return NULL;
- }
- return _collisionObject;
- }
- AIAgent* Node::getAgent() const
- {
- return _agent;
- }
- void Node::setAgent(AIAgent* agent)
- {
- if (agent != _agent)
- {
- if (_agent)
- {
- Game::getInstance()->getAIController()->removeAgent(_agent);
- _agent->_node = NULL;
- SAFE_RELEASE(_agent);
- }
- _agent = agent;
- if (_agent)
- {
- _agent->addRef();
- _agent->_node = this;
- Game::getInstance()->getAIController()->addAgent(_agent);
- }
- }
- }
- NodeCloneContext::NodeCloneContext()
- {
- }
- NodeCloneContext::~NodeCloneContext()
- {
- }
- Animation* NodeCloneContext::findClonedAnimation(const Animation* animation)
- {
- GP_ASSERT(animation);
- std::map<const Animation*, Animation*>::iterator it = _clonedAnimations.find(animation);
- return it != _clonedAnimations.end() ? it->second : NULL;
- }
- void NodeCloneContext::registerClonedAnimation(const Animation* original, Animation* clone)
- {
- GP_ASSERT(original);
- GP_ASSERT(clone);
- _clonedAnimations[original] = clone;
- }
- Node* NodeCloneContext::findClonedNode(const Node* node)
- {
- GP_ASSERT(node);
- std::map<const Node*, Node*>::iterator it = _clonedNodes.find(node);
- return it != _clonedNodes.end() ? it->second : NULL;
- }
- void NodeCloneContext::registerClonedNode(const Node* original, Node* clone)
- {
- GP_ASSERT(original);
- GP_ASSERT(clone);
- _clonedNodes[original] = clone;
- }
- }
|