00001 #include "ExampleApplication.h"
00002
00003 #include "Game.h"
00004 #include "Client.h"
00005
00006
00007
00008 #include "RTSCamera.h"
00009 #include "RTSCameraUpdater.h"
00010
00011 #include "Building.h"
00012 #include "Unit.h"
00013
00014 #include "UnitTypeManager.h"
00015
00016 #include "Team.h"
00017 #include "Game.h"
00018
00019 #include "Level.h"
00020 #include "LevelStatusVisualizer.h"
00021
00022 #include "OgreVector3.h"
00023 #include "OgreMath.h"
00024
00025 #include <math.h>
00026 #include <exception>
00027
00028 using namespace Ogre;
00029 using namespace ASR;
00030
00031 using std::exception;
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 const float UNIT_SCALE = 1.0f;
00044 const int UNIT_COUNT = 10;
00045 const int BUILDING_COUNT = 5;
00046
00047 const float TERRAIN_QUERY_HEIGHT = 5000.0f;
00048
00049 RaySceneQuery* mSceneRayQuery;
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 class TutorialApplication : public ExampleApplication
00063 {
00064
00065
00066 private:
00067 Client* mClient;
00068 Game* mGame;
00069
00070
00071
00072
00073 LevelStatusVisualizer* mLevelVisual;
00074
00075
00076
00077
00078
00079 public:
00080 TutorialApplication()
00081 {
00082 mClient = NULL;
00083 mGame = NULL;
00084
00085
00086
00087
00088 mSceneRayQuery = NULL;
00089 mLevelVisual = NULL;
00090 }
00091
00092 ~TutorialApplication()
00093 {
00094
00095
00096
00097 delete mLevelVisual;
00098 if ( mSceneRayQuery )
00099 mSceneMgr->destroyQuery ( mSceneRayQuery );
00100
00101 delete UnitTypeManager::getSingletonPtr();
00102
00103 delete mClient;
00104 delete mGame;
00105 }
00106
00107
00108
00109 protected:
00110
00111 void chooseSceneManager()
00112 {
00113 mSceneMgr = mRoot->createSceneManager( "TerrainSceneManager" );
00114 mSceneMgr->setShadowTechnique( SHADOWTYPE_STENCIL_MODULATIVE );
00115
00116
00117 mSceneRayQuery = mSceneMgr->createRayQuery( Ray() );
00118
00119 mGame = new Game ( mSceneMgr, mWindow );
00120 mGame->createLevel ();
00121
00122 mClient = new Client ( mWindow );
00123 }
00124
00125
00126
00127 void createFrameListener ()
00128 {
00129 Level* lev = Game::getSingleton ().getLevel ();
00130 Team* teamA = lev->getTeam ( 0 );
00131 SceneNode* baseNode = mSceneMgr->getRootSceneNode()->createChildSceneNode( "levelVisualizer" );
00132
00133 mLevelVisual = new LevelStatusVisualizer ( Game::getSingleton ().getLevel() );
00134
00135 Game::getSingleton ().addUpdater ( mLevelVisual );
00136
00137
00138 }
00139
00140
00141
00142 void createCamera ()
00143 {
00144 mClient->createCamera ();
00145 }
00146
00147
00148
00149 void createViewports ()
00150 {
00151
00152 }
00153
00154
00155
00156 void createUnitTypes ()
00157 {
00158
00159
00160 new UnitTypeManager();
00161 UnitTypeManager* typeMgr = UnitTypeManager::getSingletonPtr();
00162 UnitType* robotType = typeMgr->createUnitType( "robot" );
00163 robotType->setMesh( "lizardman.mesh" );
00164 }
00165
00166
00167
00168 void createScene(void)
00169 {
00170 mSceneMgr->setWorldGeometry( "ASR_terrain.cfg" );
00171
00172 createUnitTypes ();
00173
00174 Level* curLevel = Game::getSingleton ().getLevel ();
00175
00176 curLevel->setWorldDimensions ( 2560, 2560 );
00177
00178
00179 curLevel->setDimensions ( 256, 256 );
00180 curLevel->rebuild ();
00181
00182 _createTeamA ();
00183 _createTeamB ();
00184 _createTeamC ();
00185
00186 mSceneMgr->setAmbientLight( ColourValue( 0.5, 0.5, 0.5 ) );
00187
00188 Light* light = mSceneMgr->createLight( "Light1" );
00189 light->setType( Light::LT_DIRECTIONAL );
00190 light->setDirection ( Vector3(-0.5, -1, -1 ) );
00191
00192 light->setDiffuseColour( 1.0, 1.0, 1.0 );
00193 light->setSpecularColour( 1.0, 1.0, 1.0 );
00194 }
00195
00196
00197
00198 void _createTeamA ()
00199 {
00200 Team* teamA = mClient->createTeam ( "TeamA", ColourValue ( 1.0, 0.0, 0.0 ) );
00201
00202
00203
00204 for ( int i = 0; i < BUILDING_COUNT; i++ )
00205 {
00206 float x = 300 + (i % (int)sqrt(double(BUILDING_COUNT) ) ) * 290;
00207 float z = 800 + (i / (int)sqrt(double(BUILDING_COUNT) ) ) * 250;
00208
00209 Ray barracksRay ( Vector3(x, TERRAIN_QUERY_HEIGHT, z), Vector3::NEGATIVE_UNIT_Y );
00210 mSceneRayQuery->setRay ( barracksRay );
00211 RaySceneQueryResult& result = mSceneRayQuery->execute();
00212 RaySceneQueryResult::iterator iter = result.begin();
00213 if ( iter != result.end() )
00214 {
00215 if ( iter->worldFragment )
00216 {
00217
00218
00219 Real terrainHeight = iter->worldFragment->singleIntersection.y;
00220 Building* building = teamA->createBuilding( "blacksmith.mesh", Vector3(x, terrainHeight, z ) );
00221 }
00222 iter++;
00223 }
00224 }
00225
00226
00227 for ( int i = 0; i < UNIT_COUNT; i++ )
00228 {
00229 Unit* unit = teamA->createUnit ( *(UnitTypeManager::getSingleton().getUnitType ( "robot" )) );
00230 unit->scale ( UNIT_SCALE, UNIT_SCALE, UNIT_SCALE );
00231 unit->translate( Vector3((i % (int)sqrt(double(UNIT_COUNT))) * 60 + 10, 120, (i / (int)sqrt((double)UNIT_COUNT)) * 60 + 10) );
00232 }
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243 }
00244
00245
00246
00247 void _createTeamB ()
00248 {
00249 Level* curLevel = Game::getSingleton ().getLevel ();
00250 Team* teamB = curLevel->createTeam ( "TeamB", ColourValue ( 0.0, 0.0, 1.0 ) );
00251
00252 for ( int i = 0; i < UNIT_COUNT * 3; i++ )
00253 {
00254 Unit* unit = teamB->createUnit ( *(UnitTypeManager::getSingleton ().getUnitType ( "robot" ) ) );
00255 unit->scale ( UNIT_SCALE, UNIT_SCALE, UNIT_SCALE );
00256 unit->translate( Vector3( 100, 120, i * 60) );
00257 }
00258
00259 for ( int i = 0; i < UNIT_COUNT * 2; i++ )
00260 {
00261 Unit* unit = teamB->createUnit ( *(UnitTypeManager::getSingleton ().getUnitType ( "robot" ) ) );
00262 unit->scale ( UNIT_SCALE, UNIT_SCALE, UNIT_SCALE );
00263 unit->translate( Vector3( 200, 120, i * 60) );
00264 }
00265
00266
00267
00268
00269
00270
00271
00272 }
00273
00274
00275
00276 void _createTeamC ()
00277 {
00278 Level* curLevel = Game::getSingleton ().getLevel ();
00279 Team* teamC = curLevel->createTeam ( "TeamC", ColourValue ( 0.0, 1.0, 1.0 ) );
00280
00281 for ( int i = 0; i < 10; i++ )
00282 {
00283 Unit* unit = teamC->createUnit ( *(UnitTypeManager::getSingleton ().getUnitType ( "robot" ) ) );
00284 unit->scale ( UNIT_SCALE, UNIT_SCALE, UNIT_SCALE );
00285 unit->translate( Vector3(300, 120, i * 60) );
00286 }
00287 }
00288 };
00289
00290
00291
00292
00293
00294
00295
00296 #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
00297 #define WIN32_LEAN_AND_MEAN
00298 #include "windows.h"
00299
00300 INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
00301 #else
00302 int main(int argc, char **argv)
00303 #endif
00304 {
00305
00306 TutorialApplication app;
00307
00308 try {
00309 app.go();
00310 } catch( Exception& e ) {
00311 #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
00312 MessageBoxA( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
00313 #else
00314 fprintf(stderr, "An exception has occured: %s\n",
00315 e.getFullDescription().c_str());
00316 #endif
00317 }
00318 catch ( exception& e )
00319 {
00320 #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
00321 MessageBoxA( NULL, e.what (), "An unknown exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
00322 #else
00323 fprintf(stderr, "An non Ogre exception has occured: %s\n",
00324 e.what() );
00325 #endif
00326 }
00327
00328 return 0;
00329 }