ThingMO.cpp

Go to the documentation of this file.
00001 /*
00002 SagaEngine library
00003 Copyright (c) 2002-2006 Skalden Studio AS
00004 
00005 This software is provided 'as-is', without any express or implied 
00006 warranty. In no event will the authors be held liable for any 
00007 damages arising from the use of this software.
00008 
00009 Permission is granted to distribute the library under the terms of the 
00010 Q Public License version 1.0. Be sure to read and understand the license
00011 before using the library. It should be included here, or you may read it
00012 at http://www.trolltech.com/products/qt/licenses/licensing/qpl
00013 
00014 The original version of this library can be located at:
00015 http://www.sagaengine.com/
00016 
00017 Rune Myrland
00018 rune@skalden.com
00019 */
00020 
00021 
00022 #include "ThingMO.hpp"
00023 #include "ThingMOInfo.hpp"
00024 #include "../schema/O3dSchema.hpp"
00025 #include "util/math/all.hpp"
00026 #include <OgreSceneManager.h>
00027 
00028 using namespace se_client;
00029 using namespace se_core;
00030 
00031 
00032 namespace se_ogre {
00033 
00034     ThingMO
00035     ::ThingMO(se_core::PosComponent& thing, const ThingMOInfo& info, const ThingMOFactory& factory) 
00036         : thing_(thing), factory_(factory), info_(info), hasAnimation_(false), isVisible_(false), currentScale_(1) {
00037 
00038         parentNode_ = O3dSchema::sceneManager->getRootSceneNode();
00039 
00040         // Create things node, and add it to scene manager
00041         node_ = O3dSchema::sceneManager->createSceneNode();
00042         node_->setPosition(info_.offset_);
00043         node_->setOrientation(info_.rot_);
00044 
00045         // Scale all children of this node
00046         Ogre::Real s = info_.scale_;
00047         node_->setScale(s, s, s);
00048     }
00049 
00050 
00051     ThingMO
00052     ::~ThingMO() {
00053         if(O3dSchema::sceneManager) {
00054             node_->removeAndDestroyAllChildren();
00055             O3dSchema::sceneManager->destroySceneNode(node_->getName());
00056         }
00057         node_ = 0;
00058     }
00059 
00060 
00061     void ThingMO
00062     ::move(long when, float stepDelta, float timeSinceLastFrame) {
00063         // Check if mesh entity is visible
00064         const Point3& playerCoor = ClientSchema::player->pos().worldCoor();
00065         coor_double_t distSq = playerCoor.distanceSquared(thing_.pos().worldCoor());
00066         // Must be farther away than popIn distance, and closer than popOut distance
00067         // (if popOut distance is defined)
00068         bool isVisible = (distSq >= info_.popInSq_ && (info_.popOutSq_ == 0 || distSq < info_.popOutSq_));
00069         setVisible(isVisible);
00070         if(!isVisible) {
00071             return;
00072         }
00073 
00074         // If radius scales the model
00075         if(info_.doScaleByRadius_) {
00076             Ogre::Real r1 = CoorT::toFloat(thing_.pos().radius());
00077             Ogre::Real r2 = CoorT::toFloat(thing_.nextPos().radius());
00078             Ogre::Real s = r1 + stepDelta * (r2 - r1);
00079 
00080             // If scale has changed
00081             if(s != currentScale_) {
00082                 // Store scale for future scale change checks
00083                 currentScale_ = s;
00084 
00085                 //
00086                 s *= info_.scale_;
00087 
00088                 // Scale all children of this node
00089                 node_->setScale(s, s, s);
00090             }
00091         }
00092 
00093         if(hasAnimation_) {
00094             animate(when, stepDelta, timeSinceLastFrame);
00095         }
00096     }
00097 
00098 
00099     void ThingMO
00100     ::setVisible(bool state) {
00101         if(state == isVisible_) return;
00102 
00103         isVisible_ = state;
00104         if(isVisible_)
00105             parentNode_->addChild(node_);
00106         else
00107             parentNode_->removeChild(node_);
00108     }
00109 
00110 
00111     void ThingMO
00112     ::setParentNode(Ogre::SceneNode* sn) {
00113         if(sn == parentNode_)
00114             return;
00115 
00116         if(isVisible_)
00117             parentNode_->removeChild(node_);
00118 
00119         node_->setPosition(node_->getPosition() + parentNode_->getPosition());
00120         parentNode_ = sn;
00121         node_->setPosition(node_->getPosition() - parentNode_->getPosition());
00122 
00123         if(isVisible_)
00124             parentNode_->addChild(node_);
00125     }
00126 
00127 
00128     void ThingMO
00129     ::resetPos() {
00130         node_->setPosition(info_.offset_);
00131         node_->setOrientation(info_.rot_);
00132     }
00133 
00134 }

Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:09 2007 by Doxygen version 1.3.9.1.

SourceForge.net Logo