let path=GetCurrentScriptDirectory~"img/Back2.png";
let path2=GetCurrentScriptDirectory~"img/Back1.png";
let objScene=GetEnemyBossSceneObjectID();
let bspell=false;
let timeslow=false;

task TCallBg{
	yield;
	while(1){
	while(bspell==false){ yield; }
	if(bspell==true){
	ChainSet;
	Card;
	Fade;
		TSpellBackground(1);
		TSpellBackground(-1);
		Cutin;
		}
	while(bspell==true){ yield; }
	yield;
	}
}

task TSpellBackground(mult){
	let c = 0;
	let s = 0;
	let value = 0;
	let count = 256/2;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,21);
	ObjPrim_SetTexture(obj,path);
	ObjSprite2D_SetSourceRect(obj,0,0,643,787);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,1,1,1.5);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetPosition(obj,384/2,448/2,0);
	ObjRender_SetAlpha(obj, 155);
	ObjRender_SetColor(obj, 255,255,255);
	while( !Obj_IsDeleted(obj) && bspell == true){
	ObjRender_SetAngleZ(obj, count*mult);
	ObjRender_SetPosition(obj, 384/2+45*cos(count), 448/2+45+45*sin(count),0);
	count+=0.2;
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}

task Cutin{
	let sinner = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,21);
	ObjPrim_SetTexture(obj,path2);
	ObjSprite2D_SetSourceRect(obj,0,0,280,480);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,1,1,1.5);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetPosition(obj,384/2,448/2,0);
	ObjRender_SetAlpha(obj, 100);
	ObjRender_SetColor(obj, 255,255,255);
	while( !Obj_IsDeleted(obj) && bspell == true){
	ObjRender_SetAlpha(obj, 160+(95*sin(sinner)) );
	sinner+=0.5;
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}

task Fade{
	let alpha = 255;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,22);
	ObjPrim_SetTexture(obj,GetCurrentScriptDirectory~"./img/black.png");
	ObjSprite2D_SetSourceRect(obj,0,0,256,256);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,5,5,5);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetAlpha(obj, 255);
	wait(60);
	loop(60){
		alpha-=255/60;
		ObjRender_SetAlpha(obj,alpha);
	yield;}
	Obj_Delete(obj);
}

task ChainSet{
	loop(6){
	Chain(GetStgFrameWidth/2, GetStgFrameHeight/2, rand(0, 0.4),  rand(0, 0.4),  rand(0, 360));
	}
}

task Chain(x, y, xvel, yvel, ang){
	let alpha = 255;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj,0.28);
	ObjPrim_SetTexture(obj,GetCurrentScriptDirectory~"./img/Card/chain.png");
	ObjSprite2D_SetSourceRect(obj,0,0,20,2200);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,1,1,1);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetAlpha(obj, 255);
	ObjRender_SetAngleZ(obj, ang);
	ObjRender_SetPosition(obj, x,y,0);
	let count = 0;
	loop(150){
	ObjSprite2D_SetSourceRect(obj,0,count,20,count-2200);
	ObjRender_SetPosition(obj, x+(count/6*xvel),y+(count/6*yvel),0);
	count+=6;
	yield;}
	
	loop(60){
	ObjSprite2D_SetSourceRect(obj,0,count,20,count-2200);
	ObjRender_SetPosition(obj, x+(count/6*xvel),y+(count/6*yvel),0);
	count+=6;
	
	alpha-=255/60;
	ObjRender_SetAlpha(obj,alpha);
	yield;}
	Obj_Delete(obj);
}

