//**************************************************************************************************************
//	Script: Cutin script ph3
//	version 1.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.
//**************************************************************************************************************/

#include "script/thdcs/functions/function_loaddata.txt"	

// enum
let KANAKO = 0;		
let NAZRIN = 1; 
let BYAKUREN = 2;
let DUET1 = 3;
let DUET2 = 4;
let DUET3 = 5;
let DUET4 = 6;

// Main 
task cutin(type, cutinImg, left, top, right, bottom, scale) {

	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	let alphaValue = 0;
	let as = 0;
	let frameCounter = 0;
	let x = 0;
	let y = 0;

    let XYBoundaryLeftRight = left + right / 2;
	let XYBoundaryTopBottom = top + bottom / 2;

	if(type == 0) { x = GetClipMaxX; y = GetCenterY+10; }
	if(type == 1) { x = GetClipMaxX+256; y = GetCenterY-120; }
	if(type == 2) { x = GetClipMinX-256; y = GetCenterY; }
	if(type == 3) { x = GetClipMinX-256; y = GetCenterY-120; }
	
	// handlers
	summonAnimatedText;
	summonAnimatedHexagon;
	modifyObjectAlpha(obj, as, alphaValue);
	
	ObjRender_SetPosition(obj, x, y, 0);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjPrim_SetTexture(obj, cutinImg);
	Obj_SetRenderPriority(obj, 0.31);	
	ObjRender_SetScaleXYZ(obj, scale, scale, 0);
	
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(obj, 4);
	ObjPrim_SetVertexPosition(obj, 0, -XYBoundaryLeftRight, -XYBoundaryTopBottom,0);
	ObjPrim_SetVertexUVT(obj, 0, left, top);
	ObjPrim_SetVertexPosition(obj, 1, XYBoundaryLeftRight, -XYBoundaryTopBottom,0);
	ObjPrim_SetVertexUVT(obj, 1, right, top);
	ObjPrim_SetVertexPosition(obj, 2, -XYBoundaryLeftRight, XYBoundaryTopBottom,0);
	ObjPrim_SetVertexUVT(obj, 2, left, bottom);
	ObjPrim_SetVertexPosition(obj, 3, XYBoundaryLeftRight, XYBoundaryTopBottom,0);
	ObjPrim_SetVertexUVT(obj, 3, right, bottom);

	while(!Obj_IsDeleted(obj)){
		if(type == 0) {
			if(frameCounter < 15) { x-=10; y+=10 }
			if(frameCounter >= 15 && frameCounter < 45) { y-=4; }
			if(frameCounter >= 45) { y-=2; }
			if(frameCounter > 120) { Obj_Delete(obj); }
		}
		if(type == 1){
			if(frameCounter < 21) { x-=20; y+=5 }
			if(frameCounter >= 21 && frameCounter < 100) { x--; y+=0.5; }
			if(frameCounter >= 100) { x-=20; y+=5; }
			if(frameCounter > 120) { Obj_Delete(obj); }
		}
		if(type == 2) {
			if(frameCounter < 21) { x+=20; }
			if(frameCounter >= 21 && frameCounter < 100) { x++; }
			if(frameCounter >= 100) { x+=20; }
			if(frameCounter > 120) { Obj_Delete(obj); }
		}
		ObjRender_SetPosition(obj,x,y,0);
		frameCounter++;
		yield;
	}
}

// Unusual, used only in the duet cards.
task cutin2(type, spellcardname, cutinImg, left, top, right, bottom, scale) {
	
	//loop(30) { yield; } 
	
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	let alphaValue = 0;
	let as = 0;
	let frameCounter = 0;
	let x = 0;
	let y = 0;

    let XYBoundaryLeftRight = left + right / 2;
	let XYBoundaryTopBottom = top + bottom / 2;

	if(type == DUET1) { x = GetClipMinX-256; y = GetCenterY-120; }
	if(type == DUET2) { x = GetClipMaxX+256; y = GetCenterY; }
	if(type == DUET3) { x = GetClipMinX-256; y = GetCenterY; }
	if(type == DUET4) { x = GetClipMaxX+256; y = GetCenterY+32; }

	// handlers
	modifyObjectAlpha(obj, as, alphaValue);
	
	ObjRender_SetPosition(obj, x, y, 0);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjPrim_SetTexture(obj, cutinImg);
	Obj_SetRenderPriority(obj, 0.31);	
	ObjRender_SetScaleXYZ(obj, scale, scale, 0);
	
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(obj, 4);
	ObjPrim_SetVertexPosition(obj, 0, -XYBoundaryLeftRight, -XYBoundaryTopBottom,0);
	ObjPrim_SetVertexUVT(obj, 0, left, top);
	ObjPrim_SetVertexPosition(obj, 1, XYBoundaryLeftRight, -XYBoundaryTopBottom,0);
	ObjPrim_SetVertexUVT(obj, 1, right, top);
	ObjPrim_SetVertexPosition(obj, 2, -XYBoundaryLeftRight, XYBoundaryTopBottom,0);
	ObjPrim_SetVertexUVT(obj, 2, left, bottom);
	ObjPrim_SetVertexPosition(obj, 3, XYBoundaryLeftRight, XYBoundaryTopBottom,0);
	ObjPrim_SetVertexUVT(obj, 3, right, bottom);

	while(!Obj_IsDeleted(obj)){	
		if(type == 3) {
			if(frameCounter < 21) { x+=20; y+=5 }
			if(frameCounter >= 21 && frameCounter < 70) { x++; y+=0.5; }
			if(frameCounter >= 70) { x+=20; y+=5; }
			if(frameCounter > 100) { Obj_Delete(obj); }
		}
		if(type == 4) {
			if(frameCounter < 24) { x-=20; }
			if(frameCounter >= 24 && frameCounter < 100) { x--; }
			if(frameCounter >= 100) { x-=20; }
			if(frameCounter > 120) { Obj_Delete(obj); }
		}	
		if(type == 5) {
			if(frameCounter < 17) { x+=20; }
			if(frameCounter >= 17 && frameCounter < 80) { x++; }
			if(frameCounter >= 80) { x+=20; }
			if(frameCounter > 120) { Obj_Delete(obj); }
		}		
		if(type == 6) {
			if(frameCounter < 17) { x-=20; }
			if(frameCounter >= 17 && frameCounter < 80) { x--; }
			if(frameCounter >= 80) { x-=20; }
			if(frameCounter > 120) { Obj_Delete(obj); }
		}		
		ObjRender_SetPosition(obj,x,y,0);
		frameCounter++;
		yield;
	}
}

