⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 oasisobjectsystem.cpp

📁 使用stl技术,(还没看,是听说的)
💻 CPP
字号:
/******************************************************************************

 * This source file is part of Bad Camel Gaming

 * Copyright (C) 2003  Zephie Greyvenstein

 * See Readme.html for acknowledgements

 *

 * This library is free software; you can redistribute it and/or

 * modify it under the terms of the GNU Lesser General Public

 * License as published by the Free Software Foundation; either

 * version 2.1 of the License, or (at your option) any later version.

 *

 * This library is distributed in the hope that it will be useful,

 * but WITHOUT ANY WARRANTY; without even the implied warranty of

 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU

 * Lesser General Public License for more details.

 *

 * You should have received a copy of the GNU Lesser General Public

 * License along with this library; if not, write to the Free Software

 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

 *****************************************************************************/



/******************************************************************************

 * FILENAME    : oasisObjectSystem.cpp

 * DESCRIPTION : The object system

 * AUTHOR      : Zephie Greyvenstein

 *****************************************************************************/



#include "oasisObjectSystem.h"

#include "oasisObject.h"

#include "oasisObjectResource.h"



#include "OgreStringVector.h"

#include "OgreSDDataChunk.h"

#include "OgreStringConverter.h"



namespace Oasis {

  // Setup the singleton

  template< > objectSystem *singleton< objectSystem >::singletonPtr = 0;



  objectSystem::objectSystem( ) : system( "Object" ), pool( "object" ) {

    // Parse all object scripts

    _parseAllSources( ".objects" );

  }

  

  objectSystem::~objectSystem( ) {

   

  }

  

  objectSystem &objectSystem::get( void ) {

    return singleton< objectSystem >::get( );

  }

 

  void objectSystem::reset( void ) {

    // Clear the pool

    clearPool( );

  }



  void objectSystem::update( real time ) {

   

  }



  objectResource *objectSystem::getObjectTemplate( const string &name ) {

    // Try and get the template

    objectResource *objectFile = ( objectResource* )( getByName( name ) );

    

    if ( !objectFile ) {

      oasisError( exception::ET_ITEM_NOT_FOUND,

      "Object template " + name + " not found!",

      "objectSystem::getObjectTemplate" );

    }

    

    return objectFile;

  }

  

  objectResource *objectSystem::createObjectTemplate( const string &name ) { 

    return static_cast< objectResource* >( createResource( name ) );

  }



  object *objectSystem::createObject( const string &objectName,

              const string &templateName ) {   

    // Try and get the template

    objectResource *temp = getObjectTemplate( templateName );

    // Get an object from the pool

    object *newObject = static_cast< object* >( _getFreePoolable( objectName ) );

    

    // Set it up

    newObject->setup( temp );

     

    return newObject;

  }



  void objectSystem::removeObject( object *oldObject ) {

    // Return object to the pool

    _consumePoolable( oldObject );

  }



  void objectSystem::removeObject( const string &objectName ) {

    // Return object to the pool

    _consumePoolable( objectName );

  }



  object *objectSystem::getObject( const string &objectName ) {

    object *obj = static_cast< object* >( _getUsedPoolable( objectName ) );



    if ( !obj ) {

      oasisError( exception::ET_ITEM_NOT_FOUND,

      "Object " + objectName + " does not exist!",

      "objectSystem::getObject" );

    }



    return obj;

  }

  

  void objectSystem::_parseFile( Ogre::DataChunk &chunk ) {

    Ogre::String line;

    objectResource *file = NULL;

    

    while( !chunk.isEOF( ) ) {

      line = chunk.getLine( );

      // Ignore blanks & comments

      if( !line.length( ) || line.substr( 0, 2 ) == "//" ) {

  continue;

      } else {

  if ( file == NULL ) {

    // No current script

    file = createObjectTemplate( line );

    // Skip to and over next {

    chunk.skipUpTo( "{" );

  } else {  

    // Already in script

    if ( line == "}" ) {

      // Finished 

      file = NULL;    

    } else {

      _parseAttribute( line, file );

    }

  }

      }

    }

  }

  

  void objectSystem::_parseAttribute( const Ogre::String &line, 

             objectResource *file ) {

    Ogre::StringVector params = line.split( );

    Ogre::String attrib = params[ 0 ].toLowerCase( );

    

    if ( attrib == "entity" ) {

      if ( params.size( ) != 2 ) {

  _logError( line, file );

  return;

      }

      

      file->setEntity( params[ 1 ] );

    } else if ( attrib == "sound_event" ) {

      if ( params.size( ) != 3 ) {

  _logError( line, file );

  return;

      }

      

      file->addSound( params[ 1 ], params[ 2 ] );

    } else if ( attrib == "physics" ) {

      if ( params.size( ) != 2 ) {

  _logError( line, file );

  return;

      }

            

      file->setPhysics( params[ 1 ] );

    } else if ( attrib == "scale" ) {

      if ( params.size( ) != 4 ) {

  _logError( line, file );

  return;

      }

            

      file->setScale( Ogre::StringConverter::parseReal( params[ 1 ] ),

          Ogre::StringConverter::parseReal( params[ 2 ] ),

          Ogre::StringConverter::parseReal( params[ 3 ] ) );

    }



  }



  poolable *objectSystem::_createPoolable( void ) {

    // Create a new object

    object *newObject = new object( );



    return newObject;

  }



  resource *objectSystem::createResource( const string &name ) {

    // First make sure this doesn't exist

    if ( getByName( name ) != 0 ) {

      oasisError( exception::ET_DUPLICATE_ITEM,

      "Object resource file " + name + "already exists.",

      "objectSystem::create" );     

    }

    

    objectResource *objectFile = new objectResource( name );

    

    // Don't load now

    resources[ name ] = objectFile;

      

    return objectFile;

  }



}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -