//**************************************************************************************************************
//	Script: Cutin script ph3
//	Author: Helepolis
//	version 1.0
//	Types:	KANAKO   - MOF   	Middle appear -> float to top -> leave top
//			NAZRIN   - SA/UFO	Diagonal right top entrance -> slowmode -> follow path to leave
//			BYAKUREN - UFO2  	Horizontal left entrance -> slowmode -> follow path to leave (Byakuren style)
//
//	Note:	Converted directly from 0.12m to ph3
//			Does not support spell name as in 0.12m
//**************************************************************************************************************/

// enum
let KANAKO = 0;
let NAZRIN = 1; 
let BYAKUREN = 2;

// Gets the centre X of stage field
function GetCenterX() {
	let calc = GetStgFrameWidth / 2;
	return calc;
}

// Gets the centre of stage field
function GetCenterY() {
	let calc = GetStgFrameHeight / 2; 
	return calc; 
}

// Gets minimum X of stage field ( x = 0 )
function GetClipMinX() { 
	let calc = GetStgFrameWidth - GetStgFrameWidth;
	return calc; 
}

// Gets maximum X of stage field
function GetClipMaxX() { 
	let calc = GetStgFrameWidth;
	return calc; 
}

// Gets minimum Y of stage field
function GetClipMinY() { 
	let calc = GetStgFrameHeight - GetStgFrameHeight;
	return calc; 
} 

// Gets minimum Y of stage field
function GetClipMaxY() { 
	let calc = GetStgFrameHeight;
	return calc; 
} 

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

	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	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 == 0) { x = GetClipMaxX; y = GetCenterY+10; }
	if(type == 1) { x = GetClipMaxX+256; y = GetCenterY-120; }
	if(type == 2) { x = GetClipMinX-256; y = GetCenterY; }

	// handlers
	summontext;
	summonhexagon;
	cutinEffect_Alphret(obj,as,alphret);
	
	ObjRender_SetPosition(obj,x,y,0);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjPrim_SetTexture(obj,cutinImg);
	Obj_SetRenderPriority(obj,0.21);	
	ObjRender_SetScaleXYZ(obj,1,1,0);
	
	ObjPrim_SetPrimitiveType(obj,PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(obj,4);
	ObjPrim_SetVertexPosition(obj, 0, -XYbLR, -XYbTB,0);
	ObjPrim_SetVertexUVT(obj, 0, left, top);
	ObjPrim_SetVertexPosition(obj, 1, XYbLR, -XYbTB,0);
	ObjPrim_SetVertexUVT(obj, 1, right, top);
	ObjPrim_SetVertexPosition(obj, 2, -XYbLR, XYbTB,0);
	ObjPrim_SetVertexUVT(obj, 2, left, bottom);
	ObjPrim_SetVertexPosition(obj, 3, XYbLR, XYbTB,0);
	ObjPrim_SetVertexUVT(obj, 3, right, bottom);

	while(!Obj_IsDeleted(obj)){
		if(type == 0) {
			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 == 1){
			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 == 2) {
			if(c < 21) { x+=20; }
			if(c >= 21 && c < 100) { x++; }
			if(c >= 100) { x+=20; }
			if(c > 120) { Obj_Delete(obj); }
		}
		ObjRender_SetPosition(obj,x,y,0);
		c++;
		yield;
	}
}

// seperate function to make the fadein and fadeout effect.
task cutinEffect_Alphret(obj,as,alphret) {
	let c = 0;
	loop{
		ObjRender_SetAlpha(obj,alphret); 
		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 textdata = GetCurrentScriptDirectory ~ "system/spellcardanm.png";

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

	ObjRender_SetPosition(obj,x,y,0);	
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjPrim_SetTexture(obj,textdata);
	ObjRender_SetScaleXYZ(obj,sx,sy,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,alphret); 

		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 = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	let alphret = 0;
	let as = 0;
    let c = 0;
	let f = 0;
	
	ObjRender_SetPosition(obj,x,y,0);	
	ObjMove_SetSpeed(obj,v);
	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,v,-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,alphret); 
		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); } }
		f += v;
		yield;
	}
}

task ObjMove_CutSpecial(obj,x,y,v,dir) {
	let vx = v * cos(dir);
	let vy = v * sin(dir);

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