let dir = GetCurrentScriptDirectory();
let frmtime = 5;
let seobj=ObjSound_Create;

task SpellDeclare(type,code)
{
	SpellBanner1(0.63,128,-1,type);
	SpellBanner1(0.61,128,1,type);
	let ag=0;
	loop(6)
	{
		SpellBanner2(64,0.18,ag,0.63,128,1,1);
		SpellBanner2(96,0.27,ag,0.63,128,-1.25,1);
		SpellBanner2(64,0.18,ag,0.61,128,1,-1);
		SpellBanner2(96,0.27,ag,0.61,128,-1.25,-1);
		ag+=60;
	}
	SpellCutin(code);
}

task SpellBanner1(rp,alp,m,type)
{
	let obj1=ObjPrim_Create(OBJ_SPRITE_2D);
	let obj2=ObjPrim_Create(OBJ_SPRITE_2D);
	let objx=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj1,rp);
	Obj_SetRenderPriority(obj2,rp);
	Obj_SetRenderPriority(objx,rp);
	ObjPrim_SetTexture(obj1,dir ~ "eff/SpellDeclare.png");
	ObjPrim_SetTexture(obj2,dir ~ "eff/SpellDeclare.png");
	ObjPrim_SetTexture(objx,dir ~ "eff/SpellCardName.png");
	ObjSprite2D_SetSourceRect(obj1,0,0,512,64);
	ObjSprite2D_SetSourceRect(obj2,0,64,512,128);
	ObjSprite2D_SetSourceRect(objx,0,64*(type-1),1024,64*type);
	ObjSprite2D_SetDestCenter(obj1);
	ObjSprite2D_SetDestCenter(obj2);
	ObjSprite2D_SetDestCenter(objx);
	ObjRender_SetAngleZ(obj1,30);
	ObjRender_SetAngleZ(obj2,30);
	ObjRender_SetAngleZ(objx,30);
	ObjRender_SetScaleXYZ(obj1,0.5,0.5,1);
	ObjRender_SetScaleXYZ(objx,0.4,0.4,1);
	ObjRender_SetBlendType(obj1,BLEND_ADD_ARGB);
	ObjRender_SetBlendType(obj2,BLEND_ADD_ARGB);
	ObjRender_SetBlendType(objx,BLEND_ADD_ARGB);
	let c=1;
	let al=0;
	let cc=0;
	loop(15)
	{
		cc++;
		ObjRender_SetAlpha(obj1,al);
		ObjRender_SetAlpha(obj2,al);
		ObjRender_SetAlpha(objx,al);
		ObjRender_SetPosition(obj1,GetStgFrameWidth/2+GetStgFrameWidth/8*3*m*c,GetStgFrameHeight/2+GetStgFrameHeight/16*3*m*(c-1),0);
		ObjRender_SetPosition(obj2,GetStgFrameWidth/2+GetStgFrameWidth/8*3*m*c,GetStgFrameHeight/2+GetStgFrameHeight/16*3*m*(c-1)-GetStgFrameHeight/12,0);
		ObjRender_SetPosition(objx,GetStgFrameWidth/2+GetStgFrameWidth/8*3*m*c,GetStgFrameHeight/2+GetStgFrameHeight/16*3*m*(c-1)-GetStgFrameHeight/6,0);
		al+=alp/15;
		c-=0.7/15;
		yield;
	}
	loop(90)
	{
		cc++;
		ObjRender_SetAlpha(obj1,al);
		ObjRender_SetAlpha(obj2,al);
		ObjRender_SetAlpha(objx,al);
		ObjRender_SetPosition(obj1,GetStgFrameWidth/2+GetStgFrameWidth/8*3*m*c,GetStgFrameHeight/2+GetStgFrameHeight/16*3*m*(c-1),0);
		ObjRender_SetPosition(obj2,GetStgFrameWidth/2+GetStgFrameWidth/8*3*m*c,GetStgFrameHeight/2+GetStgFrameHeight/16*3*m*(c-1)-GetStgFrameHeight/12,0);
		ObjRender_SetPosition(objx,GetStgFrameWidth/2+GetStgFrameWidth/8*3*m*c,GetStgFrameHeight/2+GetStgFrameHeight/16*3*m*(c-1)-GetStgFrameHeight/6,0);
		al-=alp/8;
		c-=0.6/90;
		if(cc%4==0){alp*=-1;}
		yield;
	}
	if(alp<0){alp*=-1;}
	loop(15)
	{
		cc++;
		ObjRender_SetAlpha(obj1,al);
		ObjRender_SetAlpha(obj2,al);
		ObjRender_SetAlpha(objx,al);
		ObjRender_SetPosition(obj1,GetStgFrameWidth/2+GetStgFrameWidth/8*3*m*c,GetStgFrameHeight/2+GetStgFrameHeight/16*3*m*(c-1),0);
		ObjRender_SetPosition(obj2,GetStgFrameWidth/2+GetStgFrameWidth/8*3*m*c,GetStgFrameHeight/2+GetStgFrameHeight/16*3*m*(c-1)-GetStgFrameHeight/12,0);
		ObjRender_SetPosition(objx,GetStgFrameWidth/2+GetStgFrameWidth/8*3*m*c,GetStgFrameHeight/2+GetStgFrameHeight/16*3*m*(c-1)-GetStgFrameHeight/6,0);
		al-=alp/15;
		c-=0.7/15;
		yield;
	}
	Obj_Delete(obj1);
	Obj_Delete(obj2);
	Obj_Delete(objx);
}

