/**************************************************************************************************************
*        Script: Cutin script
*        Author: Helepolis
*        version 3.0
*        Types: 1 - MOF           Middle appear -> float to top -> leave top
*              2 - SA/UFO        Diagonal right top entrance -> slowmode -> follow path to leave
*               3 - UFO2          Horizontal left entrance -> slowmode -> follow path to leave (Byakuren style)
*        
*        This script is no longer bound any other scripts or extra functions.
*        Full support in one single file.
**************************************************************************************************************/

    // new cutin function eliminates the old commondata style. 
    task cutin(type,spellcardname,cutinImg,left,top,right,bottom){
        yield;

        // Charismatic variables
        let obj = Obj_Create(OBJ_EFFECT);
        let alphret = 0;
        let as = 0;
        let c = 0;
        let x = 0;
        let y = 0;

            let XYbLR = left + right / 2;
            let XYbTB = top + bottom / 2;

        if(type == "KANAKO") { x = GetClipMaxX; y = GetCenterY+10; }
        if(type == "NAZRIN") { x = GetClipMaxX+256; y = GetCenterY-120; }
        if(type == "BYAKUREN") { x = GetClipMinX-256; y = GetCenterY; }

        // Experts attention: you can disable this if you want your own Spellcard text but for common people it is default active.
            CutIn(YOUMU, spellcardname,cutinImg,0,0,0,0);

        summontext;
        summonhexagon;

        Obj_SetPosition(obj,x,y);
        ObjEffect_SetRenderState(obj,ALPHA);
        ObjEffect_SetTexture(obj,cutinImg);
        ObjEffect_SetLayer(obj,1);

        // this is a special function to handle the cutin alpha modification seperate.
        cutinEffect_Alphret(obj,as,alphret);
        
        // Nifty effect vertex setting ( basically a rectangular using the parameters )
        ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
        ObjEffect_CreateVertex(obj, 4);
        ObjEffect_SetVertexXY(obj, 0, -XYbLR, -XYbTB);
        ObjEffect_SetVertexUV(obj, 0, left, top);
        ObjEffect_SetVertexXY(obj, 1, XYbLR, -XYbTB);
        ObjEffect_SetVertexUV(obj, 1, right, top);
        ObjEffect_SetVertexXY(obj, 2, -XYbLR, XYbTB);
        ObjEffect_SetVertexUV(obj, 2, left, bottom);
        ObjEffect_SetVertexXY(obj, 3, XYbLR, XYbTB);
        ObjEffect_SetVertexUV(obj, 3, right, bottom);

        // hooray for complex movement path. Depending on the cutin type, perform cutin.
        while(! Obj_BeDeleted(obj)){
                if(type=="KANAKO"){
                        if(c<15){x-=10; y+=10}
                        if(c>=15 && c<45){y-=4;}
                        if(c>=45){y-=2;}
                        if(c>120){ Obj_Delete(obj); }
                }
                if(type=="NAZRIN"){
                        if(c<21){x-=20; y+=5}
                        if(c>=21 && c<100){x--; y+=0.5;}
                        if(c>=100){x-=20; y+=5;}
                        if(c>120){ Obj_Delete(obj); }
                }
                if(type=="BYAKUREN"){
                        if(c<21){x+=20;}
                        if(c>=21 && c<100){x++;}
                        if(c>=100){x+=20;}
                        if(c>120){Obj_Delete(obj);}
                }
                Obj_SetPosition(obj,x,y);
                c++;
                yield;
        }
    }

    // seperate function to make the fadein and fadeout effect.
    task cutinEffect_Alphret(obj,as,alphret) {
        let c = 0;
        loop{
                  ObjEffect_SetVertexColor(obj,0,alphret,255,255,255);
                 ObjEffect_SetVertexColor(obj,1,alphret,255,255,255);
                  ObjEffect_SetVertexColor(obj,2,alphret,255,255,255);
                  ObjEffect_SetVertexColor(obj,3,alphret,255,255,255);
                if(as==0){alphret+=20; if(alphret>250){as=1;} }
                if(as==1){c++; if(c==90){ as=2; } }
                if(as==2){alphret-=10; if(alphret<10){as=4;} }        
                yield;
        }
    }
    
    // handler for summoning the diagonal text
    task summontext {
        let yyPos = 0;
        loop(5){
                spellcardtext(GetClipMinX,GetClipMaxY-22-yyPos,1.5);
                spellcardtext(GetClipMaxX,GetClipMinY-30+yyPos,-1.5);
                yyPos+=54;
                yield;
        }
    } 

    // handler for summoning the hexagons
    task summonhexagon {
        let dir = 0;
        loop(8){
                hexagontext(GetCenterX+100,GetCenterY+150,1.5,dir,1.4,1.4);
                hexagontext(GetCenterX+100,GetCenterY+150,1.5,dir,1.2,1.2);
                hexagontext(GetCenterX+100,GetCenterY+150,-1.5,-dir,1.0,1.0);
                hexagontext(80,100,1.5,dir,0.8,0.8);
                dir+=45;
                yield;
        }
    } 

