//
//	Author: Zhan_Fox
//	Version: 1.2
//	Functions:
//		MakeExplosionA1(object,type,red,green,blue) 		- Makes explosion with "obj" coordinate.
//		MakeExplosionA2(x,y,type,red,green,blue) 		- Makes explosion with "x,y" coordinate.
//		MakeConcentrationA1(object,type,red,green,blue,frames) 	- Makes concentration with "obj" coordinate. (type one, from small to big)
//		MakeConcentrationA2(x,y,type,red,green,blue,frames) 	- Makes concentration with "x,y" coordinate. (type one, from small to big)
//		MakeConcentrationB1(object,type,red,green,blue,frames) 	- Makes concentration with "obj" coordinate. (type two, from big to small)
//		MakeConcentrationB2(x,y,type,red,green,blue,frames) 	- Makes concentration with "x,y" coordinate. (type two, from big to small)
//		MakeConcentrationC1(object,type,red,green,blue) 	- Makes concentration with "obj" coordinate and time less than 1 second (because A1/A2 and B1/B2 couldn't use short time)
//		MakeConcentrationC1(x,y,type,red,green,blue) 		- Makes concentration with "x,y" coordinate and time less than 1 second (because A1/A2 and B1/B2 couldn't use short time)
//	Types:"FLOWER","HEART","MAGIC","MAPLE","NOTE","SAKURA","SNOW","STAR","MOON","COGWHEEL","BAN","CIRCLE","PENTAGON","HEXAGON".
#include"./../../Stage/Script/LoadGraphics.txt"

task MakeExplosionA1(obj,type,r,g,b){
	CircleExplosion(ObjRender_GetX(obj),ObjRender_GetY(obj),r,g,b);
	ScreenShake(2);
	loop(5){
	ascent(i in 0..15){
		Explosion(type,ObjMove_GetX(obj),ObjMove_GetY(obj),rand(0,180)*2,rand(4,10),r,g,b);}
		yield;}}

task MakeExplosionA2(x,y,type,r,g,b){
	CircleExplosion(x,y,r,g,b);
	ScreenShake(2);
	loop(5){
	ascent(i in 0..15){
		Explosion(type,x,y,rand(0,180)*2,rand(3,10),r,g,b);}
		yield;}}

task Explosion(type,xpos,ypos,angle,speed,r,g,b){
	let a = 0;
	let xangle = cos(angle);
	let yangle = sin(angle);
	let alpha = 0;
	let scale = 3;
		let angleX = rand(0,60);
		let angleY = rand(0,60);
		let angleZ = rand(0,60);
		let Xrot = rand(-2,2);
		let Yrot = rand(-2,2);
		let Zrot = rand(-2,2);
		
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	if(type=="FLOWER"){ObjPrim_SetTexture(obj,flower);}
	if(type=="HEART"){ObjPrim_SetTexture(obj,heart);}
	if(type=="MAGIC"){ObjPrim_SetTexture(obj,magic);}
	if(type=="MAPLE"){ObjPrim_SetTexture(obj,maple);}
	if(type=="NOTE"){ObjPrim_SetTexture(obj,note);}
	if(type=="SAKURA"){ObjPrim_SetTexture(obj,sakura);}
	if(type=="SNOW"){ObjPrim_SetTexture(obj,snow);}
	if(type=="STAR"){ObjPrim_SetTexture(obj,star);}
	if(type=="MOON"){ObjPrim_SetTexture(obj,moon);}
	if(type=="COGWHEEL"){ObjPrim_SetTexture(obj,cogwheel);}
	if(type=="BAN"){ObjPrim_SetTexture(obj,ban);}
	if(type=="CIRCLE"){ObjPrim_SetTexture(obj,circles);}
	if(type=="PENTAGON"){ObjPrim_SetTexture(obj,pentagon);}
	if(type=="HEXAGON"){ObjPrim_SetTexture(obj,hexagon);}
	ObjSprite2D_SetSourceRect(obj,0, 0, 64, 64);
	Obj_SetRenderPriorityI(obj,41);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,xpos,ypos,0);
	ObjRender_SetColor(obj,r,g,b);

        loop(80){
	let mx = (speed+a)*xangle;
	let my = (speed+a)*yangle;
        	ObjRender_SetAlpha(obj,alpha);
		ObjRender_SetScaleXYZ(obj,scale,scale,scale);
        	ObjRender_SetAngleXYZ(obj,angleX,angleY,angleZ);
		ObjRender_SetPosition(obj,ObjRender_GetX(obj)+mx,ObjRender_GetY(obj)+my,0);

		a-=0.06;
		angleX+=Xrot;
		angleY+=Yrot;
		angleZ+=Zrot;
		alpha+=3;
		scale-=0.04;
		yield;}
	Obj_Delete(obj);
	return;}


