Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7374 in orxonox.OLD for trunk/src/lib/util/loading/game_loader.cc


Ignore:
Timestamp:
Apr 26, 2006, 3:28:55 AM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: added new Files shell_completion_plugin for the new Plugin Structure.
Also created the first namespace: OrxShell

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/util/loading/game_loader.cc

    r7221 r7374  
    2929
    3030
    31 using namespace std;
    32 
    33 
    34 SHELL_COMMAND(quit, GameLoader, stop)
    35     ->describe("quits the game")
    36     ->setAlias("orxoquit");
    37 
    38 
    39 GameLoader* GameLoader::singletonRef = NULL;
    40 
    41 
    42 /**
    43  *  simple constructor
    44  */
    45 GameLoader::GameLoader ()
     31namespace OrxShell
    4632{
    47   this->setClassID(CL_GAME_LOADER, "GameLoader");
    48   this->setName("GameLoader");
    49   this->bRun = true;
    50 }
    51 
    52 
    53 /**
    54  *  simple deconstructor
    55  */
    56 GameLoader::~GameLoader ()
    57 {
    58   if( this->currentCampaign)
    59     delete this->currentCampaign;
    60   this->currentCampaign = NULL;
    61 }
    62 
    63 
    64 /**
    65  *  initializes the GameLoader
    66  */
    67 ErrorMessage GameLoader::init()
    68 {
    69   if(this->currentCampaign != NULL)
    70     this->currentCampaign->init();
    71 
    72   this->eventHandler = EventHandler::getInstance();
    73   this->eventHandler->subscribe(this, ES_GAME, KeyMapper::PEV_PAUSE);
    74   this->eventHandler->subscribe(this, ES_ALL, EV_MAIN_QUIT);          //< External Quit Event
    75   this->eventHandler->subscribe(this, ES_GAME, KeyMapper::PEV_QUIT);
    76   this->eventHandler->subscribe(this, ES_GAME, KeyMapper::PEV_NEXT_WORLD);
    77   this->eventHandler->subscribe(this, ES_GAME, KeyMapper::PEV_PREVIOUS_WORLD);
    78 }
    79 
    80 
    81 /**
    82  *  reads a campaign definition file into a campaign class
    83  * @param fileName to be loaded
    84  * @returns the loaded campaign
    85  *
    86  * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns
    87  */
    88 ErrorMessage GameLoader::loadCampaign(const std::string& fileName)
    89 {
    90   ErrorMessage errorCode;
    91   std::string campaignName = ResourceManager::getFullName(fileName);
    92   if (!campaignName.empty())
     33
     34  SHELL_COMMAND(quit, GameLoader, stop)
     35  ->describe("quits the game")
     36  ->setAlias("orxoquit");
     37
     38
     39  GameLoader* GameLoader::singletonRef = NULL;
     40
     41
     42  /**
     43   *  simple constructor
     44   */
     45  GameLoader::GameLoader ()
     46  {
     47    this->setClassID(CL_GAME_LOADER, "GameLoader");
     48    this->setName("GameLoader");
     49    this->bRun = true;
     50  }
     51
     52
     53  /**
     54   *  simple deconstructor
     55   */
     56  GameLoader::~GameLoader ()
     57  {
     58    if( this->currentCampaign)
     59      delete this->currentCampaign;
     60    this->currentCampaign = NULL;
     61  }
     62
     63
     64  /**
     65   *  initializes the GameLoader
     66   */
     67  ErrorMessage GameLoader::init()
     68  {
     69    if(this->currentCampaign != NULL)
     70      this->currentCampaign->init();
     71
     72    this->eventHandler = EventHandler::getInstance();
     73    this->eventHandler->subscribe(this, ES_GAME, KeyMapper::PEV_PAUSE);
     74    this->eventHandler->subscribe(this, ES_ALL, EV_MAIN_QUIT);          //< External Quit Event
     75    this->eventHandler->subscribe(this, ES_GAME, KeyMapper::PEV_QUIT);
     76    this->eventHandler->subscribe(this, ES_GAME, KeyMapper::PEV_NEXT_WORLD);
     77    this->eventHandler->subscribe(this, ES_GAME, KeyMapper::PEV_PREVIOUS_WORLD);
     78  }
     79
     80
     81  /**
     82   *  reads a campaign definition file into a campaign class
     83   * @param fileName to be loaded
     84   * @returns the loaded campaign
     85   *
     86   * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns
     87   */
     88  ErrorMessage GameLoader::loadCampaign(const std::string& fileName)
     89  {
     90    ErrorMessage errorCode;
     91    std::string campaignName = ResourceManager::getFullName(fileName);
     92    if (!campaignName.empty())
    9393    {
    9494      this->currentCampaign = this->fileToCampaign(campaignName);
    9595    }
    96 }
    97 
    98 
    99 /**
    100  *  reads a campaign definition file into a campaign class
    101  * @param fileName to be loaded
    102  * @returns the loaded campaign
    103  *
    104  * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns
    105  */
    106 ErrorMessage GameLoader::loadNetworkCampaign(const std::string& fileName)
    107 {
    108   ErrorMessage errorCode;
    109   std::string campaignName = ResourceManager::getFullName(fileName);
    110   if (!campaignName.empty())
    111   {
    112     this->currentCampaign = this->fileToCampaign(campaignName);
    113   }
    114 }
    115 
    116 
    117 /**
    118  *  loads a debug campaign for test purposes only.
    119  * @param campaignID the identifier of the campaign.
    120  * @returns error message if not able to do so.
    121  */
    122 ErrorMessage GameLoader::loadDebugCampaign(Uint32 campaignID)
    123 {
    124   switch(campaignID)
    125     {
    126       /*
    127          Debug Level 0: Debug level used to test the base frame work.
    128          As you can see, all storyentity data is allocated before game
    129          start. the storyentity will load themselfs shortly before start
    130          through the StoryEntity::init() funtion.
    131       */
    132     case DEBUG_CAMPAIGN_0:
    133       {
    134 /*        Campaign* debugCampaign = new Campaign();
    135 
    136         World* world0 = new World(DEBUG_WORLD_0);
    137         world0->setNextStoryID(WORLD_ID_1);
    138         debugCampaign->addEntity(world0, WORLD_ID_0);
    139 
    140         World* world1 = new World(DEBUG_WORLD_1);
    141         world1->setNextStoryID(WORLD_ID_2);
    142         debugCampaign->addEntity(world1, WORLD_ID_1);
    143 
    144         World* world2 = new World(DEBUG_WORLD_2);
    145         world2->setNextStoryID(WORLD_ID_GAMEEND);
    146         debugCampaign->addEntity(world2, WORLD_ID_2);
    147 
    148         this->currentCampaign = debugCampaign;
    149         break;*/
    150       }
    151     }
    152 }
    153 
    154 
    155 /**
    156  *  starts the current entity
    157  * @returns error code if this action has caused a error
    158  */
    159 ErrorMessage GameLoader::start()
    160 {
    161   if(this->currentCampaign != NULL)
    162   {
    163     this->currentCampaign->start();
    164   }
    165 }
    166 
    167 
    168 /**
    169  *  stops the current entity
    170  * @returns error code if this action has caused a error
    171  *
    172  *  ATTENTION: this function shouldn't call other functions, or if so, they must return
    173  *  after finishing. If you ignore or forget to do so, the current entity is not able to
    174  *  terminate and it will run in the background or the ressources can't be freed or even
    175  *  worse: are freed and the program will end in a segmentation fault!
    176  *  hehehe, have ya seen it... :)
    177  */
    178 void GameLoader::stop()
    179 {
    180   if(this->currentCampaign != NULL)
    181     this->currentCampaign->stop();
    182 }
    183 
    184 
    185 /**
    186  *  pause the current entity
    187  * @returns error code if this action has caused a error
    188  *
    189  * this pauses the current entity or passes this call forth to the running entity.
    190  */
    191 ErrorMessage GameLoader::pause()
    192 {
    193   this->isPaused = true;
    194   if(this->currentCampaign != NULL)
    195     this->currentCampaign->pause();
    196 }
    197 
    198 
    199 /**
    200  *  resumes a pause
    201  * @returns error code if this action has caused a error
    202  *
    203  *  this resumess the current entity or passes this call forth to the running entity.
    204  */
    205 ErrorMessage GameLoader::resume()
    206 {
    207   this->isPaused = false;
    208   if(this->currentCampaign != NULL)
    209     this->currentCampaign->resume();
    210 }
    211 
    212 
    213 /**
    214  *  reads a campaign definition file into a campaign class
    215  * @param fileName to be loaded
    216  * @returns the loaded campaign
    217  *
    218  * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns
    219  */
    220 Campaign* GameLoader::fileToCampaign(const std::string& fileName)
    221 {
    222   /* do not entirely load the campaign. just the current world
    223      before start of each world, it has to be initialized so it
    224      can load everything it needs into memory then.
    225   */
    226 
    227   if( fileName.empty())
     96  }
     97
     98
     99  /**
     100   *  reads a campaign definition file into a campaign class
     101   * @param fileName to be loaded
     102   * @returns the loaded campaign
     103   *
     104   * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns
     105   */
     106  ErrorMessage GameLoader::loadNetworkCampaign(const std::string& fileName)
     107  {
     108    ErrorMessage errorCode;
     109    std::string campaignName = ResourceManager::getFullName(fileName);
     110    if (!campaignName.empty())
     111    {
     112      this->currentCampaign = this->fileToCampaign(campaignName);
     113    }
     114  }
     115
     116
     117  /**
     118   *  loads a debug campaign for test purposes only.
     119   * @param campaignID the identifier of the campaign.
     120   * @returns error message if not able to do so.
     121   */
     122  ErrorMessage GameLoader::loadDebugCampaign(Uint32 campaignID)
     123  {
     124    switch(campaignID)
     125    {
     126        /*
     127           Debug Level 0: Debug level used to test the base frame work.
     128           As you can see, all storyentity data is allocated before game
     129           start. the storyentity will load themselfs shortly before start
     130           through the StoryEntity::init() funtion.
     131        */
     132      case DEBUG_CAMPAIGN_0:
     133        {
     134          /*        Campaign* debugCampaign = new Campaign();
     135
     136                  World* world0 = new World(DEBUG_WORLD_0);
     137                  world0->setNextStoryID(WORLD_ID_1);
     138                  debugCampaign->addEntity(world0, WORLD_ID_0);
     139
     140                  World* world1 = new World(DEBUG_WORLD_1);
     141                  world1->setNextStoryID(WORLD_ID_2);
     142                  debugCampaign->addEntity(world1, WORLD_ID_1);
     143
     144                  World* world2 = new World(DEBUG_WORLD_2);
     145                  world2->setNextStoryID(WORLD_ID_GAMEEND);
     146                  debugCampaign->addEntity(world2, WORLD_ID_2);
     147
     148                  this->currentCampaign = debugCampaign;
     149                  break;*/
     150        }
     151    }
     152  }
     153
     154
     155  /**
     156   *  starts the current entity
     157   * @returns error code if this action has caused a error
     158   */
     159  ErrorMessage GameLoader::start()
     160  {
     161    if(this->currentCampaign != NULL)
     162    {
     163      this->currentCampaign->start();
     164    }
     165  }
     166
     167
     168  /**
     169   *  stops the current entity
     170   * @returns error code if this action has caused a error
     171   *
     172   *  ATTENTION: this function shouldn't call other functions, or if so, they must return
     173   *  after finishing. If you ignore or forget to do so, the current entity is not able to
     174   *  terminate and it will run in the background or the ressources can't be freed or even
     175   *  worse: are freed and the program will end in a segmentation fault!
     176   *  hehehe, have ya seen it... :)
     177   */
     178  void GameLoader::stop()
     179  {
     180    if(this->currentCampaign != NULL)
     181      this->currentCampaign->stop();
     182  }
     183
     184
     185  /**
     186   *  pause the current entity
     187   * @returns error code if this action has caused a error
     188   *
     189   * this pauses the current entity or passes this call forth to the running entity.
     190   */
     191  ErrorMessage GameLoader::pause()
     192  {
     193    this->isPaused = true;
     194    if(this->currentCampaign != NULL)
     195      this->currentCampaign->pause();
     196  }
     197
     198
     199  /**
     200   *  resumes a pause
     201   * @returns error code if this action has caused a error
     202   *
     203   *  this resumess the current entity or passes this call forth to the running entity.
     204   */
     205  ErrorMessage GameLoader::resume()
     206  {
     207    this->isPaused = false;
     208    if(this->currentCampaign != NULL)
     209      this->currentCampaign->resume();
     210  }
     211
     212
     213  /**
     214   *  reads a campaign definition file into a campaign class
     215   * @param fileName to be loaded
     216   * @returns the loaded campaign
     217   *
     218   * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns
     219   */
     220  Campaign* GameLoader::fileToCampaign(const std::string& fileName)
     221  {
     222    /* do not entirely load the campaign. just the current world
     223       before start of each world, it has to be initialized so it
     224       can load everything it needs into memory then.
     225    */
     226
     227    if( fileName.empty())
    228228    {
    229229      PRINTF(2)("No filename specified for loading");
     
    231231    }
    232232
    233   TiXmlDocument XMLDoc(fileName);
    234   // load the campaign document
    235   if( !XMLDoc.LoadFile(fileName))
     233    TiXmlDocument XMLDoc(fileName);
     234    // load the campaign document
     235    if( !XMLDoc.LoadFile(fileName))
    236236    {
    237237      // report an error
     
    240240    }
    241241
    242   // check basic validity
    243   TiXmlElement* root = XMLDoc.RootElement();
    244   assert( root != NULL);
    245 
    246   if( strcmp( root->Value(), "Campaign"))
     242    // check basic validity
     243    TiXmlElement* root = XMLDoc.RootElement();
     244    assert( root != NULL);
     245
     246    if( strcmp( root->Value(), "Campaign"))
    247247    {
    248248      // report an error
     
    251251    }
    252252
    253   // construct campaign
    254   return new Campaign( root);
     253    // construct campaign
     254    return new Campaign( root);
     255  }
     256
     257
     258
     259  /**
     260   *  handle keyboard commands
     261   * @param event the event to handle
     262   */
     263  void GameLoader::process(const Event& event)
     264  {
     265    if( event.type == KeyMapper::PEV_NEXT_WORLD)
     266    {
     267      if( likely(event.bPressed))
     268      {
     269        this->switchToNextLevel();
     270      }
     271    }
     272    else if( event.type == KeyMapper::PEV_PAUSE)
     273    {
     274      if( likely(event.bPressed))
     275      {
     276        if(this->isPaused)
     277          this->resume();
     278        else
     279          this->pause();
     280      }
     281    }
     282    else if( event.type == KeyMapper::PEV_QUIT)
     283    {
     284      if( event.bPressed) this->stop();
     285    }
     286    else if (event.type == EV_MAIN_QUIT)
     287      this->stop();
     288  }
     289
     290
     291  /**
     292   *  this changes to the next level
     293   */
     294  void GameLoader::switchToNextLevel()
     295  {
     296    if(this->currentCampaign != NULL)
     297      this->currentCampaign->switchToNextLevel();
     298  }
     299
    255300}
    256 
    257 
    258 
    259 /**
    260  *  handle keyboard commands
    261  * @param event the event to handle
    262  */
    263 void GameLoader::process(const Event& event)
    264 {
    265   if( event.type == KeyMapper::PEV_NEXT_WORLD)
    266   {
    267     if( likely(event.bPressed))
    268     {
    269       this->switchToNextLevel();
    270     }
    271   }
    272   else if( event.type == KeyMapper::PEV_PAUSE)
    273   {
    274     if( likely(event.bPressed))
    275     {
    276       if(this->isPaused)
    277         this->resume();
    278       else
    279         this->pause();
    280     }
    281   }
    282   else if( event.type == KeyMapper::PEV_QUIT)
    283   {
    284     if( event.bPressed) this->stop();
    285   }
    286   else if (event.type == EV_MAIN_QUIT)
    287     this->stop();
    288 }
    289 
    290 
    291 /**
    292  *  this changes to the next level
    293  */
    294 void GameLoader::switchToNextLevel()
    295 {
    296   if(this->currentCampaign != NULL)
    297     this->currentCampaign->switchToNextLevel();
    298 }
    299 
    300 
Note: See TracChangeset for help on using the changeset viewer.