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.

118 lines
3.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. //
  4. //
  5. //=============================================================================
  6. #include "cbase.h"
  7. #include "NextBot.h"
  8. #include "merasmus.h"
  9. #include "merasmus_body.h"
  10. //-------------------------------------------------------------------------------------------
  11. CMerasmusBody::CMerasmusBody( INextBot *bot ) : IBody( bot )
  12. {
  13. m_moveXPoseParameter = -1;
  14. m_moveYPoseParameter = -1;
  15. m_currentActivity = -1;
  16. }
  17. //-------------------------------------------------------------------------------------------
  18. bool CMerasmusBody::StartActivity( Activity act, unsigned int flags )
  19. {
  20. CMerasmus *me = (CMerasmus *)GetBot()->GetEntity();
  21. int animSequence = ::SelectWeightedSequence( me->GetModelPtr(), act, me->GetSequence() );
  22. if ( animSequence )
  23. {
  24. m_currentActivity = act;
  25. me->SetSequence( animSequence );
  26. me->SetPlaybackRate( 1.0f );
  27. me->SetCycle( 0 );
  28. me->ResetSequenceInfo();
  29. return true;
  30. }
  31. return false;
  32. }
  33. //-------------------------------------------------------------------------------------------
  34. void CMerasmusBody::Update( void )
  35. {
  36. CMerasmus *me = (CMerasmus *)GetBot()->GetEntity();
  37. if ( m_moveXPoseParameter < 0 )
  38. {
  39. m_moveXPoseParameter = me->LookupPoseParameter( "move_x" );
  40. }
  41. if ( m_moveYPoseParameter < 0 )
  42. {
  43. m_moveYPoseParameter = me->LookupPoseParameter( "move_y" );
  44. }
  45. // Update the pose parameters
  46. float speed = me->GetLocomotionInterface()->GetGroundSpeed(); // me->GetAbsVelocity().Length();
  47. if ( speed < 0.01f )
  48. {
  49. // stopped
  50. if ( m_moveXPoseParameter >= 0 )
  51. {
  52. me->SetPoseParameter( m_moveXPoseParameter, 0.0f );
  53. }
  54. if ( m_moveYPoseParameter >= 0 )
  55. {
  56. me->SetPoseParameter( m_moveYPoseParameter, 0.0f );
  57. }
  58. }
  59. else
  60. {
  61. Vector forward, right, up;
  62. me->GetVectors( &forward, &right, &up );
  63. const Vector &motionVector = me->GetLocomotionInterface()->GetGroundMotionVector();
  64. // move_x == 1.0 at full forward motion and -1.0 in full reverse
  65. if ( m_moveXPoseParameter >= 0 )
  66. {
  67. float forwardVel = DotProduct( motionVector, forward );
  68. me->SetPoseParameter( m_moveXPoseParameter, forwardVel );
  69. }
  70. if ( m_moveYPoseParameter >= 0 )
  71. {
  72. float sideVel = DotProduct( motionVector, right );
  73. me->SetPoseParameter( m_moveYPoseParameter, sideVel );
  74. }
  75. }
  76. // adjust animation speed to actual movement speed
  77. if ( me->m_flGroundSpeed > 0.0f )
  78. {
  79. // Clamp playback rate to avoid datatable warnings. Anything faster would look silly, anyway.
  80. float playbackRate = clamp( speed / me->m_flGroundSpeed, -4.f, 12.f );
  81. me->SetPlaybackRate( playbackRate );
  82. }
  83. // move the animation ahead in time
  84. me->StudioFrameAdvance();
  85. me->DispatchAnimEvents( me );
  86. }
  87. //---------------------------------------------------------------------------------------------
  88. // return the bot's collision mask (hack until we get a general hull trace abstraction here or in the locomotion interface)
  89. unsigned int CMerasmusBody::GetSolidMask( void ) const
  90. {
  91. return MASK_NPCSOLID | CONTENTS_PLAYERCLIP;
  92. }