C:/Users/Mike/Documents/Visual Studio Projects/GameEngine/src/BaseGame.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 "BaseGame.h"
00022 #include "GameState.h"
00023 #include "PhysicsManager.h"
00024 
00025 //template<> GameEngine::BaseGame* Ogre::Singleton<GameEngine::BaseGame>::ms_Singleton = 0;
00026 
00027 namespace GameEngine{
00028 
00029 
00030         BaseGame::BaseGame(Real width, Real height, Real depth, GameApp& application) 
00031                 :       mWidth(width), mHeight(height), mDepth(depth), mApplication(application)
00032         {
00033                 mHumanView = boost::shared_ptr<HumanView>( new HumanView(&application, this) );
00034         }
00035 
00036 
00037         bool BaseGame::init(void)
00038         {
00039                 mHumanView->init();
00040 
00041                 EventManager::getSingletonPtr()->addListener(this->shared_from_this(), EventType("ObjectMoved"));
00042                 EventManager::getSingletonPtr()->addListener(this->shared_from_this(), EventType("Collision"));
00043                 EventManager::getSingletonPtr()->addListener(this->shared_from_this(), EventType("KeyPressed"));
00044                 EventManager::getSingletonPtr()->addListener(this->shared_from_this(), EventType("KeyReleased"));
00045                 EventManager::getSingletonPtr()->addListener(this->shared_from_this(), EventType("MouseMoved"));
00046                 EventManager::getSingletonPtr()->addListener(this->shared_from_this(), EventType("MouseButtonPressed"));
00047                 EventManager::getSingletonPtr()->addListener(this->shared_from_this(), EventType("MouseButtonReleased"));
00048 
00049                 return true;
00050         }
00051 
00052 
00053 
00054         BaseGame::~BaseGame()
00055         {
00056 
00057         }
00058 
00059 
00060         bool BaseGame::handleEvent(const Event& event) 
00061         {
00062                 if(event.getEventType() == EventType("KeyPressed")){
00063                         mGameStatesStack.back()->keyPressed( event.getDataPtr<KeyboardEventData>()->mEvent );
00064                 }
00065                 else if(event.getEventType() == EventType("KeyReleased")){
00066                         mGameStatesStack.back()->keyReleased( event.getDataPtr<KeyboardEventData>()->mEvent );
00067                 }
00068                 else if(event.getEventType() == EventType("MouseMoved")){
00069                         mGameStatesStack.back()->mouseMoved( event.getDataPtr<MouseMovedEventData>()->mEvent );
00070                 }
00071                 else if(event.getEventType() == EventType("MouseButtonPressed")){
00072                         mGameStatesStack.back()->mousePressed( event.getDataPtr<MouseButtonEventData>()->mEvent, event.getDataPtr<MouseButtonEventData>()->mButtonId );
00073                 }
00074                 else if(event.getEventType() == EventType("MouseButtonReleased")){
00075                         mGameStatesStack.back()->mouseReleased( event.getDataPtr<MouseButtonEventData>()->mEvent, event.getDataPtr<MouseButtonEventData>()->mButtonId );
00076                 }
00077                 else {
00078                         mGameStatesStack.back()->handleEvent(event); 
00079                 }
00080 
00081                 return false; // Dont consume event
00082         }
00083 
00084 
00085         void BaseGame::onUpdate(const double dt)
00086         {
00087                 mGameStatesStack.back()->onUpdate(dt);
00088         }
00089 
00090 
00091         void BaseGame::changeState(GameState* state)
00092         {
00093                 while ( !mGameStatesStack.empty() ) {
00094                         mGameStatesStack.back()->stop();
00095                         mGameStatesStack.pop_back();
00096                 }
00097 
00098                 mGameStatesStack.push_back(state);
00099                 mGameStatesStack.back()->start();
00100         }
00101         
00102         
00103         void BaseGame::changeState(std::string stateName)
00104         {
00105                 GameState* state = getState(stateName);
00106                 assert(state != NULL); // Make sure we found a state with the name given
00107                 
00108                 changeState(state);
00109         }
00110 
00111         void BaseGame::pushState(GameState* state)
00112         {
00113                 if ( !mGameStatesStack.empty() ) {
00114                         mGameStatesStack.back()->pause();
00115                 }
00116 
00117                 mGameStatesStack.push_back(state);
00118                 mGameStatesStack.back()->start();
00119         }
00120 
00121         void BaseGame::popState(void)
00122         {
00123                 if ( !mGameStatesStack.empty() ) {
00124                         mGameStatesStack.back()->stop();
00125                         mGameStatesStack.pop_back();
00126                 }
00127 
00128                 if ( !mGameStatesStack.empty() ) {
00129                         mGameStatesStack.back()->resume();
00130                 }
00131         }
00132 
00133         Vector3& BaseGame::wrapCoords(Vector3& coords) const
00134         {
00135                 while (coords.mX >  mWidth/2)  coords.mX -= mWidth; 
00136                 while (coords.mY >  mHeight/2) coords.mY -= mHeight; 
00137                 while (coords.mZ >  mDepth/2) coords.mZ -= mDepth; 
00138 
00139                 while (coords.mX < -mWidth/2)  coords.mX += mWidth; 
00140                 while (coords.mY < -mHeight/2) coords.mY += mHeight; 
00141                 while (coords.mZ < -mDepth/2) coords.mZ += mDepth; 
00142 
00143                 return coords;
00144 
00145         }
00146 
00147 }

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