VActorStateTable.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #ifndef _VT_VACTORSTATETABLE_H_
  24. #define _VT_VACTORSTATETABLE_H_
  25. #ifndef _TVECTOR_H
  26. #include "core/util/tVector.h"
  27. #endif
  28. //-----------------------------------------------------------------------------
  29. class VActor;
  30. class VActorStateTable;
  31. //-----------------------------------------------------------------------------
  32. class VActorState
  33. {
  34. public:
  35. VActorState( void ) { };
  36. virtual ~VActorState( void ) { };
  37. virtual void enter( VActor *pObject ) = 0;
  38. virtual bool execute( VActor *pObject ) = 0;
  39. virtual void exit( VActor *pObject ) = 0;
  40. };
  41. //-----------------------------------------------------------------------------
  42. class VActorStateTable
  43. {
  44. public:
  45. struct sStateRef
  46. {
  47. VActorState *State;
  48. F32 Priority;
  49. };
  50. typedef Vector<sStateRef> tStateVector;
  51. typedef tStateVector::iterator tStateIterator;
  52. typedef tStateVector::const_iterator tStateConstIterator;
  53. protected:
  54. tStateVector mStateVector;
  55. VActor *mObject;
  56. VActorState *mLastState;
  57. VActorState *mCurrentState;
  58. public:
  59. VActorStateTable( void ) :
  60. mObject( NULL ),
  61. mLastState( NULL ),
  62. mCurrentState( NULL )
  63. {
  64. VECTOR_SET_ASSOCIATION( mStateVector );
  65. };
  66. virtual ~VActorStateTable( void )
  67. {
  68. // Clear Table.
  69. clear();
  70. };
  71. void registerState( VActorState *pState, const F32 &pPriority = 0.5f );
  72. virtual void enter( void ) { };
  73. virtual VActorState *execute( void );
  74. virtual void exit( void ) { };
  75. //-------------------------------------------------------------------------
  76. //
  77. // Gets
  78. //
  79. //-------------------------------------------------------------------------
  80. inline VActor *getObject( void ) { return mObject; };
  81. bool isRegisteredState( VActorState *pState );
  82. inline VActorState *getCurrentState( void ) { return mCurrentState; };
  83. inline VActorState *getLastState( void ) { return mLastState; };
  84. //-------------------------------------------------------------------------
  85. //
  86. // Sets
  87. //
  88. //-------------------------------------------------------------------------
  89. void clear( void );
  90. void sort( void );
  91. inline void setObject( VActor *pObject ) { mObject = pObject; };
  92. void setState( VActorState *pState );
  93. //-------------------------------------------------------------------------
  94. //
  95. // Accessors
  96. //
  97. //-------------------------------------------------------------------------
  98. tStateConstIterator begin( void ) const { return mStateVector.begin(); };
  99. tStateConstIterator end( void ) const { return mStateVector.end(); };
  100. S32 size( void ) const { return mStateVector.size(); };
  101. protected:
  102. static S32 QSORT_CALLBACK _onSortCallback( const VActorStateTable::sStateRef *pA, const VActorStateTable::sStateRef *pB );
  103. };
  104. #endif // _VT_VACTORSTATETABLE_H_