task SpellBanner2(r,xyz,ag,rp,alp,m,mp)
{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj,rp);
	ObjPrim_SetTexture(obj,dir ~ "eff/SpellDeclare.png");
	ObjSprite2D_SetSourceRect(obj,0,0,512,64);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	let al=0;
	let mm=1.5;
	let cc=0;
	loop(15)
	{
		cc++;
		ObjRender_SetAlpha(obj,al);
		ObjRender_SetPosition(obj,GetStgFrameWidth/2-GetStgFrameWidth/8*3*mp+r*sin(ag),GetStgFrameHeight/2+GetStgFrameHeight/8*3*mp+r*cos(ag),0);
		ObjRender_SetScaleXYZ(obj,xyz,xyz,1);
		ObjRender_SetAngleZ(obj,-ag+180);
		al+=alp/15;
		ag+=m*mm;
		yield;
	}
	loop(90)
	{
		cc++;
		if(mm>0.5){mm-=3/15;}
		ObjRender_SetAlpha(obj,al);
		ObjRender_SetPosition(obj,GetStgFrameWidth/2-GetStgFrameWidth/8*3*mp+r*sin(ag),GetStgFrameHeight/2+GetStgFrameHeight/8*3*mp+r*cos(ag),0);
		ObjRender_SetScaleXYZ(obj,xyz,xyz,1);
		ObjRender_SetAngleZ(obj,-ag+180);
		al-=alp/8;
		if(cc%4==0){alp*=-1;}
		ag+=m*mm;
		yield;
	}
	if(alp<0){alp*=-1;}
	loop(15)
	{
		cc++;
		if(mm<3){mm+=3/15;}
		ObjRender_SetAlpha(obj,al);
		ObjRender_SetPosition(obj,GetStgFrameWidth/2-GetStgFrameWidth/8*3*mp+r*sin(ag),GetStgFrameHeight/2+GetStgFrameHeight/8*3*mp+r*cos(ag),0);
		ObjRender_SetScaleXYZ(obj,xyz,xyz,1);
		ObjRender_SetAngleZ(obj,-ag+180);
		r+=3;
		xyz+=0.002815*3;
		al-=alp/15;
		ag+=m*mm;
		yield;
	}
	Obj_Delete(obj);
}

