VActorStateTable.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //-----------------------------------------------------------------------------
  2. // Verve
  3. // Copyright (C) 2014 - Violent Tulip
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //-----------------------------------------------------------------------------
  23. #include "VActorStateTable.h"
  24. #include "VActor.h"
  25. //-----------------------------------------------------------------------------
  26. bool VActorStateTable::isRegisteredState( VActorState *pState )
  27. {
  28. for ( tStateConstIterator itr = mStateVector.begin(); itr != mStateVector.end(); itr++ )
  29. {
  30. // Target State?
  31. if ( ( *itr ).State == pState )
  32. {
  33. // Yes.
  34. return true;
  35. }
  36. }
  37. // No.
  38. return false;
  39. }
  40. void VActorStateTable::clear( void )
  41. {
  42. // Clear the States.
  43. mLastState = NULL;
  44. mCurrentState = NULL;
  45. // Clear the State Vector.
  46. mStateVector.clear();
  47. };
  48. void VActorStateTable::sort( void )
  49. {
  50. mStateVector.sort( &_onSortCallback );
  51. }
  52. void VActorStateTable::registerState( VActorState *pState, const F32 &pPriority )
  53. {
  54. // Already a State?
  55. if ( isRegisteredState( pState ) )
  56. {
  57. // Exit Now.
  58. return;
  59. }
  60. // Create the Reference.
  61. sStateRef entry;
  62. entry.State = pState;
  63. entry.Priority = pPriority;
  64. // Push to Back.
  65. mStateVector.push_back( entry );
  66. // Set Current?
  67. if ( mStateVector.size() == 1 )
  68. {
  69. // Set State.
  70. setState( pState );
  71. }
  72. };
  73. void VActorStateTable::setState( VActorState *pState )
  74. {
  75. if ( !mObject || !pState || pState == mCurrentState )
  76. {
  77. // Invalid.
  78. return;
  79. }
  80. if ( mCurrentState )
  81. {
  82. // Exit.
  83. exit();
  84. // Exit the Old State.
  85. mCurrentState->exit( mObject );
  86. }
  87. // Update States.
  88. mLastState = mCurrentState;
  89. mCurrentState = pState;
  90. // Enter.
  91. enter();
  92. // Enter the New State.
  93. pState->enter( mObject );
  94. };
  95. VActorState *VActorStateTable::execute( void )
  96. {
  97. if ( !mObject || !mCurrentState )
  98. {
  99. // Invalid.
  100. return NULL;
  101. }
  102. for ( tStateConstIterator itr = mStateVector.begin(); itr != mStateVector.end(); itr++ )
  103. {
  104. // Fetch State Reference.
  105. const sStateRef &stateRef = ( *itr );
  106. // Enter State?
  107. if ( stateRef.State->execute( mObject ) )
  108. {
  109. // Set the State.
  110. setState( stateRef.State );
  111. // Return.
  112. return stateRef.State;
  113. }
  114. }
  115. // No Valid Entries, Ouch!
  116. Con::warnf( "VActorStateTable::execute() - No Valid Entries." );
  117. // Return Current State.
  118. return mCurrentState;
  119. }
  120. S32 QSORT_CALLBACK VActorStateTable::_onSortCallback( const VActorStateTable::sStateRef *pA, const VActorStateTable::sStateRef *pB )
  121. {
  122. if ( pB->Priority > pA->Priority )
  123. {
  124. return 1;
  125. }
  126. else if ( pB->Priority < pA->Priority )
  127. {
  128. return -1;
  129. }
  130. return 0;
  131. }