Code viewer for Mind: Pong Mind

function resetBall()
{
    // puts the ball back into the center of the game
    ball.position.x = 0;
    ball.position.y = 0;
    
    //The ball goes a towards a random direction
    if(step % 2 == 0)
    {
        ballXDirection = -1;
        ballYDirection = -1;
	}
	else
	{
		ballXDirection = 1;
		ballYDirection = 1;
	}
}

function Mind() { 


//--- public functions / interface / API ----------------------------------------------------------


    this.newRun = function()
    {
    };

    this.endRun = function()
    {
    };



    this.getAction = function ( x )     
    {
        // update ball position
        ball.position.x += ballXDirection * ballSpeed;
        ball.position.y += ballYDirection * ballSpeed;
        
        // update paddle position 
        paddleOpponent.position.y += pad2YDirection * paddleSpeed;
        

        // if ball touches the side of the table
        if (ball.position.y <= -tableHeight/2)
        {
            ballYDirection = -ballYDirection;
        }   
        if (ball.position.y >= tableHeight/2)
        {
            ballYDirection = -ballYDirection;
        }

        // PLAYER paddlePlayer
        //ball touches the paddle and to bounce back
        if ((ball.position.x <= paddlePlayer.position.x + paddleWidth &&  ball.position.x >= paddlePlayer.position.x) && (ball.position.y <= paddlePlayer.position.y + paddleHeight/2 &&  ball.position.y >= paddlePlayer.position.y - paddleHeight/2) && (ballXDirection < 0))
        {
            // the ball goes the other direction
            ballXDirection = -ballXDirection;
            ballYDirection -= pad1YDirection * 0.7;
        }
    
        // OPPONENT paddleOpponent - ball touches the paddle and to bounce back
        if ((ball.position.x <= paddleOpponent.position.x + paddleWidth &&  ball.position.x >= paddleOpponent.position.x) && (ball.position.y <= paddleOpponent.position.y + paddleHeight/2 &&  ball.position.y >= paddleOpponent.position.y - paddleHeight/2) && (ballXDirection > 0)) 
        {
            ballXDirection = -ballXDirection;
            ballYDirection -= pad2YDirection * 0.7;
        }
        
        //Paddle AI based on the linear interpolation function
        //move up and down according to the ball
        pad2YDirection = (ball.position.y - paddleOpponent.position.y) * 0.1;
        if (Math.abs(pad2YDirection) <= paddleSpeed) //get the absolute value and move accordingly
        {   
            paddleOpponent.position.y += pad2YDirection;
        }
        else
        {
            //Direction of the paddle movement (up or down)
            if (pad2YDirection > paddleSpeed)
            {
                paddleOpponent.position.y += paddleSpeed;
            }
            else if (pad2YDirection < -paddleSpeed)
            {
                paddleOpponent.position.y -= paddleSpeed;
            }
        }
        
        //Ball speed in the y direction kept on increasing so 
        //this code keeps a constant speed (y speed = 2* x speed) so the
        //ball speed when hit off the edge of the table is only twice the speed of
        //the ball speed to give it a bouncing effect
        if (ballYDirection > ballSpeed * 2)
        {
            ballYDirection = ballSpeed * 2;
        }
        else if (ballYDirection < -ballSpeed * 2)
        {
            ballYDirection = -ballSpeed * 2;
        }
        
        // if the ball touches the left hand side of the board
        if (ball.position.x <= -tableWidth/2)
        {   
            scoreOpponent++; //opponent score is increased
            // reset the ball
            resetBall();
        }
        
        // if the ball touches the right hand side of the board
        if (ball.position.x >= tableWidth/2)
        {   
            // player score is increased
            scorePlayer++;
            // reset the ball
            resetBall();
        }

    };
}