home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / Source / Chapter 10 / Engine / SpawnerObject.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-03-29  |  4.9 KB  |  138 lines

  1. //-----------------------------------------------------------------------------
  2. // SpawnerObject.h implementation.
  3. // Refer to the SpawnerObject.h interface for more details.
  4. //
  5. // Programming a Multiplayer First Person Shooter in DirectX
  6. // Copyright (c) 2004 Vaughan Young
  7. //-----------------------------------------------------------------------------
  8. #include "Engine.h"
  9.  
  10. //-----------------------------------------------------------------------------
  11. // The spawner object class constructor.
  12. //-----------------------------------------------------------------------------
  13. SpawnerObject::SpawnerObject( char *name, char *path, unsigned long type ) : SceneObject( type )
  14. {
  15.     // Set spawner objects as ghosts.
  16.     SetGhost( true );
  17.  
  18.     // Load the spawner's script.
  19.     Script *script = new Script( name, path );
  20.  
  21.     // Get the spawner's frequency.
  22.     m_frequency = *script->GetFloatData( "frequency" );
  23.  
  24.     // Clear the spawn timer.
  25.     m_spawnTimer = 0.0f;
  26.  
  27.     // Load the sound to play when the spawner's object is collected.
  28.     if( script->GetStringData( "sound" ) != NULL )
  29.     {
  30.         m_sound = new Sound( script->GetStringData( "sound" ) );
  31.         m_audioPath = new AudioPath3D;
  32.     }
  33.     else
  34.     {
  35.         m_sound = NULL;
  36.         m_audioPath = NULL;
  37.     }
  38.  
  39.     // Load the script for the spawner's object.
  40.     m_objectScript = g_engine->GetScriptManager()->Add( script->GetStringData( "object" ), script->GetStringData( "object_path" ) );
  41.  
  42.     // Get the name of the spawner's object.
  43.     m_name = new char[strlen( m_objectScript->GetStringData( "name" ) ) + 1];
  44.     strcpy( m_name, m_objectScript->GetStringData( "name" ) );
  45.  
  46.     // Set the spawner's mesh to use the object's mesh.
  47.     SetMesh( m_objectScript->GetStringData( "mesh" ), m_objectScript->GetStringData( "mesh_path" ) );
  48.  
  49.     // Set the object to spin slowly.
  50.     SetSpin( 0.0f, 1.0f, 0.0f );
  51.  
  52.     // Get the spawner's radius. A radius of 0.0 indicates that it must be
  53.     // taken from the object to be spawned.
  54.     if( *script->GetFloatData( "radius" ) != 0.0f )
  55.         SetBoundingSphere( D3DXVECTOR3( 0.0f, 0.0f, 0.0f ), *script->GetFloatData( "radius" ) );
  56.     else if( GetMesh() != NULL )
  57.         SetEllipsoidRadius( *m_objectScript->GetVectorData( "ellipse_radius" ) );
  58.  
  59.     // Destroy the spawner's script.
  60.     SAFE_DELETE( script );
  61. }
  62.  
  63. //-----------------------------------------------------------------------------
  64. // The spawner object class destructor.
  65. //-----------------------------------------------------------------------------
  66. SpawnerObject::~SpawnerObject()
  67. {
  68.     // Destroy the string buffer containing the name of the spawner's object.
  69.     SAFE_DELETE_ARRAY( m_name );
  70.  
  71.     // Destroy the collect sound and its audio path.
  72.     SAFE_DELETE( m_sound );
  73.     SAFE_DELETE( m_audioPath );
  74.  
  75.     // Destroy the spawner's object script.
  76.     g_engine->GetScriptManager()->Remove( &m_objectScript );
  77. }
  78.  
  79. //-----------------------------------------------------------------------------
  80. // Updates the spawner object.
  81. //-----------------------------------------------------------------------------
  82. void SpawnerObject::Update( float elapsed, bool addVelocity )
  83. {
  84.     // Allow the base scene object to update.
  85.     SceneObject::Update( elapsed, addVelocity );
  86.  
  87.     // Check if the spawner is not visible
  88.     if( GetVisible() == false )
  89.     {
  90.         // The spawner will become visible again after the set amount of
  91.         // time (frequency) has passed.
  92.         m_spawnTimer += elapsed;
  93.         if( m_spawnTimer >= m_frequency )
  94.         {
  95.             SetVisible( true );
  96.             SetIgnoreCollisions( false );
  97.             m_spawnTimer = 0.0f;
  98.         }
  99.     }
  100.  
  101.     // Update the collect sound's audio path.
  102.     if( m_audioPath != NULL )
  103.     {
  104.         m_audioPath->SetPosition( GetTranslation() );
  105.         m_audioPath->SetVelocity( GetVelocity() );
  106.     }
  107. }
  108.  
  109. //-----------------------------------------------------------------------------
  110. // Called when something collides with the spawner object.
  111. //-----------------------------------------------------------------------------
  112. void SpawnerObject::CollisionOccurred( SceneObject *object, unsigned long collisionStamp )
  113. {
  114.     // Allow the base scene object to register the collision.
  115.     SceneObject::CollisionOccurred( object, collisionStamp );
  116.  
  117.     // Prevent the spawner's object from being collected by any of the default
  118.     // engine objects. It can only be collected by user defined objects.
  119.     if( object->GetType() == TYPE_SCENE_OBJECT || object->GetType() == TYPE_ANIMATED_OBJECT || object->GetType() == TYPE_SPAWNER_OBJECT )
  120.         return;
  121.  
  122.     // Make the spawner invisible. This will prevent other objects from
  123.     // colliding with it until it respawns (i.e. becomes visible again).
  124.     SetVisible( false );
  125.     SetIgnoreCollisions( true );
  126.  
  127.     // Play the collected sound.
  128.     if( m_audioPath != NULL && m_sound != NULL )
  129.         m_audioPath->Play( m_sound->GetSegment() );
  130. }
  131.  
  132. //-----------------------------------------------------------------------------
  133. // Returns the script of the spawned object.
  134. //-----------------------------------------------------------------------------
  135. Script *SpawnerObject::GetObjectScript()
  136. {
  137.     return m_objectScript;
  138. }