home *** CD-ROM | disk | FTP | other *** search
- class Game.Objects.Paddle extends Game.Objects.CollisionObject
- {
- var __width;
- var __halfWidth;
- var bounds;
- var __myBall;
- var dispatchEvent;
- var __frontEdge;
- var edges;
- var guide;
- var world;
- var target;
- var __thickness;
- static var assetLeftID = "PaddleLeft";
- static var assetRightID = "PaddleRight";
- var assetID = Game.Objects.Paddle.assetRightID;
- var className = "Paddle";
- static var sizes = [42,72,26,42,42,42];
- static var PADDLE_SIZE_DEFAULT = 0;
- static var PADDLE_SIZE_WIDE = 1;
- static var PADDLE_SIZE_SMALL = 2;
- static var PADDLE_SIZE_FROZEN = 3;
- static var PADDLE_SIZE_STICKY = 4;
- static var PADDLE_SIZE_MISSILE = 5;
- var __orientation = "right";
- var __orientationChanged = false;
- var __widthChanged = false;
- var __sticky = false;
- var __frozen = false;
- var __blast = false;
- var __assetFrame = 1;
- static var MAX_TILT = 20;
- var __topBottomOffset = 5;
- function Paddle(myOrientation, myWidth)
- {
- super();
- if(myWidth != undefined)
- {
- this.width = myWidth;
- }
- this.orientation = myOrientation;
- }
- function activateItem(item)
- {
- if(item == Game.PongLevels.PADDLE_WIDE)
- {
- this.width = Game.Objects.Paddle.PADDLE_SIZE_WIDE;
- }
- else if(item == Game.PongLevels.PADDLE_SMALL)
- {
- this.width = Game.Objects.Paddle.PADDLE_SIZE_SMALL;
- }
- else if(item == Game.PongLevels.FREEZE_YOU || item == Game.PongLevels.FREEZE_OPPONENT)
- {
- if(!this.frozen)
- {
- this.frozen = true;
- }
- }
- else if(item == Game.PongLevels.PADDLE_STICKY)
- {
- if(!this.sticky)
- {
- this.width = Game.Objects.Paddle.PADDLE_SIZE_STICKY;
- this.sticky = true;
- }
- }
- else if(item == Game.PongLevels.MISSILE)
- {
- if(!this.blast)
- {
- this.blast = true;
- }
- }
- }
- function deactivateItem()
- {
- if(this.sticky)
- {
- this.sticky = false;
- }
- else if(this.blast)
- {
- this.blast = false;
- }
- else if(this.frozen)
- {
- this.frozen = false;
- }
- else
- {
- this.width = Game.Objects.Paddle.PADDLE_SIZE_DEFAULT;
- }
- }
- function set width(newWidth)
- {
- newWidth = Math.max(0,newWidth);
- newWidth = Math.min(Game.Objects.Paddle.sizes.length - 1,newWidth);
- this.__assetFrame = newWidth + 1;
- this.__width = Game.Objects.Paddle.sizes[newWidth];
- this.__halfWidth = Math.floor(this.__width / 2);
- this.__widthChanged = true;
- if(this.y - this.__halfWidth < this.bounds.yMin)
- {
- this.moveTo(this.x,this.bounds.yMin + this.__halfWidth);
- }
- else if(this.bounds.yMax < this.y + this.__halfWidth)
- {
- this.moveTo(this.x,this.bounds.yMax - this.__halfWidth);
- }
- else
- {
- this.queueForDisplay();
- }
- this.updateBorders();
- this.calcEdges();
- }
- function get width()
- {
- return this.__width;
- }
- function set orientation(newDir)
- {
- if(newDir == undefined)
- {
- return;
- }
- newDir = newDir.toLowerCase();
- if(newDir == this.__orientation)
- {
- return;
- }
- if(newDir != Engine.PongObject.LEFT_DIR && newDir != Engine.PongObject.RIGHT_DIR)
- {
- return;
- }
- if(newDir == Engine.PongObject.LEFT_DIR)
- {
- this.setAssetID(Game.Objects.Paddle.assetLeftID);
- }
- else
- {
- this.setAssetID(Game.Objects.Paddle.assetRightID);
- }
- this.__orientation = newDir;
- this.__orientationChanged = true;
- this.queueForDisplay();
- }
- function get orientation()
- {
- return this.__orientation;
- }
- function set frozen(newFrozen)
- {
- this.__frozen = newFrozen;
- if(this.__frozen)
- {
- this.width = Game.Objects.Paddle.PADDLE_SIZE_FROZEN;
- }
- else
- {
- this.width = Game.Objects.Paddle.PADDLE_SIZE_DEFAULT;
- }
- }
- function get frozen()
- {
- return this.__frozen;
- }
- function set blast(newBlast)
- {
- this.__blast = newBlast;
- if(this.__blast)
- {
- this.width = Game.Objects.Paddle.PADDLE_SIZE_MISSILE;
- }
- else
- {
- this.width = Game.Objects.Paddle.PADDLE_SIZE_DEFAULT;
- }
- }
- function get blast()
- {
- return this.__blast;
- }
- function set sticky(newSticky)
- {
- this.__sticky = newSticky;
- if(!this.__sticky)
- {
- if(this.__myBall != undefined)
- {
- this.releaseBall();
- }
- this.width = Game.Objects.Paddle.PADDLE_SIZE_DEFAULT;
- }
- }
- function get sticky()
- {
- return this.__sticky;
- }
- function holdBall(myBall)
- {
- if(this.__myBall == myBall)
- {
- return undefined;
- }
- this.__myBall = myBall;
- this.__myBall.stickTo(this);
- if(this.__myBall != undefined)
- {
- this.__sticky = true;
- }
- if(this.blast)
- {
- myBall.blast = true;
- }
- this.dispatchEvent({type:"onHoldBall"});
- }
- function releaseBall()
- {
- if(!this.__myBall)
- {
- return undefined;
- }
- this.__myBall.stuck = false;
- this.__myBall = null;
- this.sticky = false;
- this.dispatchEvent({type:"onReleaseBall"});
- }
- function moveByPlayer(dy, myBall)
- {
- if(this.frozen || dy == 0)
- {
- return undefined;
- }
- var _loc3_ = this.getSpaceAvailable(myBall);
- if(_loc3_ == null)
- {
- return undefined;
- }
- if(dy < 0)
- {
- dy = Math.max(dy,_loc3_.top);
- }
- else
- {
- dy = Math.min(dy,_loc3_.bottom);
- }
- this.moveBy(0,dy,0);
- this.updateBorders();
- }
- function ballInHitArea(myBall)
- {
- return this.left < myBall.right && myBall.left < this.right;
- }
- function ballAbovePaddle(myBall)
- {
- return this.top >= myBall.bottom;
- }
- function ballBelowPaddle(myBall)
- {
- return myBall.top >= this.bottom;
- }
- function getSpaceAvailable(myBall)
- {
- var _loc3_ = this.bounds.yMin - this.top - this.__topBottomOffset;
- var _loc2_ = this.bounds.yMax - this.bottom + this.__topBottomOffset;
- if(this.ballInHitArea(myBall))
- {
- if(myBall.y < this.y)
- {
- _loc3_ = myBall.bottom - this.top;
- }
- else
- {
- _loc2_ = myBall.top - this.bottom;
- }
- }
- if(_loc3_ > 0)
- {
- _loc3_ = 0;
- }
- if(_loc2_ < 0)
- {
- _loc2_ = 0;
- }
- return {top:_loc3_,bottom:_loc2_};
- }
- function setBounds(newBounds)
- {
- this.bounds = newBounds;
- }
- function checkCollision(myBall)
- {
- var _loc4_ = Engine.Collision.sweepSphereToStaticShape(myBall,this);
- if(_loc4_ == null)
- {
- return false;
- }
- var _loc7_ = undefined;
- var _loc2_ = _loc4_.point;
- if(_loc4_.edge == this.__frontEdge)
- {
- var _loc6_ = this.calcReflectionNormal(_loc2_.y);
- if(myBall.direction.x > 0)
- {
- _loc7_ = true;
- _loc6_ = 270 - _loc6_;
- _loc2_.x -= 3;
- }
- else
- {
- _loc7_ = false;
- _loc6_ += 90;
- _loc2_.x += 3;
- }
- var _loc5_ = this.angleToVector(_loc6_);
- }
- else
- {
- _loc5_ = _loc4_.normal;
- if(_loc4_.edge = this.edges[0])
- {
- _loc2_.y -= 3;
- _loc2_.y = Math.max(this.bounds.yMin,_loc2_.y);
- }
- else if(_loc4_.edge = this.edges[2])
- {
- _loc2_.y += 3;
- _loc2_.y = Math.min(this.bounds.yMax,_loc2_.y);
- }
- }
- if(this.ballInHitArea(myBall))
- {
- if(this.ballAbovePaddle(myBall))
- {
- if(myBall.y < this.bounds.yMin + myBall.radius * 2.5)
- {
- if(this.__orientation == Engine.PongObject.LEFT_DIR)
- {
- _loc2_.x = this.right + myBall.radius + 3;
- _loc5_ = new Vector(1,0,0);
- }
- else
- {
- _loc2_.x = this.left - myBall.radius - 3;
- _loc5_ = new Vector(-1,0,0);
- }
- }
- }
- else if(this.ballBelowPaddle(myBall))
- {
- if(myBall.y > this.bounds.yMax - myBall.radius * 2.5)
- {
- if(this.__orientation == Engine.PongObject.LEFT_DIR)
- {
- _loc2_.x = this.right + myBall.radius + 3;
- _loc5_ = new Vector(1,0,0);
- }
- else
- {
- _loc2_.x = this.left - myBall.radius - 3;
- _loc5_ = new Vector(-1,0,0);
- }
- }
- }
- }
- myBall.deflect(_loc2_,_loc5_,_loc7_);
- this.collide(myBall);
- return true;
- }
- function drawCollision(point, normal, edge)
- {
- var _loc2_ = point.copy();
- _loc2_.x -= this.x;
- _loc2_.y -= this.y;
- var _loc3_ = normal.copy();
- _loc3_.length = 30;
- this.guide.clear();
- this.guide.lineStyle(1,16711680);
- this.guide.moveTo(_loc2_.x,_loc2_.y);
- this.guide.lineTo(_loc2_.x + _loc3_.x,_loc2_.y + _loc3_.y);
- this.guide.lineStyle(1,65280);
- this.guide.moveTo(edge.a.x,edge.a.y);
- this.guide.lineTo(edge.b.x,edge.b.y);
- }
- function collide(myBall)
- {
- !this.blast && myBall.blast;
- this.world.deactivate;
- myBall.blast = false;
- if(this.sticky || this.blast)
- {
- this.holdBall(myBall);
- }
- else
- {
- Engine.PongSFX.playPuckPaddle();
- }
- }
- function checkItemCollision(o)
- {
- if(o.motionLeft <= this.right && this.left <= o.motionRight)
- {
- if(o.motionTop <= this.bottom && this.top <= o.motionBottom)
- {
- return true;
- }
- }
- return false;
- }
- function onDisplay()
- {
- if(this.__widthChanged)
- {
- this.target.gotoAndStop(this.__assetFrame);
- this.__widthChanged = false;
- this.updateBorders();
- }
- }
- function updateBorders()
- {
- this.top = this.y - this.__halfWidth - this.__topBottomOffset;
- this.bottom = this.y + this.__halfWidth + this.__topBottomOffset;
- if(this.__orientation == Engine.PongObject.LEFT_DIR)
- {
- this.left = this.x - this.__thickness;
- this.right = this.x - 1;
- }
- else
- {
- this.left = this.x + 1;
- this.right = this.x + this.__thickness;
- }
- }
- function onAddToScene()
- {
- this.target.gotoAndStop(this.__assetFrame);
- this.__widthChanged = false;
- this.__thickness = this.target._width - 3;
- this.updateBorders();
- this.calcEdges();
- this.__orientationChanged = false;
- }
- function calcReflectionNormal(collY)
- {
- var _loc2_ = collY - this.y;
- var _loc3_ = _loc2_ / this.__halfWidth;
- var _loc4_ = _loc3_ * Game.Objects.Paddle.MAX_TILT;
- return _loc4_;
- }
- function calcEdges()
- {
- if(this.__orientation == Engine.PongObject.LEFT_DIR)
- {
- var _loc6_ = new _types_.ShapeVertex(- this.__thickness,- this.__halfWidth - this.__topBottomOffset);
- var _loc5_ = new _types_.ShapeVertex(-1,- this.__halfWidth - this.__topBottomOffset);
- var _loc3_ = new _types_.ShapeVertex(-1,this.__halfWidth + this.__topBottomOffset);
- var _loc4_ = new _types_.ShapeVertex(- this.__thickness,this.__halfWidth + this.__topBottomOffset);
- }
- else
- {
- _loc6_ = new _types_.ShapeVertex(1,- this.__halfWidth - this.__topBottomOffset);
- _loc5_ = new _types_.ShapeVertex(this.__thickness,- this.__halfWidth - this.__topBottomOffset);
- _loc3_ = new _types_.ShapeVertex(this.__thickness,this.__halfWidth + this.__topBottomOffset);
- _loc4_ = new _types_.ShapeVertex(1,this.__halfWidth + this.__topBottomOffset);
- }
- this.createEdges(_loc6_,_loc5_,_loc3_,_loc4_);
- if(this.__orientation == Engine.PongObject.LEFT_DIR)
- {
- this.__frontEdge = this.edges[1];
- }
- else
- {
- this.__frontEdge = this.edges[3];
- }
- var _loc2_ = this.edges.length;
- while((_loc2_ = _loc2_ - 1) > -1)
- {
- this.edges[_loc2_].props = Game.Objects.CollisionObject.EDGE_COLLISION;
- }
- }
- }
-