/* PinguJump uja 060627
3.7.06: Windrichtung ändert sich nur schrittweise

 ok: Konqueror, Opera, Mozilla 1.7
 noch ok: Firefox 1.5, IE6

 aus Performancegründen gestrichen:
 1. Auftrennung Felsen/Himmel
 2. Anisequenz, um Pingis erscheinen zu lassen (Schlinder-Modus)
 3. Pingis mit Welle davonschwimmen zu lassen
 4. Cleanup der Blutflecken zerschmetterter Pingis
 5. Punktedarstellung in Welle
 6. irgendwelcher Sound (Juchuh!, Quäks, Platsch)
 ---------------------------------------------------------------- */
zeit0=150;
maxpingis=5;
baseline=416;
wellenlaenge=384;
wellenstart=-wellenlaenge;

ima=new Array();
ima[0]=new Image(); ima[0].src='pingu_0.gif';
ima[1]=new Image(); ima[1].src='pingu_tot.gif';
ima[2]=new Image(); ima[2].src='pingu_ro.gif';
ima[3]=new Image(); ima[3].src='pingu_ge.gif';
ima[4]=new Image(); ima[4].src='pingu_cy.gif';
ima[5]=new Image(); ima[5].src='pingu_bl.gif';
ima[6]=new Image(); ima[6].src='pingu.gif';


pingu=new Array();
koo=new Array();
i=0; koo[i]=new point(256,32);
i++; koo[i]=new point(320,32);
i++; koo[i]=new point(400,32);
i++; koo[i]=new point(208,180);
i++; koo[i]=new point(304,144);
i++; koo[i]=new point(464,124);
i++; koo[i]=new point(400,240);
i++; koo[i]=new point(120,200);

welle=new sprite(0,wellenstart,336,6,2,0,1024,96,0);
for (i=1; i<=maxpingis; i++) pingu[i]=new sprite(i,0,0,3,0,0,32,48);
var npingis,zeit,punkte,timrec,timecount,kptr,seitenwind,timetic=50;

function point(x,y) { this.x=x; this.y=y; this.pingi=0; }

function sprite(id,x,y,z,vx,vy,breite,hoehe)
{ this.id=id;
  this.breite=breite;
  this.hoehe=hoehe;
  this.lay='l_'+id;
  this.x=x;
  this.y=y;
  this.z=z;
  this.vx=vx;
  this.vy=vy;
  this.aktiv=false;
  this.phase=0;
  this.bild='i'+id;
  this.vmax=2*Math.floor(id/2)+4;
}

function neu()
{ var i;
  if (timrec) clearTimeout(timrec);
  for (i=1; i<=maxpingis; i++) with (pingu[i]) { x=-breite; phase=1; aktiv=false; document.getElementById(lay).style.left=x; }
  for (i=0; i<koo.length; i++) koo[i].pingi=0;
  punkte=0;
  zeit=zeit0;
  timecount=0;
  seitenwind=0;
  kptr=0;
  npingis=maxpingis;
  document.form1.punkte.value=punkte;
  document.form1.wind.value=seitenwind;
  document.form1.pingis.value=npingis;
  neuer_pingu();
  move_it();
}

function neuer_pingu()
{ var i,nr=0;
  for (i=1; i<=maxpingis; i++) with(pingu[i]) if (!aktiv) if (phase>0) nr=i;
  if (nr>0) with (pingu[nr]) 
  { kptr=Math.floor(koo.length*Math.random());
    x=koo[kptr].x+Math.floor(8*Math.random())-4; y=koo[kptr].y; phase=0; 
    document.getElementById(lay).style.top=y;
    document.getElementById(lay).style.left=x;
    document.images[bild].src=ima[id+1].src;
  }
}

function move_it() // aller auf 1 Time umgeschrieben
{ if (zeit>0)
  { with (welle) { x=x+vx; if (x==0) x=x-wellenlaenge; document.getElementById(lay).style.left=x; }
    for (i=1; i<=maxpingis; i++) with (pingu[i]) if (aktiv)
    { phase++;
      flug(i);
      x=x+seitenwind; y=y+vy;
      if (y>(baseline-10)) crash(i); else if (y>baseline-welle.hoehe) check_welle(i);
      document.getElementById(lay).style.top=y;
      document.getElementById(lay).style.left=x;
    }
    timecount++;
    if (timetic==50)
    { if (timecount%100==0) { timecount=0; windri(); }
      if (timecount%40==0) neuer_pingu();
      if (timecount%20==0) { zeit--; document.form1.zeit.value=zeit; }
    }
    else // if (timetic==35)
   { if (timecount%143==0) { timecount=0; windri(); }
      if (timecount%57==0) neuer_pingu();
      if (timecount%29==0) { zeit--; document.form1.zeit.value=zeit; }
    }
    timrec=setTimeout('move_it()',timetic);
  }
  else gameover();
}

function jump(nr) { with (pingu[nr]) if (!aktiv) if (phase>=0) { phase=0; aktiv=true; vy=-4; } }
function flug(nr) { with (pingu[nr]) { if (phase==4) document.images[bild].src=ima[0].src; vy++; if (vy>vmax) vy=vmax; } }

function crash(nr) 
{ with(pingu[nr]) { phase=-1; y=baseline-12; aktiv=false; document.images[bild].src=ima[1].src; }
  npingis--; document.form1.pingis.value=npingis; if (npingis<1) zeit=0;
}

function check_welle(nr) // crash mit y=a0*sin(x)-a0-pingu.hoehe+baseline, a0=welle.hoehe/2
{ var phase,ampli=welle.hoehe/2,wy;
  with (pingu[nr])
  { phase=2*Math.PI*((x+breite/2-welle.x)%wellenlaenge)/wellenlaenge;
    wy=Math.round(ampli-Math.round(ampli*Math.cos(phase)));
    if (y>=baseline-wy) 
    { punkte=punkte+wy; document.form1.punkte.value=punkte; aktiv=false;
      x=-breite;
    }
  }
}

function windri()
{ if (Math.random()<0.5) { seitenwind++; if (seitenwind>2) seitenwind=2; } else { seitenwind--; if (seitenwind<-2) seitenwind=-2; }
  switch(seitenwind+2)
  { case 0: t='<-- O2';break;
    case 1: t=' <- O1';break;
    case 2: t='Flaute';break;
    case 3: t='W1 -> ';break;
    default: t='W2 -->';break;
  }
  document.form1.wind.value=t;
}

function change_tic(watt)
{ switch(watt)
  { case 1:  timetic=35; break;
    default: timetic=50; break;
  }
}


