
//////////////////////
//Tunnel Vertices  //
//Code By Ayoros  //
///////////////////

function part4() {
    this.vbl = 0 ;
    this.initdone = 0 ;
    this.canvaspart;
var test_tunnel=1;
var date;    
var width = 600;
var height = 400;

var scale = height/3;

var rings = 96;
var ringsize = 48;

var zmax = 2.5;
var zstep = 2*zmax / rings;
var tstep = 2*Math.PI/ringsize;

var zshift = zstep;

var t;
var x_tunnel=[];
var y_tunnel=[];
var xt_tunnel=[];
var yt_tunnel=[];
var xtz_tunnel=[];
var ytz_tunnel=[];
var xz_tunnel=[];
var yz_tunnel=[];
var wx_tunnel;
var wy_tunnel;
var wxz_tunnel;
var wyz_tunnel;
var multiplier;
var multiplier2;
function rgb(r,g,b) { return "rgb("+ ~~r +","+ ~~g +","+ ~~b +")" }
function hsl(h,s,l) { return "hsl("+(~~h)+","+(150*s)+"%,"+(150*l)+"%)" }
var color_tunnel=0;

    var offsetx = 300;
    var offsety = 200;

function line_tunnel(dest) {
  var newDate = +new Date;
  var ticks = newDate - date;

    if (test_tunnel == 1) { rings = 72; }
    else { rings = 24 ; }

    zstep = 2*zmax / rings;

    if (test_tunnel == 1) { zshift-=zstep/3 * ticks / 32; }
    else { zshift-=zstep/3 * ticks / 96; }

  if (zshift < 0) 
  { 
    zshift += zstep;
  };
  
  var zr=rings;

  var ctx = dest.contex ;
  var oldcolor = ctx.fillStyle ;

  while ( zr-- )
  {     
    if (zr < 1) continue;
    var z = zr * zstep + zshift;
    var z2 = z- zstep;
    color_tunnel = hsl((date/40 + 20*z)%360,0.5,0.2); //rgb(32,32,64)
    // tunnel oldschool method 
    wx_tunnel = offsetx+ Math.sin(z+date/570) * 70;
    wy_tunnel = offsety+ Math.cos(z+date/510) * 70;
    wxz_tunnel = offsetx+ Math.sin(z2+date/570) * 70;
    wyz_tunnel = offsety+ Math.cos(z2+date/510) * 70;
    multiplier = scale / z;
    multiplier2 = scale / z2;    

    for (var r=0 ; r<ringsize ; r++)
    {
      t = r*tstep + Math.PI + Math.sin(date/2500); 
      var toto1 = Math.cos(t) * multiplier;
      var toto2 = Math.sin(t) * multiplier;
      var toto3 = Math.cos(t+tstep) * multiplier;
      var toto4 = Math.sin(t+tstep) * multiplier;
      var toto5 = Math.cos(t) * multiplier2;
      var toto6 = Math.sin(t) * multiplier2;
      var toto7 = Math.cos(t+tstep) * multiplier2;
      var toto8 = Math.sin(t+tstep) * multiplier2;
      
      x_tunnel[r]   = (wx_tunnel + toto1)  ;
      y_tunnel[r]   = (wy_tunnel + toto2)  ;

      xt_tunnel[r]  = (wx_tunnel + toto3)  ;
      yt_tunnel[r]  = (wy_tunnel + toto4)  ;
    
      xtz_tunnel[r] = (wxz_tunnel + toto5) ;
      ytz_tunnel[r] = (wyz_tunnel + toto6) ;

      xz_tunnel[r]  = (wxz_tunnel + toto7) ;      
      yz_tunnel[r]  = (wyz_tunnel + toto8) ;
    }

    if (test_tunnel==1){
        ctx.lineWidth=1;

		ctx.beginPath();
		ctx.strokeStyle='#c63eb4';
        
        for (var r=0 ; r<ringsize ; r++)
        {
            // dest.line(x_tunnel,y_tunnel,xt_tunnel,yt_tunnel,1,'#e65ed4');
            ctx.moveTo(x_tunnel[r],y_tunnel[r]);
            ctx.lineTo(xt_tunnel[r],yt_tunnel[r]);
        }
        ctx.closePath();
		ctx.stroke();

		ctx.beginPath();
		ctx.strokeStyle='#b6312e';
        for (var r=0 ; r<ringsize ; r++)
        {
            //dest.line(x_tunnel,y_tunnel,xtz_tunnel,ytz_tunnel,1,'#e6615e');
            ctx.moveTo(x_tunnel[r],y_tunnel[r]);
            ctx.lineTo(xtz_tunnel[r],ytz_tunnel[r]);
        }
        ctx.closePath();
        ctx.stroke();

		ctx.beginPath();
		ctx.strokeStyle='#b65e2e';
        for (var r=0 ; r<ringsize ; r++)
        {
            //dest.line(x_tunnel,y_tunnel,xz_tunnel,yz_tunnel,1,'#e68e5e');
            ctx.moveTo(x_tunnel[r],y_tunnel[r]);
            ctx.lineTo(xz_tunnel[r],yz_tunnel[r]);
        }
		ctx.closePath();
        ctx.stroke();
      }
      else
      {
        ctx.lineWidth=0.5;

		ctx.beginPath();
		ctx.strokeStyle="#c6413e";
		ctx.fillStyle=color_tunnel;
        for (var r=0 ; r<ringsize ; r++)
        {
            ctx.moveTo(x_tunnel[r],y_tunnel[r]);
            ctx.lineTo(xt_tunnel[r],yt_tunnel[r]);
            ctx.lineTo(xz_tunnel[r],yz_tunnel[r]);
            ctx.lineTo(xtz_tunnel[r],ytz_tunnel[r]);
        }
		ctx.closePath();
        ctx.stroke();
        ctx.fill() ;

       //for (var r=0 ; r<ringsize ; r++)
       //     dest.quad(x_tunnel[r],y_tunnel[r],xt_tunnel[r],yt_tunnel[r],xz_tunnel[r],yz_tunnel[r],xtz_tunnel[r],ytz_tunnel[r],color_tunnel) ;
      }
  }
  ctx.fillStyle=oldcolor;
  date = newDate;
}

    this.render = function(dest) {
        if (this.initdone == 0) { this.canvaspart = new canvas(600,400) ; this.initdone = 1 ;}

        if ( this.vbl == 0  ) { test_tunnel=1 ; date = new Date ; this.canvaspart.contex.globalCompositeOperation='lighter';}
        if ( this.vbl == 400) { test_tunnel=0 ; date = new Date ; this.canvaspart.contex.globalCompositeOperation='source-over';}
        
        this.canvaspart.clear();
        line_tunnel(this.canvaspart);
        this.canvaspart.draw(dest,60,88);
        
        if (this.vbl<100) mycanvasblack.draw(dest,60,88,1-this.vbl/100);
        if (this.vbl>intro_parts[part_running].part_duration-100) mycanvasblack.draw(dest,60,88,1-(intro_parts[part_running].part_duration-this.vbl)/100);
        
        this.vbl++ ;
        if (this.vbl >= this.vbl>intro_parts[part_running].part_duration) this.vbl = 0 ;
    }
}

//////////////
//END PART4//
////////////
