Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.2 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "c_baseentity.h"
  9. #include "soundinfo.h"
  10. // memdbgon must be the last include file in a .cpp file!!!
  11. #include "tier0/memdbgon.h"
  12. //-----------------------------------------------------------------------------
  13. // An entity which emits other entities at points
  14. //-----------------------------------------------------------------------------
  15. class C_FuncTrackTrain : public C_BaseEntity
  16. {
  17. public:
  18. DECLARE_CLASS( C_FuncTrackTrain, C_BaseEntity );
  19. DECLARE_CLIENTCLASS();
  20. public:
  21. virtual void OnDataChanged( DataUpdateType_t updateType );
  22. virtual bool GetSoundSpatialization( SpatializationInfo_t& info );
  23. virtual bool IsBaseTrain( void ) const { return true; }
  24. private:
  25. int m_nLongAxis;
  26. float m_flRadius;
  27. float m_flLineLength;
  28. };
  29. //-----------------------------------------------------------------------------
  30. // Datatable
  31. //-----------------------------------------------------------------------------
  32. IMPLEMENT_CLIENTCLASS_DT( C_FuncTrackTrain, DT_FuncTrackTrain, CFuncTrackTrain )
  33. END_RECV_TABLE()
  34. //-----------------------------------------------------------------------------
  35. // Sound spatialization
  36. //-----------------------------------------------------------------------------
  37. void C_FuncTrackTrain::OnDataChanged( DataUpdateType_t updateType )
  38. {
  39. BaseClass::OnDataChanged( updateType );
  40. if (updateType == DATA_UPDATE_CREATED)
  41. {
  42. // Compute the cross-sectional area and dimension and length of the line segment
  43. int nIndex1, nIndex2;
  44. const Vector &vecOBBSize = CollisionProp()->OBBSize();
  45. if ( ( vecOBBSize.x > vecOBBSize.y ) && ( vecOBBSize.x > vecOBBSize.z ) )
  46. {
  47. m_nLongAxis = 0;
  48. nIndex1 = 1; nIndex2 = 2;
  49. }
  50. else if ( vecOBBSize.y > vecOBBSize.z )
  51. {
  52. m_nLongAxis = 1;
  53. nIndex1 = 0; nIndex2 = 2;
  54. }
  55. else
  56. {
  57. m_nLongAxis = 2;
  58. nIndex1 = 0; nIndex2 = 1;
  59. }
  60. m_flRadius = sqrt( vecOBBSize[nIndex1] * vecOBBSize[nIndex1] + vecOBBSize[nIndex2] * vecOBBSize[nIndex2] ) * 0.5f;
  61. m_flLineLength = vecOBBSize[m_nLongAxis];
  62. }
  63. }
  64. //-----------------------------------------------------------------------------
  65. // Sound spatialization
  66. //-----------------------------------------------------------------------------
  67. bool C_FuncTrackTrain::GetSoundSpatialization( SpatializationInfo_t& info )
  68. {
  69. // Out of PVS
  70. if ( IsDormant() )
  71. return false;
  72. if ( info.pflRadius )
  73. {
  74. *info.pflRadius = m_flRadius;
  75. }
  76. if ( info.pOrigin )
  77. {
  78. Vector vecStart, vecEnd, vecWorldDir;
  79. Vector vecDir = vec3_origin;
  80. vecDir[m_nLongAxis] = 1.0f;
  81. VectorRotate( vecDir, EntityToWorldTransform(), vecWorldDir );
  82. VectorMA( WorldSpaceCenter(), -0.5f * m_flLineLength, vecWorldDir, vecStart );
  83. VectorMA( vecStart, m_flLineLength, vecWorldDir, vecEnd );
  84. float t;
  85. CalcClosestPointOnLine( info.info.vListenerOrigin, vecStart, vecEnd, *info.pOrigin, &t );
  86. if ( t < 0.0f )
  87. {
  88. *info.pOrigin = vecStart;
  89. }
  90. else if ( t > 1.0f )
  91. {
  92. *info.pOrigin = vecEnd;
  93. }
  94. }
  95. if ( info.pAngles )
  96. {
  97. VectorCopy( CollisionProp()->GetCollisionAngles(), *info.pAngles );
  98. }
  99. return true;
  100. }