home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / game / client / ClientEntity.cpp < prev    next >
C/C++ Source or Header  |  2005-11-14  |  8KB  |  359 lines

  1. //----------------------------------------------------------------
  2. // ClientEntity.cpp
  3. //
  4. // Copyright 2002-2004 Raven Software
  5. //----------------------------------------------------------------
  6.  
  7. #include "../../idlib/precompiled.h"
  8. #pragma hdrstop
  9.  
  10. #include "../Game_local.h"
  11.  
  12. ABSTRACT_DECLARATION( idClass, rvClientEntity )
  13. END_CLASS
  14.  
  15. /*
  16. ================
  17. rvClientEntity::rvClientEntity
  18. ================
  19. */
  20. rvClientEntity::rvClientEntity( void ) {
  21.     bindMaster = NULL;
  22.     entityNumber = -1;
  23.  
  24.     bindOrigin.Zero();
  25.     bindAxis.Identity();
  26.     bindJoint = INVALID_JOINT;
  27.     
  28.     spawnNode.SetOwner( this );
  29.     bindNode.SetOwner( this );
  30.  
  31.     memset( &refSound, 0, sizeof(refSound) );
  32.     refSound.referenceSoundHandle = -1;
  33.  
  34.     gameLocal.RegisterClientEntity( this );
  35. }
  36.  
  37. /*
  38. ================
  39. rvClientEntity::~rvClientEntity
  40. ================
  41. */
  42. rvClientEntity::~rvClientEntity( void ) {
  43.     Unbind();
  44.     gameLocal.UnregisterClientEntity( this );
  45.  
  46.     // Free sound emitter
  47.     soundSystem->FreeSoundEmitter( SOUNDWORLD_GAME, refSound.referenceSoundHandle, true );
  48.     refSound.referenceSoundHandle = -1;
  49. }
  50.  
  51. /*
  52. ================
  53. rvClientEntity::Present
  54. ================
  55. */
  56. void rvClientEntity::Present ( void ) {
  57. }
  58.  
  59. /*
  60. ================
  61. rvClientEntity::FreeEntityDef
  62. ================
  63. */
  64. void rvClientEntity::FreeEntityDef ( void ) {
  65. }
  66.  
  67. /*
  68. ================
  69. rvClientEntity::Think
  70. ================
  71. */
  72. void rvClientEntity::Think ( void ) {
  73.     UpdateBind ( );
  74.     UpdateSound ( );
  75.     Present ( );
  76. }
  77.  
  78. /*
  79. ================
  80. rvClientEntity::Bind
  81. ================
  82. */
  83. void rvClientEntity::Bind ( idEntity* master, jointHandle_t joint ) {
  84.     Unbind ( );
  85.  
  86.     if ( joint != INVALID_JOINT && !dynamic_cast<idAnimatedEntity*>(master) ) {
  87.         gameLocal.Warning( "rvClientEntity::Bind: entity '%s' cannot support skeletal models.", master->GetName() );
  88.         joint = INVALID_JOINT;
  89.     }
  90.  
  91.     bindMaster = master;
  92.     bindJoint  = joint;
  93.     bindOrigin = worldOrigin;
  94.     bindAxis   = worldAxis;
  95.  
  96.     bindNode.AddToEnd ( bindMaster->clientEntities );
  97.     
  98.     UpdateBind();
  99. }
  100.  
  101. /*
  102. ================
  103. rvClientEntity::Bind
  104. ================
  105. */
  106. void rvClientEntity::Unbind    ( void ) {
  107.     if ( !bindMaster ) {
  108.         return;
  109.     }
  110.  
  111.     bindMaster = NULL;
  112.     bindNode.Remove ( );
  113. }
  114.  
  115. /*
  116. ================
  117. rvClientEntity::SetOrigin
  118. ================
  119. */
  120. void rvClientEntity::SetOrigin ( const idVec3& origin ) {
  121.     if ( bindMaster ) {
  122.         bindOrigin = origin;
  123.     } else {
  124.         worldOrigin = origin;
  125.     }
  126. }
  127.  
  128. /*
  129. ================
  130. rvClientEntity::SetAxis
  131. ================
  132. */
  133. void rvClientEntity::SetAxis ( const idMat3& axis ) {
  134.     if ( bindMaster ) {
  135.         bindAxis = axis * bindMaster->GetRenderEntity()->axis.Transpose();        
  136.     } else {
  137.         worldAxis = axis;
  138.     }
  139. }
  140.  
  141. /*
  142. ================
  143. rvClientEntity::UpdateBind
  144. ================
  145. */
  146. void rvClientEntity::UpdateBind ( void ) {
  147.     if ( !bindMaster ) {
  148.         return;
  149.     }
  150.  
  151.     if ( bindJoint != INVALID_JOINT ) {
  152.         static_cast<idAnimatedEntity*>(bindMaster.GetEntity())->GetJointWorldTransform ( bindJoint, gameLocal.time, worldOrigin, worldAxis );
  153.     } else {
  154.         bindMaster->GetPosition( worldOrigin, worldAxis );
  155.         //if ( !bindMaster->GetPhysicsToVisualTransform( worldOrigin, worldAxis ) ) {
  156.         //    bindMaster->GetPosition( worldOrigin, worldAxis );
  157.         //}
  158.     }
  159.  
  160.     worldOrigin += (bindOrigin * worldAxis);
  161.     worldAxis    = bindAxis * worldAxis;
  162. }
  163.  
  164. /*
  165. ================
  166. rvClientEntity::IsClient
  167. ================
  168. */
  169. bool rvClientEntity::IsClient ( void ) const {
  170.     return true;
  171. }
  172.  
  173. /*
  174. ================
  175. rvClientEntity::DrawDebugInfo
  176. ================
  177. */
  178. void rvClientEntity::DrawDebugInfo ( void ) const {
  179.     idBounds bounds ( idVec3(-8,-8,-8), idVec3(8,8,8) );
  180.     
  181.     gameRenderWorld->DebugBounds ( colorGreen, bounds, worldOrigin );
  182.  
  183.     if ( gameLocal.GetLocalPlayer() ) {    
  184.         gameRenderWorld->DrawText ( GetClassname ( ), worldOrigin, 0.1f, colorWhite, gameLocal.GetLocalPlayer()->viewAngles.ToMat3(), 1 );
  185.     }
  186. }
  187.  
  188. /*
  189. ================
  190. rvClientEntity::UpdateSound
  191. ================
  192. */
  193. void rvClientEntity::UpdateSound( void ) {
  194.     idSoundEmitter *emitter = soundSystem->EmitterForIndex( SOUNDWORLD_GAME, refSound.referenceSoundHandle );
  195.     if ( emitter ) {
  196.         refSound.origin = worldOrigin;
  197.         refSound.velocity = vec3_origin;
  198.         emitter->UpdateEmitter( refSound.origin, refSound.velocity, refSound.listenerId, &refSound.parms );
  199.     }
  200. }
  201.  
  202. /*
  203. ================
  204. rvClientEntity::StartSoundShader
  205. ================
  206. */
  207. int rvClientEntity::StartSoundShader ( const idSoundShader* shader, const s_channelType channel, int soundShaderFlags )  {
  208.     if ( !shader ) {
  209.         return 0;
  210.     }
  211.     
  212.     idSoundEmitter *emitter = soundSystem->EmitterForIndex( SOUNDWORLD_GAME, refSound.referenceSoundHandle );
  213.     if ( !emitter ) {
  214.         refSound.referenceSoundHandle = soundSystem->AllocSoundEmitter( SOUNDWORLD_GAME );
  215.     }
  216.  
  217.     UpdateSound();
  218.     
  219.     emitter = soundSystem->EmitterForIndex( SOUNDWORLD_GAME, refSound.referenceSoundHandle );
  220.     if( !emitter ) { 
  221.         return( 0 );
  222.     }
  223.  
  224.     emitter->UpdateEmitter( refSound.origin, refSound.velocity, refSound.listenerId, &refSound.parms );
  225.     return emitter->StartSound( shader, channel, gameLocal.random.RandomFloat(), soundShaderFlags  );
  226. }
  227.  
  228. /*
  229. ================
  230. rvClientEntity::Size
  231.  
  232. Returns Returns memory size of an rvClientEntity.
  233. ================
  234. */
  235.  
  236. size_t rvClientEntity::Size ( void ) const {
  237.     return sizeof( rvClientEntity );
  238. }
  239.  
  240. /*
  241. ================
  242. rvClientEntity::Save
  243. ================
  244. */
  245. void rvClientEntity::Save( idSaveGame *savefile ) const {
  246.     savefile->WriteInt( entityNumber );
  247.  
  248.     // idLinkList<rvClientEntity>    spawnNode;        - reconstructed in the master entity load
  249.     // idLinkList<rvClientEntity>    bindNode;        - reconstructed in the master entity load
  250.  
  251.     savefile->WriteVec3( worldOrigin );
  252.     savefile->WriteMat3( worldAxis );
  253.  
  254.     bindMaster.Save( savefile );
  255.     savefile->WriteVec3( bindOrigin );
  256.     savefile->WriteMat3( bindAxis );
  257.     savefile->WriteJoint( bindJoint );
  258.     
  259.     savefile->WriteRefSound( refSound );
  260. }
  261.  
  262. /*
  263. ================
  264. rvClientEntity::Restore
  265. ================
  266. */
  267. void rvClientEntity::Restore( idRestoreGame *savefile ) {
  268.     savefile->ReadInt( entityNumber );
  269.  
  270.     // idLinkList<rvClientEntity>    spawnNode;        - reconstructed in the master entity load
  271.     // idLinkList<rvClientEntity>    bindNode;        - reconstructed in the master entity load
  272.  
  273.     savefile->ReadVec3( worldOrigin );
  274.     savefile->ReadMat3( worldAxis );
  275.  
  276.     bindMaster.Restore( savefile );
  277.     savefile->ReadVec3( bindOrigin );
  278.     savefile->ReadMat3( bindAxis );
  279.     savefile->ReadJoint( bindJoint );
  280.     
  281.     savefile->ReadRefSound( refSound );
  282. }
  283.  
  284. /*
  285. ================
  286. rvClientEntity::RunPhysics
  287. ================
  288. */
  289. void rvClientEntity::RunPhysics ( void ) {
  290.     idPhysics* physics = GetPhysics( );
  291.     if( !physics ) {
  292.         return;
  293.     }
  294.  
  295.     idEntity* clientPhysics = gameLocal.entities[ENTITYNUM_CLIENT];
  296.     static_cast<rvClientPhysics*>( clientPhysics )->currentEntityNumber = entityNumber;
  297.  
  298.     clientPhysics->SetPhysics( physics );
  299.     physics->Evaluate ( gameLocal.time - gameLocal.previousTime, gameLocal.time );
  300.     clientPhysics->SetPhysics( NULL );
  301.  
  302.     worldOrigin = physics->GetOrigin ( );
  303.     worldAxis = physics->GetAxis ( );
  304. }
  305.  
  306. /*
  307. ================
  308. rvClientEntity::GetPhysics
  309. ================
  310. */
  311. idPhysics* rvClientEntity::GetPhysics ( void ) const {
  312.     return NULL;
  313. }
  314.  
  315. /*
  316. ================
  317. rvClientEntity::Collide
  318. ================
  319. */
  320. bool rvClientEntity::Collide ( const trace_t &collision, const idVec3 &velocity ) {
  321.     return false;
  322. }
  323.  
  324. /*
  325. ===============================================================================
  326.  
  327. rvClientPhysics
  328.  
  329. ===============================================================================
  330. */
  331.  
  332. CLASS_DECLARATION( idEntity, rvClientPhysics )
  333. END_CLASS
  334.  
  335. /*
  336. =====================
  337. rvClientPhysics::Spawn
  338. =====================
  339. */
  340. void rvClientPhysics::Spawn( void ) {
  341. }
  342.  
  343. /*
  344. =====================
  345. rvClientPhysics::Spawn
  346. =====================
  347. */
  348. bool rvClientPhysics::Collide( const trace_t &collision, const idVec3 &velocity ) {
  349.     assert ( currentEntityNumber >= 0 && currentEntityNumber < MAX_CENTITIES );
  350.     
  351.     rvClientEntity* cent;
  352.     cent = gameLocal.clientEntities [ currentEntityNumber ];
  353.     if ( cent ) {
  354.         return cent->Collide ( collision, velocity );
  355.     }
  356.     
  357.     return false;
  358. }
  359.