//////////////////////
// Cube Explode    //
//Code By Totorman//
///////////////////
function part11(){
    this.canvaspart ;
    this.initdone = 0 ;
    this.vbl = 0 ;
    this.text = new text_cursor() ;

    var vbl_startcoming = 0 ;
    var vbl_endcoming = 30 ;
    var vbl_startturning = 30 ;
    var vbl_endturning = 300 ;
    var vbl_startshaking = 300 ;
    var vbl_stopshaking = 500 ;
    var vbl_startexploding = 500 ;
    var vbl_stopexploding = 800 ;

    var mycanvas_3D;
    var mycanvas_3D2;
    var my3d ;
    var my3d2 ;

    var VIEW_ANGLE = 40 ;
    var angle = 1.5 ;
    var vbl = 0 ;
    var vbl2 = 0 ;
    var anglex = 0 ;
    var angley = 0 ;
    var anglez = 0 ;
    var mymove = 0 ;
    var exploded = 0 ;

    var mystarfield;
    var mycanvas_stars;

    var mycubes = new Array() ;

    function addcube(myobj3d,x,y,z,sz) {
        var myobj = new Array();
        var myobjvert = new Array();
        myobjvert=[
                {x:-1, y: 1, z: 1},
                {x:-1, y:-1, z: 1},
                {x: 1, y:-1, z: 1},
                {x: 1, y: 1, z: 1}, 
                {x: 1, y: 1, z:-1},
                {x: 1, y:-1, z:-1},
                {x:-1, y:-1, z:-1},
                {x:-1, y: 1, z:-1}, 
               ];

        myobj=[ 
         {p1:0, p2:1, p3:2, p4:3, params:new MeshLambertMaterial({ color: 0xa1e65e, opacity:1, shading: FlatShading})},
            {p1:3, p2:2, p3:5, p4:4, params:new MeshLambertMaterial({ color: 0xe68e5e, opacity:1, shading: FlatShading})},
            {p1:7, p2:6, p3:1, p4:0, params:new MeshLambertMaterial({ color: 0xe65ed4, opacity:1, shading: FlatShading})},
            {p1:7, p2:0, p3:3, p4:4, params:new MeshLambertMaterial({ color: 0x5ec4e6, opacity:1, shading: FlatShading})},
            {p1:1, p2:6, p3:5, p4:2, params:new MeshLambertMaterial({ color: 0xab5ee6, opacity:1, shading: FlatShading})},	
            {p1:4, p2:5, p3:6, p4:7, params:new MeshLambertMaterial({ color: 0xe6c85e, opacity:1, shading: FlatShading})},	
              ];    

        var geom = new THREE.Geometry(); 
        for(var i=0; i<myobjvert.length; i++){
            geom.vertices.push(new THREE.Vector3( myobjvert[i].x,  myobjvert[i].y,  myobjvert[i].z));
        }

        for(var i=0; i<myobj.length; i++){
            myobj[i].params.overdraw = false;
            geom.materials.push(myobj[i].params);
            geom.faces.push( new THREE.Face4( myobj[i].p1,myobj[i].p2,myobj[i].p3, myobj[i].p4, null, null,i ));
            if(typeof(myobj[i].v4)!='undefined'){
                geom.faceVertexUvs[ 0 ].push( [
                    new THREE.UV( myobj[i].u1,myobj[i].v1 ),
                    new THREE.UV( myobj[i].u2,myobj[i].v2 ),
                    new THREE.UV( myobj[i].u3,myobj[i].v3 ),
                    new THREE.UV( myobj[i].u4,myobj[i].v4 )
                ] );
            }			
        }

        geom.computeFaceNormals();
        geom.computeCentroids();
        geom.computeVertexNormals();

        form = new THREE.Mesh( geom, new THREE.MeshFaceMaterial());
        form.position.x = x ;
        form.position.y = y ;
        form.position.z = z ;
        myobj3d.group.add(form );
    }

    function mycube() {
        this.x = 0 ;
        this.y = 0 ;
        this.z = 0 ;
        this.a = 0 ;
        this.b = 0 ;
        this.c = 0 ;
    }

    function base3D() {
        var center = 128;
        var width = 127;
        frequency = Math.PI*2/100;

        var red   = Math.sin(frequency*vbl+2+anglex) * width + center;
        var green = Math.sin(frequency*vbl+0+angley) * width + center;
        var blue  = Math.sin(frequency*vbl+4+anglez) * width + center;

        anglex += 0.03 ;
        angley += 0.02 ;
        anglez += 0.04 ;

        angle += 0.03 ;
    }

    function cubeiscoming(vbl,dest) {
        base3D() ;

        my3d2.group.position.y = -20*(vbl_endcoming - vbl)/(vbl_endcoming-vbl_startcoming) ;

        my3d2.group.rotation.x = anglex ;
        my3d2.group.rotation.y = angley ;
        my3d2.group.rotation.z = anglez ;
        my3d2.camera.lookAt({x: 0, y: 0, z: 0}) ;

        mycanvas3D2.clear();
        my3d2.draw() ;
        mycanvas3D2.draw(dest) ;
    }

    function cubeisturning(vbl,dest) {

        base3D() ;

        my3d2.group.rotation.x = anglex ;
        my3d2.group.rotation.y = angley ;
        my3d2.group.rotation.z = anglez ;
        my3d2.camera.lookAt({x: 0, y: 0, z: 0}) ;

        mycanvas3D2.clear();
        my3d2.draw() ;
        mycanvas3D2.draw(dest) ;
    }

    function cubeisshaking(vbl,dest) {

        if ( vbl == 0 ) {
            for (var i=0; i< mycubes.length; i++) {
                my3d.group.children[i].position.x = my3d2.group.children[i].position.x ;
                my3d.group.children[i].position.y = my3d2.group.children[i].position.y ;
                my3d.group.children[i].position.z = my3d2.group.children[i].position.z ;

                my3d.group.children[i].rotation.x = my3d2.group.children[i].rotation.x ;
                my3d.group.children[i].rotation.y = my3d2.group.children[i].rotation.y ;
                my3d.group.children[i].rotation.z = my3d2.group.children[i].rotation.z ;

                my3d.group.position.x = my3d2.group.position.x ;
                my3d.group.position.y = my3d2.group.position.y ;
                my3d.group.position.z = my3d2.group.position.z ;
            }
            my3d.camera.lookAt({x: 0, y: 0, z: 0}) ;
        }

        base3D() ;

        my3d.group.rotation.x = anglex ;
        my3d.group.rotation.y = angley ;
        my3d.group.rotation.z = anglez ;

        my3d.camera.lookAt({x: mymove * Math.sin(angle*100)/400,y: mymove * Math.cos(angle*50)/300,z:0}) ;

        if ( vbl %3 == 0) mymove ++ ;

        mycanvas3D.clear();
        my3d.draw() ;
        mycanvas3D.draw(dest) ;

        if (vbl == vbl_stopshaking - vbl_startshaking - 1) {
            vbl2 = vbl_startexploding ;
            mymove = 0 ;
            exploded = 0 ;
        }
    }


    function explodeobj(vbl) {
        for (var i=0; i< mycubes.length; i++) {
            var xInc = mycubes[i].x * Math.sin (mycubes[i].a) ;
            var yInc = mycubes[i].y * Math.sin (mycubes[i].b) ;
            var zInc = -mycubes[i].z * Math.sin (mycubes[i].c) ;

            my3d.group.children[i].position.x += xInc * vbl/60;
            my3d.group.children[i].position.y += yInc * vbl/60;
            my3d.group.children[i].position.z += zInc * vbl/60;

            my3d.group.children[i].rotation.x = anglex ;
            my3d.group.children[i].rotation.y = angley ;
            my3d.group.children[i].rotation.z = anglez ;
        }

        if ( vbl >= (vbl_stopexploding - vbl_startexploding - 1) ) {
            exploded = 1 ;
            vbl2 = 0 ;
        }
    }

    function reinit3d() {
        for (var i=0; i< mycubes.length; i++) {
            my3d.group.children[i].position.x = my3d2.group.children[i].position.x ;
            my3d.group.children[i].position.y = my3d2.group.children[i].position.y ;
            my3d.group.children[i].position.z = my3d2.group.children[i].position.z ;

            my3d.group.children[i].rotation.x = my3d2.group.children[i].rotation.x ;
            my3d.group.children[i].rotation.y = my3d2.group.children[i].rotation.y ;
            my3d.group.children[i].rotation.z = my3d2.group.children[i].rotation.z ;
        }
    }

    function cubeisexploding(vbl,dest) {
        base3D() ;

        explodeobj(vbl) ;

        mycanvas3D.clear();
        my3d.draw() ;
        mycanvas3D.draw(dest) ;

        if (vbl == vbl_stopexploding -1) exploded = 0 ;
    }

    this.init = function(dest) {
        this.text.init(choix_text[4],10,8,font_key) ;

        mycanvas3D=new canvas(640,400);
        mycanvas3D2=new canvas(640,400);

        my3d=new codef3D(mycanvas3D, 50, VIEW_ANGLE, 0.1, 600 );
        my3d.addAmbiLight(0x444444);
        my3d.addDirLight(-0.5,-0.5,1,0xffffff);

        my3d.camera.position.x = 0 ;
        my3d.camera.position.y = 0 ;
        my3d.camera.position.z = 20 ;

        my3d.camera.lookAt({x:0,y:0,z:0}) ;	
        my3d.matrixAutoUpdate = true ;

        my3d2=new codef3D(mycanvas3D2, 50, VIEW_ANGLE, 0.1, 600 );
        my3d2.addAmbiLight(0x444444);
        my3d2.addDirLight(-0.5,-0.5,1,0xffffff);

        my3d2.camera.position.x = 0 ;
        my3d2.camera.position.y = 0 ;
        my3d2.camera.position.z = 20 ;

        my3d2.camera.lookAt({x:0,y:0,z:0}) ;	
        my3d2.matrixAutoUpdate = true ;

        var CHUNK_SIZE = 5 ;
        var nbcubes = 0 ;
        for (z = 0; z < CHUNK_SIZE; z++)
        {
            for (y = 0; y < CHUNK_SIZE; y++)
            {
                for (x = 0; x < CHUNK_SIZE; x++)
                {
                    addcube(my3d,(x-CHUNK_SIZE/2),(y-CHUNK_SIZE/2),(z-CHUNK_SIZE/2),1.1) ;
                    addcube(my3d2,(x-CHUNK_SIZE/2),(y-CHUNK_SIZE/2),(z-CHUNK_SIZE/2),1.1) ;
                    mycubes[nbcubes] = new mycube() ;
                    var tmp = mycubes[nbcubes++] ;
                    tmp.x = (x-CHUNK_SIZE/2) ;
                    tmp.y = (y-CHUNK_SIZE/2) ;
                    tmp.z = (z-CHUNK_SIZE/2) ;
                    tmp.a = Math.random()*2*Math.PI;
                    tmp.b = Math.random()*2*Math.PI;
                    tmp.c = Math.random()*2*Math.PI;
                }
            }
        }
        mycanvas_stars = new canvas(640,400) ;

        // function starfield3D(           dest,    nb, speed, w, h, centx, centy, color, ratio, offsetx, offsety)
        mystarfield=new starfield3D(mycanvas_stars, 500, 4, 640,400, 320, 200,'#999999', 200,0,0);

        this.initdone = 1 ;
    }


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

        this.canvaspart.fill("#000000") ;
        mycanvas_stars.clear() ;

        mystarfield.draw() ;
        mycanvas_stars.draw(this.canvaspart,0,0) ;

        if ( ( vbl2 >= vbl_startexploding ) && ( exploded == 0           ) ) { cubeisexploding (vbl2 - vbl_startexploding , this.canvaspart) ; }
        if ( ( vbl >= vbl_startcoming    )  && ( vbl < vbl_endcoming     ) ) { cubeiscoming    (vbl  - vbl_startcoming    , this.canvaspart) ; }
        if ( ( vbl >= vbl_startturning   )  && ( vbl < vbl_endturning    ) ) { cubeisturning   (vbl  - vbl_startturning   , this.canvaspart) ; }
        if ( ( vbl >= vbl_startshaking   )  && ( vbl < vbl_stopshaking   ) ) { cubeisshaking   (vbl  - vbl_startshaking   , this.canvaspart) ; }

        vbl ++ ;
        vbl2 ++ ;
        if (vbl >= (vbl_stopexploding-280) ) { vbl = 0 ; }

        this.text.render(this.canvaspart,choix_pos[4].x,choix_pos[4].y) ;
        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++ ;
    }
}
///////////////
//END PART11//
/////////////
