//////////////////////
//Text Writing Cube//
//Code By Ayoros  //
///////////////////

function part3() {
    this.initdone = 0 ;
    this.vbl = 0 ;
    this.canvaspart ;

    var myobj = new Array();
    var myobjvert = new Array();
    myobjvert=[
            {x:-100, y: 100, z: 100},
            {x:-100, y:-100, z: 100},
            {x: 100, y:-100, z: 100},
            {x: 100, y: 100, z: 100}, 
            {x: 100, y: 100, z:-100},
            {x: 100, y:-100, z:-100},
            {x:-100, y:-100, z:-100},
            {x:-100, y: 100, z:-100}, 
           ];


    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 rasposY=0;

    //        ".................." 
    var mytxt=
      //"ABCDEFGHIJKLMNOPQRSTUVWXYZ.,;:"+
          "------------------------------"+ 
          "          HEMOROIDS!          "+
          "------------------------------"+
          "       WHEN PREPARATION H     "+
          "         IS NOT ENOUGH        "+
          "          WE ARE VERY        "+
          "      PROUD TO PRESENT YOU    "+
          "       A NEW DEMO CALLED      ";
    var mytxt1=
      //"ABCDEFGHIJKLMNOPQRSTUVWXYZ.,;:"+
          " THE CREDITS FOR THIS DENTRO :"+ 
          "   CODE BY AYOROS, TOTORMAN.  "+
          "    DESIGN ART BY PANDAFOX    "+
          "         MUSIC BY 505         "+
          "                              "+
          " FRAMEWORK CODEF BY NONAMENO! "+
          "    PROTRACKER ROUTINE BY     "+
          "         TDA-FIREHAWK         ";
    var mytxt2=
      //"ABCDEFGHIJKLMNOPQRSTUVWXYZ.,;:"+
          "     MEMBERS OF HEMOROIDS     "+ 
          " SINK,ANGEL,KELLY.X,PANDAFOX, "+
          " SHOOTERS,TOTORMAN,STC,AYOROS "+
          "------------------------------"+
          "    THIS DENTRO IS MADE FOR   "+
          "    THE REVISION PARTY 2014   "+
          "------------------------------"+
          "PREPARATION H IS GOOD FOR YOU!";

    var tab_mytxt= new Array();
    tab_mytxt[0]=mytxt;
    tab_mytxt[1]=mytxt1; 
    tab_mytxt[2]=mytxt2;
    var myscrolltext;

    var myscrolltext;

    var myletters = new Array();
    var num=0;
    var tempo=0;
    var fullpos= new Array();
    var mycanvas_part2;

    function myletter(){
        this.position;
        this.target;
        this.tween;
        this.ltr=1;
        return this;
    }

    var my3d;
    var cube;

    var cube_movx=0;
    var cube_movy=0;
    var vbl_txt_effect=0;
    var myfont_tile;

    this.init = function(dest) {
        mycanvas_part2=new canvas(136,136);

        myfont_tile = new codef_Tile() ;
        myfont_tile.initTile(myfont,myfont.img.width, myfont.img.height,16,16,32) ;
        myfont_tile.setmidhandle() ;
        //myfont.initTile(16,16,32);
        //myfont.setmidhandle();

        mycanvas_part2.setmidhandle();

        /*init Cube 3d*/
        my3d=new codef3D(mycanvas_part2, 600, 33, 1, 1600 );
        my3d.faces4(myobjvert,myobj, false, true );
        my3d.addAmbiLight(0x444444);
        my3d.addDirLight(-0.5,-0.5,1,0xffffff);

        var plop=0;
        for(var y=0; y<8; y++){
          for(var x=0; x<30; x++){
              fullpos[plop]=new Object();
              fullpos[plop]={x:x*16+64,y:y*32+90};
              plop++;
          }
        }

        this.initdone = 1 ;

    }

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

   
        cube_movx+=0.04;
        cube_movy+=0.06;
   
        if(num<fullpos.length&& tempo>4){
          myletters[num] = new myletter;
          
          myletters[num].ltr=tab_mytxt[txt_next].charCodeAt(num)-32;
          myletters[num].position = { x : 300+Math.sin(cube_movx)*250, y: 200+Math.sin(cube_movy)*120, z:0 };
          myletters[num].target = { x : fullpos[num].x, y: fullpos[num].y, z:0.0 };

          myletters[num].tween = new createjs.Tween.get(myletters[num].position)
            .to(myletters[num].target, 800,createjs.Ease.sineOut);

          num++;
	
          tempo=0;
        }
 
        tempo++;

        for(var i=0; i<num; i++){
            myfont_tile.drawTile(this.canvaspart,myletters[i].ltr,myletters[i].position.x,myletters[i].position.y,1,0,1,1);  
            // myfont.drawTile(this.canvaspart,myletters[i].ltr,myletters[i].position.x,myletters[i].position.y);  
        }
        if (num==240){ 
            if (vbl_txt_effect>298){
                this.canvaspart.clear();
                txt_next++;
                if (txt_next==2) txt_next=0;
                vbl_txt_effect=0;
                num=0;
            }
            vbl_txt_effect++;
        }

        this.canvaspart.contex.globalCompositeOperation='source-in';
        myrasters.draw(this.canvaspart,0,-(rasposY%444));
        this.canvaspart.contex.globalCompositeOperation='source-over';

        rasposY+=1.5;
        this.canvaspart.draw(dest,60,88);
        mycanvas_part2.clear();
        my3d.group.rotation.x-=0.05;
        my3d.group.rotation.z-=0.04;

        my3d.draw();
        mycanvas_part2.draw(dest,360+Math.sin(cube_movx)*220,284+Math.sin(cube_movy)*120);
        createjs.Tween.tick(15);
        
        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);
    }

}
//////////////
//END PART3//
////////////
