#include <avr/io.h>
#include <avr/delay.h>
#include "stdlib.h"

#define F_OSC 16000000

short int viewport_top = 0;
short int viewport_left = 0;
short int viewport_right = 127;
short int viewport_bottom = 63;

#include "ks0108.h"
#include "avrlcd.h"
#include "avrlcd_fonts.h"
//#include "avrlcd_ctrls.h"
#include "font.h"

  int counter = 0;
  int xmet = 0;
  float ymet = 0;
  float zrych = 0;
  int xlod = 30;
  int xlodpom = 0;
  int strilet = 0;
  int xstr = 0;
  int ystr = 60;
  int flag = 1;
  int zivot = 4;
  int reset = 0;

void lod(void)
{
  rectangle(xlod-1,57,xlod+1,62,CL_BLACK);
  rectangle(xlod-3,60,xlod+3,62,CL_BLACK);
  ks0108SetDot(xlod-3, 59, CL_BLACK);
  ks0108SetDot(xlod+3, 59, CL_BLACK);
}

void meteor(void)
{
  if(ymet>57)
  {
    xmet = rand()/520;			//generovani nahodneho mista objevu meteoru
    ymet = 3;
  }
  else ymet=ymet+1+zrych;

  fillrect(xmet-3, ymet-3, xmet+3, ymet+3, CL_BLACK, CL_BLACK);
  circle(xmet, ymet, 4, CL_BLACK);
}

void strela(int x)			//x je atkualni umisteni lodi
{
  if(strilet == 1)			//reakce na zmacknuti tlacitka strilet
  {
    xstr = x;
	flag = 2;				//prehozeni do pozice, kdy se bude ignorovat poloha lodi
    if(ystr==2)				//pri dojeti na konec obrazovky reset strely
    {
      strilet = 0;
	  ystr = 60;
	  flag = 1;
    }
    else ystr--;

    rectangle(xstr-1, ystr-2, xstr+1, ystr+2, CL_BLACK);
  }
}

void zasah(void)
{
      if(abs(xstr-xmet) < 4 & abs(ystr-ymet) < 5 ) //pokud se potka strela a meteor
	  {
	    ymet = 100;
	    ystr = 60;
	    strilet = 0;
		flag = 1;			//opet se bude zapisovat aktualni poloha lodi
      }
}

void kontakt(void)
{
      if(abs(xlod-xmet) < 10 & abs(60-ymet) < 5 ) //pokud se potka lod a meteor
	  {
		flag = 3;
	    ymet = 100;
	    ystr = 60;
	    strilet = 0;
      }
}

void displej(void)
{
  ks0108ClearScreenright();
  draw_text_ex(72, 1, 55, 10, 0, 0, "Prulet", DT_NORMAL);
  draw_text_ex(72, 10, 55, 10, 0, 0, "meteory", DT_NORMAL);
  switch(zivot)
  {
  case 1:
    draw_text_ex(72, 40, 55, 10, 0, 0, "looser", DT_NORMAL);
	draw_text_ex(72, 50, 55, 10, 0, 0, "vystrel", DT_NORMAL);
	while(reset != 1)
	{
	  if(bit_is_set(PINB,6))
      {
        reset = 1;
		zivot = 4;
		strilet = 0;
		zrych = 0;
      }
	}
	reset = 0;
	ks0108ClearScreenright();
	draw_text_ex(72, 1, 55, 10, 0, 0, "Prulet", DT_NORMAL);
    draw_text_ex(72, 10, 55, 10, 0, 0, "meteory", DT_NORMAL);
	draw_text_ex(72, 40, 55, 10, 0, 0, "zivotu 3", DT_NORMAL);
  break;
  case 2:
    draw_text_ex(72, 40, 55, 10, 0, 0, "zivotu 1", DT_NORMAL);
  break;
  case 3:
    draw_text_ex(72, 40, 55, 10, 0, 0, "zivotu 2", DT_NORMAL);
  break;
  case 4:
    draw_text_ex(72, 40, 55, 10, 0, 0, "zivotu 3", DT_NORMAL);
  break;
  }
}


int main(void)
{
  DDRB = 0b11111111;
  PORTB = 0b00001111;  

  current_font=font13;

  ks0108ClearScreen();
  ks0108SetReset(1); 	// Reset nastavim na HIGH a tak musi i zustat
  _delay_ms(1000); // Cekam nez LCD "nabehne:"
  ks0108Init(NON_INVERTED); // Inicializace LCD

  line(70,0,70,63,1);	//rozdelovaci cara
  
  displej();


  xmet = rand()/520;

  while(1)
  {
    switch(flag)
    {
	case 1:
      lod();
  	  strela(xlod);
      meteor();
	  zasah();
	  kontakt();

	  xlodpom = xlod;
	break;
	case 2:
	  lod();	
  	  strela(xlodpom);
      meteor();
	  zasah();
	  kontakt();
	break;
	case 3:
	  zivot--;
	  displej();
	  flag = 1;	
	break;
    }

    _delay_ms(500);	//musi byt az za vsema objektama

	counter++;				//postupne zrychlovani meteoritu
	if(counter == 10)
	{
	  counter = 0;
	  zrych = zrych + 0.1;
	}
	
    ks0108ClearScreenleft();

    if(bit_is_set(PINB,7))
    {
      xlod--;
	  if(xlod < 2) xlod = 2;		//zabrana proti vyjeti z displeje
    }

    if(bit_is_set(PINB,6))
    {
	  if(strilet==1)
	  {
		ystr = 60;
		xlodpom = xlod;
	  }
      else strilet = 1;
    }

    if(bit_is_set(PINB,5))
    {
      xlod++;
	  if(xlod > 68) xlod = 68;		//zabrana proti vyjeti z displeje
    }

  }
  
}
