import java.applet.*; 
import java.awt.*; 
import java.awt.image.*; 
import java.awt.event.*; 
import java.io.*; 
import java.net.*; 
import java.text.*; 
import java.util.*; 
import java.util.zip.*; 

public class udk_final_baliko extends BApplet {

int num=17;
int x[];
int y[];
int p=1;
int i;
int ii;
boolean init=true;
boolean finish =true;
boolean rot;
int xmax;
int ymax;
int xmin;
int ymin;
int xmove;
int ymove;
///------->bug
boolean forward=true;
boolean back;
float dis;
float vx, vy;

float pAx;
float pAy;
float pBx;
float pBy;

float xdis;
float ydis;

float speed = 1.0f;

float bugx=0;
float bugy=0;

int By=20;
float rotb=1;
boolean le;
boolean fel;
float rota;
float f;
int ib=1;
//boolean turned=true;
///<----------bug
//------------>stars
int starNum=30;
float starx[];
float stary[];
float siz[];
int stari;
boolean init2=true;
///<-------stars
boolean turn;
float starrot;
int starmovex=12000;
int starmovey=12000;


void setup()
{
framerate(30);
size(400,300);
background(0);
stroke(255);
ellipseMode(CENTER_DIAMETER);
//framerate(10);
x= new int[num];
y= new int[num];
//fontA = loadFont("Meta.vlw.gz"); ask marius

starx = new float [starNum];
stary = new float [starNum];
siz = new float [starNum];


}

void loop()
{
 
push();
translate(width/2, height/2);  

        if(init2){
            for(stari=0;stari<=28;stari++){
                starx[stari+1]=random(width)-width/2;
                stary[stari+1]=random(height)-height/2;
                siz[stari+1]=random(4)/10;
                }
            if(stari>=19) init2=false;
            }else{          
            push();           
            //move and draw the stars
            starrot-=constrain((width/2-mouseX)/2, -1, 1);
            rotate(radians(starrot/4));         
            starmovex+=0.02*(width/2-mouseX);            
            starmovey+=0.02*(height/2-mouseY);

            
            if(starmovey<1200) starmovey=12000;
            if(starmovex<1200) starmovex=12000;
            
            
             for(stari=1;stari<=28;stari++){
             
                ellipse(((starx[stari]+starmovex)%400)-200, ((stary[stari]+starmovey)%300)-150, siz[stari], siz[stari]);                
                if(stari<10){
                stroke(50);
                }else{
                stroke(255);
                }
                }
             ////stars end
             pop();
            }
     
if(rot){

//rotation whole path and bug
ii+=constrain((width/2-mouseX)/2, -20, 20);
rotate(radians(ii/4));

///move the path to the center
xmax= -111111;
xmin= 111111;
ymax= -111111;
ymin= 111111;
for(int iii=1;iii!=num-1;iii++){
if(x[iii]>xmax) xmax=x[iii];
if(x[iii]<xmin) xmin=x[iii];
if(y[iii]>ymax) ymax=y[iii];
if(y[iii]<ymin) ymin=y[iii];
}

if(xmove<0-((xmax+xmin)/2)) xmove+=1.8;
if(xmove>0-((xmax+xmin)/2)) xmove-=1.8;

if(ymove<0-((ymax+ymin)/2)) ymove+=1.8;
if(ymove>0-((ymax+ymin)/2)) ymove-=1.8;

translate (xmove, ymove);
//-------------------------------------------------------->bug

         ///find current line for the bug
        if(ib==num-2) {
        forward=false;
        back=true;
        
        }
        if(ib==1) {
        forward=true;
        back=false;
        }
        
        //if((width/2-mouseX<2)&&(width/2-mouseX>-2)) turn=true;
        //if((width/2-mouseX<-2)&&(turn)){
              //if(back){
                //ib=ib-1;
                //bugy=pBy-bugy;
                //bugx=pBx-bugx;
                //back=false;
                //forward=true;
                //turn=false;
                ///}
              //if(forward){
                ///ib=ib+1;
                //bugy=pAy-bugy;
                //bugx=pAx-bugx;
                //back=true;
                //forward=false;
                //turn=false;
                //}                
            //}
         //if((width/2-mouseX>2)&&(turn)){
              //if(back){
                //ib=ib+1;
                //bugy=pAy-bugy;
                //bugx=pAx-bugx;
                //back=false;
                //forward=true;
                //turn=false;
                //}
              //if(forward){
                //bugy=pAy-bugy;
                //bugx=pAx-bugx;
                //ib=ib-1;
                //back=true;
                //forward=false;
                //turn=false;
                //}                
            //}
          
        
        
        
        if((forward)&&(ib<num-2)){
            if(abs(bugx)>abs(xdis)){
            ib++;
            bugy=0;
            bugx=0;
            }
            pAx=x[ib];
            pAy=y[ib];
            pBx=x[ib+1];
            pBy=y[ib+1];
            }
            
            
         if((back)&&(ib>1)){
            if(abs(bugx)>abs(xdis)){
            ib--;
            bugy=0;
            bugx=0;                                
            }
            if(ib==1){
                pAx=x[ib];
                pAy=y[ib];
                pBx=x[ib+1];
                pBy=y[ib+1];
                }else{
                pAx=x[ib];
                pAy=y[ib];
                pBx=x[ib-1];
                pBy=y[ib-1];
                }            
            }
        
        ///move the bug along current line
        push();
        translate(x[ib], y[ib]);

        xdis=abs(pAx-pBx);
        ydis=abs(pAy-pBy);

	dis=sqrt ( xdis*xdis + ydis*ydis );
	vx=(pBx-pAx)/dis;
	vy=(pBy-pAy)/dis;
	bugx+=vx*speed;
	bugy+=vy*speed;

        translate(bugx, bugy);
        

       /////rotate the bug to the line
       f = atan2(pBy-pAy, pBx-pAx);
       rotate(f);

       //// move up and down the bug
       translate(0,rota-23);
       rota= 150*rotb/250;

      /////the bug and its legs        
      for(int a=0; a<2; a++){
            rotate(radians(45*rotb/12));
            line(0,0, 0, By);
            rotate(radians(-135*rotb/12));
            }

     if((rotb<10)&&(fel)){
           rotb+=1.1;
           }

     if((rotb>3)&&(le)){
           rotb-=1.1;
           }

     if(rotb>=10){
           fel=false;
           le=true;
           }
     if(rotb<=3){
           le=false;
           fel=true;
           }
     ellipse(0, 0, 10, 10);
    
      pop();
//<-----------------------------------------------------------bug end

}else{
//rotation whole
ii=0;
}

///the path
if(p>=1){
	for(i=0; i!=p-1; i++) {
              line(x[i], y[i], x[i+1], y[i+1]);
                             }
        }
        
        
        pop();
        rect (5, 5, 15, 15);


//setFont(fontA, 15); ask marius
//text("clear", 5,20);
}


void mousePressed()
{
if(init){

xmove=0;
ymove=0;
x[0]=-(width/2-mouseX);
y[0]=-(height/2-mouseY);
init=false;
            bugy=0;
            bugx=0;
            ib=1;

}

if(rot==false){
    if((mouseY<=20)&&(mouseX<=20)){    
      p=num-1;
      init=true;
      }else{
      x[p]=-(width/2-mouseX);
      y[p]=-(height/2-mouseY); 
      }

}

if(((mouseY<=20)&&(mouseX<=20))||(p<num-1)){
    p++;
    if(p==num) {
      init=true;
      p=1;
}
}

if(p==num-1) rot=true;
if(p==1) rot=false;
}

}
