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.

110 lines
2.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "proxyentity.h"
  9. #include "materialsystem/imaterial.h"
  10. #include "materialsystem/imaterialvar.h"
  11. #include "materialsystem/imaterialsystem.h"
  12. #include <KeyValues.h>
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include "tier0/memdbgon.h"
  15. // $sineVar : name of variable that controls the alpha level (float)
  16. class CShieldProxy : public CEntityMaterialProxy
  17. {
  18. public:
  19. CShieldProxy();
  20. virtual ~CShieldProxy();
  21. virtual bool Init( IMaterial *pMaterial, KeyValues *pKeyValues );
  22. virtual void OnBind( C_BaseEntity *pC_BaseEntity );
  23. virtual IMaterial *GetMaterial();
  24. private:
  25. IMaterialVar *m_AlphaVar;
  26. IMaterialVar *m_pTextureScrollVar;
  27. float m_ScrollRate;
  28. float m_ScrollAngle;
  29. };
  30. CShieldProxy::CShieldProxy()
  31. {
  32. m_AlphaVar = NULL;
  33. m_pTextureScrollVar = NULL;
  34. m_ScrollRate = 0;
  35. m_ScrollAngle = 0;
  36. }
  37. CShieldProxy::~CShieldProxy()
  38. {
  39. }
  40. bool CShieldProxy::Init( IMaterial *pMaterial, KeyValues *pKeyValues )
  41. {
  42. bool foundVar;
  43. m_AlphaVar = pMaterial->FindVar( "$translucency", &foundVar, false );
  44. if( !foundVar )
  45. return false;
  46. char const* pScrollVarName = pKeyValues->GetString( "textureScrollVar" );
  47. if (!pScrollVarName)
  48. return false;
  49. m_pTextureScrollVar = pMaterial->FindVar( pScrollVarName, &foundVar, false );
  50. if( !foundVar )
  51. return false;
  52. m_ScrollRate = pKeyValues->GetFloat( "textureScrollRate", 1 );
  53. m_ScrollAngle = pKeyValues->GetFloat( "textureScrollAngle", 0 );
  54. return true;
  55. }
  56. void CShieldProxy::OnBind( C_BaseEntity *pEnt )
  57. {
  58. if (m_AlphaVar)
  59. {
  60. m_AlphaVar->SetFloatValue( pEnt->m_clrRender->a );
  61. }
  62. if( !m_pTextureScrollVar )
  63. {
  64. return;
  65. }
  66. float sOffset, tOffset;
  67. sOffset = gpGlobals->curtime * sin( m_ScrollAngle * ( M_PI / 180.0f ) ) * m_ScrollRate;
  68. tOffset = gpGlobals->curtime * cos( m_ScrollAngle * ( M_PI / 180.0f ) ) * m_ScrollRate;
  69. // make sure that we are positive
  70. if( sOffset < 0.0f )
  71. {
  72. sOffset += 1.0f + -( int )sOffset;
  73. }
  74. if( tOffset < 0.0f )
  75. {
  76. tOffset += 1.0f + -( int )tOffset;
  77. }
  78. // make sure that we are in a [0,1] range
  79. sOffset = sOffset - ( int )sOffset;
  80. tOffset = tOffset - ( int )tOffset;
  81. m_pTextureScrollVar->SetVecValue( sOffset, tOffset, 0.0f );
  82. }
  83. IMaterial *CShieldProxy::GetMaterial()
  84. {
  85. if ( !m_AlphaVar )
  86. return NULL;
  87. return m_AlphaVar->GetOwningMaterial();
  88. }
  89. EXPOSE_INTERFACE( CShieldProxy, IMaterialProxy, "Shield" IMATERIAL_PROXY_INTERFACE_VERSION );