home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Aventura / starisland.swf / scripts / __Packages / org / cove / flade / surfaces / LineSurface.as < prev    next >
Encoding:
Text File  |  2007-12-10  |  7.4 KB  |  249 lines

  1. class org.cove.flade.surfaces.LineSurface extends org.cove.flade.surfaces.AbstractTile implements org.cove.flade.surfaces.Surface
  2. {
  3.    var p1;
  4.    var p2;
  5.    var collNormal;
  6.    var isVisible;
  7.    var dmc;
  8.    var faceNormal;
  9.    var collisionDepth;
  10.    var minY;
  11.    var maxY;
  12.    var minX;
  13.    var maxX;
  14.    var sideNormal;
  15.    var minS;
  16.    var maxS;
  17.    var minF;
  18.    var maxF;
  19.    var rise;
  20.    var run;
  21.    var sign;
  22.    var slope;
  23.    var invB;
  24.    var p3;
  25.    var p4;
  26.    var verts;
  27.    function LineSurface(p1x, p1y, p2x, p2y, rootmc)
  28.    {
  29.       super(0,0,rootmc);
  30.       this.p1 = new org.cove.flade.util.Vector(p1x,p1y);
  31.       this.p2 = new org.cove.flade.util.Vector(p2x,p2y);
  32.       this.calcFaceNormal();
  33.       this.collNormal = new org.cove.flade.util.Vector(0,0);
  34.       this.setCollisionDepth(60);
  35.    }
  36.    function clear()
  37.    {
  38.       this.p1 = new org.cove.flade.util.Vector(0,0);
  39.       this.p2 = new org.cove.flade.util.Vector(1,1);
  40.       this.calcFaceNormal();
  41.       this.collNormal = new org.cove.flade.util.Vector(0,0);
  42.       this.setCollisionDepth(30);
  43.       this.paint();
  44.    }
  45.    function paint()
  46.    {
  47.       if(this.isVisible)
  48.       {
  49.          this.dmc.clear();
  50.          this.dmc.lineStyle(0,2237064,100);
  51.          org.cove.flade.graphics.Graphics.paintLine(this.dmc,this.p1.x,this.p1.y,this.p2.x,this.p2.y);
  52.       }
  53.    }
  54.    function resolveCircleCollision(p, sysObj)
  55.    {
  56.       if(this.isCircleColliding(p))
  57.       {
  58.          this.onContact();
  59.          p.resolveCollision(this.faceNormal,sysObj);
  60.          p.SetCollision();
  61.       }
  62.    }
  63.    function resolveRectangleCollision(p, sysObj)
  64.    {
  65.       if(this.isRectangleColliding(p))
  66.       {
  67.          this.onContact();
  68.          p.resolveCollision(this.collNormal,sysObj);
  69.          p.SetCollision();
  70.       }
  71.    }
  72.    function setCollisionDepth(d)
  73.    {
  74.       this.collisionDepth = d;
  75.       this.precalculate();
  76.    }
  77.    function isCircleColliding(p)
  78.    {
  79.       this.findClosestPoint(p.curr,p.closestPoint);
  80.       var _loc3_ = p.closestPoint.minusNew(p.curr);
  81.       _loc3_.normalize();
  82.       if(this.inequality(p.curr))
  83.       {
  84.          var _loc5_ = Math.abs(_loc3_.x);
  85.          _loc3_.x = this.faceNormal.x >= 0 ? - _loc5_ : _loc5_;
  86.          _loc3_.y = Math.abs(_loc3_.y);
  87.       }
  88.       var _loc4_ = p.curr.plusNew(_loc3_.mult(p.radius));
  89.       if(this.segmentInequality(_loc4_))
  90.       {
  91.          if(_loc4_.distance(p.closestPoint) > this.collisionDepth)
  92.          {
  93.             return false;
  94.          }
  95.          var _loc7_ = _loc4_.x - p.closestPoint.x;
  96.          var _loc6_ = _loc4_.y - p.closestPoint.y;
  97.          p.mtd.setTo(- _loc7_,- _loc6_);
  98.          return true;
  99.       }
  100.       return false;
  101.    }
  102.    function isRectangleColliding(p)
  103.    {
  104.       p.getCardYProjection();
  105.       var _loc7_ = this.testIntervals(p.bmin,p.bmax,this.minY,this.maxY);
  106.       if(_loc7_ == 0)
  107.       {
  108.          return false;
  109.       }
  110.       p.getCardXProjection();
  111.       var _loc8_ = this.testIntervals(p.bmin,p.bmax,this.minX,this.maxX);
  112.       if(_loc8_ == 0)
  113.       {
  114.          return false;
  115.       }
  116.       p.getAxisProjection(this.sideNormal);
  117.       var _loc10_ = this.testIntervals(p.bmin,p.bmax,this.minS,this.maxS);
  118.       if(_loc10_ == 0)
  119.       {
  120.          return false;
  121.       }
  122.       p.getAxisProjection(this.faceNormal);
  123.       var _loc9_ = this.testIntervals(p.bmin,p.bmax,this.minF,this.maxF);
  124.       if(_loc9_ == 0)
  125.       {
  126.          return false;
  127.       }
  128.       var _loc4_ = Math.abs(_loc8_);
  129.       var _loc3_ = Math.abs(_loc7_);
  130.       var _loc6_ = Math.abs(_loc10_);
  131.       var _loc5_ = Math.abs(_loc9_);
  132.       if(_loc4_ <= _loc3_ && _loc4_ <= _loc6_ && _loc4_ <= _loc5_)
  133.       {
  134.          p.mtd.setTo(_loc8_,0);
  135.          this.collNormal.setTo(p.mtd.x / _loc4_,0);
  136.       }
  137.       else if(_loc3_ <= _loc4_ && _loc3_ <= _loc6_ && _loc3_ <= _loc5_)
  138.       {
  139.          p.mtd.setTo(0,_loc7_);
  140.          this.collNormal.setTo(0,p.mtd.y / _loc3_);
  141.       }
  142.       else if(_loc5_ <= _loc4_ && _loc5_ <= _loc3_ && _loc5_ <= _loc6_)
  143.       {
  144.          p.mtd = this.faceNormal.multNew(_loc9_);
  145.          this.collNormal.copy(this.faceNormal);
  146.       }
  147.       else if(_loc6_ <= _loc4_ && _loc6_ <= _loc3_ && _loc6_ <= _loc5_)
  148.       {
  149.          p.mtd = this.sideNormal.multNew(_loc10_);
  150.          this.collNormal.copy(this.sideNormal);
  151.       }
  152.       return true;
  153.    }
  154.    function precalculate()
  155.    {
  156.       this.rise = this.p2.y - this.p1.y;
  157.       this.run = this.p2.x - this.p1.x;
  158.       this.sign = this.run < 0 ? -1 : 1;
  159.       this.slope = this.rise / this.run;
  160.       this.invB = 1 / (this.run * this.run + this.rise * this.rise);
  161.       this.createRectangle();
  162.       this.calcSideNormal();
  163.       this.setCardProjections();
  164.       this.setAxisProjections();
  165.    }
  166.    function calcFaceNormal()
  167.    {
  168.       this.faceNormal = new org.cove.flade.util.Vector(0,0);
  169.       var _loc3_ = this.p2.x - this.p1.x;
  170.       var _loc2_ = this.p2.y - this.p1.y;
  171.       this.faceNormal.setTo(_loc2_,- _loc3_);
  172.       this.faceNormal.normalize();
  173.    }
  174.    function segmentInequality(toPoint)
  175.    {
  176.       var _loc2_ = this.findU(toPoint);
  177.       var _loc3_ = this.inequality(toPoint);
  178.       return _loc2_ >= 0 && _loc2_ <= 1 && _loc3_;
  179.    }
  180.    function inequality(toPoint)
  181.    {
  182.       var _loc2_ = (this.slope * (toPoint.x - this.p1.x) + (this.p1.y - toPoint.y)) * this.sign;
  183.       return _loc2_ <= 0;
  184.    }
  185.    function findClosestPoint(toPoint, returnVect)
  186.    {
  187.       var _loc2_ = this.findU(toPoint);
  188.       if(_loc2_ <= 0)
  189.       {
  190.          returnVect.copy(this.p1);
  191.          return undefined;
  192.       }
  193.       if(_loc2_ >= 1)
  194.       {
  195.          returnVect.copy(this.p2);
  196.          return undefined;
  197.       }
  198.       var _loc5_ = this.p1.x + _loc2_ * (this.p2.x - this.p1.x);
  199.       var _loc4_ = this.p1.y + _loc2_ * (this.p2.y - this.p1.y);
  200.       returnVect.setTo(_loc5_,_loc4_);
  201.    }
  202.    function findU(p)
  203.    {
  204.       var _loc2_ = (p.x - this.p1.x) * this.run + (p.y - this.p1.y) * this.rise;
  205.       return _loc2_ * this.invB;
  206.    }
  207.    function createRectangle()
  208.    {
  209.       var _loc5_ = this.p2.x + (- this.faceNormal.x) * this.collisionDepth;
  210.       var _loc3_ = this.p2.y + (- this.faceNormal.y) * this.collisionDepth;
  211.       var _loc4_ = this.p1.x + (- this.faceNormal.x) * this.collisionDepth;
  212.       var _loc2_ = this.p1.y + (- this.faceNormal.y) * this.collisionDepth;
  213.       this.p3 = new org.cove.flade.util.Vector(_loc5_,_loc3_);
  214.       this.p4 = new org.cove.flade.util.Vector(_loc4_,_loc2_);
  215.       this.verts.push(this.p1);
  216.       this.verts.push(this.p2);
  217.       this.verts.push(this.p3);
  218.       this.verts.push(this.p4);
  219.    }
  220.    function setAxisProjections()
  221.    {
  222.       var _loc2_ = undefined;
  223.       this.minF = this.p2.dot(this.faceNormal);
  224.       this.maxF = this.p3.dot(this.faceNormal);
  225.       if(this.minF > this.maxF)
  226.       {
  227.          _loc2_ = this.minF;
  228.          this.minF = this.maxF;
  229.          this.maxF = _loc2_;
  230.       }
  231.       this.minS = this.p1.dot(this.sideNormal);
  232.       this.maxS = this.p2.dot(this.sideNormal);
  233.       if(this.minS > this.maxS)
  234.       {
  235.          _loc2_ = this.minS;
  236.          this.minS = this.maxS;
  237.          this.maxS = _loc2_;
  238.       }
  239.    }
  240.    function calcSideNormal()
  241.    {
  242.       this.sideNormal = new org.cove.flade.util.Vector(0,0);
  243.       var _loc3_ = this.p3.x - this.p2.x;
  244.       var _loc2_ = this.p3.y - this.p2.y;
  245.       this.sideNormal.setTo(_loc2_,- _loc3_);
  246.       this.sideNormal.normalize();
  247.    }
  248. }
  249.