//////////////////////////
//PARTICLES            //
////////////////////////
function part16() {
    this.vbl = 0 ;
    this.canvaspart;
    this.initdone = 0 ;

    var mycanvastemp;
    var hmd_ch_part = 0;
    var hmd_vbl_particle = 0;
    var hmd_xf1 = 0;
    var hmd_yf1 = 0;
    var hmd_xf2 = 0;
    var hmd_yf2 = 0;
    var hmd_ch_part = 0;

    var up_st  = 0.25;
    var up_st2 = 1.5;
    var up_st3 = 1.5;
    var up_st4 = 0.25;

    up_st = 6 ;
    up_st2 = 35 ;
    up_st3 = 27 ;
    up_st4 = 4 ;

    var move_pac2=0;
    var i_pacmove2=0;
    var i_pac2=0;
    var mycanvas_pacman2;
    var mycanvas_white ;

    function hmd_drawline(dest) {
        var time = Date.now() / 800;
        var mix = Math.sin(time);
        for (var xy, t=0; t<Math.PI*2; t+=Math.PI/52) {
            xy = mixf(f1(t,hmd_xf1,hmd_yf1), f2(t,hmd_xf2,hmd_yf2), mix);

            var x = xy.x ; 
            var y = xy.y ;

            particle[0].draw(dest, x *  4,-y *  4);
            particle[0].draw(dest,-x *  4, y *  4);
            particle[0].draw(dest,-y *  4, x *  4);
            particle[0].draw(dest, y *  4,-x *  4);
            particle[1].draw(dest, x *  6,-y *  6);
            particle[1].draw(dest,-x *  6, y *  6);
            particle[1].draw(dest,-y *  6, x *  6);
            particle[1].draw(dest, y *  6,-x *  6);
            particle[2].draw(dest, x *  8,-y *  8);
            particle[2].draw(dest,-x *  8, y *  8);
            particle[2].draw(dest,-y *  8, x *  8);
            particle[2].draw(dest, y *  8,-x *  8);
            particle[3].draw(dest, x * 10,-y * 10);
            particle[3].draw(dest,-x * 10, y * 10);
            particle[3].draw(dest,-y * 10, x * 10);
            particle[3].draw(dest, y * 10,-x * 10);
            particle[4].draw(dest, x * 12,-y * 12);
            particle[4].draw(dest,-x * 12, y * 12);
            particle[4].draw(dest,-y * 12, x * 12);
            particle[4].draw(dest, y * 12,-x * 12);
            particle[5].draw(dest, x * 14,-y * 14);
            particle[5].draw(dest,-x * 14, y * 14);
            particle[5].draw(dest,-y * 14, x * 14);
            particle[5].draw(dest, y * 14,-x * 14);
            particle[6].draw(dest, x * 16,-y * 16);
            particle[6].draw(dest,-x * 16, y * 16);
            particle[6].draw(dest,-y * 16, x * 16);
            particle[6].draw(dest, y * 16,-x * 16);
            particle[7].draw(dest, x * 18,-y * 18);
            particle[7].draw(dest,-x * 18, y * 18);
            particle[7].draw(dest,-y * 18, x * 18);
            particle[7].draw(dest, y * 18,-x * 18);
        }

        var tmp = Math.cos(hmd_vbl_particle) ;

        hmd_xf1 = up_st  + (tmp + 18 ) * tmp ;
        hmd_yf1 = up_st2 + (tmp + 12 ) * tmp ;
        hmd_xf2 = up_st3 + (tmp +  8 ) * tmp ;
        hmd_yf2 = up_st4 + (tmp +  4 ) * tmp ;

        hmd_vbl_particle+=0.04;
        up_st  += 0.001;
        up_st2 += 0.005;
        up_st3 -= 0.005;
        up_st4 -= 0.001;
    }

    function mixf(xy1, xy2, mix) { 
        return {
            x: xy1.x * mix + xy2.x * (1-mix),
            y: xy1.y * mix + xy2.y * (1-mix)
        };
    }

    function f1(t,hmd_xf1,hmd_yf1) {
        return {
            x: hmd_xf1*Math.cos(t) - 6*Math.cos(11/6*t),
            y: hmd_yf1*Math.sin(t) - 6*Math.sin(11/6*t)
        };
    }

    function f2(t,hmd_xf2,hmd_yf2) {
        return {
            x: hmd_xf2 * (Math.cos(3*t)),
            y: hmd_yf2 * (3*Math.sin(t))
        };
    }


    this.init = function(dest) {
        mycanvastemp = new canvas(dest.width, dest.height) ;
        this.initdone = 1 ;

        mycanvas_pacman2=new canvas(720,576);
        mycanvas_white = new canvas(720,576) ;
        mycanvas_white.fill("#ffffff") ;
    }

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

        this.canvaspart.clear();
        mycanvastemp.fill("rgba(0,0,0,0.2)");

        hmd_drawline(mycanvastemp); 

        var time2 = Date.now() / 800;
        var mix2 = Math.sin(time2);
        mycanvastemp.draw(this.canvaspart,300,200,1,  0  + ( Date.now()/1600%(Math.PI*30) *(4-mix2) ) );
        mycanvastemp.draw(this.canvaspart,300,200,1, 180 + ( Date.now()/1600%(Math.PI*30) *(4-mix2) ) );
        mycanvastemp.draw(this.canvaspart,300,200,1,  90 + ( Date.now()/1600%(Math.PI*30) *(4-mix2) ) );
        mycanvastemp.draw(this.canvaspart,300,200,1, -90 + ( Date.now()/1600%(Math.PI*30) *(4-mix2) ) );

        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);
        }

        
        if (this.vbl>intro_parts[part_running].part_duration-300){ 
//            mycanvasblack.draw(dest,60,88,1-(intro_parts[part_running].part_duration-this.vbl)/100);
            fond_global.draw(mycanvas_pacman2,0,0);
            if (move_pac2>220){
                mycanvas_pacman.drawPart(dest,0,0,0,0,move_pac2-210,576,1,0,1,1);}
        
            bigpac[i_pac2].draw(dest,-500+move_pac2,10);
            move_pac2+=5;
          
            i_pacmove2=i_pacmove2+1;
            if (i_pacmove2>=5){i_pac2=i_pac2-1;}
            if (i_pacmove2<=4){i_pac2=i_pac2+1;}
            if (i_pacmove2==8){i_pac2=0;i_pacmove2=0;}

            // sound going down to zero
            if (this.vbl > intro_parts[part_running].part_duration-300){
                if (gain_created == 1) {
                    if (gainNode.gain.value > -1) { gainNode.gain.value -= 0.01; }
                    else { module.stop() ; } // when sound off => stop module
                    }
            }
            mycanvas_white.draw(dest,0,0,1-(intro_parts[part_running].part_duration-this.vbl)/300);
        }

        this.vbl++ ;
    }

    return this ;
}

///////////////
//END PART16//
/////////////
