//////////////////////
/// GENERAL STUFFS ///
//////////////////////
let dir = GetCurrentScriptDirectory();
let frmtime = 5;
let seobj=ObjSound_Create;
////////////////////////////////
/// (GENERAL) BULLET EFFECTS ///
////////////////////////////////
task StartSpawn (obj,spd,time,spellresist,autodelete,intersectionenable,colorR,colorG,colorB,blend)
{
	ObjShot_SetItemChange(obj,false);
	ObjRender_SetColor(obj,colorR,colorG,colorB);
	ObjRender_SetBlendType(obj,blend);
	ObjShot_SetSpellResist(obj,true);
	ObjShot_SetAutoDelete(obj,false);
	ObjShot_SetIntersectionEnable(obj,false);
	if(spd>0){ObjMove_SetSpeed(obj,spd);}
	let alpha=0;
	let xyz=2;
	while(alpha<256 && xyz>1)
	{
		if(alpha<256){alpha+=256/time;}
		if(xyz>1){xyz-=1/time;}
		ObjRender_SetAlpha(obj,alpha);
		if(spd>0){ObjMove_SetSpeed(obj,ObjMove_GetSpeed(obj)-spd/time);}
		ObjRender_SetScaleXYZ(obj,xyz,xyz,1);
		if(!Obj_IsDeleted(obj)){yield;}
	}
	ObjShot_SetSpellResist(obj,false);
	if(spellresist==0){ObjShot_SetSpellResist(obj,true);}
	ObjShot_SetAutoDelete(obj,true);
	if(autodelete==0){ObjShot_SetAutoDelete(obj,false);}
	ObjShot_SetIntersectionEnable(obj,true);
	if(intersectionenable==0){ObjShot_SetIntersectionEnable(obj,false);}
	if(spd>0){ObjMove_SetSpeed(obj,0);}
}
//////////////////////
/// VISUAL EFFECTS ///
//////////////////////
task ImageShow(source,x,y,x1,y1,x2,y2,getobject,duration,scale)
{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj,source);
	ObjSprite2D_SetSourceRect(obj,x1,y1,x2,y2);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,x,y,0);
	ObjRender_SetScaleXYZ(obj,scale,scale,1);
	let alpha=0;
	if(duration>0)
	{
		let count=0;
		loop(duration)
		{
			count++;
			if(alpha<256 && count<60){alpha+=256/60;}
			if(alpha>0 && count>duration-60){alpha-=256/60;}
			ObjRender_SetAlpha(obj,alpha);
			yield;
		}
	}
	if(duration<=0)
	{
		while(ObjEnemy_GetInfo(getobject, INFO_LIFE) > 0)
		{	
			if(alpha<256){alpha+=256/60;}
			ObjRender_SetAlpha(obj,alpha);
			yield;
		}
		while(alpha>0)
		{
			alpha-=256/60;
			ObjRender_SetAlpha(obj,alpha);
			yield;
		}
	}
	Obj_Delete(obj);
}
task ImageShowSpin(source,x,yo,yy,x1,y1,x2,y2,duration,scale1,scale2,ay0,ay1,maxalpha)
{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj,source);
	ObjSprite2D_SetSourceRect(obj,x1,y1,x2,y2);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetAngleY(obj,ay0);
	let alpha=0;
	let count=0;
	let xyz=scale2-scale1;
	let yd=yy-yo;
	loop(duration)
	{
		count++;
		if(alpha<maxalpha && count<duration/3){alpha+=maxalpha/(duration/3);}
		if(alpha>0 && count>duration/3*2){alpha-=maxalpha/(duration/3);}
		ObjRender_SetAlpha(obj,alpha);
		ObjRender_SetAngleY(obj,ay0);
		ObjRender_SetScaleXYZ(obj,scale1,scale1,1);
		ObjRender_SetPosition(obj,x,yo,0);
		scale1+=xyz/duration;
		yo+=yd/duration;
		ay0+=ay1;
		yield;
	}
	Obj_Delete(obj);
}
task SpellRemain(id1,idmax,angle,getobject,duration,type)
{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj,0.4);
	ObjPrim_SetTexture(obj,dir ~ "graphic/SpellCard.png");
	ObjSprite2D_SetSourceRect(obj,0,0,128,128);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,1/8,1/4,1);
	ObjRender_SetColor(obj,255,255,255);
	if(type==1){ObjRender_SetColor(obj,240,60,60);}
	let post=[0,0];
	position;
	task position
	{
		let ang=0;
		let aa=0;
		let count=0;
		loop
		{
			if(count<id1*20){aa+=9;}
			if(count==id1*20 && type==1){SEPlay(dir ~ "audio/j_hit.wav",100);}
			if(count==id1*20 && type==0){SEPlay(dir ~ "audio/j_barrier_on.wav",100);}
			post=[ObjMove_GetX(getobject),ObjMove_GetY(getobject)];
			ObjRender_SetPosition(obj,post[0]+64*cos(angle+ang),post[1]+64*sin(angle+ang),0);
			ObjRender_SetAngleZ(obj,angle+ang+90+aa);
			count++;
			ang++;
			yield;
		}
	}
	let alpha=0;
	if(duration>0)
	{
		let count=0;
		loop(duration)
		{
			count++;
			if(alpha<256 && count<60){alpha+=256/60;}
			if(alpha>0 && count>duration-60){alpha-=256/60;}
			ObjRender_SetAlpha(obj,alpha*0.4);
			yield;
		}
	}
	if(duration<=0)
	{
		while(ObjEnemy_GetInfo(getobject, INFO_LIFE) > 0)
		{	
			if(alpha<256){alpha+=256/60;}
			ObjRender_SetAlpha(obj,alpha*0.4);
			yield;
		}
		while(alpha>0)
		{
			alpha-=256/60;
			ObjRender_SetAlpha(obj,alpha*0.4);
			yield;
		}
	}
	Obj_Delete(obj);
}
////////////////////////
/// PARTICLE EFFECTS ///
////////////////////////
task Ring (x,y,duration,colorR,colorG,colorB,blend,sizescale,alpha,type)
{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj,dir ~ "graphic/Halo.png");
	ObjSprite2D_SetSourceRect(obj,0,0,512,512);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetBlendType(obj,blend);
	ObjRender_SetColor(obj,colorR,colorG,colorB);
	let alp=0;
	let count=0;
	let scalexyz=0;
	if(type==-1){scalexyz=1;}
	//type 1 = expand;
	//type-1 = shrink;
	loop(duration)
	{
		if(alp<1 && count<duration-(duration/3)){alp+=1/(duration/3);}
		if(alp>0 && count>=duration-(duration/3)){alp-=1/(duration/3);}
		scalexyz+=1/duration*type;
		ObjRender_SetAlpha(obj,alpha*alp);
		ObjRender_SetPosition(obj,x,y,0);
		ObjRender_SetScaleXYZ(obj,sizescale*scalexyz/512,sizescale*scalexyz/512,0);
		count++;
		yield;
	}
	Obj_Delete(obj);
}
task Charge(x,y,range,duration,colorR,colorG,colorB,maxalpha,blend,type,spinspeed,direction,sizescale)
{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj,dir ~ "graphic/Charge.png");
	ObjSprite2D_SetSourceRect(obj,0,0,512,512);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetBlendType(obj,blend);
	ObjRender_SetColor(obj,colorR,colorG,colorB);
	let minalpha=0;
	let c=0;
	if(type==-1){c=1;}
	let count=0;
	let multi=1;
	//type 1 = disperse;
	//type-1 = gather;
	loop(duration)
	{
		if(count>duration-(duration/3) || count<duration/3)
		{
			if(count<duration/3){multi=1;}
			if(count>duration-(duration/3)){multi=-1;}
			minalpha+=maxalpha/(duration/3)*multi;
		}
		c+=(1*type)/duration;
		ObjRender_SetAlpha(obj,minalpha);
		ObjRender_SetPosition(obj,x+range*c*sin(direction),y+range*c*cos(direction),0);
		ObjRender_SetScaleXYZ(obj,sizescale/512,sizescale/512,0);
		ObjRender_SetAngleZ(obj,direction);
		direction+=spinspeed;
		count++;
		yield;
	}
	Obj_Delete(obj);
}
///////////////////////
/// ENEMY ANIMATION ///
///////////////////////
task idlesprite(obj,duration,imgObj)
{
	ObjPrim_SetTexture(obj,imgObj);
	let xx=80;
	let yy=90;
	let c=1;
	let count=0;
	while(count<duration)
	{
		if(c>4){c=1;}
		loop(frmtime)
		{
			count++;
			if(count<=duration)
			{
				ObjSprite2D_SetSourceRect(obj,xx*(c-1),0,xx*c,yy);
				ObjSprite2D_SetDestCenter(obj);
				yield;
			}
		}
		c++;
	}
}
task movesprite(obj,duration,imgObj,type)
{
	ObjPrim_SetTexture(obj,imgObj);
	let xx=80;
	let yy=90;
	let dimY=0;
	if(type == -1){dimY=0;}  // Move to Right or Left
	if(type ==  1){dimY=yy;} // Move to Left  or Right
	loop(duration)
	{
		ObjSprite2D_SetSourceRect(obj,0,yy+dimY,xx,yy*2+dimY);
		ObjSprite2D_SetDestCenter(obj);
		yield;
	}
}
////////////////////
/// ITEMS EFFECT ///
////////////////////
task itemdrop(xx,yy,score)
{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj,0.4);
	ObjPrim_SetTexture(obj,dir ~ "graphic/Item.png");
	ObjSprite2D_SetSourceRect(obj,0,0,128,128);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetAlpha(obj,0);
	ObjRender_SetScaleXYZ(obj,0.15,0.15,1);
	ObjRender_SetColor(obj,60,240,240);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	if(xx>0 && xx<GetStgFrameWidth && yy>0 && yy<GetStgFrameHeight)
	{
		SEPlay(dir ~ "audio/bullet_del.wav",100);
		ScoreAdd(score);
		let ag=rand(0,360);
		let al=0;
		let w=30;
		let c=1;
		loop(w)
		{
			al+=256/w;
			ag+=90/w;
			c-=1/w;
			yy-=1*c;
			ObjRender_SetAlpha(obj,al);
			ObjRender_SetAngleZ(obj,ag);
			ObjRender_SetPosition(obj,xx,yy,0);
			yield;
		}
		loop(w)
		{
			al-=256/w;
			ag+=90/w;
			c+=1/w;
			yy+=1*c*2;
			ObjRender_SetAlpha(obj,al);
			ObjRender_SetAngleZ(obj,ag);
			ObjRender_SetPosition(obj,xx,yy,0);
			ObjRender_SetScaleXYZ(obj,0.15*(1+c),0.15*(1+c),1);
			yield;
		}
	}
	Obj_Delete(obj);
}
task ScoreAdd(score)
{
	let point=truncate(score/10);
	loop(score/point)
	{
		AddScore(point);
		if(GetScore%100000==0 && GetPlayerSpell<3){SpellAdd(1);}
		if(GetScore%400000==0 && GetPlayerLife<3){LifeAdd(1);}
		yield;
	}
}
task PowerAdd(power)
{
	let x=GetPlayerPower+power;
	loop(power/0.1)
	{
		if(GetPlayerPower<x){SetPlayerPower(GetPlayerPower+0.1);}
		if(GetPlayerPower>=x){SetPlayerPower(x);}
		yield;
	}
	SetPlayerPower(x);
}
task SpellAdd(spell)
{
	let x=GetPlayerSpell+spell;
	loop(spell/0.1)
	{
		if(GetPlayerSpell<x){SetPlayerSpell(GetPlayerSpell+0.1);}
		if(GetPlayerSpell>=x){SetPlayerSpell(x);}
		yield;
	}
	SetPlayerSpell(x);
	SEPlay(dir ~ "audio/bom_get.wav",100);
}
task LifeAdd(life)
{
	let x=GetPlayerLife+life;
	loop(life/0.1)
	{
		if(GetPlayerLife<x){SetPlayerLife(GetPlayerLife+0.1);}
		if(GetPlayerLife>=x){SetPlayerLife(x);}
		yield;
	}
	SetPlayerLife(x);
	SEPlay(dir ~ "audio/1up.wav",100);
}
/////////////////////
/// OTHER EFFECTS ///
/////////////////////
function GetDistance(x1,y1,x2,y2){return(((x2-x1)^2+(y2-y1)^2)^(1/2))}
task ScreenShake(time,shake)
{
	let c=1;
	loop(time)
	{
		Set2DCameraFocusX(GetStgFrameWidth/2+rand(-shake,shake)*c);
		Set2DCameraFocusY(GetStgFrameHeight/2+rand(-shake,shake)*c);
		c-=1/time;
		yield;
	}
	Set2DCameraFocusX(GetStgFrameWidth/2);
	Set2DCameraFocusY(GetStgFrameHeight/2);
}
task SEPlay(path,vol)
{
	ObjSound_Load(seobj,path);
	ObjSound_SetRestartEnable(seobj,false);
	ObjSound_SetLoopEnable(seobj,false);
	ObjSound_SetVolumeRate(seobj,vol*GetCommonData("sevol",100)/100);
	ObjSound_Play(seobj);
}