home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Diversos / wone.swf / scripts / __Packages / CWheel.as < prev   
Encoding:
Text File  |  2007-02-20  |  6.2 KB  |  185 lines

  1. class CWheel extends MovieClip
  2. {
  3.    var mRadius;
  4.    var pRadius;
  5.    var mCircumference;
  6.    var mFriction;
  7.    var mMass;
  8.    var mDelta;
  9.    var mInertia;
  10.    var mPosition;
  11.    var mRotation;
  12.    var mAngularVelocity;
  13.    var mTrackPosition;
  14.    var mTrackPositionIndex;
  15.    var mControlLeft;
  16.    var mControlRight;
  17.    var mArena;
  18.    var _Interior;
  19.    var mCollide;
  20.    var mDeltaTravelledTotal;
  21.    var mDeltaTravelled;
  22.    var mCount = 0;
  23.    static var mTime = 1;
  24.    static var GARWIN_ALPHA = 0.4;
  25.    static var Ex = 0.2;
  26.    static var Ey = 0.4;
  27.    function CWheel()
  28.    {
  29.       super();
  30.       this.mRadius = this._width / 2;
  31.       this.pRadius = new flash.geom.Point(this.mRadius,this.mRadius);
  32.       this.mCircumference = this.mRadius * 2 * 3.141592653589793;
  33.       this.mFriction = 1;
  34.       this.mMass = 1;
  35.       this.mDelta = new flash.geom.Point(0,0);
  36.       this.mInertia = new flash.geom.Point(0,0);
  37.       this.mPosition = new flash.geom.Point(this._x,this._y);
  38.       this.mRotation = 0;
  39.       this.mAngularVelocity = 0;
  40.       this.mTrackPosition = new Array(20);
  41.       this.mTrackPositionIndex = 0;
  42.       this.mControlLeft = this.mControlRight = false;
  43.       this._visible = true;
  44.    }
  45.    function Process()
  46.    {
  47.       var _loc5_ = this.mPosition.clone();
  48.       this.mCount = 0;
  49.       this.mDelta = this.mDelta.add(this.mArena.mGravity);
  50.       this.mDelta.x += this.mAngularVelocity / 1000;
  51.       this.mAngularVelocity *= 0.99;
  52.       this.mRotation += this.mAngularVelocity;
  53.       _root.mDebug = Math.round(this.mAngularVelocity) + ", " + Math.round(this.mDelta.length);
  54.       if(Math.abs(this.mAngularVelocity) >= 25)
  55.       {
  56.          var _loc4_ = Math.round(Math.abs(this.mAngularVelocity) / 25);
  57.          if(_loc4_ > 4)
  58.          {
  59.             _loc4_ = 4;
  60.          }
  61.          this._Interior.filters = [new flash.filters.BlurFilter(_loc4_,_loc4_,3)];
  62.       }
  63.       else
  64.       {
  65.          this._Interior.filters = [];
  66.       }
  67.       this.mCount = 0;
  68.       this.mCollide = false;
  69.       this.Collide();
  70.       this.mArena.mMap.WheelCollide(this,_loc5_,this.mPosition);
  71.       this._x = this.mPosition.x;
  72.       this._y = this.mPosition.y;
  73.       this._Interior._rotation = this.mRotation;
  74.       this.mTrackPositionIndex = (this.mTrackPositionIndex + 1) % this.mTrackPosition.length;
  75.       this.mTrackPosition[this.mTrackPositionIndex] = this.mPosition.clone();
  76.       if(_global.mKeyControl)
  77.       {
  78.          this.mControlLeft = Key.isDown(37);
  79.          this.mControlRight = Key.isDown(39);
  80.       }
  81.       if(this.mControlLeft)
  82.       {
  83.          if(this.mAngularVelocity > 0)
  84.          {
  85.             this.mAngularVelocity = 0;
  86.          }
  87.          this.mAngularVelocity -= 4;
  88.          this.mControlLeft = false;
  89.       }
  90.       if(this.mControlRight)
  91.       {
  92.          if(this.mAngularVelocity < 0)
  93.          {
  94.             this.mAngularVelocity = 0;
  95.          }
  96.          this.mAngularVelocity += 4;
  97.          this.mControlRight = false;
  98.       }
  99.    }
  100.    function Collide(tNotThisLine)
  101.    {
  102.       this.mDeltaTravelledTotal = this.mDelta.length;
  103.       if(tNotThisLine == undefined)
  104.       {
  105.          this.mDeltaTravelled = 1;
  106.       }
  107.       var _loc5_ = new flash.geom.Point(this.mDelta.x * this.mDeltaTravelled,this.mDelta.y * this.mDeltaTravelled);
  108.       var _loc6_ = this.mPosition.add(_loc5_);
  109.       var _loc7_ = this.mArena.mLineManager.GetLineList(this.mPosition,_loc6_,this.pRadius);
  110.       var _loc2_ = CLine.WheelToLineList(_loc7_,this.mPosition,_loc6_,this.mRadius);
  111.       if(_loc2_.length > 1)
  112.       {
  113.          trace(" ********* 2 Intersections ***************");
  114.       }
  115.       for(var _loc4_ in _loc2_)
  116.       {
  117.          if(tNotThisLine != _loc2_[_loc4_])
  118.          {
  119.             if(this.LineCollide(_loc2_[_loc4_]))
  120.             {
  121.                this.mDeltaTravelled -= flash.geom.Point.distance(this.mPosition,_loc2_[_loc4_].mPointOfIntersection) / this.mDeltaTravelledTotal;
  122.                this.mPosition = _loc2_[_loc4_].mPointOfIntersection;
  123.                if(this.mDeltaTravelled <= 0)
  124.                {
  125.                   this.mDeltaTravelled = 0;
  126.                }
  127.                this.Collide(_loc2_[_loc4_]);
  128.                return undefined;
  129.             }
  130.          }
  131.       }
  132.       this.mPosition = CLine.CollisionError(_loc7_,this.mPosition,_loc5_);
  133.       if(this.mArena.mState == "stop")
  134.       {
  135.          trace("mDelta = " + this.mDelta);
  136.          trace("nDelta = " + _loc5_);
  137.          trace("nLineList = " + _loc2_.length);
  138.          trace("nLineList[0].POI = " + _loc2_[0].mPointOfIntersection);
  139.          trace("tNotThisLine.POI = " + tNotThisLine.mPointOfIntersection);
  140.       }
  141.    }
  142.    function LineCollide(tLine, tDeflectOnly)
  143.    {
  144.       if(tLine == undefined)
  145.       {
  146.          return false;
  147.       }
  148.       var _loc8_ = tLine.mMatrix.transformPoint(tLine.mP1);
  149.       var _loc7_ = tLine.mMatrix.transformPoint(tLine.mP2);
  150.       var _loc6_ = tLine.mMatrix.transformPoint(this.mPosition);
  151.       var _loc3_ = tLine.mMatrix.transformPoint(this.mDelta);
  152.       var _loc5_ = this.mAngularVelocity * 3.141592653589793 / 180;
  153.       _loc3_.y = this.computeVyGarwin(_loc3_.y);
  154.       _loc3_.x = this.computeVxGarwin(_loc3_.x,_loc5_,this.mRadius);
  155.       this.mAngularVelocity = this.computeAVGarwin(_loc3_.x,_loc5_,this.mRadius) * 180 / 3.141592653589793;
  156.       this.mDelta = tLine.iMatrix.transformPoint(_loc3_);
  157.       tLine.mOnline = _loc3_.y != 0 ? false : true;
  158.       if(tLine.mOnline)
  159.       {
  160.          var _loc4_ = new flash.geom.Point(tLine.mP2.x - tLine.mP1.x,tLine.mP2.y - tLine.mP1.y);
  161.          _loc4_.normalize(_loc3_.x < 0 ? - this.mDelta.length : this.mDelta.length);
  162.          this.mDelta = _loc4_;
  163.       }
  164.       this.mCollide = true;
  165.       return true;
  166.    }
  167.    function computeVxGarwin(vx, av, r)
  168.    {
  169.       return ((1 - CWheel.GARWIN_ALPHA * CWheel.Ex) * vx + CWheel.GARWIN_ALPHA * (1 + CWheel.Ex) * r * av) / (1 + CWheel.GARWIN_ALPHA) * this.mFriction;
  170.    }
  171.    function computeVyGarwin(vy)
  172.    {
  173.       var _loc1_ = - CWheel.Ey * vy;
  174.       if(_loc1_ >= -5)
  175.       {
  176.          _loc1_ = 0;
  177.       }
  178.       return _loc1_;
  179.    }
  180.    function computeAVGarwin(vx, av, r)
  181.    {
  182.       return ((1 + CWheel.Ex) * vx + (CWheel.GARWIN_ALPHA - CWheel.Ex) * r * av) / (r * (1 + CWheel.GARWIN_ALPHA)) * this.mFriction;
  183.    }
  184. }
  185.