home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 06 General Architectures / 05 Rabin / robot.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-12-10  |  1.7 KB  |  73 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. #include "robot.h"
  12. #include "stdlib.h"
  13.  
  14.  
  15. //Add new states here
  16. enum States { STATE_Initialize,
  17.               STATE_Wander,
  18.               STATE_Dead
  19. };
  20.  
  21.  
  22. //Note: The macro keywords can be highlighted by placing them in the file 
  23. //USERTYPE.DAT in the same directory as MSDEV.EXE
  24.  
  25. bool Robot::States( StateMachineEvent event, int state )
  26. {
  27. BeginStateMachine
  28.  
  29.     /////////////////////////////////////////////////////////////////
  30.     State( STATE_Initialize )
  31.         OnEnter
  32.             //Put any C++ initialization code here
  33.             m_timer = 0;
  34.  
  35.         OnUpdate
  36.             //Put any C++ code here that gets run on every game tick
  37.             m_timer++;
  38.             if( m_timer > 10 ) {
  39.                 SetState( STATE_Wander );
  40.             }
  41.             
  42.         OnExit
  43.             //Put any C++ cleanup code here
  44.             m_timer = 0;
  45.  
  46.     /////////////////////////////////////////////////////////////////
  47.     State( STATE_Wander )
  48.         OnEnter
  49.             //Put any C++ initialization code here
  50.             m_timer = 0;
  51.  
  52.         OnUpdate
  53.             //Put any C++ code here that gets run on every game tick
  54.             m_timer++;
  55.             if( m_timer > 10 ) {
  56.                 SetState( STATE_Initialize );
  57.             }
  58.             else if( rand()%1000 == 0 ) {
  59.                 SetState( STATE_Dead );
  60.             }
  61.             
  62.         OnExit
  63.             //Put any C++ cleanup code here
  64.             m_timer = 0;
  65.  
  66.     /////////////////////////////////////////////////////////////////
  67.     State( STATE_Dead )
  68.         OnEnter
  69.             //Just die on enter...
  70.  
  71. EndStateMachine
  72. }
  73.