#include "ball.h"
#include "ansi.h"
#include <iostream>
#include <cstdlib>

using namespace std;

//constructor
Ball::Ball() {

}

//bounce functions
void
Ball::bouncePaddle(int y) {
 dy += (-y) * 0.5;
 dx = -dx;
 if(dx<0)
   dx -= 0.5;
 else
   dx += 0.5;
}


void
Ball::bounceWall() {
 dy = -dy;
}

//go back to the center
void
Ball::reset() {

 //position in the center
 x = 40;
 y = 12;

 //get a random speed
 dy = 0;
 dx = 2 * (rand() % 7 <3) ? -1 : 1;
}

//standard element stuff
void
Ball::draw() {
 cout << whiteBackground;
 cout << cursorPosition((int) x, (int) y) << "  "
      << cursorPosition((int) x, (int) y+1) << "  ";
 cout << normal;
 cout.flush();
}


void
Ball::erase() {
 cout << normal;
 cout << cursorPosition((int) x, (int) y) << "  "
      << cursorPosition((int) x, (int) y+1) << "  ";
 cout.flush();
}


void
Ball::update() {
 if(dx > 5.0)
   dx = 5.0;
 if(dy > 5.0)
   dy=5.0;

 //updates position based on the speed
 x += dx;
 y += dy;
}


int
Ball::getX() {
 return (int) x;
}


int
Ball::getY() {
 return (int) y;
}