home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Diversos / beauty_resort.swf / scripts / classes / basic / StateMachine / TFStateMachine.as next >
Encoding:
Text File  |  2008-09-04  |  8.0 KB  |  257 lines

  1. package classes.basic.StateMachine
  2. {
  3.    public class TFStateMachine
  4.    {
  5.       
  6.       public static var SM_STATECHANGE:Number = 1;
  7.       
  8.       public static var SM_EVENTARRIVE:Number = 5;
  9.       
  10.       public static var SM_PROCESS:Number = 4;
  11.       
  12.       public static var SM_EVENT:Number = 2;
  13.       
  14.       public static var SM_ENDSTATE:Number = 3;
  15.       
  16.       public static var SM_INITIALIZE:Number = 0;
  17.        
  18.       
  19.       private var listState:Array;
  20.       
  21.       private var eventHandler:Function;
  22.       
  23.       private var currentState:TFStateMachineState;
  24.       
  25.       private var listTransition:Array;
  26.       
  27.       private var listEvent:Array;
  28.       
  29.       private var bReady:Boolean;
  30.       
  31.       public function TFStateMachine(param1:Function)
  32.       {
  33.          super();
  34.          this.bReady = false;
  35.          this.listState = new Array();
  36.          this.listEvent = new Array();
  37.          this.listTransition = new Array();
  38.          this.eventHandler = param1;
  39.          this.currentState = null;
  40.       }
  41.       
  42.       private function clearList(param1:Array) : *
  43.       {
  44.          var _loc2_:Object = null;
  45.          var _loc3_:* = undefined;
  46.          for(_loc3_ in param1)
  47.          {
  48.             _loc2_ = param1.pop();
  49.             _loc2_.destroy();
  50.          }
  51.       }
  52.       
  53.       public function getCurrentState() : TFStateMachineState
  54.       {
  55.          return currentState;
  56.       }
  57.       
  58.       private function init(param1:TFStateMachineState) : Boolean
  59.       {
  60.          if(this.currentState != null)
  61.          {
  62.             if(this.currentState.getCallBackStop() != null)
  63.             {
  64.                this.currentState.getCallBackStop().call(param1.getParam());
  65.             }
  66.          }
  67.          this.currentState = param1;
  68.          if(this.eventHandler != null)
  69.          {
  70.             this.eventHandler(SM_STATECHANGE,this.currentState);
  71.             if(this.currentState.isEndState())
  72.             {
  73.                this.eventHandler(SM_ENDSTATE,this.currentState);
  74.             }
  75.          }
  76.          if(this.currentState.getCallBackStart() != null)
  77.          {
  78.             this.currentState.getCallBackStart().call(this.currentState.getParam());
  79.          }
  80.          return true;
  81.       }
  82.       
  83.       private function searchTransition(param1:String, param2:String) : TFStateMachineTransition
  84.       {
  85.          var _loc3_:Array = null;
  86.          var _loc4_:String = null;
  87.          var _loc5_:* = undefined;
  88.          _loc3_ = this.listTransition[param1 + "-" + param2];
  89.          if(_loc3_ == null)
  90.          {
  91.             throw new Error("Transition state: " + param1 + " event: " + param2 + " does not exist!");
  92.          }
  93.          var _loc6_:int = 0;
  94.          var _loc7_:* = _loc3_;
  95.          for(_loc5_ in _loc7_)
  96.          {
  97.             _loc4_ = _loc5_;
  98.          }
  99.          return _loc3_[_loc4_];
  100.       }
  101.       
  102.       public function initialize(param1:String) : Boolean
  103.       {
  104.          var _loc2_:TFStateMachineState = null;
  105.          if(this.listTransition.length == 0)
  106.          {
  107.             throw new Error("It does not have any transitions registered!");
  108.          }
  109.          if(!this.existState(param1))
  110.          {
  111.             throw new Error("State " + param1 + " does not exist!");
  112.          }
  113.          _loc2_ = this.listState[param1];
  114.          if(this.eventHandler != null)
  115.          {
  116.             this.eventHandler(SM_INITIALIZE,_loc2_);
  117.          }
  118.          this.init(_loc2_);
  119.          this.bReady = true;
  120.          return true;
  121.       }
  122.       
  123.       private function existEvent(param1:String) : Boolean
  124.       {
  125.          return listEvent[param1] != null;
  126.       }
  127.       
  128.       private function existState(param1:String) : Boolean
  129.       {
  130.          return listState[param1] != null;
  131.       }
  132.       
  133.       private function existTransition(param1:String, param2:String, param3:String) : *
  134.       {
  135.          var _loc4_:Array = null;
  136.          if((_loc4_ = this.listTransition[param1 + "-" + param2]) == null)
  137.          {
  138.             return false;
  139.          }
  140.          return _loc4_[param3] != null;
  141.       }
  142.       
  143.       public function process(param1:Object) : *
  144.       {
  145.          if(!bReady)
  146.          {
  147.             throw new Error("State Machine not initialized!");
  148.          }
  149.          if(this.eventHandler != null)
  150.          {
  151.             this.eventHandler(SM_PROCESS,this.currentState);
  152.          }
  153.          if(this.currentState.getCallBackRun() != null)
  154.          {
  155.             (this.currentState.getCallBackRun() as Function).call(this.currentState.getParam(),param1);
  156.          }
  157.       }
  158.       
  159.       public function createTransition(param1:String, param2:String, param3:String) : *
  160.       {
  161.          var _loc4_:TFStateMachineState = null;
  162.          var _loc5_:TFStateMachineEvent = null;
  163.          var _loc6_:TFStateMachineState = null;
  164.          var _loc7_:TFStateMachineTransition = null;
  165.          var _loc8_:Array = null;
  166.          var _loc9_:TFStateMachineTransition = null;
  167.          if(!this.existState(param1))
  168.          {
  169.             throw new Error("State " + param1 + " does not exist!");
  170.          }
  171.          if(!this.existState(param3))
  172.          {
  173.             throw new Error("State " + param3 + " does not exist!");
  174.          }
  175.          if(!existEvent(param2))
  176.          {
  177.             throw new Error("Event " + param2 + " does not exist!");
  178.          }
  179.          if(existTransition(param1,param2,param3))
  180.          {
  181.             throw new Error("Transition " + param1 + "-" + param2 + "-" + param3 + " already exists!");
  182.          }
  183.          _loc4_ = this.listState[param1];
  184.          _loc5_ = this.listEvent[param2];
  185.          _loc6_ = this.listState[param3];
  186.          _loc9_ = new TFStateMachineTransition(_loc4_,_loc5_,_loc6_);
  187.          if((_loc8_ = this.listTransition[param1 + "-" + param2]) == null)
  188.          {
  189.             _loc8_ = new Array();
  190.             this.listTransition[param1 + "-" + param2] = _loc8_;
  191.             ++this.listTransition.length;
  192.          }
  193.          _loc8_[param3] = _loc9_;
  194.          ++_loc8_.length;
  195.       }
  196.       
  197.       public function createState(param1:String, param2:Boolean, param3:Object, param4:Function, param5:Function, param6:Function) : *
  198.       {
  199.          var _loc7_:TFStateMachineState = null;
  200.          if(this.existState(param1))
  201.          {
  202.             throw new Error("State " + param1 + " already exists!");
  203.          }
  204.          _loc7_ = new TFStateMachineState(param1,param2,param3,param4,param5,param6);
  205.          this.listState[param1] = _loc7_;
  206.          ++this.listState.length;
  207.       }
  208.       
  209.       public function receiveEvent(param1:String) : Boolean
  210.       {
  211.          var _loc2_:TFStateMachineTransition = null;
  212.          if(!bReady)
  213.          {
  214.             throw new Error("State Machine not initialized!");
  215.          }
  216.          if(this.eventHandler != null)
  217.          {
  218.             this.eventHandler(SM_EVENT,this.listEvent[param1]);
  219.          }
  220.          if(this.currentState.isEndState())
  221.          {
  222.             return false;
  223.          }
  224.          if(!existEvent(param1))
  225.          {
  226.             throw new Error("Event " + param1 + " does not exist!");
  227.          }
  228.          if(this.eventHandler != null)
  229.          {
  230.             this.eventHandler(SM_EVENTARRIVE,this.listEvent[param1]);
  231.          }
  232.          _loc2_ = this.searchTransition(this.currentState.getName(),param1);
  233.          return this.init(_loc2_.getNewState());
  234.       }
  235.       
  236.       public function destroy() : *
  237.       {
  238.          this.clearList(this.listState);
  239.          this.clearList(this.listEvent);
  240.          this.clearList(this.listTransition);
  241.          delete global[this];
  242.       }
  243.       
  244.       public function createEvent(param1:String, param2:Object) : *
  245.       {
  246.          var _loc3_:TFStateMachineEvent = null;
  247.          if(existEvent(param1))
  248.          {
  249.             throw new Error("Event " + param1 + " already exists!");
  250.          }
  251.          _loc3_ = new TFStateMachineEvent(param1,param2);
  252.          this.listEvent[param1] = _loc3_;
  253.          ++this.listEvent.length;
  254.       }
  255.    }
  256. }
  257.