//===================================================================
// Spell line and Cutin for Phantasmagoria, and other scripts.
//===================================================================

//-------------------------------------------------------------------
// Spell line image
//-------------------------------------------------------------------

/** Directory */
let dir = GetCurrentScriptDirectory() ~ "Image\";

/** Spell line image and name */
let yono = dir ~ "yono.png";
let name = dir ~ "gibaku namaiyo.png";
LoadGraphic( yono );
LoadGraphic( name );

//-------------------------------------------------------------------
// Define Spell line Variables
//-------------------------------------------------------------------

/** Spell line type Kouma */
let CL_TT_TYPE_KOUMA	= 		1; 	// Embodiment of Scarlet Devil line
/** Spell line type IN */
let CL_TT_TYPE_EIYA =			 2;	// Imperishable Night line
/** Spell line type Flower */
let CL_TT_TYPE_KAEI =			3;	// Phantasmagoria of Flower View's line
/** Spell line type Magic */
let CL_TT_TYPE_GENSOKYO	=		 4;	// Lotus Land Story line
/** Spell line type Youmu */
//let CL_TT_TYPE_YOUMU =		 5;	// Perfect Cherry Blossom line
/** Spell line type Onmyo */
let CL_TT_TYPE_ONMYO =			6;	// Special Reimu line
/** Spell line type Suimu */
let CL_TT_TYPE_SUIMU = 			7;	// Immaterial and Missing Power line
/** Spell line type Blanks */
let CL_TT_TYPE_BLANK01 =		8;	// Blank line
let CL_TT_TYPE_BLANK02 =		9;	// Blank line

//-------------------------------------------------------------------
// CutIn Function
//-------------------------------------------------------------------

/**
*	Push the cutin image to the right, then disappear
*	with descent.
*
*	@param nImage	( Image directory )
*	@param nLeft	( Left boundary )
*	@param nTop	( Top boundary )
*	@param nRight	( Right boundary )
*	@param nBottom	( Bottom boundary )
*/
task PushCLCutIn(  let nImage, let nLeft, let nTop, let nRight, let nBottom ){
    yield;
    let nX = 0;
    let nY = 0;
    let nX2 = 0;
    let nY2 = 0;
    let backalpha = 165;
    let nCutInLR = nLeft + nRight / 2;
    let nCutInTB = nTop + nBottom / 2;
    let ObjR = Obj_Create( OBJ_EFFECT );
    let Obj = Obj_Create( OBJ_EFFECT );

    /** Obj Functions */
    ObjEffect_SetRenderState( Obj, ALPHA );
    ObjEffect_SetTexture( Obj, nImage );
    ObjEffect_SetLayer( Obj, 2 );
    ObjEffect_SetRenderState( ObjR, ALPHA );
    ObjEffect_SetTexture( ObjR, nImage );
    ObjEffect_SetLayer( ObjR, 2 );

    /** Vertex Setting */
    ObjEffect_SetPrimitiveType( Obj, PRIMITIVE_TRIANGLESTRIP );
    ObjEffect_CreateVertex( Obj, 4 );
    ObjEffect_SetVertexXY( Obj, 0, -nCutInLR, -nCutInTB );
    ObjEffect_SetVertexUV( Obj, 0, nLeft, nTop );
    ObjEffect_SetVertexXY( Obj, 1, nCutInLR, -nCutInTB );
    ObjEffect_SetVertexUV( Obj, 1, nRight, nTop );
    ObjEffect_SetVertexXY( Obj, 2, -nCutInLR, nCutInTB );
    ObjEffect_SetVertexUV( Obj, 2, nLeft, nBottom );
    ObjEffect_SetVertexXY( Obj, 3, nCutInLR, nCutInTB );
    ObjEffect_SetVertexUV( Obj, 3, nRight, nBottom );
    /** ObjR Vertex Setting */
    ObjEffect_SetPrimitiveType( ObjR, PRIMITIVE_TRIANGLESTRIP );
    ObjEffect_CreateVertex( ObjR, 4 );
    ObjEffect_SetVertexXY( ObjR, 0, -nCutInLR, -nCutInTB );
    ObjEffect_SetVertexUV( ObjR, 0, nLeft, nTop );
    ObjEffect_SetVertexXY( ObjR, 1, nCutInLR, -nCutInTB );
    ObjEffect_SetVertexUV( ObjR, 1, nRight, nTop );
    ObjEffect_SetVertexXY( ObjR, 2, -nCutInLR, nCutInTB );
    ObjEffect_SetVertexUV( ObjR, 2, nLeft, nBottom );
    ObjEffect_SetVertexXY( ObjR, 3, nCutInLR, nCutInTB );
    ObjEffect_SetVertexUV( ObjR, 3, nRight, nBottom );
    /** Set Vertex Color */
    ObjEffect_SetVertexColor( Obj, 0, 255, 255, 255, 255 );
    ObjEffect_SetVertexColor( Obj, 1, 255, 255, 255, 255 );
    ObjEffect_SetVertexColor( Obj, 2, 255, 255, 255, 255 );
    ObjEffect_SetVertexColor( Obj, 3, 255, 255, 255, 255 );
    /** Set Vertex Color( ObjR ) */
    ObjEffect_SetVertexColor( ObjR, 0, backalpha, 255, 255, 255 );
    ObjEffect_SetVertexColor( ObjR, 1, backalpha, 255, 255, 255 );
    ObjEffect_SetVertexColor( ObjR, 2, backalpha, 255, 255, 255 );
    ObjEffect_SetVertexColor( ObjR, 3, backalpha, 255, 255, 255 );

    /** Movement of CutIN */
    nX = GetClipMaxX-96; nY = GetClipMaxY-160; nX2 = GetCenterX-32; nY2 = GetCenterY+84;
    let n1X = 1; let n1Y = 1;
    let n2X = 13/4; let n2Y = 13/4;
    ObjEffect_SetScale( Obj, n1X, n1Y );
    ObjEffect_SetScale( ObjR, n2X, n2Y );
    descent(i in 0..56) {
             nX -= i^2*1/100; nY2 += i*1/2;
    }
    descent(i in 0..56) {
             nX += i^2*1/100; nY2 -= i*1/2;
             Obj_SetPosition( Obj, nX, nY );
             Obj_SetPosition( ObjR, nX2, nY2 );
             yield;
    }
    loop(15) { yield; }
    ascent(i in 0..20) {
             n1X += 3 * (i/190); n1Y -= i/190; n2X += ((7)*(i/190)); n2Y += ((7)*(i/190));
             ObjEffect_SetScale( Obj, n1X, n1Y );
             ObjEffect_SetScale( ObjR, n2X, n2Y );
             ObjEffect_SetVertexColor( ObjR, 0, backalpha*(20-i)/20, 255, 255, 255 );
             ObjEffect_SetVertexColor( ObjR, 1, backalpha*(20-i)/20, 255, 255, 255 );
             ObjEffect_SetVertexColor( ObjR, 2, backalpha*(20-i)/20, 255, 255, 255 );
             ObjEffect_SetVertexColor( ObjR, 3, backalpha*(20-i)/20, 255, 255, 255 );
             yield;
    }
    Obj_Delete( Obj ); Obj_Delete( ObjR );
}


