/*//////////
//
//  paddle.c 
//
//      AUTHOR: Russell Lowke
//   CSCIE-215: Assignment Four
//
//
// Description: Modular Paddle
//
//      Method: Modualar paddle.c called from pong.c
//
//              init_paddle - sets the paddle
//              up_paddle & down_paddle - move the paddle
//              draw_paddle displays the paddle
//              is_a_hit - tells if the paddle hits a ball
*/

#include        "paddle.h"

static struct paddle mPaddle;

/*
//  init_paddle
//
//     initialize paddle data structure & draw it.
*/
void init_paddle()
{
    mPaddle.top    = 6;
    mPaddle.size   = 6;
    mPaddle.symbol = '#';
    
    /* note: paddle.bottom set in draw_paddle */

    draw_paddle();
}

/*
//  up_paddle
//
//     check if there is space to move the paddle and if so, do.
*/
void up_paddle()
{
    // check bounds
    if ( mPaddle.top > getTopLimit()) {

        // move the paddle
        mPaddle.top -= 1;
        draw_paddle();
    }
}

/*
//  down_paddle
//
//     Check if there is space to move the paddle, and if so, do.
*/
void down_paddle()
{
    if ( mPaddle.bottom < getBottomLimit()) {

        // move the paddle
        mPaddle.top += 1;
        draw_paddle();
    }
}

/*
//  draw_paddle
//
//     draw the paddle, removing trail for old paddle position
*/
void draw_paddle()
{
    // establish paddle bottom
    mPaddle.bottom = mPaddle.top + mPaddle.size - 1;

    // draw the paddle
    vLine(getCourtRight(), 
          mPaddle.top, 
          mPaddle.bottom, 
          mPaddle.symbol);

    /* erase area above and below paddle */
    /* removing the trail made by the old paddle */
    if (mPaddle.top > getTopLimit()) {            /* as above */
         move(mPaddle.top - 1, getCourtRight());
         addch(' ');
    }
    if (mPaddle.bottom < getBottomLimit()) {       /* so below */
         move(mPaddle.bottom + 1, getCourtRight());
         addch(' ');
    }

    /* ensure cursor hidden */
    hideCursor();
}

/*
//  is_a_hit
//
//     Check if loc [x,y] is a hit on the paddle
//
//     args:  x         --  x componant being checked
//            y         --  y componant being checked
*/
int is_a_hit(int x, int y)
{
   if (x >= getRightLimit()  &&                 /* right of court */
       y >= mPaddle.top      &&                 /* top of paddle */
       y <= mPaddle.bottom)                     /* bottom of paddle */
       return YES;                              /* ...is a hit */
   else
       return NO;
}
