D:/simple_rts/src/Projectile.cpp

Go to the documentation of this file.
00001 #include "Projectile.h"
00002 
00003 #include "Unit.h"
00004 #include "Game.h"
00005 #include "Timer.h"
00006 
00007 #include "OgreString.h"
00008 #include "OgreSceneNode.h"
00009 #include "OgreStringConverter.h"
00010 
00011 using Ogre::String;
00012 using Ogre::StringConverter;
00013 using Ogre::SceneNode;
00014 
00015 
00016 namespace ASR
00017 {
00018         int Projectile::mNumBullets = 0;
00019 
00020         // ----------------------------------------------------------------------------
00021         Projectile::Projectile( Unit* source, Unit* destination )
00022                 : mSource ( source ), mTarget ( destination )
00023         {
00024                 _generateEntity ();
00025 
00026                 mSpeed = 0.0f;
00027         }
00028 
00029 
00030         // ----------------------------------------------------------------------------
00031         Projectile::~Projectile ()
00032         {
00033                 _destroyEntity ();
00034         }
00035 
00036 
00037         // ----------------------------------------------------------------------------
00038         void Projectile::setSpeed (float speed )
00039         {
00040                 mSpeed = speed;
00041         }
00042 
00043 
00044         // ----------------------------------------------------------------------------
00045         void Projectile::setDamage (float damage )
00046         {
00047                 mDamage = damage;
00048         }
00049 
00050 
00051         // ----------------------------------------------------------------------------
00052         void Projectile::_generateEntity ()
00053         {
00054                 mNumBullets++;
00055                 String bulletName = mSource->getUnitName() + "_bullet_" + StringConverter::toString ( mNumBullets );
00056 
00057 
00058                 // Create a new cube projectile and add it to the list
00059                 mEntity = Game::getSingleton ().getSceneManager()->createEntity ( bulletName, "cube.mesh" );
00060 
00061                 SceneNode* bulletParent = Game::getSingleton ().getSceneManager()->getRootSceneNode()->createChildSceneNode ();
00062                 bulletParent->attachObject ( mEntity );
00063                 bulletParent->setPosition ( mSource->getPosition() );
00064                 bulletParent->setScale ( 0.1, 0.1, 0.1 );
00065 
00066                 mEntity->setMaterialName ( "ASR/Bullet" );
00067         }
00068 
00069 
00070         // ----------------------------------------------------------------------------
00071         Vector3 Projectile::getPosition () const
00072         {
00073                 OgreAssert ( mEntity, "Projectile has no entity" );
00074                 return mEntity->getParentSceneNode ()->getWorldPosition();
00075         }
00076 
00077 
00078         // ----------------------------------------------------------------------------
00079         bool Projectile::update ()
00080         {
00081                 Vector3 direction = mTarget->getPosition () - getPosition ();
00082                 Vector3 trans = direction.normalisedCopy();
00083                 trans = trans * Timer::getSingleton ().getTimeSinceLastFrame () * getSpeed();
00084 
00085                 // Assign damage if we are close enough
00086                 if ( trans.length() > direction.length() || direction.isZeroLength () )
00087                 {
00088                         mTarget->assignDamage ( getDamage() );
00089                         return true;
00090                 }
00091 
00092                 mEntity->getParentSceneNode()->translate ( trans );
00093 
00094                 return false;
00095         }
00096 
00097 
00098         // ----------------------------------------------------------------------------
00099         float Projectile::getDamage () const
00100         {
00101                 return mDamage;
00102         }
00103 
00104 
00105         // ----------------------------------------------------------------------------
00106         float Projectile::getSpeed () const
00107         {
00108                 return mSpeed;
00109         }
00110 
00111 
00112         // ----------------------------------------------------------------------------
00113         void Projectile::_destroyEntity ()
00114         {
00115                 SceneNode* node = mEntity->getParentSceneNode ();
00116 
00117                 node->detachAllObjects ();
00118 
00119                 Game::getSingleton ().getSceneManager()->destroyEntity ( mEntity );
00120                 Game::getSingleton ().getSceneManager()->destroySceneNode ( node->getName() );
00121         }
00122 }

Generated on Sun Jun 25 19:23:43 2006 for Valors End by  doxygen 1.4.7