//-------------------------------------------------------------------
// Spell line Function
//-------------------------------------------------------------------

/**
*	Push the spell line into the upper
*	part of the screen. Used only for thC sprites
*	Note: spellname graphic may be altered due
*	to height interference. So use the X and Y
*	parameters to set it properly.
*
*	@param LineSelect	( Line selection )
*/
task SetSpellLine( let LineSelect ){
	yield;
	let obj = Obj_Create( OBJ_EFFECT );
	let obj2 = Obj_Create( OBJ_EFFECT );
	ObjEffect_SetTexture( obj, yono );
	ObjEffect_SetLayer( obj, 5 );
	ObjEffect_SetPrimitiveType( obj, PRIMITIVE_TRIANGLEFAN );
	ObjEffect_CreateVertex( obj, 4 );
	ObjEffect_SetVertexUV( obj, 0, 0, 0 + 36 * LineSelect );
	ObjEffect_SetVertexUV( obj, 1, 512, 0 + 36 * LineSelect );
	ObjEffect_SetVertexUV( obj, 2, 512, 36 + 36 * LineSelect );
	ObjEffect_SetVertexUV( obj, 3, 0, 36 + 36 * LineSelect );
	ObjEffect_SetVertexXY( obj, 0, 0, - 32 );
	ObjEffect_SetVertexXY( obj, 1, 512, - 32 );
	ObjEffect_SetVertexXY( obj, 2, 512, 4 );
	ObjEffect_SetVertexXY( obj, 3, 0, 4 );
	/** Controls */
	task ControlSpellLine{
		let SY = 69; let SScale = 1;
		ascent( let i in 0 .. 60 ){ SY += i / 8; }
		ascent( i in 0 .. 30 ){ SScale += i / 300; }
		Obj_SetPosition( obj, GetClipMinX(), SY );
		ascent( let n in 0 .. 4 ){ ObjEffect_SetVertexColor( obj, n, 0, 255, 255, 255 ); }
		descent( i in 0 .. 30 ){
			SScale -= i / 300;
			ObjEffect_SetScale( obj, SScale, SScale );
			ascent( n in 0 .. 4 ){ ObjEffect_SetVertexColor( obj, n, ( 30 - i ) / 30 * 255, 255, 255, 255 ); }
			yield;
		}
		loop( 30 ){ yield; }
		descent( i in 0 .. 60 ){ SY -= i / 8; Obj_SetPosition( obj, GetClipMinX(), SY ); yield; }
	}
	ControlSpellLine;
}

