home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 06 General Architectures / 05 Rabin / statemch.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-12-10  |  1.5 KB  |  55 lines

  1. /* Copyright (C) Steve Rabin, 2001. 
  2.  * All rights reserved worldwide.
  3.  *
  4.  * This software is provided "as is" without express or implied
  5.  * warranties. You may freely copy and compile this source into
  6.  * applications you distribute provided that the copyright text
  7.  * below is included in the resulting source code, for example:
  8.  * "Portions Copyright (C) Steve Rabin, 2001"
  9.  */
  10.  
  11. #ifndef __STATEMCH_H__
  12. #define __STATEMCH_H__
  13.  
  14. #include <assert.h>
  15.  
  16. enum StateMachineEvent { EVENT_Update,
  17.                          EVENT_Enter,
  18.                          EVENT_Exit
  19. };
  20.  
  21.  
  22. //State Machine Language Macros (put these keywords in the file USERTYPE.DAT in the same directory as MSDEV.EXE)
  23. #define BeginStateMachine        if( state < 0 ) { if(0) {
  24. #define EndStateMachine            return( true ); } } else { assert(0); return( false ); }  return( false );
  25. #define State(a)                return( true ); } } else if( a == state ) { if(0) {
  26. #define OnEvent(a)                return( true ); } else if( a == event ) {
  27. #define OnEnter                    OnEvent( EVENT_Enter )
  28. #define OnUpdate                OnEvent( EVENT_Update )
  29. #define OnExit                    OnEvent( EVENT_Exit )
  30.  
  31.  
  32. class StateMachine
  33. {
  34. public:
  35.  
  36.     StateMachine( void );
  37.     ~StateMachine( void ) {}
  38.  
  39.     void Initialize( void );
  40.     void Update( void );
  41.     void SetState( unsigned int newState );
  42.  
  43. private:
  44.  
  45.     unsigned int m_currentState;
  46.     unsigned int m_nextState;
  47.     bool m_stateChange;
  48.  
  49.     void Process( StateMachineEvent event );
  50.     virtual bool States( StateMachineEvent event, int state ) = 0;
  51.  
  52. };
  53.  
  54.  
  55. #endif    // __STATEMCH_H__