task Card{
let a =ObjPrim_Create(OBJ_SPRITE_2D);
let b =ObjPrim_Create(OBJ_SPRITE_2D);
let c =ObjPrim_Create(OBJ_SPRITE_2D);
let d =ObjPrim_Create(OBJ_SPRITE_2D);
let e =ObjPrim_Create(OBJ_SPRITE_2D);
let f =ObjPrim_Create(OBJ_SPRITE_2D);
let g =ObjPrim_Create(OBJ_SPRITE_2D);
let h =ObjPrim_Create(OBJ_SPRITE_2D);
let SplitCard = [a, b, c, d, e, f, g, h];

	ascent(i in 0..length(SplitCard)){
	ObjPrim_SetTexture(SplitCard[i], CardAr[i]);
	ObjRender_SetBlendType(SplitCard[i], BLEND_ALPHA);
	Obj_SetRenderPriority(SplitCard[i], 0.28);
	ObjSprite2D_SetSourceRect(SplitCard[i], 0, 0, 190, 500);
	ObjSprite2D_SetDestCenter(SplitCard[i]);
	ObjRender_SetColor(SplitCard[i], 255, 0, 255);
	ObjRender_SetAlpha(SplitCard[i], 0);
	ObjRender_SetScaleXYZ(SplitCard[i], 4, 4, 1);
	ObjRender_SetPosition(SplitCard[i], GetStgFrameWidth/2, GetStgFrameHeight/2, 0);
	ScaleDown(SplitCard[i]);
	}
	wait(30);
	let angle = 45;
	CornerMove(SplitCard[0],0, 180, -1);
	CornerMove(SplitCard[1],90, 90, 1);
	CornerMove(SplitCard[2],180, 90, 1);
	CornerMove(SplitCard[3],270, 180, -1);
	
	BorderSpin(SplitCard[4]);
	
	let Scaling = 0;
	
	ScaleUpABit(SplitCard[5], 1);
	ScaleUpABit(SplitCard[6], 200);
	
	ScalaleUpdown(SplitCard[5], 1.5);
	
	Spiiiin(SplitCard[5], 1);
	Spiiiin(SplitCard[6], -1);
	Spiiiin(SplitCard[7], 1);
	
	wait(60);
	
	AlphaDownAndDelete(SplitCard[5],100);
	AlphaDownAndDelete(SplitCard[6],100);
	wait(30);
	AlphaDownAndDelete(SplitCard[4],100);
	AlphaDownAndDelete(SplitCard[7],100);
	AlphaDownAndDelete(SplitCard[0],100);
	AlphaDownAndDelete(SplitCard[1],100);
	AlphaDownAndDelete(SplitCard[2],100);
	AlphaDownAndDelete(SplitCard[3],100);
	
	task AlphaDownAndDelete(obj,mult){
	let alpha = 255;
	loop(mult){
	alpha-=255/mult;
	ObjRender_SetAlpha(obj,alpha);
	yield;}
	Obj_Delete(obj);
	}
	
	task ScalaleUpdown(obj, mult){
	wait(35);
	while(!Obj_IsDeleted(obj)){
	Scaling+=0.002*mult;
	ObjRender_SetScaleXYZ(obj, 1+Scaling, 1+Scaling, 1);
	yield;}
	}
	
	task Spiiiin(obj, mult){
	wait(30);
		while(!Obj_IsDeleted(obj)){
		let angle = ObjRender_GetAngleZ(obj);
		ObjRender_SetAngleZ(obj, angle+(6*mult));
		yield;
		}
	}
	
	task ScaleUpABit(obj, mult){
	loop(5){
	Scaling+=0.0005*(mult*0.7);
	ObjRender_SetScaleXYZ(obj, 1+Scaling, 1+Scaling, 1);
	yield;}
	}
	
	
	task BorderSpin(obj){
	let scale = 0;
		while(!Obj_IsDeleted(obj)){
		let angle = ObjRender_GetAngleZ(obj);
		ObjRender_SetAngleZ(obj, angle+6);
		scale+=0.002;
		ObjRender_SetScaleXYZ(obj, 0.7+scale, 0.7+scale, 1);
		yield;
		}
	}
	
	task ScaleDown(obj){
	let alpha = 0;
	let scale = 0;
	loop(15){
	alpha+=4.1*4;
	ObjRender_SetAlpha(obj,alpha);
	scale+=0.055*4;
	ObjRender_SetScaleXYZ(obj, 4-scale, 4-scale, 1);
	yield;}
	}	
		task CornerMove(obj, i,mult, multi){
		let radius = 0;
		let X = GetStgFrameWidth/2;
		let Y = GetStgFrameHeight/2;
		loop(10){
		ObjRender_SetPosition(obj, X+radius*cos(angle+(i*360/4)+mult), Y+radius*sin(angle+(i*360/4)+mult), 0);
		radius+=6;
		yield;
		}
		wait(40);
		loop(160){
		ObjRender_SetPosition(obj, X+radius*cos(angle+(i*360/4)+mult), Y+radius*sin(angle+(i*360/4)+mult), 0);
		ObjRender_SetAngleZ(obj, ObjRender_GetAngleZ(obj)+(1*multi));
		radius-=0.5;
		yield;
		}
		}
}
let C1 =  GetCurrentScriptDirectory~"./img/Card/card1.png";
let C2 =  GetCurrentScriptDirectory~"./img/Card/card2.png";
let C3 =  GetCurrentScriptDirectory~"./img/Card/card3.png";
let C4 =  GetCurrentScriptDirectory~"./img/Card/card4.png";
let C5 =  GetCurrentScriptDirectory~"./img/Card/card5.png";
let C6 =  GetCurrentScriptDirectory~"./img/Card/card6.png";
let C7 =  GetCurrentScriptDirectory~"./img/Card/card7.png";
let C8 =  GetCurrentScriptDirectory~"./img/Card/card8.png";
let CardAr = [C1, C2, C3, C4, C5, C6, C7, C8];
