C:/Users/Mike/Documents/Visual Studio Projects/GameEngine/src/GameObject.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 Copyright (C) 2006 Michael Harnetty
00004 
00005 This program is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU General Public License
00007 as published by the Free Software Foundation; either version 2
00008 of the License, or (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018 -----------------------------------------------------------------------------
00019 */
00020 
00021 #include "GameObject.h"
00022 
00023 namespace GameEngine {
00024 
00025         GameObject::GameObject( char const * const objectType, 
00026                                                         const std::string& objectID, 
00027                                                         GameState* gameState,
00028                                                         int timeToLive
00029                                                         )       :       mType(objectType),
00030                                                                         mID(objectID),
00031                                                                         mGameState(gameState),
00032                                                                         mLifeTime(timeToLive),
00033                                                                         mTimeLived(0)
00034         {
00035 
00036         }
00037 
00038 
00039         GameObject::~GameObject()
00040         {
00041 
00042         }
00043 
00044 
00045         void GameObject::addComponent( boost::shared_ptr<GameObjectComponent> component)
00046         {
00047                 for( GameObjectComponentList::iterator it = mComponents.begin(); it != mComponents.end(); it++){
00048                         if( (*it) == component){
00049                                 return; // Component is already in our list of components, don't add again
00050                         }
00051                 }
00052 
00053                 // Component isn't already in our list, add it now
00054                 mComponents.push_back( component );
00055                 component->setParent( this->shared_from_this() );
00056         }
00057 
00058 
00059         void GameObject::removeComponent( boost::shared_ptr<GameObjectComponent> component)
00060         {
00061                 mComponents.remove( component );
00062                 //component->setParent( NULL );
00063         }
00064 
00065 
00066         bool GameObject::init()
00067         {
00068                 assert( !isInitialised() ); // Can't initialise same object twice
00069 
00070 
00071                 if( !Process::init() ) return false;
00072 
00073                 bool success = false;
00074                 for( GameObjectComponentList::iterator it = mComponents.begin(); it != mComponents.end(); it++){
00075                         success = (*it)->init();
00076 
00077                         if(!success) return false;
00078                 }
00079 
00080                 return true;
00081         }
00082 
00083 
00084         void GameObject::destroy()
00085         {
00086                 assert(isInitialised()); // Must have initialised object before we can destroy it!
00087 
00088                 Process::destroy();
00089 
00090                 for( GameObjectComponentList::iterator it = mComponents.begin(); it != mComponents.end(); it++){
00091                         (*it)->destroy();
00092                 }
00093         }
00094 
00095 
00096         void GameObject::update(const double& dt)
00097         {
00098                 assert( isInitialised() ) ; // Can't start updating until object is initialised
00099 
00100                 if(mLifeTime != -1 && mTimeLived > mLifeTime){ // Object has reached the end of it's life and should be destroyed
00101                         this->destroy();
00102                 }
00103                 else{
00104                         mTimeLived += dt;
00105 
00106                         Process::update(dt);
00107 
00108                         for( GameObjectComponentList::iterator it = mComponents.begin(); it != mComponents.end(); it++){
00109                                 (*it)->update(dt);
00110                         }
00111                 }
00112         }
00113 }

Generated on Thu Apr 19 09:47:02 2007 for GameEngine by  doxygen 1.5.2