task SpellCutin(code)
{
	let path;
	let xx=0;
	if(code=="tenkai"){path=dir ~ "eff/Tenkai.png";}
	if(code=="senri"){path=dir ~ "eff/Senri.png";}
	if(code=="both"){path=dir ~ "eff/Senkai.png";xx=32;}
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj,0.62);
	ObjPrim_SetTexture(obj,path);
	ObjSprite2D_SetSourceRect(obj,0,0,768,1024);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,480/1024,480/1024,1);
	let m=1;
	let c=1;
	let al=0;
	loop(15)
	{
		ObjRender_SetAlpha(obj,al);
		ObjRender_SetPosition(obj,GetStgFrameWidth/2-GetStgFrameWidth/32*m*c+xx,GetStgFrameHeight/2+GetStgFrameHeight/32*m*(c-1),0);
		al+=256/15;
		c-=0.7/15;
		yield;
	}
	loop(90)
	{
		ObjRender_SetPosition(obj,GetStgFrameWidth/2-GetStgFrameWidth/32*m*c+xx,GetStgFrameHeight/2+GetStgFrameHeight/32*m*(c-1),0);
		c-=0.6/90;
		yield;
	}
	loop(15)
	{
		ObjRender_SetAlpha(obj,al);
		ObjRender_SetPosition(obj,GetStgFrameWidth/2-GetStgFrameWidth/32*m*c+xx,GetStgFrameHeight/2+GetStgFrameHeight/32*m*(c-1),0);
		al-=256/15;
		c-=0.7/15;
		yield;
	}
	Obj_Delete(obj);
}

task BossMarker(BossObj)
{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj,1);
	ObjPrim_SetTexture(obj,dir ~ "eff/BossMarker.png");
	ObjSprite2D_SetSourceRect(obj,0,0,128,64);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetScaleXYZ(obj,0.25,0.25,1);
	let al=256;
	let alp=32;
	let cc=0;
	while(!Obj_IsDeleted(obj))
	{
		cc++;
		ObjRender_SetPosition(obj,ObjRender_GetX(BossObj)+90,475,0);
		ObjRender_SetAlpha(obj,al);
		al-=alp;
		if(cc%4==0){alp*=-1;}
		yield;
	}
	Obj_Delete(obj);
}

task SpellLeft(obj,count,out,blend)
{
	let a=rand(0,360);
	let ct=0;
	loop(count+out)
	{
		ct++;
		SpellCardLeft(ct,a);
		a+=360/(count+out)*3;
	}
	
	task SpellCardLeft(number,a)
	{
		let objx=ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(objx,dir ~ "eff/barrier.png");
		ObjSprite2D_SetSourceRect(objx,0,0,256,256);
		ObjSprite2D_SetDestCenter(objx);
		ObjRender_SetBlendType(objx,blend);
		let c=0;
		while(!Obj_IsDeleted(obj))
		{
			if(c<1){c+=1/60;}
			ObjRender_SetPosition(objx,ObjRender_GetX(obj)+60*c*sin(a),ObjRender_GetY(obj)+60*c*cos(a),1);
			ObjRender_SetScaleXYZ(objx,1/16*c,1/16*c,1);
			ObjRender_SetAngleZ(objx,-a+90);
			ObjRender_SetAlpha(objx,128*c);
			if(number>count){ObjRender_SetAlpha(objx,32*c);}
			a+=2;
			yield;
		}
		loop(30)
		{
			c-=1/30;
			ObjRender_SetPosition(objx,ObjRender_GetX(obj)+60*c*sin(a),ObjRender_GetY(obj)+60*c*cos(a),1);
			ObjRender_SetScaleXYZ(objx,1/16*c,1/16*c,1);
			ObjRender_SetAngleZ(objx,-a+90);
			ObjRender_SetAlpha(objx,128*c);
			if(number>count){ObjRender_SetAlpha(objx,32*c);}
			a+=2;
			yield;
		}
		Obj_Delete(objx);
	}
}

