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.

170 lines
4.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "tf_passtime_ball.h"
  9. #include "passtime_ballcontroller.h"
  10. // memdbgon must be the last include file in a .cpp file!!!
  11. #include "tier0/memdbgon.h"
  12. //-----------------------------------------------------------------------------
  13. namespace
  14. {
  15. int SortDescendingPriority( CPasstimeBallController *const *ppA, CPasstimeBallController *const *ppB )
  16. {
  17. return static_cast< CPasstimeBallController* >( *ppB )->GetPriority()
  18. - static_cast< CPasstimeBallController* >( *ppA )->GetPriority();
  19. }
  20. }
  21. //-----------------------------------------------------------------------------
  22. // static
  23. int CPasstimeBallController::ApplyTo( CPasstimeBall *pBall )
  24. {
  25. // Sort controllers by iPriority, then call Apply until one returns true.
  26. // From then on, skip controllers with a lower iPriority.
  27. // This sorts the autolist, but that should be OK.
  28. auto &controllers = GetAutoList();
  29. controllers.Sort( &SortDescendingPriority );
  30. int iMinPriority = INT_MIN;
  31. int iNumControllers = 0;
  32. for ( auto controller : controllers )
  33. {
  34. if ( !controller->IsEnabled() || !controller->IsActive() )
  35. {
  36. continue;
  37. }
  38. if ( controller->GetPriority() < iMinPriority )
  39. {
  40. break;
  41. }
  42. if ( controller->Apply( pBall ) )
  43. {
  44. iMinPriority = controller->GetPriority();
  45. ++iNumControllers;
  46. }
  47. }
  48. return iNumControllers;
  49. }
  50. //-----------------------------------------------------------------------------
  51. // static
  52. int CPasstimeBallController::DisableOn( const CPasstimeBall *pBall )
  53. {
  54. auto &controllers = GetAutoList();
  55. int iCount = 0;
  56. for ( auto pController : controllers )
  57. {
  58. if ( pController->IsEnabled() )
  59. {
  60. pController->SetIsEnabled( false );
  61. ++iCount;
  62. }
  63. }
  64. return iCount;
  65. }
  66. //-----------------------------------------------------------------------------
  67. // static
  68. void CPasstimeBallController::BallCollision( CPasstimeBall *pBall,
  69. int iCollisionIndex, gamevcollisionevent_t *pEvent )
  70. {
  71. auto &controllers = GetAutoList();
  72. for ( auto pController : controllers )
  73. {
  74. if ( pController->IsEnabled() && pController->IsActive() )
  75. {
  76. pController->OnBallCollision( pBall, iCollisionIndex, pEvent );
  77. }
  78. }
  79. }
  80. //-----------------------------------------------------------------------------
  81. // static
  82. void CPasstimeBallController::BallPickedUp( CPasstimeBall *pBall,
  83. CTFPlayer *pCatcher )
  84. {
  85. auto &controllers = GetAutoList();
  86. for ( auto pController : controllers )
  87. {
  88. if ( pController->IsEnabled() && pController->IsActive() )
  89. {
  90. pController->OnBallPickedUp( pBall, pCatcher );
  91. }
  92. }
  93. }
  94. //-----------------------------------------------------------------------------
  95. // static
  96. void CPasstimeBallController::BallDamaged( CPasstimeBall *pBall )
  97. {
  98. auto &controllers = GetAutoList();
  99. for ( auto pController : controllers )
  100. {
  101. if ( pController->IsEnabled() && pController->IsActive() )
  102. {
  103. pController->OnBallDamaged( pBall );
  104. }
  105. }
  106. }
  107. //-----------------------------------------------------------------------------
  108. // static
  109. void CPasstimeBallController::BallSpawned( CPasstimeBall *pBall )
  110. {
  111. auto &controllers = GetAutoList();
  112. for ( auto pController : controllers )
  113. {
  114. if ( pController->IsEnabled() && pController->IsActive() )
  115. {
  116. pController->OnBallSpawned( pBall );
  117. }
  118. }
  119. }
  120. //-----------------------------------------------------------------------------
  121. CPasstimeBallController::CPasstimeBallController( int iPriority )
  122. : m_bEnabled( false )
  123. , m_iPriority( iPriority ) {}
  124. //-----------------------------------------------------------------------------
  125. void CPasstimeBallController::SetIsEnabled( bool bEnabled )
  126. {
  127. if ( m_bEnabled == bEnabled )
  128. {
  129. return;
  130. }
  131. m_bEnabled = bEnabled;
  132. if ( bEnabled )
  133. {
  134. OnEnabled();
  135. }
  136. else
  137. {
  138. OnDisabled();
  139. }
  140. }
  141. //-----------------------------------------------------------------------------
  142. bool CPasstimeBallController::IsEnabled() const
  143. {
  144. return m_bEnabled;
  145. }
  146. //-----------------------------------------------------------------------------
  147. int CPasstimeBallController::GetPriority() const
  148. {
  149. return m_iPriority;
  150. }