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.

121 lines
3.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: NPC Puppet
  4. //
  5. //=============================================================================
  6. #include "cbase.h"
  7. #include "ai_basenpc.h"
  8. // Must be the last file included
  9. #include "memdbgon.h"
  10. class CNPC_Puppet : public CAI_BaseNPC
  11. {
  12. DECLARE_CLASS( CNPC_Puppet, CAI_BaseNPC );
  13. public:
  14. virtual void Spawn( void );
  15. virtual void Precache( void );
  16. void InputSetAnimationTarget( inputdata_t &inputdata );
  17. private:
  18. string_t m_sAnimTargetname;
  19. string_t m_sAnimAttachmentName;
  20. CNetworkVar( EHANDLE, m_hAnimationTarget ); // NPC that will drive what animation we're playing
  21. CNetworkVar( int, m_nTargetAttachment ); // Attachment point to match to on the target
  22. DECLARE_DATADESC();
  23. DECLARE_SERVERCLASS();
  24. };
  25. LINK_ENTITY_TO_CLASS( npc_puppet, CNPC_Puppet );
  26. BEGIN_DATADESC( CNPC_Puppet )
  27. DEFINE_KEYFIELD( m_sAnimTargetname, FIELD_STRING, "animationtarget" ),
  28. DEFINE_KEYFIELD( m_sAnimAttachmentName, FIELD_STRING, "attachmentname" ),
  29. DEFINE_FIELD( m_nTargetAttachment, FIELD_INTEGER ),
  30. DEFINE_FIELD( m_hAnimationTarget, FIELD_EHANDLE ),
  31. DEFINE_INPUTFUNC( FIELD_STRING, "SetAnimationTarget", InputSetAnimationTarget ),
  32. END_DATADESC()
  33. IMPLEMENT_SERVERCLASS_ST( CNPC_Puppet, DT_NPC_Puppet )
  34. SendPropEHandle( SENDINFO( m_hAnimationTarget ) ),
  35. SendPropInt( SENDINFO( m_nTargetAttachment) ),
  36. END_SEND_TABLE()
  37. //-----------------------------------------------------------------------------
  38. // Purpose:
  39. //-----------------------------------------------------------------------------
  40. void CNPC_Puppet::Precache( void )
  41. {
  42. BaseClass::Precache();
  43. PrecacheModel( STRING( GetModelName() ) );
  44. }
  45. //-----------------------------------------------------------------------------
  46. // Purpose:
  47. //-----------------------------------------------------------------------------
  48. void CNPC_Puppet::Spawn( void )
  49. {
  50. BaseClass::Spawn();
  51. Precache();
  52. SetModel( STRING( GetModelName() ) );
  53. NPCInit();
  54. SetHealth( 100 );
  55. // Find our animation target
  56. CBaseEntity *pTarget = gEntList.FindEntityByName( NULL, m_sAnimTargetname );
  57. m_hAnimationTarget = pTarget;
  58. if ( pTarget )
  59. {
  60. CBaseAnimating *pAnimating = pTarget->GetBaseAnimating();
  61. if ( pAnimating )
  62. {
  63. m_nTargetAttachment = pAnimating->LookupAttachment( STRING( m_sAnimAttachmentName ) );
  64. }
  65. }
  66. // Always be scripted
  67. SetInAScript( true );
  68. }
  69. //-----------------------------------------------------------------------------
  70. // Purpose:
  71. // Input : &inputdata -
  72. //-----------------------------------------------------------------------------
  73. void CNPC_Puppet::InputSetAnimationTarget( inputdata_t &inputdata )
  74. {
  75. // Take the new name
  76. m_sAnimTargetname = MAKE_STRING( inputdata.value.String() );
  77. // Find our animation target
  78. CBaseEntity *pTarget = gEntList.FindEntityByName( NULL, m_sAnimTargetname );
  79. if ( pTarget == NULL )
  80. {
  81. Warning("Failed to find animation target %s for npc_puppet (%s)\n", STRING( m_sAnimTargetname ), STRING( GetEntityName() ) );
  82. return;
  83. }
  84. m_hAnimationTarget = pTarget;
  85. CBaseAnimating *pAnimating = pTarget->GetBaseAnimating();
  86. if ( pAnimating )
  87. {
  88. // Cache off our target attachment
  89. m_nTargetAttachment = pAnimating->LookupAttachment( STRING( m_sAnimAttachmentName ) );
  90. }
  91. // Stuff us at the owner's core for visibility reasons
  92. SetParent( pTarget );
  93. SetLocalOrigin( vec3_origin );
  94. SetLocalAngles( vec3_angle );
  95. }