home *** CD-ROM | disk | FTP | other *** search
- class Game.Players.ComputerPlayer extends Engine.PongPlayer
- {
- var __paddle;
- var __ball;
- var __goal;
- var side;
- var __handicap = Game.PongLevels.DEFAULT_COMPUTER_HANDICAP;
- var __waitTime = 0;
- var className = "ComputerPlayer";
- function ComputerPlayer(myWorld, myPaddle, myBall, myGoal, mySide)
- {
- super(myWorld,myPaddle,myBall,myGoal,mySide);
- this.__speed = Engine.PongPlayer.SPEED - this.__handicap;
- }
- function onUpdate(elapsed)
- {
- var _loc5_ = elapsed * this.__speed;
- var _loc4_ = this.__paddle.bounds;
- var _loc6_ = this.__paddle.y;
- var _loc2_ = undefined;
- if(!this.__holdingBall)
- {
- _loc2_ = this.__ball.y;
- if(this.ballOnOtherSide())
- {
- _loc2_ = this.__ball.y;
- if(_loc2_ < this.__goal.top)
- {
- _loc2_ = this.__goal.top;
- }
- else if(_loc2_ > this.__goal.bottom)
- {
- _loc2_ = this.__goal.bottom;
- }
- }
- else if(this.ballBehindPaddle())
- {
- _loc2_ = _loc4_.vCenter;
- }
- else if(this.ballAbovePaddle())
- {
- if(this.ballInHitArea())
- {
- _loc2_ = _loc4_.yMin + this.__paddle.width / 2 + this.__ball.radius * 3;
- }
- else if(this.__ball.direction.y > 0)
- {
- }
- }
- else if(this.ballBelowPaddle())
- {
- if(this.ballInHitArea())
- {
- _loc2_ = _loc4_.yMax - this.__paddle.width / 2 - this.__ball.radius * 3;
- }
- else if(this.__ball.direction.y < 0)
- {
- }
- }
- }
- else
- {
- _loc2_ = _loc4_.vCenter;
- }
- var _loc3_ = _loc2_ - _loc6_;
- if(this.__holdingBall)
- {
- this.__waitTime += elapsed;
- if(Math.abs(_loc3_) <= 10 && this.__waitTime >= Engine.PongPlayer.FRAME_TIME * 5)
- {
- this.__paddle.releaseBall();
- delete this.__waitTime;
- }
- }
- if(_loc3_ < 0)
- {
- _loc3_ = - Math.min(- _loc3_,_loc5_);
- }
- else
- {
- _loc3_ = Math.min(_loc3_,_loc5_);
- }
- this.__paddle.moveByPlayer(_loc3_,this.__ball);
- }
- function onHoldBall(eventObj)
- {
- this.__waitTime = 0;
- this.__holdingBall = true;
- }
- function ballInHitArea()
- {
- return this.__paddle.ballInHitArea(this.__ball);
- }
- function ballAbovePaddle()
- {
- return this.__paddle.ballAbovePaddle(this.__ball);
- }
- function ballBelowPaddle()
- {
- return this.__paddle.ballBelowPaddle(this.__ball);
- }
- function ballBehindPaddle()
- {
- if(this.side == Engine.PongObject.RIGHT_DIR)
- {
- if(this.__paddle.right < this.__ball.left)
- {
- return true;
- }
- }
- else if(this.__ball.right < this.__paddle.left)
- {
- return true;
- }
- return false;
- }
- function ballOnOtherSide()
- {
- if(this.side == Engine.PongObject.RIGHT_DIR)
- {
- if(this.__ball.right < this.__paddle.bounds.hCenter)
- {
- return true;
- }
- }
- else if(this.__paddle.bounds.hCenter < this.__ball.left)
- {
- return true;
- }
- return false;
- }
- }
-