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

  1. package classes.basic.Path
  2. {
  3.    import classes.basic.Algorithm.TFDijkstra;
  4.    import flash.display.MovieClip;
  5.    import flash.geom.Point;
  6.    
  7.    public class TFPath
  8.    {
  9.        
  10.       
  11.       private var arNodes:Array;
  12.       
  13.       private var arCurrPath:Array;
  14.       
  15.       private var iBaseSpeed:Number;
  16.       
  17.       private var iCurrentSpeed:Number;
  18.       
  19.       private var weightMatrix:Array;
  20.       
  21.       private var iTimePos:Number;
  22.       
  23.       private var bConstSpeed:Boolean;
  24.       
  25.       private var bCalcSpeed:Boolean;
  26.       
  27.       private var mcObject:MovieClip;
  28.       
  29.       private var nodeEnd:TFPathNode;
  30.       
  31.       public function TFPath(param1:Number, param2:Boolean, param3:MovieClip)
  32.       {
  33.          super();
  34.          arNodes = new Array();
  35.          iBaseSpeed = param1;
  36.          iCurrentSpeed = param1;
  37.          bConstSpeed = param2;
  38.          bCalcSpeed = true;
  39.          mcObject = param3;
  40.          arCurrPath = null;
  41.          nodeEnd = null;
  42.          iTimePos = 0;
  43.       }
  44.       
  45.       public static function nodeDistance(param1:TFPathNode, param2:TFPathNode) : Number
  46.       {
  47.          return distance(param1.getPoint(),param2.getPoint());
  48.       }
  49.       
  50.       private static function distance(param1:Point, param2:Point) : Number
  51.       {
  52.          var _loc3_:Point = null;
  53.          _loc3_ = new Point(param2.x - param1.x,param2.y - param1.y);
  54.          return Math.sqrt(_loc3_.x * _loc3_.x + _loc3_.y * _loc3_.y);
  55.       }
  56.       
  57.       public static function getPathUsingAlgorithm(param1:Array, param2:TFPathNode, param3:TFPathNode, param4:Function, param5:Array) : Array
  58.       {
  59.          return param4(param1,param2,param3,param5);
  60.       }
  61.       
  62.       public static function getPosition(param1:TFPathNode, param2:TFPathNode, param3:Number) : Point
  63.       {
  64.          return Point.interpolate(param1.getPoint(),param2.getPoint(),param3);
  65.       }
  66.       
  67.       private function calcSpeed(param1:TFPathNode, param2:TFPathNode) : *
  68.       {
  69.          var _loc3_:Number = NaN;
  70.          _loc3_ = nodeDistance(param1,param2);
  71.          if(_loc3_ == 0)
  72.          {
  73.             iCurrentSpeed = iBaseSpeed;
  74.          }
  75.          else
  76.          {
  77.             iCurrentSpeed = iBaseSpeed / _loc3_;
  78.          }
  79.          bCalcSpeed = false;
  80.       }
  81.       
  82.       public function getWeightMatrix() : Array
  83.       {
  84.          var _loc1_:Number = NaN;
  85.          var _loc2_:Number = NaN;
  86.          var _loc3_:Number = NaN;
  87.          var _loc4_:Array = null;
  88.          var _loc5_:String = null;
  89.          var _loc6_:TFPathNode = null;
  90.          var _loc7_:Array = null;
  91.          var _loc8_:Number = NaN;
  92.          _loc1_ = arNodes.length;
  93.          _loc4_ = new Array(_loc1_);
  94.          _loc2_ = 0;
  95.          while(_loc2_ < _loc1_)
  96.          {
  97.             _loc5_ = String(arNodes[_loc2_].getName());
  98.             _loc4_[_loc5_] = new Array(_loc1_);
  99.             _loc3_ = 0;
  100.             while(_loc3_ < _loc1_)
  101.             {
  102.                _loc4_[_loc5_][arNodes[_loc3_].getName()] = 99999999;
  103.                _loc3_++;
  104.             }
  105.             _loc4_[_loc5_][_loc5_] = 0;
  106.             _loc2_++;
  107.          }
  108.          _loc2_ = 0;
  109.          while(_loc2_ < _loc1_)
  110.          {
  111.             _loc8_ = (_loc7_ = (_loc6_ = arNodes[_loc2_]).getNeighbors()).length;
  112.             _loc3_ = 0;
  113.             while(_loc3_ < _loc8_)
  114.             {
  115.                _loc4_[arNodes[_loc2_].getName()][_loc7_[_loc3_].getName()] = nodeDistance(arNodes[_loc2_],_loc7_[_loc3_]);
  116.                _loc3_++;
  117.             }
  118.             _loc2_++;
  119.          }
  120.          return _loc4_;
  121.       }
  122.       
  123.       public function createPathNode(param1:MovieClip) : *
  124.       {
  125.          arNodes.push(new TFPathNode(param1));
  126.       }
  127.       
  128.       public function setPath(param1:TFPathNode, param2:TFPathNode) : Boolean
  129.       {
  130.          var _loc3_:Array = null;
  131.          _loc3_ = this.getWeightMatrix();
  132.          nodeEnd = param2;
  133.          arCurrPath = getPathUsingAlgorithm(arNodes,param1,param2,TFDijkstra.calculate,_loc3_);
  134.          nodeEnd.playAnimDest();
  135.          return arCurrPath.length > 0;
  136.       }
  137.       
  138.       public function pushNeighbor(param1:Number, param2:Number) : *
  139.       {
  140.          arNodes[param1].pushNeighbor(arNodes[param2]);
  141.       }
  142.       
  143.       public function process(param1:Function, param2:Object) : Boolean
  144.       {
  145.          var _loc3_:Boolean = false;
  146.          var _loc4_:Point = null;
  147.          _loc3_ = false;
  148.          if(arCurrPath == null)
  149.          {
  150.             return true;
  151.          }
  152.          if(iTimePos == 1)
  153.          {
  154.             iTimePos = 0;
  155.             arCurrPath.splice(0,1);
  156.             bCalcSpeed = true;
  157.          }
  158.          if(arCurrPath.length == 1 || arCurrPath.length == 0)
  159.          {
  160.             arCurrPath = null;
  161.             nodeEnd.stopAnimDest();
  162.             nodeEnd = null;
  163.             _loc3_ = true;
  164.          }
  165.          else
  166.          {
  167.             if(bCalcSpeed == true && bConstSpeed == true)
  168.             {
  169.                calcSpeed(arCurrPath[0],arCurrPath[1]);
  170.             }
  171.             if(param1 != null && param2 != null && arCurrPath[1] != null && arCurrPath[0] != null && (iTimePos == 0 || iTimePos == 1))
  172.             {
  173.                param1.call(param2,mcObject,arCurrPath[0],arCurrPath[1]);
  174.             }
  175.             iTimePos += iCurrentSpeed;
  176.             if(iTimePos > 1)
  177.             {
  178.                iTimePos = 1;
  179.             }
  180.             _loc4_ = getPosition(arCurrPath[1],arCurrPath[0],iTimePos);
  181.             mcObject.visible = false;
  182.             mcObject.x = _loc4_.x;
  183.             mcObject.y = _loc4_.y;
  184.             mcObject.visible = true;
  185.          }
  186.          return _loc3_;
  187.       }
  188.       
  189.       public function getNode(param1:Number) : TFPathNode
  190.       {
  191.          return arNodes[param1];
  192.       }
  193.       
  194.       public function getNodes() : Array
  195.       {
  196.          return arNodes;
  197.       }
  198.    }
  199. }
  200.