#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#include "point.hpp"

// ------------------------------------------------------ CLASS Point

Point::Point(int initX, int initY, int initCol)
{
  x = initX;
  y = initY;
  visible = false;
  color = initCol;
}

Point::Point()
{
  delay(100);
  x = y = 0;
}

Point::~Point()
{
  if (visible)
    Hide();
}

void Point::Show(int col)
{
  if (col != -1) // F„rgbyte?
    color = col;
  setcolor(color);
  setfillstyle(SOLID_FILL, color);
  fillellipse(x, y, 2, 2);
  visible = true;
}

void Point::Hide(void)
{
  if (visible) {
    int col = getbkcolor();
    setcolor(col);
    setfillstyle(SOLID_FILL, col);
    fillellipse(x, y, 2, 2);
    visible = false;
  }
}

void Point::MoveTo(int new_x, int new_y)
{
  if (visible)
    Hide();       // make current point invisible
  x = new_x;      // change X and Y coordinates to new location
  y = new_y;
  Show(color);         // show point at new location
}

// ------------------------------------------------------- CLASS Target

Target::Target(int InitValue, int InitColor, int InitSize)
{
  value = InitValue;
  color = InitColor;
  size = InitSize;
  visible = false;
}

void Target::Show(void)
{
  setfillstyle(SOLID_FILL, color);
  bar(1+5*(x_pos), 1+5*(y_pos),
      3+5*(x_pos+size-1), 3+5*(y_pos+size-1));
  visible = true;
}

void Target::Hide(void)
{
  if (visible) {
    setfillstyle(SOLID_FILL, BLACK);
    bar(1+5*(x_pos), 1+5*(y_pos), 
        3+5*(x_pos+size-1), 3+5*(y_pos+size-1));
    visible = false;
  }
}

Boolean occupied(int x, int y, int size)
{
  int i, j;
  for (i=0; i<size; i++)
    for (j=0; j<size; j++)
      if (getpixel(2 + 5*(x+i), 2 + 5*(y+j)) != BLACK) return true;

  return false;
}

void Target::RandomPlace(void)
{
  if (visible) return;
  do {
    x_pos = 1 + random(127-size);
    y_pos = 3 + random(91-size);
  } while (occupied(x_pos, y_pos, size));
  Show();
}

Boolean Target::Hit(int x, int y)
{
  if (x >= x_pos && y >= y_pos &&
      x <= x_pos+size && y <= y_pos+size)
    return true;
  else
    return false;
}