task CircleExplosion(x,y,r,g,b){
	let alpha = 0;
	let scale = 0;
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj,chargeg);
	ObjSprite2D_SetSourceRect(obj,0,0,128,128);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,x,y,0);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetAlpha(obj,150);
	Obj_SetRenderPriorityI(obj,40);
	ObjRender_SetColor(obj,r,g,b);
		loop(64){
			ObjRender_SetAlpha(obj,alpha);
			ObjRender_SetScaleXYZ(obj,scale,scale,0);
			scale+=0.2;
			alpha+=2;
			yield;}
		loop(64){
			ObjRender_SetAlpha(obj,alpha);
			ObjRender_SetScaleXYZ(obj,scale,scale,0);
			scale+=0.2;
			alpha-=2;
			yield;}
		Obj_Delete(obj);
	return;}

task MakeConcentrationA1(obj,type,r,g,b,frames){
	let dir = 0;
	CircleCharge(ObjRender_GetX(obj),ObjRender_GetY(obj),r,g,b,frames);
	loop(frames-60){
		Charge(type,ObjRender_GetX(obj)+280*cos(dir),ObjRender_GetY(obj)+280*sin(dir),
							dir,-5,r,g,b,0.02,0.04,60,false);
		dir+=rand(1,20)*5;
	yield;}}

task MakeConcentrationA2(x,y,type,r,g,b,frames){
	let dir = 0;
	CircleCharge(x,y,r,g,b,frames);
	loop(frames-60){
		Charge(type,x+280*cos(dir),y+280*sin(dir),dir,-6,r,g,b,0.02,0.04,60,false);
		dir+=rand(1,20)*5;
	yield;}}

task MakeConcentrationB1(obj,type,r,g,b,frames){
	let dir = 0;
	CircleCharge(ObjRender_GetX(obj),ObjRender_GetY(obj),r,g,b,frames);
	loop(frames-50){
		Charge(type,ObjRender_GetX(obj)+280*cos(dir),ObjRender_GetY(obj)+280*sin(dir),
							dir,-4,r,g,b,3,-0.04,45,true);
		dir+=rand(1,20)*5;
	yield;}}

task MakeConcentrationB2(x,y,type,r,g,b,frames){
	let dir = 0;
	CircleCharge(x,y,r,g,b,frames);
	loop(frames-50){
		Charge(type,x+250*cos(dir),y+250*sin(dir),dir,-6,r,g,b,3,-0.04,45,true);
		dir+=rand(1,20)*5;
	yield;}}