//************************************************************************
//         Text animation for spellcard spawn ( HEXAGON STYLE )
//   
//        alphret - alpha value
//        as - variable acting like a switch
//        c - counter variable
//************************************************************************

    let CSD = GetCurrentScriptDirectory;
    let textdata = CSD ~ ".\lib\cutin\system\spellcardanm.png";
    LoadGraphic(textdata);

    task hexagontext(x,y,v,draai,sx,sy) {
        let obj = Obj_Create(OBJ_EFFECT);
        let alphret = 0;
        let as = 0;
        let c = 0;
        let angle = draai;
        let speed = v;

        Obj_SetPosition(obj,x,y);        
        ObjEffect_SetRenderState(obj,ADD);
        ObjEffect_SetTexture(obj,textdata);
        ObjEffect_SetScale(obj, sx, sy);
        ObjEffect_SetLayer(obj,2);

        ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
        ObjEffect_CreateVertex(obj, 4);

        ObjEffect_SetVertexXY(obj, 0, -48, -128);
        ObjEffect_SetVertexUV(obj, 0, 0, 0);
        ObjEffect_SetVertexXY(obj, 1, 48, -128);
        ObjEffect_SetVertexUV(obj, 1, 128, 0);
        ObjEffect_SetVertexXY(obj, 2, -48, -104);
        ObjEffect_SetVertexUV(obj, 2, 0, 32);
        ObjEffect_SetVertexXY(obj, 3, 48, -104);
        ObjEffect_SetVertexUV(obj, 3, 128, 32);

        while(! Obj_BeDeleted(obj)){
                ObjEffect_SetAngle(obj,0,0,angle);
                  ObjEffect_SetVertexColor(obj,0,alphret,255,255,255);
                 ObjEffect_SetVertexColor(obj,1,alphret,255,255,255);
                  ObjEffect_SetVertexColor(obj,2,alphret,255,255,255);
                  ObjEffect_SetVertexColor(obj,3,alphret,255,255,255);

                if(as==0){alphret+=20; if(alphret>185){as=1;} }
                if(as==1){c++; if(c==90){as=2;} }                
                if(as==2){alphret-=10; if(alphret<0){ Obj_Delete(obj); } }
                angle += speed;

                yield;
          }
    }

//************************************************************************
//        Text animation for spellcard spawn ( Diagonal text ) 
//   
//        alphret - alpha value
//        as - variable acting like a switch
//        c - counter variable
//************************************************************************
    task spellcardtext(x,y,v) {
        let obj = Obj_Create(OBJ_EFFECT);
        let alphret = 0;
        let as = 0;
        let c = 0;
        
        Obj_SetPosition(obj,x,y);
        Obj_SetSpeed(obj,v);
        Obj_SetAngle(obj,-35);
        ObjEffect_SetAngle(obj,0,0,-35);
        ObjEffect_SetRenderState(obj,ADD);
        ObjEffect_SetTexture(obj,textdata);
        ObjEffect_SetLayer(obj,2);

        ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
        ObjEffect_CreateVertex(obj, 4);
        ObjEffect_SetVertexXY(obj, 0, -1024, -16);
        ObjEffect_SetVertexUV(obj, 0, 0, 0);
        ObjEffect_SetVertexXY(obj, 1, 1024, -16);
        ObjEffect_SetVertexUV(obj, 1, 2048, 0);
        ObjEffect_SetVertexXY(obj, 2, -1024, 16);
        ObjEffect_SetVertexUV(obj, 2, 0, 32);
        ObjEffect_SetVertexXY(obj, 3, 1024, 16);
        ObjEffect_SetVertexUV(obj, 3, 2048, 32);

        while(! Obj_BeDeleted(obj)){
                  ObjEffect_SetVertexColor(obj,0,alphret,255,255,255);
                 ObjEffect_SetVertexColor(obj,1,alphret,255,255,255);
                  ObjEffect_SetVertexColor(obj,2,alphret,255,255,255);
                  ObjEffect_SetVertexColor(obj,3,alphret,255,255,255);
                if(as==0){alphret+=20; if(alphret>185){as=1;} }
                if(as==1){c++; if(c==90){as=2;} }                
                if(as==2){alphret-=10; if(alphret<0){ Obj_Delete(obj); } }
                yield;
        }
    }