// seperate function to make the fadein and fadeout effect.
task modifyObjectAlpha(obj, as, alphaValue) {
	let frameCounter = 0;
	loop {
		ObjRender_SetAlpha(obj, alphaValue); 
		if(as == 0) { alphaValue+=20; if(alphaValue > 250) { as = 1; } }
		if(as == 1) { frameCounter++; if(frameCounter == 90) { as = 2; } }
		if(as == 2) { alphaValue-=10; if(alphaValue < 10) { as = 4; } }	
		yield;
	}
}
    
// handler for summoning the diagonal text
task summonAnimatedText {
	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 summonAnimatedHexagon {
	let direction = 0;
	loop(8) {
		hexagonText(GetCenterX+100, GetCenterY+150, 1.5, direction, 1.4, 1.4);
		hexagonText(GetCenterX+100, GetCenterY+150,1.5, direction, 1.2, 1.2);
		hexagonText(GetCenterX+100, GetCenterY+150, -1.5, -direction, 1.0, 1.0);
		hexagonText(80, 100, 1.5, direction, 0.8, 0.8);
		direction+=45;
		yield;
	}
} 

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

task hexagonText(x, y, velocity, angle, scaleX, scaleY) {
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	let alphaValue = 0;
	let as = 0;
	let frameCounter = 0;

	ObjRender_SetPosition(obj, x, y, 0);	
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	ObjPrim_SetTexture(obj, textdata);
	ObjRender_SetScaleXYZ(obj, scaleX, scaleY, 0);
	Obj_SetRenderPriority(obj, 0.2);	

	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(obj, 4);

	ObjPrim_SetVertexPosition(obj, 0, -48, -128,0);
	ObjPrim_SetVertexUVT(obj, 0, 0, 0);
	ObjPrim_SetVertexPosition(obj, 1, 48, -128,0);
	ObjPrim_SetVertexUVT(obj, 1, 128, 0);
	ObjPrim_SetVertexPosition(obj, 2, -48, -104,0);
	ObjPrim_SetVertexUVT(obj, 2, 0, 32);
	ObjPrim_SetVertexPosition(obj, 3, 48, -104,0);
	ObjPrim_SetVertexUVT(obj, 3, 128, 32);

	while(!Obj_IsDeleted(obj)) {
		ObjRender_SetAngleZ(obj, angle);
		ObjRender_SetAlpha(obj, alphaValue); 

		if(as == 0) { alphaValue+=20; if(alphaValue > 185) { as=1; } }
		if(as == 1) { frameCounter++; if(frameCounter == 90){ as=2; } }		
		if(as == 2) { alphaValue-=10; if(alphaValue < 0) { Obj_Delete(obj); } }
		angle += velocity;

		yield;
	}
}

//************************************************************************
//	Text animation for spellcard spawn ( Diagonal text ) 
//   
//	alphaValue - alpha value
//	as - variable acting like a switch
//	frameCounter - counter variable
//************************************************************************

task spellcardText(x, y, velocity) {
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	let alphaValue = 0;
	let as = 0;
    let frameCounter = 0;
	let f = 0;
	
	ObjRender_SetPosition(obj, x, y, 0);	
	ObjMove_SetSpeed(obj, velocity);
	ObjRender_SetAngleZ(obj, -35);
	ObjPrim_SetTexture(obj, textdata);
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	Obj_SetRenderPriority(obj, 0.2);	

	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(obj, 4);
	
	ObjMove_CutSpecial(obj, x, y, velocity, -35);

	while(! Obj_IsDeleted(obj)){
		ObjPrim_SetVertexPosition(obj, 0, -1024, -16,0);
		ObjPrim_SetVertexUVT(obj, 0, 0, 0);
		ObjPrim_SetVertexPosition(obj, 1, 1024, -16,0);
		ObjPrim_SetVertexUVT(obj, 1, 2048, 0);
		ObjPrim_SetVertexPosition(obj, 2, -1024, 16,0);
		ObjPrim_SetVertexUVT(obj, 2, 0, 32);
		ObjPrim_SetVertexPosition(obj, 3, 1024, 16,0);
		ObjPrim_SetVertexUVT(obj, 3, 2048, 32);
	
		ObjRender_SetAlpha(obj,alphaValue); 
		if(as == 0) { alphaValue+=20; if(alphaValue > 185) { as=1; } }
		if(as == 1) { frameCounter++; if(frameCounter == 90) { as=2; } }		
		if(as == 2) { alphaValue-=10; if(alphaValue < 0) { Obj_Delete(obj); } }
		f += velocity;
		yield;
	}
}

task ObjMove_CutSpecial(obj, x, y, velocity, direction) {
	let velocityX = velocity * cos(direction);
	let velocityY = velocity * sin(direction);

	while(!Obj_IsDeleted(obj)) {
		ObjRender_SetPosition(obj,x,y,0);
		yield;
		x += velocityX;
		y += velocityY;
	}
}