home *** CD-ROM | disk | FTP | other *** search
/ Champak 48 / cdrom_image.iso / Games / breakawish.swf / scripts / __Packages / FallingItem.as < prev    next >
Encoding:
Text File  |  2007-09-28  |  2.9 KB  |  97 lines

  1. class FallingItem extends BaseObject
  2. {
  3.    var nItemType;
  4.    var nSpeedX;
  5.    var nSpeedY;
  6.    var mcRef;
  7.    static var sSTATE_FALLING = "Falling";
  8.    static var sSTATE_CATCHED = "Catched";
  9.    static var nINITIAL_SPEED_X_MAX = 3;
  10.    static var nINITIAL_SPEED_Y_MIN = -4;
  11.    static var nINITIAL_SPEED_Y_MAX = 2;
  12.    static var nSPEED_X_LOSS = 0.15;
  13.    static var nACCELERATION = 0.2;
  14.    static var nMAX_FALL_SPEED = 14;
  15.    function FallingItem(_mcRef, _nItemType)
  16.    {
  17.       super(_mcRef);
  18.       this.setState(FallingItem.sSTATE_FALLING);
  19.       this.nItemType = _nItemType;
  20.       this.nSpeedX = Library.Utils.MoreMath.getRandomRange(- FallingItem.nINITIAL_SPEED_X_MAX,FallingItem.nINITIAL_SPEED_X_MAX);
  21.       this.nSpeedY = Library.Utils.MoreMath.getRandomRange(FallingItem.nINITIAL_SPEED_Y_MIN,FallingItem.nINITIAL_SPEED_Y_MAX);
  22.       LevelManager.Instance.doAddListener(this);
  23.    }
  24.    function doDestroy()
  25.    {
  26.       LevelManager.Instance.doRemoveListener(this);
  27.       super.doDestroy();
  28.    }
  29.    function get ItemType()
  30.    {
  31.       return this.nItemType;
  32.    }
  33.    function doCatched()
  34.    {
  35.       if(this.isStateComplete())
  36.       {
  37.          this.doDestroy();
  38.       }
  39.    }
  40.    function doFalling()
  41.    {
  42.       if(this.mcRef._y < LevelManager.nLIMITS_FLOOR)
  43.       {
  44.          this.nSpeedY += FallingItem.nACCELERATION;
  45.          if(this.nSpeedY > FallingItem.nMAX_FALL_SPEED)
  46.          {
  47.             this.nSpeedY = FallingItem.nMAX_FALL_SPEED;
  48.          }
  49.          var _loc2_ = this.mcRef._x + this.nSpeedX;
  50.          var _loc3_ = this.mcRef._y + this.nSpeedY;
  51.          _loc2_ = this.doCheckSides(this.nSpeedX,_loc2_);
  52.          this.mcRef._x = _loc2_;
  53.          this.mcRef._y = _loc3_;
  54.          this.doCheckPaddle();
  55.       }
  56.       else
  57.       {
  58.          LevelManager.Instance.onItemLost(this);
  59.          this.doDestroy();
  60.       }
  61.    }
  62.    function doCheckPaddle()
  63.    {
  64.       var _loc2_ = LevelManager.Instance.MainPaddle;
  65.       var _loc3_ = this.getCollideBottom(_loc2_) || this.getCollideRight(_loc2_) || this.getCollideLeft(_loc2_);
  66.       if(_loc3_)
  67.       {
  68.          LevelManager.Instance.onPlayerCatchItem(this);
  69.          this.setState(FallingItem.sSTATE_CATCHED);
  70.       }
  71.    }
  72.    function doCheckSides(_nSpeedX, _nFutureX)
  73.    {
  74.       switch(Library.Utils.MoreMath.getPolarity(_nSpeedX))
  75.       {
  76.          case 1:
  77.             if(_nFutureX + this.StateBounds.xMax > LevelManager.nLIMITS_RIGHT)
  78.             {
  79.                this.onBounceX();
  80.                _nFutureX = LevelManager.nLIMITS_RIGHT - this.StateBounds.xMax;
  81.             }
  82.             break;
  83.          case -1:
  84.             if(_nFutureX + this.StateBounds.xMin < LevelManager.nLIMITS_LEFT)
  85.             {
  86.                this.onBounceX();
  87.                _nFutureX = LevelManager.nLIMITS_LEFT - this.StateBounds.xMin;
  88.             }
  89.       }
  90.       return _nFutureX;
  91.    }
  92.    function onBounceX()
  93.    {
  94.       this.nSpeedX *= -1;
  95.    }
  96. }
  97.