home *** CD-ROM | disk | FTP | other *** search
/ 600 Games / 600games.iso / Classicos / smashout.swf / scripts / __Packages / Engine / PongGrid.as < prev    next >
Encoding:
Text File  |  2005-11-09  |  7.4 KB  |  238 lines

  1. class Engine.PongGrid extends GDK.Grid
  2. {
  3.    var left;
  4.    var right;
  5.    var top;
  6.    var bottom;
  7.    var __world;
  8.    var x = 0;
  9.    var y = 0;
  10.    var gridSize = 15;
  11.    var className = "PongGrid";
  12.    var destructibleCount = 0;
  13.    static var SOLID = Game.PongLevels.S;
  14.    static var DESTRUCTIBLE = Game.PongLevels.D;
  15.    static var EDGE_DEFAULT = Game.Objects.CollisionObject.EDGE_DEFAULT;
  16.    static var EDGE_COLLISION = Game.Objects.CollisionObject.EDGE_COLLISION;
  17.    static var BOUNCE_VARIANCE = 6;
  18.    function PongGrid(w, h, defaultValue)
  19.    {
  20.       super();
  21.       this.setSize(w != null ? w : 0,h != null ? h : 0,true);
  22.       if(defaultValue != null)
  23.       {
  24.          this.defaultValue = defaultValue;
  25.       }
  26.    }
  27.    function moveTo(newX, newY)
  28.    {
  29.       this.x = newX == undefined ? 0 : newX;
  30.       this.y = newY == undefined ? 0 : newY;
  31.       this.left = this.x;
  32.       this.right = this.x + this._width * this.gridSize;
  33.       this.top = this.y;
  34.       this.bottom = this.y + this._height * this.gridSize;
  35.    }
  36.    function checkCollision(myBall)
  37.    {
  38.       if(myBall.motionRight < this.left || this.right < myBall.motionLeft)
  39.       {
  40.          return null;
  41.       }
  42.       var _loc16_ = Math.max(0,Math.floor((myBall.motionLeft - this.left) / this.gridSize));
  43.       var _loc17_ = Math.min(Math.ceil((myBall.motionRight - this.left) / this.gridSize),this._width);
  44.       var _loc13_ = Math.max(0,Math.floor((myBall.motionTop - this.top) / this.gridSize));
  45.       var _loc12_ = Math.min(Math.ceil((myBall.motionBottom - this.top) / this.gridSize),this._height);
  46.       var _loc8_ = [];
  47.       var _loc4_ = undefined;
  48.       var _loc2_ = undefined;
  49.       var _loc5_ = undefined;
  50.       var _loc3_ = undefined;
  51.       _loc4_ = _loc16_;
  52.       while(_loc4_ < _loc17_)
  53.       {
  54.          _loc2_ = _loc13_;
  55.          while(_loc2_ < _loc12_)
  56.          {
  57.             _loc5_ = this.getCell(_loc4_,_loc2_);
  58.             if(_loc5_)
  59.             {
  60.                _loc3_ = _loc5_.edges;
  61.                if(!myBall.blast)
  62.                {
  63.                   _loc3_[0].props = !(_loc2_ == 0 || !this.getCell(_loc4_,_loc2_ - 1)) ? Engine.PongGrid.EDGE_DEFAULT : Engine.PongGrid.EDGE_COLLISION;
  64.                   _loc3_[1].props = !(_loc4_ == this._width - 1 || !this.getCell(_loc4_ + 1,_loc2_)) ? Engine.PongGrid.EDGE_DEFAULT : Engine.PongGrid.EDGE_COLLISION;
  65.                   _loc3_[2].props = !(_loc2_ == this._height - 1 || !this.getCell(_loc4_,_loc2_ + 1)) ? Engine.PongGrid.EDGE_DEFAULT : Engine.PongGrid.EDGE_COLLISION;
  66.                   _loc3_[3].props = !(_loc4_ == 0 || !this.getCell(_loc4_ - 1,_loc2_)) ? Engine.PongGrid.EDGE_DEFAULT : Engine.PongGrid.EDGE_COLLISION;
  67.                }
  68.                else
  69.                {
  70.                   _loc3_[0].props = Engine.PongGrid.EDGE_COLLISION;
  71.                   _loc3_[1].props = Engine.PongGrid.EDGE_COLLISION;
  72.                   _loc3_[2].props = Engine.PongGrid.EDGE_COLLISION;
  73.                   _loc3_[3].props = Engine.PongGrid.EDGE_COLLISION;
  74.                }
  75.                _loc8_.push(_loc5_);
  76.             }
  77.             _loc2_ = _loc2_ + 1;
  78.          }
  79.          _loc4_ = _loc4_ + 1;
  80.       }
  81.       var _loc11_ = _loc8_.length;
  82.       if(_loc11_ <= 0)
  83.       {
  84.          return null;
  85.       }
  86.       var _loc10_ = 1.7976931348623157e+308;
  87.       var _loc9_ = undefined;
  88.       var _loc15_ = undefined;
  89.       var _loc14_ = undefined;
  90.       var _loc7_ = undefined;
  91.       while((_loc11_ = _loc11_ - 1) > -1)
  92.       {
  93.          _loc5_ = _loc8_[_loc11_];
  94.          _loc7_ = Engine.Collision.sweepSphereToStaticShape(myBall,_loc5_);
  95.          if(_loc7_)
  96.          {
  97.             if(myBall.blast)
  98.             {
  99.                this.removeItemAt(_loc5_.xPos,_loc5_.yPos,true);
  100.                this.__world.scorePointsForBrick(_loc5_);
  101.             }
  102.             else
  103.             {
  104.                _loc9_ = new Vector(myBall.lastPos.x - _loc7_.point.x,myBall.lastPos.y - _loc7_.point.y,0).length;
  105.                if(_loc9_ < _loc10_)
  106.                {
  107.                   _loc10_ = _loc9_;
  108.                   _loc14_ = _loc7_;
  109.                   _loc15_ = _loc5_;
  110.                }
  111.             }
  112.          }
  113.       }
  114.       if(_loc10_ == 1.7976931348623157e+308)
  115.       {
  116.          return null;
  117.       }
  118.       if(myBall.blast)
  119.       {
  120.          myBall.calcNewPosTo(_loc14_.point.x,_loc14_.point.y,true);
  121.       }
  122.       else
  123.       {
  124.          var _loc18_ = this.vectorToAngle(_loc14_.normal);
  125.          var _loc19_ = Math.random() * Engine.PongGrid.BOUNCE_VARIANCE - Engine.PongGrid.BOUNCE_VARIANCE / 2;
  126.          _loc18_ += _loc19_;
  127.          var _loc20_ = this.angleToVector(_loc18_);
  128.          myBall.deflect(_loc14_.point,_loc20_);
  129.       }
  130.       if(_loc15_.type == Engine.PongGrid.DESTRUCTIBLE || myBall.blast)
  131.       {
  132.          this.removeItemAt(_loc15_.xPos,_loc15_.yPos);
  133.       }
  134.       else
  135.       {
  136.          Engine.PongSFX.playBounce();
  137.       }
  138.       return _loc15_;
  139.    }
  140.    function set world(myWorld)
  141.    {
  142.       this.__world = myWorld;
  143.    }
  144.    function get world()
  145.    {
  146.       return this.__world;
  147.    }
  148.    function addItemAt(itemX, itemY, type, data)
  149.    {
  150.       var _loc2_ = new Game.Objects.Brick(type,data);
  151.       _loc2_.xPos = itemX;
  152.       _loc2_.yPos = itemY;
  153.       this.setCell(itemX,itemY,_loc2_);
  154.       _loc2_.moveTo(this.x + itemX * this.gridSize,this.y + itemY * this.gridSize,0);
  155.       this.world.addObject(_loc2_);
  156.       if(type == Engine.PongGrid.DESTRUCTIBLE)
  157.       {
  158.          this.destructibleCount = this.destructibleCount + 1;
  159.       }
  160.    }
  161.    function removeItemAt(itemX, itemY, instantly)
  162.    {
  163.       var _loc2_ = this.getCell(itemX,itemY);
  164.       if(_loc2_.type == Engine.PongGrid.DESTRUCTIBLE)
  165.       {
  166.          this.destructibleCount = this.destructibleCount - 1;
  167.       }
  168.       _loc2_.destroySelf(instantly);
  169.       this.setCell(itemX,itemY,null);
  170.    }
  171.    function removeAll()
  172.    {
  173.       var _loc3_ = undefined;
  174.       var _loc2_ = this.length;
  175.       while((_loc2_ = _loc2_ - 1) > -1)
  176.       {
  177.          _loc3_ = this[_loc2_];
  178.          if(_loc3_)
  179.          {
  180.             _loc3_.destroySelf(true);
  181.             this[_loc2_] = null;
  182.          }
  183.       }
  184.       this.destructibleCount = 0;
  185.    }
  186.    function vectorToAngle(v)
  187.    {
  188.       return Math.atan2(v.y,v.x) / 3.141592653589793 * 180 + 90;
  189.    }
  190.    function angleToVector(angle)
  191.    {
  192.       var _loc1_ = angle / 180 * 3.141592653589793;
  193.       return new Vector(Math.sin(_loc1_),- Math.cos(_loc1_),0);
  194.    }
  195.    function toString()
  196.    {
  197.       var _loc3_ = this.className + ": w: " + this._width + " h: " + this._height + "\n";
  198.       var _loc5_ = undefined;
  199.       var _loc2_ = 0;
  200.       var _loc4_ = undefined;
  201.       _loc3_ += "[ \r";
  202.       _loc5_ = 0;
  203.       while(_loc5_ < this._height)
  204.       {
  205.          _loc3_ += "\t[";
  206.          _loc2_ = 0;
  207.          while(_loc2_ < this._width)
  208.          {
  209.             _loc4_ = this.getCell(_loc2_,_loc5_);
  210.             if(!_loc4_)
  211.             {
  212.                _loc3_ += "e";
  213.             }
  214.             else
  215.             {
  216.                _loc3_ += _loc4_ != Engine.PongGrid.SOLID ? "D" : "S";
  217.             }
  218.             if(_loc2_ < this._width - 1)
  219.             {
  220.                _loc3_ += ",";
  221.             }
  222.             _loc2_ = _loc2_ + 1;
  223.          }
  224.          if(_loc5_ < this._height - 1)
  225.          {
  226.             _loc3_ += "],\r";
  227.          }
  228.          else
  229.          {
  230.             _loc3_ += "]\r";
  231.          }
  232.          _loc5_ = _loc5_ + 1;
  233.       }
  234.       _loc3_ += " ]; ";
  235.       return _loc3_;
  236.    }
  237. }
  238.