home *** CD-ROM | disk | FTP | other *** search
- class Engine.PongGrid extends GDK.Grid
- {
- var left;
- var right;
- var top;
- var bottom;
- var __world;
- var x = 0;
- var y = 0;
- var gridSize = 15;
- var className = "PongGrid";
- var destructibleCount = 0;
- static var SOLID = Game.PongLevels.S;
- static var DESTRUCTIBLE = Game.PongLevels.D;
- static var EDGE_DEFAULT = Game.Objects.CollisionObject.EDGE_DEFAULT;
- static var EDGE_COLLISION = Game.Objects.CollisionObject.EDGE_COLLISION;
- static var BOUNCE_VARIANCE = 6;
- function PongGrid(w, h, defaultValue)
- {
- super();
- this.setSize(w != null ? w : 0,h != null ? h : 0,true);
- if(defaultValue != null)
- {
- this.defaultValue = defaultValue;
- }
- }
- function moveTo(newX, newY)
- {
- this.x = newX == undefined ? 0 : newX;
- this.y = newY == undefined ? 0 : newY;
- this.left = this.x;
- this.right = this.x + this._width * this.gridSize;
- this.top = this.y;
- this.bottom = this.y + this._height * this.gridSize;
- }
- function checkCollision(myBall)
- {
- if(myBall.motionRight < this.left || this.right < myBall.motionLeft)
- {
- return null;
- }
- var _loc16_ = Math.max(0,Math.floor((myBall.motionLeft - this.left) / this.gridSize));
- var _loc17_ = Math.min(Math.ceil((myBall.motionRight - this.left) / this.gridSize),this._width);
- var _loc13_ = Math.max(0,Math.floor((myBall.motionTop - this.top) / this.gridSize));
- var _loc12_ = Math.min(Math.ceil((myBall.motionBottom - this.top) / this.gridSize),this._height);
- var _loc8_ = [];
- var _loc4_ = undefined;
- var _loc2_ = undefined;
- var _loc5_ = undefined;
- var _loc3_ = undefined;
- _loc4_ = _loc16_;
- while(_loc4_ < _loc17_)
- {
- _loc2_ = _loc13_;
- while(_loc2_ < _loc12_)
- {
- _loc5_ = this.getCell(_loc4_,_loc2_);
- if(_loc5_)
- {
- _loc3_ = _loc5_.edges;
- if(!myBall.blast)
- {
- _loc3_[0].props = !(_loc2_ == 0 || !this.getCell(_loc4_,_loc2_ - 1)) ? Engine.PongGrid.EDGE_DEFAULT : Engine.PongGrid.EDGE_COLLISION;
- _loc3_[1].props = !(_loc4_ == this._width - 1 || !this.getCell(_loc4_ + 1,_loc2_)) ? Engine.PongGrid.EDGE_DEFAULT : Engine.PongGrid.EDGE_COLLISION;
- _loc3_[2].props = !(_loc2_ == this._height - 1 || !this.getCell(_loc4_,_loc2_ + 1)) ? Engine.PongGrid.EDGE_DEFAULT : Engine.PongGrid.EDGE_COLLISION;
- _loc3_[3].props = !(_loc4_ == 0 || !this.getCell(_loc4_ - 1,_loc2_)) ? Engine.PongGrid.EDGE_DEFAULT : Engine.PongGrid.EDGE_COLLISION;
- }
- else
- {
- _loc3_[0].props = Engine.PongGrid.EDGE_COLLISION;
- _loc3_[1].props = Engine.PongGrid.EDGE_COLLISION;
- _loc3_[2].props = Engine.PongGrid.EDGE_COLLISION;
- _loc3_[3].props = Engine.PongGrid.EDGE_COLLISION;
- }
- _loc8_.push(_loc5_);
- }
- _loc2_ = _loc2_ + 1;
- }
- _loc4_ = _loc4_ + 1;
- }
- var _loc11_ = _loc8_.length;
- if(_loc11_ <= 0)
- {
- return null;
- }
- var _loc10_ = 1.7976931348623157e+308;
- var _loc9_ = undefined;
- var _loc15_ = undefined;
- var _loc14_ = undefined;
- var _loc7_ = undefined;
- while((_loc11_ = _loc11_ - 1) > -1)
- {
- _loc5_ = _loc8_[_loc11_];
- _loc7_ = Engine.Collision.sweepSphereToStaticShape(myBall,_loc5_);
- if(_loc7_)
- {
- if(myBall.blast)
- {
- this.removeItemAt(_loc5_.xPos,_loc5_.yPos,true);
- this.__world.scorePointsForBrick(_loc5_);
- }
- else
- {
- _loc9_ = new Vector(myBall.lastPos.x - _loc7_.point.x,myBall.lastPos.y - _loc7_.point.y,0).length;
- if(_loc9_ < _loc10_)
- {
- _loc10_ = _loc9_;
- _loc14_ = _loc7_;
- _loc15_ = _loc5_;
- }
- }
- }
- }
- if(_loc10_ == 1.7976931348623157e+308)
- {
- return null;
- }
- if(myBall.blast)
- {
- myBall.calcNewPosTo(_loc14_.point.x,_loc14_.point.y,true);
- }
- else
- {
- var _loc18_ = this.vectorToAngle(_loc14_.normal);
- var _loc19_ = Math.random() * Engine.PongGrid.BOUNCE_VARIANCE - Engine.PongGrid.BOUNCE_VARIANCE / 2;
- _loc18_ += _loc19_;
- var _loc20_ = this.angleToVector(_loc18_);
- myBall.deflect(_loc14_.point,_loc20_);
- }
- if(_loc15_.type == Engine.PongGrid.DESTRUCTIBLE || myBall.blast)
- {
- this.removeItemAt(_loc15_.xPos,_loc15_.yPos);
- }
- else
- {
- Engine.PongSFX.playBounce();
- }
- return _loc15_;
- }
- function set world(myWorld)
- {
- this.__world = myWorld;
- }
- function get world()
- {
- return this.__world;
- }
- function addItemAt(itemX, itemY, type, data)
- {
- var _loc2_ = new Game.Objects.Brick(type,data);
- _loc2_.xPos = itemX;
- _loc2_.yPos = itemY;
- this.setCell(itemX,itemY,_loc2_);
- _loc2_.moveTo(this.x + itemX * this.gridSize,this.y + itemY * this.gridSize,0);
- this.world.addObject(_loc2_);
- if(type == Engine.PongGrid.DESTRUCTIBLE)
- {
- this.destructibleCount = this.destructibleCount + 1;
- }
- }
- function removeItemAt(itemX, itemY, instantly)
- {
- var _loc2_ = this.getCell(itemX,itemY);
- if(_loc2_.type == Engine.PongGrid.DESTRUCTIBLE)
- {
- this.destructibleCount = this.destructibleCount - 1;
- }
- _loc2_.destroySelf(instantly);
- this.setCell(itemX,itemY,null);
- }
- function removeAll()
- {
- var _loc3_ = undefined;
- var _loc2_ = this.length;
- while((_loc2_ = _loc2_ - 1) > -1)
- {
- _loc3_ = this[_loc2_];
- if(_loc3_)
- {
- _loc3_.destroySelf(true);
- this[_loc2_] = null;
- }
- }
- this.destructibleCount = 0;
- }
- function vectorToAngle(v)
- {
- return Math.atan2(v.y,v.x) / 3.141592653589793 * 180 + 90;
- }
- function angleToVector(angle)
- {
- var _loc1_ = angle / 180 * 3.141592653589793;
- return new Vector(Math.sin(_loc1_),- Math.cos(_loc1_),0);
- }
- function toString()
- {
- var _loc3_ = this.className + ": w: " + this._width + " h: " + this._height + "\n";
- var _loc5_ = undefined;
- var _loc2_ = 0;
- var _loc4_ = undefined;
- _loc3_ += "[ \r";
- _loc5_ = 0;
- while(_loc5_ < this._height)
- {
- _loc3_ += "\t[";
- _loc2_ = 0;
- while(_loc2_ < this._width)
- {
- _loc4_ = this.getCell(_loc2_,_loc5_);
- if(!_loc4_)
- {
- _loc3_ += "e";
- }
- else
- {
- _loc3_ += _loc4_ != Engine.PongGrid.SOLID ? "D" : "S";
- }
- if(_loc2_ < this._width - 1)
- {
- _loc3_ += ",";
- }
- _loc2_ = _loc2_ + 1;
- }
- if(_loc5_ < this._height - 1)
- {
- _loc3_ += "],\r";
- }
- else
- {
- _loc3_ += "]\r";
- }
- _loc5_ = _loc5_ + 1;
- }
- _loc3_ += " ]; ";
- return _loc3_;
- }
- }
-