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.

150 lines
3.4 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #include "cbase.h"
  7. #include "materialsystem/imaterialproxy.h"
  8. #include "materialsystem/imaterial.h"
  9. #include "materialsystem/imaterialvar.h"
  10. #include "functionproxy.h"
  11. #include <keyvalues.h>
  12. #include "mathlib/vmatrix.h"
  13. #include "imaterialproxydict.h"
  14. // memdbgon must be the last include file in a .cpp file!!!
  15. #include "tier0/memdbgon.h"
  16. class C_FuncConveyor : public C_BaseEntity
  17. {
  18. public:
  19. DECLARE_CLASS( C_FuncConveyor, C_BaseEntity );
  20. DECLARE_CLIENTCLASS();
  21. C_FuncConveyor();
  22. float GetConveyorSpeed() { return m_flConveyorSpeed; }
  23. private:
  24. float m_flConveyorSpeed;
  25. };
  26. IMPLEMENT_CLIENTCLASS_DT( C_FuncConveyor, DT_FuncConveyor, CFuncConveyor )
  27. RecvPropFloat( RECVINFO( m_flConveyorSpeed ) ),
  28. END_RECV_TABLE()
  29. C_FuncConveyor::C_FuncConveyor()
  30. {
  31. m_flConveyorSpeed = 0.0;
  32. }
  33. class CConveyorMaterialProxy : public IMaterialProxy
  34. {
  35. public:
  36. CConveyorMaterialProxy();
  37. virtual ~CConveyorMaterialProxy();
  38. virtual bool Init( IMaterial *pMaterial, KeyValues *pKeyValues );
  39. virtual void OnBind( void *pC_BaseEntity );
  40. virtual void Release( void ) { delete this; }
  41. virtual IMaterial *GetMaterial();
  42. private:
  43. C_BaseEntity *BindArgToEntity( void *pArg );
  44. IMaterialVar *m_pTextureScrollVar;
  45. };
  46. CConveyorMaterialProxy::CConveyorMaterialProxy()
  47. {
  48. m_pTextureScrollVar = NULL;
  49. }
  50. CConveyorMaterialProxy::~CConveyorMaterialProxy()
  51. {
  52. }
  53. bool CConveyorMaterialProxy::Init( IMaterial *pMaterial, KeyValues *pKeyValues )
  54. {
  55. char const* pScrollVarName = pKeyValues->GetString( "textureScrollVar" );
  56. if( !pScrollVarName )
  57. return false;
  58. bool foundVar;
  59. m_pTextureScrollVar = pMaterial->FindVar( pScrollVarName, &foundVar, false );
  60. if( !foundVar )
  61. return false;
  62. return true;
  63. }
  64. C_BaseEntity *CConveyorMaterialProxy::BindArgToEntity( void *pArg )
  65. {
  66. IClientRenderable *pRend = (IClientRenderable *)pArg;
  67. return pRend->GetIClientUnknown()->GetBaseEntity();
  68. }
  69. void CConveyorMaterialProxy::OnBind( void *pC_BaseEntity )
  70. {
  71. if( !pC_BaseEntity )
  72. return;
  73. C_BaseEntity *pEntity = BindArgToEntity( pC_BaseEntity );
  74. if ( !pEntity )
  75. return;
  76. C_FuncConveyor *pConveyor = dynamic_cast<C_FuncConveyor*>(pEntity);
  77. if ( !pConveyor )
  78. return;
  79. if ( !m_pTextureScrollVar )
  80. {
  81. return;
  82. }
  83. float flConveyorSpeed = pConveyor->GetConveyorSpeed();
  84. float flRate = abs( flConveyorSpeed ) / 128.0;
  85. float flAngle = (flConveyorSpeed >= 0) ? 180 : 0;
  86. float sOffset = gpGlobals->curtime * cos( flAngle * ( M_PI / 180.0f ) ) * flRate;
  87. float tOffset = gpGlobals->curtime * sin( flAngle * ( M_PI / 180.0f ) ) * flRate;
  88. // make sure that we are positive
  89. if( sOffset < 0.0f )
  90. {
  91. sOffset += 1.0f + -( int )sOffset;
  92. }
  93. if( tOffset < 0.0f )
  94. {
  95. tOffset += 1.0f + -( int )tOffset;
  96. }
  97. // make sure that we are in a [0,1] range
  98. sOffset = sOffset - ( int )sOffset;
  99. tOffset = tOffset - ( int )tOffset;
  100. if (m_pTextureScrollVar->GetType() == MATERIAL_VAR_TYPE_MATRIX)
  101. {
  102. VMatrix mat;
  103. MatrixBuildTranslation( mat, sOffset, tOffset, 0.0f );
  104. m_pTextureScrollVar->SetMatrixValue( mat );
  105. }
  106. else
  107. {
  108. m_pTextureScrollVar->SetVecValue( sOffset, tOffset, 0.0f );
  109. }
  110. }
  111. IMaterial *CConveyorMaterialProxy::GetMaterial()
  112. {
  113. return m_pTextureScrollVar ? m_pTextureScrollVar->GetOwningMaterial() : NULL;
  114. }
  115. EXPOSE_MATERIAL_PROXY( CConveyorMaterialProxy, ConveyorScroll );