///////////////////////
// Red Sector Man   //
// Code By Totorman//
////////////////////
function part12() {

    this.canvaspart ;
    this.vbl = 0;
    this.initdone = 0 ;
    this.text = new text_cursor() ;

    var mycanvas3D ;
    var mycanvas3D1 ;
    var mycanvas3D2 ;
    var mycanvas3D3 ;

    var angle = 0 ;
    var angleball1 = 0 ;
    var angleball2 = Math.PI/2 ;
    var angleball3 = Math.PI ;
    var angleball4 = 3*Math.PI/2 ;

    var my3d;
    var my3d1;
    var my3d2;

    var mystarfield;
    var mycanvas_stars;

    var myobj = new Array();

    var myman = new Array() ;
    myman = [
          {x: 0, y: 1.15, z: 0, img:6}, // 0 // //head
          {x: 0.1, y: 1.15, z: 0.19, img:10}, //left eye
          {x: -0.1, y: 1.15, z: 0.19, img:10}, // right eye
          {x: 0, y: 0.85, z: 0, img:5}, // neck
          {x: 0, y: 0.5, z: 0, img:0}, //body 1
          {x: 0, y: 0, z: 0, img:1}, // 5 // // body 2
          
        {x: 0.35, y: 0.6, z: 0, img:7}, // 6 // // left arm
        {x: 0.345, y: 0.38, z: 0.05, img:8},
        {x: 0.35, y: 0.17, z: 0.10, img:8},
        {x: 0.35, y: 0.05, z: 0.2, img:8},
        {x: 0.35, y: 0.05, z: 0.35, img:9},
        {x: 0.35, y: 0.1, z: 0.45, img:10}, // 11
        
        {x: -0.35, y: 0.6, z: 0, img:7}, // 12 // right arm
        {x: -0.345, y: 0.38, z: 0.05, img:8},
        {x: -0.35, y: 0.17, z: 0.10, img:8},
        {x: -0.35, y: 0.05, z: 0.2, img:8},
        {x: -0.35, y: 0.05, z: 0.35, img:9},
        {x: -0.35, y: 0.1, z: 0.45, img:10}, // 17

        {x: 0.3, y: -0.12, z: 0, img:7}, // 18 // left leg
        {x: 0.3, y: -0.35, z: 0, img:8},
        {x: 0.32, y: -0.55, z: 0.02, img:8},
        {x: 0.34, y: -0.75, z: 0.04, img:8},
        {x: 0.32, y: -0.95, z: 0.02, img:8},
        {x: 0.32, y: -1.15, z: 0.02, img:8},
        {x: 0.30, y: -1.35, z: 0.02, img:8}, // 24

        {x: -0.3, y: -0.12, z: 0, img:7}, // 25 // right leg
        {x: -0.3, y: -0.35, z: 0, img:8},
        {x: -0.32, y: -0.55, z: 0.02, img:8},
        {x: -0.34, y: -0.75, z: 0.04, img:8},
        {x: -0.32, y: -0.95, z: 0.02, img:8},
        {x: -0.32, y: -1.15, z: 0.02, img:8},
        {x: -0.30, y: -1.35, z: 0.02, img:8}, // 31
        
        {x:0, y:1.2, z:0.5, img:11}, // 32 // ball1
        {x:0, y:1.2, z:0.5, img:11}, // 33 // ball2
        {x:0, y:1.2, z:0.5, img:11}, // 34 // ball3
        {x:0, y:1.2, z:0.5, img:11}, // 34 // ball4
    ] ;
           
    var myobj2 = new Array();
    var myobjvert2 = new Array();
            myobjvert2=[
                    {x:-1, y: -1, z: 1},
                    {x:-1, y:-1.5, z: 1},
                    {x: 1, y:-1.5, z: 1},
                    {x: 1, y: -1, z: 1}, 
                    {x: 1, y: 1.5, z:-1},
                    {x: 1, y:-1.5, z:-1},
                    {x:-1, y:-1.5, z:-1},
                    {x:-1, y: 1.5, z:-1}, 
                    {x:-1, y: 1.5, z: 1},
                    {x: 1, y: 1.5, z: 1}, 
                   ];

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

    var myobj3 = new Array() ;
            myobj3=[ 
                {p1:0, p2:1, p3:2, p4:3, params:new MeshLambertMaterial({ color: 0xa1e65e, opacity:0.9, shading: FlatShading})},
                {p1:9, p2:2, p3:5, p4:4, params:new MeshLambertMaterial({ color: 0xe68e5e, opacity:0.9, shading: FlatShading})},
                {p1:1, p2:8, p3:7, p4:6, params:new MeshLambertMaterial({ color: 0xe65ed4, opacity:0.9, 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:0.9, shading: FlatShading})},	
                //{p1:7, p2:0, p3:0, p4:8, params:new MeshLambertMaterial({ color: 0xe65ed4, opacity:0.9, shading: FlatShading})},	
                //{p1:9, p2:3, p3:3, p4:4, params:new MeshLambertMaterial({ color: 0xe68e5e, opacity:0.9, shading: FlatShading})},	
                {p1:4, p2:7, p3:8, p4:9, params:new MeshLambertMaterial({ color: 0xe68e5e, opacity:0.9, shading: FlatShading})},	
                  ]; 

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

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

        my3d=new codef3D(mycanvas3D, 700, 40, 1, 2000 );
        my3d.vectorball_img(myman, ball );
        my3d.group.scale.x=my3d.group.scale.y=my3d.group.scale.z=100;

        my3d1=new codef3D(mycanvas3D1, 700, 40, 1, 2000 );
        my3d1.faces4(myobjvert2,myobj2,false,true) ;
        my3d1.group.scale.x=my3d1.group.scale.y=my3d1.group.scale.z=100;

        my3d2=new codef3D(mycanvas3D2, 700, 40, 1, 2000 );
        my3d2.faces4(myobjvert2,myobj3,false,true) ;
        my3d2.group.scale.x=my3d2.group.scale.y=my3d2.group.scale.z=100;
        my3d.group.rotation.y = my3d1.group.rotation.y = my3d2.group.rotation.y = 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.vbl = 0 ;

        this.initdone = 1 ;
    }

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

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

        mycanvas3D.clear() ;
        mycanvas3D2.clear() ;
        mycanvas3D1.clear() ;
    
     //   mymoon.draw(this.canvaspart,30,30) ;

        mycanvas_stars.clear() ;

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

      //  my3d1.group.rotation.x+=0.01;
        my3d1.group.rotation.y-=0.02;
        //my3d1.group.rotation.z+=0.005;
        my3d1.draw();
        mycanvas3D1.draw(this.canvaspart) ;

      //  my3d.group.rotation.x+=0.01;
        my3d.group.rotation.y-=0.02;
        //my3d.group.rotation.z+=0.005;

        my3d.group.children[32].position.x = - 0.35 * Math.sin(angleball1) ;
        if ( (angleball1>3*Math.PI/2) ||( angleball1 < Math.PI/2 ) ) {
            my3d.group.children[32].position.y = 0.1 + Math.abs( (0.5-0.1) * Math.cos(angleball1) ) ;
        } else {
            my3d.group.children[32].position.y = 0.1 + Math.abs( (1.2-0.1) * Math.cos(angleball1) ) ;
        }

        my3d.group.children[33].position.x = - 0.35 * Math.sin(angleball2) ;
        if ( (angleball2>3*Math.PI/2) ||( angleball2 < Math.PI/2 ) ) {
            my3d.group.children[33].position.y = 0.1 + Math.abs( (0.5-0.1) * Math.cos(angleball2) ) ;
        } else {
            my3d.group.children[33].position.y = 0.1 + Math.abs( (1.2-0.1) * Math.cos(angleball2) ) ;
        }
        
        my3d.group.children[34].position.x = - 0.35 * Math.sin(angleball3) ;
        if ( (angleball3>3*Math.PI/2) ||( angleball3 < Math.PI/2 ) ) {
            my3d.group.children[34].position.y = 0.1 + Math.abs( (0.5-0.1) * Math.cos(angleball3) ) ;
        } else {
            my3d.group.children[34].position.y = 0.1 + Math.abs( (1.2-0.1) * Math.cos(angleball3) ) ;
        }

        my3d.group.children[35].position.x = - 0.35 * Math.sin(angleball4) ;
        if ( (angleball4>3*Math.PI/2) ||( angleball4 < Math.PI/2 ) ) {
            my3d.group.children[35].position.y = 0.1 + Math.abs( (0.5-0.1) * Math.cos(angleball4) ) ;
        } else {
            my3d.group.children[35].position.y = 0.1 + Math.abs( (1.2-0.1) * Math.cos(angleball4) ) ;
        }

        my3d.group.children[16].position.y = 0.05 - 0.05 * Math.sin( (angleball1+0.2) * 3) ;
        my3d.group.children[17].position.y = 0.1 - 0.1 * Math.sin( (angleball1+0.2) * 3) ;

        my3d.group.children[10].position.y = 0.05 - 0.05 * Math.sin((angleball1-0.2) * 3) ;
        my3d.group.children[11].position.y = 0.1 - 0.1 * Math.sin((angleball1-0.2) * 3) ;

        my3d.draw();
        mycanvas3D.draw(this.canvaspart) ;

     //   my3d2.group.rotation.x+=0.01;
        my3d2.group.rotation.y-=0.02;
        //my3d2.group.rotation.z+=0.005;
        my3d2.draw();
        mycanvas3D2.draw(this.canvaspart) ;

        my3d.group.position.x  = -150 * Math.cos(angle) ;
        my3d1.group.position.x = -150 * Math.cos(angle) ;
        my3d2.group.position.x = -150 * Math.cos(angle) ;
        //my3d3.group.position.x = -150 * Math.cos(angle) ;

        my3d.group.position.z  = 300 * Math.sin(angle/3) ;
        my3d1.group.position.z = 300 * Math.sin(angle/3) ;
        my3d2.group.position.z = 300 * Math.sin(angle/3) ;
        //my3d3.group.position.z = 300 * Math.sin(angle/3) ;

        angle += 0.05 ;

        angleball1 += 0.05 ;
        if (angleball1 > 2*Math.PI) angleball1 -= 2*Math.PI ;

        angleball2 += 0.05 ;
        if (angleball2 > 2*Math.PI) angleball2 -= 2*Math.PI ;

        angleball3 += 0.05 ;
        if (angleball3 > 2*Math.PI) angleball3 -= 2*Math.PI ;

        angleball4 += 0.05 ;
        if (angleball4 > 2*Math.PI) angleball4 -= 2*Math.PI ;

        this.text.render(this.canvaspart,choix_pos[5].x,choix_pos[5].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++ ;
    }

return this ;
}
///////////////
//END PART12//
/////////////
