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.

183 lines
4.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. // memdbgon must be the last include file in a .cpp file!!!
  9. #include "tier0/memdbgon.h"
  10. #define SF_NAV_START_ON 0x0001
  11. enum navproperties_t
  12. {
  13. NAV_IGNORE = 1<<0,
  14. };
  15. //-----------------------------------------------------------------------------
  16. // Purpose:
  17. //-----------------------------------------------------------------------------
  18. class CLogicNavigation : public CLogicalEntity,
  19. public IEntityListener
  20. {
  21. DECLARE_CLASS( CLogicNavigation, CLogicalEntity );
  22. bool KeyValue( const char *szKeyName, const char *szValue );
  23. void Activate( void );
  24. private:
  25. void UpdateOnRemove();
  26. void OnEntitySpawned( CBaseEntity *pEntity );
  27. // Inputs
  28. void InputTurnOn( inputdata_t &inputdata ) { TurnOn(); }
  29. void InputTurnOff( inputdata_t &inputdata ) { TurnOff(); }
  30. void InputToggle( inputdata_t &inputdata )
  31. {
  32. if ( m_isOn )
  33. TurnOff();
  34. else
  35. TurnOn();
  36. }
  37. void TurnOn();
  38. void TurnOff();
  39. void UpdateProperty();
  40. DECLARE_DATADESC();
  41. bool m_isOn;
  42. navproperties_t m_navProperty;
  43. };
  44. LINK_ENTITY_TO_CLASS(logic_navigation, CLogicNavigation);
  45. BEGIN_DATADESC( CLogicNavigation )
  46. DEFINE_FIELD( m_isOn, FIELD_BOOLEAN ),
  47. DEFINE_FIELD( m_navProperty, FIELD_INTEGER ),
  48. // Inputs
  49. DEFINE_INPUTFUNC(FIELD_VOID, "TurnOn", InputTurnOn),
  50. DEFINE_INPUTFUNC(FIELD_VOID, "TurnOff", InputTurnOff),
  51. DEFINE_INPUTFUNC(FIELD_VOID, "Toggle", InputToggle),
  52. END_DATADESC()
  53. //-----------------------------------------------------------------------------
  54. // Purpose:
  55. // Input : *szKeyName -
  56. // *szValue -
  57. // Output : Returns true on success, false on failure.
  58. //-----------------------------------------------------------------------------
  59. bool CLogicNavigation::KeyValue( const char *szKeyName, const char *szValue )
  60. {
  61. if ( FStrEq(szKeyName, "navprop") )
  62. {
  63. if ( FStrEq( szValue, "Ignore" ) )
  64. {
  65. m_navProperty = NAV_IGNORE;
  66. }
  67. else
  68. {
  69. DevMsg( 1, "Unknown nav property %s\n", szValue );
  70. }
  71. return true;
  72. }
  73. return BaseClass::KeyValue( szKeyName, szValue );
  74. }
  75. //-----------------------------------------------------------------------------
  76. // Purpose:
  77. //-----------------------------------------------------------------------------
  78. void CLogicNavigation::Activate()
  79. {
  80. BaseClass::Activate();
  81. if ( HasSpawnFlags( SF_NAV_START_ON ) )
  82. {
  83. TurnOn();
  84. RemoveSpawnFlags( SF_NAV_START_ON );
  85. }
  86. else if ( m_isOn )
  87. {
  88. gEntList.AddListenerEntity( this );
  89. }
  90. }
  91. //-----------------------------------------------------------------------------
  92. // Purpose:
  93. //-----------------------------------------------------------------------------
  94. void CLogicNavigation::UpdateOnRemove()
  95. {
  96. if ( m_isOn )
  97. {
  98. gEntList.RemoveListenerEntity( this );
  99. }
  100. BaseClass::UpdateOnRemove();
  101. }
  102. //-----------------------------------------------------------------------------
  103. // Purpose:
  104. //-----------------------------------------------------------------------------
  105. void CLogicNavigation::OnEntitySpawned( CBaseEntity *pEntity )
  106. {
  107. if ( m_isOn && ( m_navProperty & NAV_IGNORE ) && pEntity->NameMatches( m_target ) )
  108. {
  109. pEntity->SetNavIgnore();
  110. }
  111. }
  112. //-----------------------------------------------------------------------------
  113. // Purpose:
  114. //-----------------------------------------------------------------------------
  115. void CLogicNavigation::TurnOn()
  116. {
  117. if ( m_isOn )
  118. return;
  119. m_isOn = true;
  120. gEntList.AddListenerEntity( this );
  121. UpdateProperty();
  122. }
  123. void CLogicNavigation::UpdateProperty()
  124. {
  125. CBaseEntity *pEntity = NULL;
  126. while ( ( pEntity = gEntList.FindEntityByName( pEntity, STRING(m_target) ) ) != NULL )
  127. {
  128. if ( m_isOn )
  129. {
  130. if ( m_navProperty & NAV_IGNORE )
  131. pEntity->SetNavIgnore();
  132. }
  133. else
  134. {
  135. if ( m_navProperty & NAV_IGNORE )
  136. pEntity->ClearNavIgnore();
  137. }
  138. }
  139. }
  140. //-----------------------------------------------------------------------------
  141. // Purpose:
  142. //-----------------------------------------------------------------------------
  143. void CLogicNavigation::TurnOff()
  144. {
  145. if ( !m_isOn )
  146. return;
  147. m_isOn = false;
  148. gEntList.RemoveListenerEntity( this );
  149. UpdateProperty();
  150. }