//////////////////////
//Particles Perlin //
//Code By Ayoros  //
///////////////////

function part5() {
    this.vbl = 0 ;
    this.canvaspart;
    this.initdone = 0 ;
var grad_perlin=0;
var noise_pp=0;
var perlin=0;
var dx_perlin=0;
var dy_perlin=0;
var ang_perlin=0;
var ix_perlin=0;
var iy_perlin=0;
var sy_perlin=0;
var z_perlin=0;
var number_perlin=400;
var scale_noise=350;
var dots_noise=[];
var x0_perlin; 
var y0_perlin; 
var i1_perlin; 
var perlin_noise_i;
var q_noise=0;

this.init=function(){for(i=0;i<number_perlin;i++)0==q_noise&&(dots_noise[i]={x:i,y:i,sprite:particle[0]}),1==q_noise&&(dots_noise[i]={x:i,y:i,sprite:particle[1]}),2==q_noise&&(dots_noise[i]={x:i,y:i,sprite:particle[2]}),3==q_noise&&(dots_noise[i]={x:i,y:i,sprite:particle[3]}),4==q_noise&&(dots_noise[i]={x:i,y:i,sprite:particle[4]}),5==q_noise&&(dots_noise[i]={x:i,y:i,sprite:particle[5]}),6==q_noise&&(dots_noise[i]={x:i,y:i,sprite:particle[6]}),7==q_noise&&(dots_noise[i]={x:i,y:i,sprite:particle[7]}),
8==q_noise&&(dots_noise[i]={x:i,y:i,sprite:particle[8]}),q_noise++,9==q_noise&&(q_noise=0);this.initdone=1};
function draw_perlin(a){z_perlin+=0.003;for(i=0;i<number_perlin;){noise_pp=dots_noise[i++];ix_perlin=4*noise_algo(noise_pp.x/scale_noise+z_perlin,sy_perlin=noise_pp.y/scale_noise)+noise_pp.x;iy_perlin=4*noise_algo(noise_pp.x/scale_noise-z_perlin,sy_perlin)+noise_pp.y;ix_perlin+=(300-ix_perlin)/50;iy_perlin+=(200-iy_perlin)/50;for(j=i+1;j<number_perlin;)b=dots_noise[j++],dx_perlin=b.x-ix_perlin,dy_perlin=b.y-iy_perlin,20>Math.sqrt(dx_perlin*dx_perlin+dy_perlin*dy_perlin)&&(ang_perlin=Math.atan2(dy_perlin,
dx_perlin),b.x+=(ix_perlin+20*Math.cos(ang_perlin)-b.x)/4,b.y+=(iy_perlin+20*Math.sin(ang_perlin)-b.y)/4);390<=iy_perlin&&(iy_perlin=390);0>=iy_perlin&&(iy_perlin=0);590<=ix_perlin&&(ix_perlin=590);0>=ix_perlin&&(ix_perlin=0);noise_pp.sprite.draw(a,noise_pp.x=ix_perlin,noise_pp.y=iy_perlin)}}grad_perlin=[[1,1],[-1,1],[1,-1],[-1,-1],[1,0],[-1,0],[1,0],[-1,0],[0,1],[0,-1],[0,1],[0,-1]];
function noise_algo(a,c){perlin=0.366*(a+c);dx_perlin=Math.floor(a+perlin);dy_perlin=Math.floor(c+perlin);j_perlin=0.21*(dx_perlin+dy_perlin);x0_perlin=a-(dx_perlin-j_perlin);y0_perlin=c-(dy_perlin-j_perlin);i1_perlin=x0_perlin>y0_perlin;return 70*(perlin_calc(x0_perlin,y0_perlin,(perlin_noise_i=(dx_perlin&255)+(dy_perlin&255))%12)+perlin_calc(x0_perlin-i1_perlin+0.21,y0_perlin-!i1_perlin+0.21,(perlin_noise_i+1)%12)+perlin_calc(x0_perlin-0.58,y0_perlin-0.58,(perlin_noise_i+2)%12))}
function perlin_calc(a,c,d){j_perlin=0.5-a*a-c*c;return 0>j_perlin?0:j_perlin*j_perlin*j_perlin*(grad_perlin[d][0]*a+grad_perlin[d][1]*c)}
this.render=function(a){0==this.initdone&&(this.canvaspart=new canvas(600,400),this.init());this.canvaspart.clear();draw_perlin(this.canvaspart);this.canvaspart.draw(a,60,88);100>this.vbl&&mycanvasblack.draw(a,60,88,1-this.vbl/100);this.vbl>intro_parts[part_running].part_duration-100&&mycanvasblack.draw(a,60,88,1-(intro_parts[part_running].part_duration-this.vbl)/100);this.vbl++};
return this;
}
//////////////
//END PART5//
////////////