task Barrier(x,y,time,scale,av)
{
	SEPlay(dir ~ "se/laser_2.wav");
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj,0.5);
	ObjPrim_SetTexture(obj,dir ~ "eff/Barrier.png");
	ObjSprite2D_SetSourceRect(obj,0,0,256,256);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetBlendType(obj,BLEND_SUBTRACT);
	ObjRender_SetPosition(obj,x,y,0);
	let xyz=0;
	let a=rand(0,360);
	let c=0;
	loop(10)
	{
		xyz+=1/10;
		c+=1/10;
		ObjRender_SetScaleXYZ(obj,scale/256*xyz,scale/256*xyz,0);
		ObjRender_SetAngleZ(obj,a);
		a+=av*c;
		yield;
	}
	loop(time-20)
	{
		ObjRender_SetAngleZ(obj,a);
		a+=av*c;
		yield;
	}
	loop(10)
	{
		xyz-=1/10;
		c-=1/10;
		ObjRender_SetScaleXYZ(obj,scale/256*xyz,scale/256*xyz,0);
		ObjRender_SetAngleZ(obj,a);
		a+=av*c;
		yield;
	}
	Obj_Delete(obj)
}

task Charge(x,y,time,r,g,b,type,blend)
{
	let alp=64;
	let z=rand(0,360);
	let d=time*rand(0.5,2);
	if(type==-1){d*=2;}
	let scale=rand(0.4,0.2);
	if(type==-1){scale*=2;}
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj,0.4);
	ObjPrim_SetTexture(obj,dir ~ "eff/Charge.png");
	ObjSprite2D_SetSourceRect(obj,0,0,256,256);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetBlendType(obj,blend);
	let rr=255-r;
	let gg=255-g;
	let bb=255-b;
	let al=0;
	let c=0;
	let cc=0;
	if(type==1){c=0;}
	if(type==-1){c=1;}
	ObjRender_SetAngleZ(obj,rand(0,360));
	loop(10)
	{
		cc+=1/10;
		al+=alp/10;
		c+=(1*type)/time;
		ObjRender_SetAlpha(obj,al/2);
		ObjRender_SetColor(obj,255-rr*cc,255-gg*cc,255-bb*cc);
		ObjRender_SetPosition(obj,x+d*c*sin(z),y+d*c*cos(z),0);
		ObjRender_SetScaleXYZ(obj,scale*(1-c),scale*(1-c),0);
		yield;
	}
	loop(time-20)
	{
		c+=(1*type)/time;
		ObjRender_SetAlpha(obj,al/2);
		ObjRender_SetColor(obj,r,g,b);
		ObjRender_SetPosition(obj,x+d*c*sin(z),y+d*c*cos(z),0);
		ObjRender_SetScaleXYZ(obj,scale*(1-c),scale*(1-c),0);
		yield;
	}
	loop(10)
	{
		cc-=1/10;
		al-=alp/10;
		c+=(1*type)/time;
		ObjRender_SetAlpha(obj,al/2);
		ObjRender_SetColor(obj,255-rr*cc,255-gg*cc,255-bb*cc);
		ObjRender_SetPosition(obj,x+d*c*sin(z),y+d*c*cos(z),0);
		ObjRender_SetScaleXYZ(obj,scale*(1-c),scale*(1-c),0);
		yield;
	}
	Obj_Delete(obj);
}

task movesprite(obj,anim,dimY,rangeX,rangeY,imgObj,time) 
{
	let c=0;
	ObjPrim_SetTexture(obj,imgObj);
	loop(anim-1)
	{
		loop(frmtime)
		{
			ObjSprite2D_SetSourceRect(obj,rangeX*c,dimY,rangeX*(c+1),rangeY+dimY);
			ObjSprite2D_SetDestCenter(obj);
			yield;
		}
		c++;
	}
	c=anim-1;
	loop(time-(frmtime*(anim-1)*2))
	{
		ObjSprite2D_SetSourceRect(obj,rangeX*c,dimY,rangeX*(c+1),rangeY+dimY);
		ObjSprite2D_SetDestCenter(obj);
		yield;
	}
	loop(anim-1)
	{
		c--;
		loop(frmtime)
		{
			ObjSprite2D_SetSourceRect(obj,rangeX*c,dimY,rangeX*(c+1),rangeY+dimY);
			ObjSprite2D_SetDestCenter(obj);
			yield;
		}
	}
}

task idlesprite(obj,anim,rangeX,rangeY,imgObj)
{
	let c=0;
	ObjPrim_SetTexture(obj,imgObj);
	while(!Obj_IsDeleted(obj))
	{
		loop(anim-1)
		{
			loop(frmtime)
			{
				ObjSprite2D_SetSourceRect(obj,rangeX*c,0,rangeX*(c+1),rangeY);
				ObjSprite2D_SetDestCenter(obj);
				yield;
			}
			c++;
		}
		loop(anim-1)
		{
			loop(frmtime)
			{
				ObjSprite2D_SetSourceRect(obj,rangeX*c,0,rangeX*(c+1),rangeY);
				ObjSprite2D_SetDestCenter(obj);
				yield;
			}
			c--;
		}
	}
}

task ScreenShaker(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 itemdrop(xx,yy,score)
{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj,0.4);
	ObjPrim_SetTexture(obj,dir ~ "eff/Item.png");
	ObjSprite2D_SetSourceRect(obj,0,0,128,128);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetAlpha(obj,0);
	ObjRender_SetScaleXYZ(obj,0.05+0.05*(score/100),0.05+0.05*(score/100),1);
	ObjRender_SetColor(obj,0,255,255);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	if(xx>0 && xx<GetStgFrameWidth && yy>0 && yy<GetStgFrameHeight)
	{
		let ag=rand(0,360);
		let al=0;
		let w=20;
		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);
			yield;
		}
		ScoreAdd(score);
	}
	Obj_Delete(obj);
}

task otherdrop(xx,yy,score,type)
{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj,0.4);
	ObjPrim_SetTexture(obj,dir ~ "eff/Item.png");
	ObjSprite2D_SetSourceRect(obj,0,0,128,128);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetAlpha(obj,0);
	ObjRender_SetScaleXYZ(obj,0.05+0.05*(score/100),0.05+0.05*(score/100),1);
	ObjRender_SetColor(obj,255,0,0);
	if(type=="p"){ObjRender_SetColor(obj,255,0,0);}
	if(type=="s"){ObjRender_SetColor(obj,0,255,0);}
	if(type=="l"){ObjRender_SetColor(obj,255,0,255);}
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	if(xx>0 && xx<GetStgFrameWidth && yy>0 && yy<GetStgFrameHeight)
	{
		let ag=rand(0,360);
		let al=0;
		let w=20;
		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);
			yield;
		}
		if(type=="p"){PowerAdd(score);}
		if(type=="s"){SpellAdd(score/100);}
		if(type=="l"){LifeAdd(score/100);}
	}
	Obj_Delete(obj);
}

function GetDistances(x1,y1,x2,y2)
{
	return(((x2-x1)^2+(y2-y1)^2)^(1/2))
}

task SEPlay(path)
{
	ObjSound_Load(seobj,path);
	ObjSound_SetRestartEnable(seobj,false);
	ObjSound_SetLoopEnable(seobj,false);
	ObjSound_SetVolumeRate(seobj,GetCommonData("sevol",100));
	ObjSound_Play(seobj);
}

task scoring(graze,life,bomb,power)
{
	let dif=GetCommonData("diff",1);
	let mul=1+((dif-2)*0.2);
	ScoreAdd(graze*10*mul);
	ScoreAdd(power*100*mul);
	ScoreAdd(bomb*1000*mul);
	ScoreAdd(life*10000*mul);
}

task ScoreAdd(score)
{
	loop(30)
	{
		AddScore(score/30);
		yield;
	}
}

task PowerAdd(score)
{
	let p=GetPlayerPower;
	loop(30)
	{
		SetPlayerPower(p+score/30);
		yield;
	}
	SetPlayerPower(p+score);
}

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);
	if(GetPlayerSpell%1==0){SEPlay(dir ~ "se/bom_get.wav");}
}

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);
	if(GetPlayerLife%1==0){SEPlay(dir ~ "se/1up.wav");}
}