package pl.helion.j2mepp.graplatformowa;

import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.Image;
public class ObiektRuchomy extends Sprite
{
 public ObiektRuchomy(Image _obraz, TiledLayer _tlo, int _nr_planszy_x, int _nr_planszy_y)
 {
  super(_obraz, _obraz.getWidth(), _obraz.getHeight());
  tlo = _tlo;
  nr_planszy_x = _nr_planszy_x;
  nr_planszy_y = _nr_planszy_y;
  this.defineReferencePixel(this.getWidth()/2, this.getHeight()/2);
 }
 public boolean leci()
 {
  return lot;
 }
 public byte przesunLewo(int x)
 {
  if (this.getX()-x>=(nr_planszy_x-1)*Plotno.SZEROKOSC || nr_planszy_x==1)
  {
   if (this.getX()-x>0)
   {
    this.move(-x,0);
    this.setTransform(Sprite.TRANS_MIRROR);
    if (this.collidesWith(tlo,true))
    {
     this.move(x,0);
     return 0;
    } else return 1;
   } else return 0;
  } else
  {
   nr_planszy_x -= 1;
   return 2;
  }
 }
 public byte przesunGora(int y)
 {
  if (this.getY()-y>=(nr_planszy_y-1)*Plotno.WYSOKOSC)
  {
   if (this.getY()-y>0)
   {
    this.move(0,-y);
    if (this.collidesWith(tlo,true))
    {
     this.move(0,y);
     return 0;
    } else return 1;
   } else return 0;
  } else
  {
   nr_planszy_y -= 1;
   this.move(0,-y);
   if (this.collidesWith(tlo,true))
   {
     this.move(0,y);
     return 0;
   }
   return 2;
  }
 }
 public byte przesunDol(int y)
 {
  if (this.getY()+y<=(nr_planszy_y*Plotno.WYSOKOSC-this.getHeight()))
  {
   if (this.getY()+y<tlo.getHeight()-this.getHeight())
   {
    this.move(0,y);
    if (this.collidesWith(tlo,true))
    {
     this.move(0,-y);
     return 0;
    } else return 1;
   } else return 0;
  } else
  {
   nr_planszy_y += 1;
   return 2;
  }
 }
 public byte przesunPrawo(int x)
 {
  if (this.getX()+x<=nr_planszy_x*Plotno.SZEROKOSC-this.getWidth())
  {
   if (this.getX()+x<tlo.getWidth()-this.getWidth())
   {
    this.move(x,0);
    this.setTransform(Sprite.TRANS_NONE);
    if (this.collidesWith(tlo,true))
    {
     this.move(-x,0);
     return 0;
    } else return 1;
   } else return 0;
  } 
  else 
  {
   nr_planszy_x += 1;
   return 2;
  }
 }
 public void podskocz()
 {
  if ((energia == 0)&&(!this.collidesWith(tlo,true)))
  {
   lot = true;
   energia = Plotno.ENERGIA;
  }
 }
 public void spadek()
 {
  if (energia == 0)
  {
   if (this.getY()<tlo.getHeight()-this.getHeight())
   {
    lot = true;
    if (przesunDol(Plotno.DYSTANS)==0)
     lot = false;
   } else lot = false;
  } else
  {
   if (przesunGora(Plotno.DYSTANS)>0)
    energia -= 1; 
   else
    energia = 0;
  }
 }
 public int getNrPlanszyX()
 {
  return nr_planszy_x;
 }
 public int getNrPlanszyY()
 {
  return nr_planszy_y;
 }
 public boolean getKierunek()
 {
  return kierunek;
 }
 public void setKierunek(boolean wartosc)
 {
  kierunek = wartosc;
 }
 protected TiledLayer tlo;
 protected boolean lot = false;
 protected boolean kierunek = true;
 protected int energia = 0;
 protected int nr_planszy_x ;
 protected int nr_planszy_y ;
}