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.

93 lines
2.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "cbase.h"
  7. #include "fx_interpvalue.h"
  8. CInterpolatedValue::CInterpolatedValue( void ) : m_flStartTime( 0.0f ), m_flEndTime( 0.0f ), m_flStartValue( 0.0f ), m_flEndValue( 0.0f ), m_nInterpType( INTERP_LINEAR )
  9. {
  10. }
  11. CInterpolatedValue::CInterpolatedValue( float startTime, float endTime, float startValue, float endValue, InterpType_t type ) :
  12. m_flStartTime( startTime ), m_flEndTime( endTime ), m_flStartValue( startValue ), m_flEndValue( endValue ), m_nInterpType( type )
  13. {
  14. }
  15. void CInterpolatedValue::SetTime( float start, float end )
  16. {
  17. m_flStartTime = start; m_flEndTime = end;
  18. }
  19. void CInterpolatedValue::SetRange( float start, float end )
  20. {
  21. m_flStartValue = start; m_flEndValue = end;
  22. }
  23. void CInterpolatedValue::SetType( InterpType_t type )
  24. {
  25. m_nInterpType = type;
  26. }
  27. // Set the value with no range
  28. void CInterpolatedValue::SetAbsolute( float value )
  29. {
  30. m_flStartValue = m_flEndValue = value;
  31. m_flStartTime = m_flEndTime = gpGlobals->curtime;
  32. m_nInterpType = INTERP_LINEAR;
  33. }
  34. // Set the value with range and time supplied
  35. void CInterpolatedValue::Init( float startValue, float endValue, float dt, InterpType_t type /*= INTERP_LINEAR*/ )
  36. {
  37. if ( dt <= 0.0f )
  38. {
  39. SetAbsolute( endValue );
  40. return;
  41. }
  42. SetTime( gpGlobals->curtime, gpGlobals->curtime + dt );
  43. SetRange( startValue, endValue );
  44. SetType( type );
  45. }
  46. // Start from the current value and move towards the end value
  47. void CInterpolatedValue::InitFromCurrent( float endValue, float dt, InterpType_t type /*= INTERP_LINEAR*/ )
  48. {
  49. Init( Interp( gpGlobals->curtime ), endValue, dt, type );
  50. }
  51. // Find our interpolated value at the given point in time
  52. float CInterpolatedValue::Interp( float curTime )
  53. {
  54. switch( m_nInterpType )
  55. {
  56. case INTERP_LINEAR:
  57. {
  58. if ( curTime >= m_flEndTime )
  59. return m_flEndValue;
  60. if ( curTime <= m_flStartTime )
  61. return m_flStartValue;
  62. return RemapVal( curTime, m_flStartTime, m_flEndTime, m_flStartValue, m_flEndValue );
  63. }
  64. case INTERP_SPLINE:
  65. {
  66. if ( curTime >= m_flEndTime )
  67. return m_flEndValue;
  68. if ( curTime <= m_flStartTime )
  69. return m_flStartValue;
  70. return SimpleSplineRemapVal( curTime, m_flStartTime, m_flEndTime, m_flStartValue, m_flEndValue );
  71. }
  72. }
  73. // NOTENOTE: You managed to pass in a bogus interpolation type!
  74. Assert(0);
  75. return -1.0f;
  76. }