task Charge(type,xpos,ypos,angle,speed,r,g,b,min,max,time,nTime){
	let scale = min;
	let alpha = 150;
	let xangle = cos(angle);
	let yangle = sin(angle);
	let a = 0;
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	if(type=="FLOWER"){ObjPrim_SetTexture(obj,flower);}
	if(type=="HEART"){ObjPrim_SetTexture(obj,heart);}
	if(type=="MAGIC"){ObjPrim_SetTexture(obj,magic);}
	if(type=="MAPLE"){ObjPrim_SetTexture(obj,maple);}
	if(type=="NOTE"){ObjPrim_SetTexture(obj,note);}
	if(type=="SAKURA"){ObjPrim_SetTexture(obj,sakura);}
	if(type=="SNOW"){ObjPrim_SetTexture(obj,snow);}
	if(type=="STAR"){ObjPrim_SetTexture(obj,star);}
	if(type=="MOON"){ObjPrim_SetTexture(obj,moon);}
	if(type=="COGWHEEL"){ObjPrim_SetTexture(obj,cogwheel);}
	if(type=="BAN"){ObjPrim_SetTexture(obj,ban);}
	if(type=="CIRCLE"){ObjPrim_SetTexture(obj,circles);}
	if(type=="PENTAGON"){ObjPrim_SetTexture(obj,pentagon);}
	if(type=="HEXAGON"){ObjPrim_SetTexture(obj,hexagon);}
	ObjSprite2D_SetSourceRect(obj,0,0,64,64);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetAngleZ(obj,rand(0,20)*18);
	ObjRender_SetPosition(obj,xpos,ypos,0);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetAlpha(obj,150);
	Obj_SetRenderPriorityI(obj,40);
	ObjRender_SetColor(obj,r,g,b);

	loop(time){
	let mx = (speed+a)*xangle;
	let my = (speed+a)*yangle;
	if(nTime==true){
		ObjRender_SetAlpha(obj,alpha);
		ObjRender_SetScaleXYZ(obj,scale,scale,scale);
		ObjRender_SetPosition(obj,ObjRender_GetX(obj)+mx,ObjRender_GetY(obj)+my,0);
		scale+=max;
		alpha-=1.5;
		a-=0.1;}
	if(nTime==false){
		ObjRender_SetAlpha(obj,alpha);
		ObjRender_SetScaleXYZ(obj,scale,scale,scale);
		ObjRender_SetPosition(obj,ObjRender_GetX(obj)+mx,ObjRender_GetY(obj)+my,0);
		scale+=max;
		alpha-=1.5;
		a-=0;}
		yield;}
	Obj_Delete(obj);
	return;}

task CircleCharge(x,y,r,g,b,time){
	let alpha = 120;
	let scale = 0;
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj,chargeg);
	ObjSprite2D_SetSourceRect(obj,0,0,128,128);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,x,y,0);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetAlpha(obj,150);
	Obj_SetRenderPriorityI(obj,40);
	ObjRender_SetColor(obj,r,g,b);
		loop(time){
			ObjRender_SetAlpha(obj,alpha);
			ObjRender_SetScaleXYZ(obj,7-scale,7-scale,0);
		scale+=7/time;
		alpha-=120/time;
		yield;}
		Obj_Delete(obj);
	return;}

task ScreenShake(time){
	let shakeamount = 4;
	let delay = 1;
	
	loop(time){
	Set2DCameraFocusX(GetStgFrameWidth/2+shakeamount);
	Set2DCameraFocusY(GetStgFrameHeight/2+shakeamount);
	loop(delay){yield;}
	Set2DCameraFocusX(GetStgFrameWidth/2-shakeamount);
	Set2DCameraFocusY(GetStgFrameHeight/2-shakeamount);
	loop(delay){yield;}
	Set2DCameraFocusX(GetStgFrameWidth/2+shakeamount);
	Set2DCameraFocusY(GetStgFrameHeight/2-shakeamount);
	loop(delay){yield;}
	Set2DCameraFocusX(GetStgFrameWidth/2-shakeamount);
	Set2DCameraFocusY(GetStgFrameHeight/2+shakeamount);
	loop(delay){yield;}}
	
	Set2DCameraFocusX(GetStgFrameWidth/2);
	Set2DCameraFocusY(GetStgFrameHeight/2);}