Ember

Simple State Machine

Jul 1st, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Simple State Machine
  2. switch(State)
  3. {
  4.     case Idle:
  5.         //Idle in place
  6.         if(Wait) {Wait -= 1;} else
  7.         {
  8.             //Move to a random location
  9.             TargetX = irandom(room_width);
  10.             TargetY = irandom(room_height);
  11.             State = Moving;
  12.         }
  13.     break;
  14.    
  15.     case Moving:
  16.         //We're vertical with target location
  17.         if(TargetX = x)
  18.         {
  19.             //We're horizontal with target location
  20.             if(TargetY = y)
  21.             {
  22.                 //Stand in place for a few seconds
  23.                 Wait = 120 + irandom(120);
  24.                 State = Idle;
  25.             }
  26.             else
  27.             {
  28.                 //Move horizontal towards target
  29.                 y += median(-Speed, (TargetY - y), Speed);
  30.             }
  31.         }
  32.         else
  33.         {
  34.             //Move vertical towards target
  35.             x += median(-Speed, (TargetX - x), Speed);
  36.             //--//
  37.             if(TargetY != y)
  38.             {
  39.                 //Move diagonal towards target
  40.                 y += median(-Speed, (TargetY - y), Speed);
  41.             }
  42.         }
  43.     break;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment