home *** CD-ROM | disk | FTP | other *** search
/ 600 Games / 600games.iso / Classicos / smashout.swf / scripts / __Packages / Game / Objects / Paddle.as < prev   
Encoding:
Text File  |  2005-11-09  |  12.8 KB  |  484 lines

  1. class Game.Objects.Paddle extends Game.Objects.CollisionObject
  2. {
  3.    var __width;
  4.    var __halfWidth;
  5.    var bounds;
  6.    var __myBall;
  7.    var dispatchEvent;
  8.    var __frontEdge;
  9.    var edges;
  10.    var guide;
  11.    var world;
  12.    var target;
  13.    var __thickness;
  14.    static var assetLeftID = "PaddleLeft";
  15.    static var assetRightID = "PaddleRight";
  16.    var assetID = Game.Objects.Paddle.assetRightID;
  17.    var className = "Paddle";
  18.    static var sizes = [42,72,26,42,42,42];
  19.    static var PADDLE_SIZE_DEFAULT = 0;
  20.    static var PADDLE_SIZE_WIDE = 1;
  21.    static var PADDLE_SIZE_SMALL = 2;
  22.    static var PADDLE_SIZE_FROZEN = 3;
  23.    static var PADDLE_SIZE_STICKY = 4;
  24.    static var PADDLE_SIZE_MISSILE = 5;
  25.    var __orientation = "right";
  26.    var __orientationChanged = false;
  27.    var __widthChanged = false;
  28.    var __sticky = false;
  29.    var __frozen = false;
  30.    var __blast = false;
  31.    var __assetFrame = 1;
  32.    static var MAX_TILT = 20;
  33.    var __topBottomOffset = 5;
  34.    function Paddle(myOrientation, myWidth)
  35.    {
  36.       super();
  37.       if(myWidth != undefined)
  38.       {
  39.          this.width = myWidth;
  40.       }
  41.       this.orientation = myOrientation;
  42.    }
  43.    function activateItem(item)
  44.    {
  45.       if(item == Game.PongLevels.PADDLE_WIDE)
  46.       {
  47.          this.width = Game.Objects.Paddle.PADDLE_SIZE_WIDE;
  48.       }
  49.       else if(item == Game.PongLevels.PADDLE_SMALL)
  50.       {
  51.          this.width = Game.Objects.Paddle.PADDLE_SIZE_SMALL;
  52.       }
  53.       else if(item == Game.PongLevels.FREEZE_YOU || item == Game.PongLevels.FREEZE_OPPONENT)
  54.       {
  55.          if(!this.frozen)
  56.          {
  57.             this.frozen = true;
  58.          }
  59.       }
  60.       else if(item == Game.PongLevels.PADDLE_STICKY)
  61.       {
  62.          if(!this.sticky)
  63.          {
  64.             this.width = Game.Objects.Paddle.PADDLE_SIZE_STICKY;
  65.             this.sticky = true;
  66.          }
  67.       }
  68.       else if(item == Game.PongLevels.MISSILE)
  69.       {
  70.          if(!this.blast)
  71.          {
  72.             this.blast = true;
  73.          }
  74.       }
  75.    }
  76.    function deactivateItem()
  77.    {
  78.       if(this.sticky)
  79.       {
  80.          this.sticky = false;
  81.       }
  82.       else if(this.blast)
  83.       {
  84.          this.blast = false;
  85.       }
  86.       else if(this.frozen)
  87.       {
  88.          this.frozen = false;
  89.       }
  90.       else
  91.       {
  92.          this.width = Game.Objects.Paddle.PADDLE_SIZE_DEFAULT;
  93.       }
  94.    }
  95.    function set width(newWidth)
  96.    {
  97.       newWidth = Math.max(0,newWidth);
  98.       newWidth = Math.min(Game.Objects.Paddle.sizes.length - 1,newWidth);
  99.       this.__assetFrame = newWidth + 1;
  100.       this.__width = Game.Objects.Paddle.sizes[newWidth];
  101.       this.__halfWidth = Math.floor(this.__width / 2);
  102.       this.__widthChanged = true;
  103.       if(this.y - this.__halfWidth < this.bounds.yMin)
  104.       {
  105.          this.moveTo(this.x,this.bounds.yMin + this.__halfWidth);
  106.       }
  107.       else if(this.bounds.yMax < this.y + this.__halfWidth)
  108.       {
  109.          this.moveTo(this.x,this.bounds.yMax - this.__halfWidth);
  110.       }
  111.       else
  112.       {
  113.          this.queueForDisplay();
  114.       }
  115.       this.updateBorders();
  116.       this.calcEdges();
  117.    }
  118.    function get width()
  119.    {
  120.       return this.__width;
  121.    }
  122.    function set orientation(newDir)
  123.    {
  124.       if(newDir == undefined)
  125.       {
  126.          return;
  127.       }
  128.       newDir = newDir.toLowerCase();
  129.       if(newDir == this.__orientation)
  130.       {
  131.          return;
  132.       }
  133.       if(newDir != Engine.PongObject.LEFT_DIR && newDir != Engine.PongObject.RIGHT_DIR)
  134.       {
  135.          return;
  136.       }
  137.       if(newDir == Engine.PongObject.LEFT_DIR)
  138.       {
  139.          this.setAssetID(Game.Objects.Paddle.assetLeftID);
  140.       }
  141.       else
  142.       {
  143.          this.setAssetID(Game.Objects.Paddle.assetRightID);
  144.       }
  145.       this.__orientation = newDir;
  146.       this.__orientationChanged = true;
  147.       this.queueForDisplay();
  148.    }
  149.    function get orientation()
  150.    {
  151.       return this.__orientation;
  152.    }
  153.    function set frozen(newFrozen)
  154.    {
  155.       this.__frozen = newFrozen;
  156.       if(this.__frozen)
  157.       {
  158.          this.width = Game.Objects.Paddle.PADDLE_SIZE_FROZEN;
  159.       }
  160.       else
  161.       {
  162.          this.width = Game.Objects.Paddle.PADDLE_SIZE_DEFAULT;
  163.       }
  164.    }
  165.    function get frozen()
  166.    {
  167.       return this.__frozen;
  168.    }
  169.    function set blast(newBlast)
  170.    {
  171.       this.__blast = newBlast;
  172.       if(this.__blast)
  173.       {
  174.          this.width = Game.Objects.Paddle.PADDLE_SIZE_MISSILE;
  175.       }
  176.       else
  177.       {
  178.          this.width = Game.Objects.Paddle.PADDLE_SIZE_DEFAULT;
  179.       }
  180.    }
  181.    function get blast()
  182.    {
  183.       return this.__blast;
  184.    }
  185.    function set sticky(newSticky)
  186.    {
  187.       this.__sticky = newSticky;
  188.       if(!this.__sticky)
  189.       {
  190.          if(this.__myBall != undefined)
  191.          {
  192.             this.releaseBall();
  193.          }
  194.          this.width = Game.Objects.Paddle.PADDLE_SIZE_DEFAULT;
  195.       }
  196.    }
  197.    function get sticky()
  198.    {
  199.       return this.__sticky;
  200.    }
  201.    function holdBall(myBall)
  202.    {
  203.       if(this.__myBall == myBall)
  204.       {
  205.          return undefined;
  206.       }
  207.       this.__myBall = myBall;
  208.       this.__myBall.stickTo(this);
  209.       if(this.__myBall != undefined)
  210.       {
  211.          this.__sticky = true;
  212.       }
  213.       if(this.blast)
  214.       {
  215.          myBall.blast = true;
  216.       }
  217.       this.dispatchEvent({type:"onHoldBall"});
  218.    }
  219.    function releaseBall()
  220.    {
  221.       if(!this.__myBall)
  222.       {
  223.          return undefined;
  224.       }
  225.       this.__myBall.stuck = false;
  226.       this.__myBall = null;
  227.       this.sticky = false;
  228.       this.dispatchEvent({type:"onReleaseBall"});
  229.    }
  230.    function moveByPlayer(dy, myBall)
  231.    {
  232.       if(this.frozen || dy == 0)
  233.       {
  234.          return undefined;
  235.       }
  236.       var _loc3_ = this.getSpaceAvailable(myBall);
  237.       if(_loc3_ == null)
  238.       {
  239.          return undefined;
  240.       }
  241.       if(dy < 0)
  242.       {
  243.          dy = Math.max(dy,_loc3_.top);
  244.       }
  245.       else
  246.       {
  247.          dy = Math.min(dy,_loc3_.bottom);
  248.       }
  249.       this.moveBy(0,dy,0);
  250.       this.updateBorders();
  251.    }
  252.    function ballInHitArea(myBall)
  253.    {
  254.       return this.left < myBall.right && myBall.left < this.right;
  255.    }
  256.    function ballAbovePaddle(myBall)
  257.    {
  258.       return this.top >= myBall.bottom;
  259.    }
  260.    function ballBelowPaddle(myBall)
  261.    {
  262.       return myBall.top >= this.bottom;
  263.    }
  264.    function getSpaceAvailable(myBall)
  265.    {
  266.       var _loc3_ = this.bounds.yMin - this.top - this.__topBottomOffset;
  267.       var _loc2_ = this.bounds.yMax - this.bottom + this.__topBottomOffset;
  268.       if(this.ballInHitArea(myBall))
  269.       {
  270.          if(myBall.y < this.y)
  271.          {
  272.             _loc3_ = myBall.bottom - this.top;
  273.          }
  274.          else
  275.          {
  276.             _loc2_ = myBall.top - this.bottom;
  277.          }
  278.       }
  279.       if(_loc3_ > 0)
  280.       {
  281.          _loc3_ = 0;
  282.       }
  283.       if(_loc2_ < 0)
  284.       {
  285.          _loc2_ = 0;
  286.       }
  287.       return {top:_loc3_,bottom:_loc2_};
  288.    }
  289.    function setBounds(newBounds)
  290.    {
  291.       this.bounds = newBounds;
  292.    }
  293.    function checkCollision(myBall)
  294.    {
  295.       var _loc4_ = Engine.Collision.sweepSphereToStaticShape(myBall,this);
  296.       if(_loc4_ == null)
  297.       {
  298.          return false;
  299.       }
  300.       var _loc7_ = undefined;
  301.       var _loc2_ = _loc4_.point;
  302.       if(_loc4_.edge == this.__frontEdge)
  303.       {
  304.          var _loc6_ = this.calcReflectionNormal(_loc2_.y);
  305.          if(myBall.direction.x > 0)
  306.          {
  307.             _loc7_ = true;
  308.             _loc6_ = 270 - _loc6_;
  309.             _loc2_.x -= 3;
  310.          }
  311.          else
  312.          {
  313.             _loc7_ = false;
  314.             _loc6_ += 90;
  315.             _loc2_.x += 3;
  316.          }
  317.          var _loc5_ = this.angleToVector(_loc6_);
  318.       }
  319.       else
  320.       {
  321.          _loc5_ = _loc4_.normal;
  322.          if(_loc4_.edge = this.edges[0])
  323.          {
  324.             _loc2_.y -= 3;
  325.             _loc2_.y = Math.max(this.bounds.yMin,_loc2_.y);
  326.          }
  327.          else if(_loc4_.edge = this.edges[2])
  328.          {
  329.             _loc2_.y += 3;
  330.             _loc2_.y = Math.min(this.bounds.yMax,_loc2_.y);
  331.          }
  332.       }
  333.       if(this.ballInHitArea(myBall))
  334.       {
  335.          if(this.ballAbovePaddle(myBall))
  336.          {
  337.             if(myBall.y < this.bounds.yMin + myBall.radius * 2.5)
  338.             {
  339.                if(this.__orientation == Engine.PongObject.LEFT_DIR)
  340.                {
  341.                   _loc2_.x = this.right + myBall.radius + 3;
  342.                   _loc5_ = new Vector(1,0,0);
  343.                }
  344.                else
  345.                {
  346.                   _loc2_.x = this.left - myBall.radius - 3;
  347.                   _loc5_ = new Vector(-1,0,0);
  348.                }
  349.             }
  350.          }
  351.          else if(this.ballBelowPaddle(myBall))
  352.          {
  353.             if(myBall.y > this.bounds.yMax - myBall.radius * 2.5)
  354.             {
  355.                if(this.__orientation == Engine.PongObject.LEFT_DIR)
  356.                {
  357.                   _loc2_.x = this.right + myBall.radius + 3;
  358.                   _loc5_ = new Vector(1,0,0);
  359.                }
  360.                else
  361.                {
  362.                   _loc2_.x = this.left - myBall.radius - 3;
  363.                   _loc5_ = new Vector(-1,0,0);
  364.                }
  365.             }
  366.          }
  367.       }
  368.       myBall.deflect(_loc2_,_loc5_,_loc7_);
  369.       this.collide(myBall);
  370.       return true;
  371.    }
  372.    function drawCollision(point, normal, edge)
  373.    {
  374.       var _loc2_ = point.copy();
  375.       _loc2_.x -= this.x;
  376.       _loc2_.y -= this.y;
  377.       var _loc3_ = normal.copy();
  378.       _loc3_.length = 30;
  379.       this.guide.clear();
  380.       this.guide.lineStyle(1,16711680);
  381.       this.guide.moveTo(_loc2_.x,_loc2_.y);
  382.       this.guide.lineTo(_loc2_.x + _loc3_.x,_loc2_.y + _loc3_.y);
  383.       this.guide.lineStyle(1,65280);
  384.       this.guide.moveTo(edge.a.x,edge.a.y);
  385.       this.guide.lineTo(edge.b.x,edge.b.y);
  386.    }
  387.    function collide(myBall)
  388.    {
  389.       !this.blast && myBall.blast;
  390.       this.world.deactivate;
  391.       myBall.blast = false;
  392.       if(this.sticky || this.blast)
  393.       {
  394.          this.holdBall(myBall);
  395.       }
  396.       else
  397.       {
  398.          Engine.PongSFX.playPuckPaddle();
  399.       }
  400.    }
  401.    function checkItemCollision(o)
  402.    {
  403.       if(o.motionLeft <= this.right && this.left <= o.motionRight)
  404.       {
  405.          if(o.motionTop <= this.bottom && this.top <= o.motionBottom)
  406.          {
  407.             return true;
  408.          }
  409.       }
  410.       return false;
  411.    }
  412.    function onDisplay()
  413.    {
  414.       if(this.__widthChanged)
  415.       {
  416.          this.target.gotoAndStop(this.__assetFrame);
  417.          this.__widthChanged = false;
  418.          this.updateBorders();
  419.       }
  420.    }
  421.    function updateBorders()
  422.    {
  423.       this.top = this.y - this.__halfWidth - this.__topBottomOffset;
  424.       this.bottom = this.y + this.__halfWidth + this.__topBottomOffset;
  425.       if(this.__orientation == Engine.PongObject.LEFT_DIR)
  426.       {
  427.          this.left = this.x - this.__thickness;
  428.          this.right = this.x - 1;
  429.       }
  430.       else
  431.       {
  432.          this.left = this.x + 1;
  433.          this.right = this.x + this.__thickness;
  434.       }
  435.    }
  436.    function onAddToScene()
  437.    {
  438.       this.target.gotoAndStop(this.__assetFrame);
  439.       this.__widthChanged = false;
  440.       this.__thickness = this.target._width - 3;
  441.       this.updateBorders();
  442.       this.calcEdges();
  443.       this.__orientationChanged = false;
  444.    }
  445.    function calcReflectionNormal(collY)
  446.    {
  447.       var _loc2_ = collY - this.y;
  448.       var _loc3_ = _loc2_ / this.__halfWidth;
  449.       var _loc4_ = _loc3_ * Game.Objects.Paddle.MAX_TILT;
  450.       return _loc4_;
  451.    }
  452.    function calcEdges()
  453.    {
  454.       if(this.__orientation == Engine.PongObject.LEFT_DIR)
  455.       {
  456.          var _loc6_ = new _types_.ShapeVertex(- this.__thickness,- this.__halfWidth - this.__topBottomOffset);
  457.          var _loc5_ = new _types_.ShapeVertex(-1,- this.__halfWidth - this.__topBottomOffset);
  458.          var _loc3_ = new _types_.ShapeVertex(-1,this.__halfWidth + this.__topBottomOffset);
  459.          var _loc4_ = new _types_.ShapeVertex(- this.__thickness,this.__halfWidth + this.__topBottomOffset);
  460.       }
  461.       else
  462.       {
  463.          _loc6_ = new _types_.ShapeVertex(1,- this.__halfWidth - this.__topBottomOffset);
  464.          _loc5_ = new _types_.ShapeVertex(this.__thickness,- this.__halfWidth - this.__topBottomOffset);
  465.          _loc3_ = new _types_.ShapeVertex(this.__thickness,this.__halfWidth + this.__topBottomOffset);
  466.          _loc4_ = new _types_.ShapeVertex(1,this.__halfWidth + this.__topBottomOffset);
  467.       }
  468.       this.createEdges(_loc6_,_loc5_,_loc3_,_loc4_);
  469.       if(this.__orientation == Engine.PongObject.LEFT_DIR)
  470.       {
  471.          this.__frontEdge = this.edges[1];
  472.       }
  473.       else
  474.       {
  475.          this.__frontEdge = this.edges[3];
  476.       }
  477.       var _loc2_ = this.edges.length;
  478.       while((_loc2_ = _loc2_ - 1) > -1)
  479.       {
  480.          this.edges[_loc2_].props = Game.Objects.CollisionObject.EDGE_COLLISION;
  481.       }
  482.    }
  483. }
  484.