Team Fortress 2 Source Code as on 22/4/2020
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.

136 lines
4.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: A clientside, visual only model that's attached to players
  4. //
  5. //=============================================================================
  6. #include "cbase.h"
  7. #include "c_playerattachedmodel.h"
  8. // Todo: Turn these all into parameters
  9. #define PAM_ANIMATE_TIME 0.075
  10. #define PAM_ROTATE_TIME 0.075
  11. #define PAM_SCALE_SPEED 7
  12. #define PAM_MAX_SCALE 3
  13. #define PAM_SPIN_SPEED 360
  14. //-----------------------------------------------------------------------------
  15. // Purpose:
  16. //-----------------------------------------------------------------------------
  17. C_PlayerAttachedModel *C_PlayerAttachedModel::Create( const char *pszModelName, C_BaseEntity *pParent, int iAttachment, Vector vecOffset, float flLifetime, int iFlags )
  18. {
  19. C_PlayerAttachedModel *pFlash = new C_PlayerAttachedModel;
  20. if ( !pFlash )
  21. return NULL;
  22. if ( !pFlash->Initialize( pszModelName, pParent, iAttachment, vecOffset, flLifetime, iFlags ) )
  23. return NULL;
  24. return pFlash;
  25. }
  26. //-----------------------------------------------------------------------------
  27. // Purpose:
  28. //-----------------------------------------------------------------------------
  29. bool C_PlayerAttachedModel::Initialize( const char *pszModelName, C_BaseEntity *pParent, int iAttachment, Vector vecOffset, float flLifetime, int iFlags )
  30. {
  31. AddEffects( EF_NORECEIVESHADOW | EF_NOSHADOW );
  32. if ( InitializeAsClientEntity( pszModelName, RENDER_GROUP_OPAQUE_ENTITY ) == false )
  33. {
  34. Release();
  35. return false;
  36. }
  37. SetParent( pParent, iAttachment );
  38. SetLocalOrigin( vecOffset );
  39. SetLocalAngles( vec3_angle );
  40. AddSolidFlags( FSOLID_NOT_SOLID );
  41. SetLifetime( flLifetime );
  42. SetNextClientThink( CLIENT_THINK_ALWAYS );
  43. SetCycle( 0 );
  44. m_iFlags = iFlags;
  45. m_flScale = 0;
  46. if ( m_iFlags & PAM_ROTATE_RANDOMLY )
  47. {
  48. m_flRotateAt = gpGlobals->curtime + PAM_ANIMATE_TIME;
  49. }
  50. if ( m_iFlags & PAM_ANIMATE_RANDOMLY )
  51. {
  52. m_flAnimateAt = gpGlobals->curtime + PAM_ROTATE_TIME;
  53. }
  54. return true;
  55. }
  56. //-----------------------------------------------------------------------------
  57. // Purpose:
  58. //-----------------------------------------------------------------------------
  59. void C_PlayerAttachedModel::SetLifetime( float flLifetime )
  60. {
  61. if ( flLifetime == PAM_PERMANENT )
  62. {
  63. m_flExpiresAt = PAM_PERMANENT;
  64. }
  65. else
  66. {
  67. // Expire when the lifetime is up
  68. m_flExpiresAt = gpGlobals->curtime + flLifetime;
  69. }
  70. }
  71. //-----------------------------------------------------------------------------
  72. // Purpose:
  73. //-----------------------------------------------------------------------------
  74. void C_PlayerAttachedModel::ClientThink( void )
  75. {
  76. if ( !GetMoveParent() || (m_flExpiresAt != PAM_PERMANENT && gpGlobals->curtime > m_flExpiresAt) )
  77. {
  78. Release();
  79. return;
  80. }
  81. if ( m_iFlags & PAM_ANIMATE_RANDOMLY && gpGlobals->curtime > m_flAnimateAt )
  82. {
  83. float flDelta = RandomFloat(0.2,0.4) * (RandomInt(0,1) == 1 ? 1 : -1);
  84. float flCycle = clamp( GetCycle() + flDelta, 0.f, 1.f );
  85. SetCycle( flCycle );
  86. m_flAnimateAt = gpGlobals->curtime + PAM_ANIMATE_TIME;
  87. }
  88. if ( m_iFlags & PAM_ROTATE_RANDOMLY && gpGlobals->curtime > m_flRotateAt )
  89. {
  90. SetLocalAngles( QAngle(0,0,RandomFloat(0,360)) );
  91. m_flRotateAt = gpGlobals->curtime + PAM_ROTATE_TIME;
  92. }
  93. if ( m_iFlags & PAM_SPIN_Z )
  94. {
  95. float flAng = GetAbsAngles().y + (gpGlobals->frametime * PAM_SPIN_SPEED);
  96. SetLocalAngles( QAngle(0,flAng,0) );
  97. }
  98. if ( m_iFlags & PAM_SCALEUP )
  99. {
  100. m_flScale = MIN( m_flScale + (gpGlobals->frametime * PAM_SCALE_SPEED), PAM_MAX_SCALE );
  101. }
  102. }
  103. //-----------------------------------------------------------------------------
  104. // Purpose:
  105. //-----------------------------------------------------------------------------
  106. void C_PlayerAttachedModel::ApplyBoneMatrixTransform( matrix3x4_t& transform )
  107. {
  108. BaseClass::ApplyBoneMatrixTransform( transform );
  109. if ( !(m_iFlags & PAM_SCALEUP) )
  110. return;
  111. VectorScale( transform[0], m_flScale, transform[0] );
  112. VectorScale( transform[1], m_flScale, transform[1] );
  113. VectorScale( transform[2], m_flScale, transform[2] );
  114. }