#include "./../../../system/functions/functions_boss.txt"

let imgB = GetCurrentScriptDirectory ~ "img/Sprite_Boss.png";
let Spell = false;

let FACE_SEI_PLAIN = GetCurrentScriptDirectory ~ "./img/Face/Sei_Plain.png";
let FACE_SEI_SPEC = GetCurrentScriptDirectory ~ "./img/Face/Sei_Special.png";

let img_line = GetCurrentScriptDirectory ~ "./img/img_line.png";
let img_fam = GetCurrentScriptDirectory ~ "./img/img_fam.png";

@MainLoop
	{
	enmx = ObjMove_GetX(objEnemy);
	enmy = ObjMove_GetY(objEnemy);
	yield;
	}
@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
		{
		SetScriptResult(life);
		}
	case(EV_REQUEST_TIMER)
		{
		SetScriptResult(-1);
		}
	case(EV_REQUEST_SPELL_SCORE)
		{
		SetScriptResult(30000);
		}
}
@Finalize{DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);}
task TBackground
	{
	}

let SHI_MAIN = 0;
let SHI_CARD = 1;
let SHI_POINT = 2;
let SHI_HAND1 = 3;
let SHI_HAND2 = 4;

task ObjEnemy_Render(objEnemy)
{	
	let img = imgB;
	ObjPrim_SetTexture(objEnemy,img);
	ObjSprite2D_SetDestRect(objEnemy,-32,-32,32,32);

	let anim = 0;
	let spell = 0;
	let declare = 0;
	let spellD = 0;
	let render_x = 0;
	let render_y = 0;
	
	let count = 1/8;
	Obj_SetRenderPriority(objEnemy,0.34);

	while(!Obj_IsDeleted(objEnemy))
		{
		if(spellD != EnemySpell)
			{
			spell = 0;
			}
		spellD = EnemySpell;
		if(ObjMove_GetSpeed(objEnemy) > 0.1)
			{
			anim = 0;
			spell = 0;
			declare = 0;
			render_x = 2;
			render_y = 3;
			if(cos(ObjMove_GetAngle(objEnemy)) > cos(75))
				{
				ObjRender_SetScaleX(objEnemy,absolute(ObjRender_GetScaleX(objEnemy)));	
				}
			else if(cos(ObjMove_GetAngle(objEnemy)) < cos(105))
				{
				ObjRender_SetScaleX(objEnemy,-absolute(ObjRender_GetScaleX(objEnemy)));	
				}
			}
		else if(EnemySpell == -1 || declare > 0)
			{
			anim = 0;
			spell = 0;
			if(EnemySpell == -1)
				{
				if(declare < 3)
					{
					declare+=count;
					}
				}
			else
				{
				declare-=count;
				}
			if(declare < 2)
				{
				render_x = 3+floor(declare);
				render_y = 0;
				}
			else
				{
				render_x = 3;
				render_y = floor(declare)-1;
				}
			}	
		else if(EnemySpell == 1 || EnemySpell == 2)
			{
			anim = 0;
			declare = 0;
			if(spell < 3)
				{
				spell += count;
				render_x = 2+floor(spell);
				render_y = 0;
				}
			else if(EnemySpell == 1)
				{	
				if(spell < 6)
					{	
					spell += count;
					}
				render_x = 5;
				render_y = floor(spell)-3;
				}
			else
				{	
				if(spell < 5)
					{
					spell += count;
					}
				else
					{	
					spell = 5;
					}
				render_x = 4;
				render_y = floor(spell-3);
				}
			}
		else
			{
			anim+=count;
			spell = 0;
			declare = 0;	
			if(floor(anim)%4 < 3)
				{
				render_y = floor(anim)%4;
				}
			else
				{
				render_y = 1;
				}
			render_x = 0;
			}
		ObjSprite2D_SetSourceRect(objEnemy,render_x*128+32,render_y*128+32,(render_x+1)*128-32,(render_y+1)*128-32);
		yield;
		}
}
task SetEnemyAnim(val)
{
	Obj_SetValue(objEnemy,"cast",val);
}

task CreateScreenShake(time,pow,val)
{
	let obj = [];
	let flash = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(flash,"");
	Obj_SetRenderPriority(flash,0.7);
	ObjSprite2D_SetSourceRect(flash,0,0,1,1);
	ObjSprite2D_SetDestRect(flash,0,0,GetStgFrameWidth,GetStgFrameHeight);		
	Obj_SetVisible(flash,val);
	ObjRender_SetAlpha(flash,235);
	ObjRender_SetColorHSV(flash,60,35,255);
	let valA = [0,40,50,70,90,110,140,150,180];
	ascent(t in 0..time)
		{
		let d = (t*t)*pow/(time*time);
		let xA = rand(-1,1)*d;
		let yA = rand(-1,1)*d;

		ObjRender_SetPosition(obj,xA,yA,0);
		if(Obj_IsDeleted(objEnemy)){break;}
		if(val == true)
			{
			if(t >= time-17)
				{
				PlaySound("se_Shot1",60);
				Obj_SetVisible(flash,true);
				}		
			else
				{
				ascent(v in 0..length(valA))
					{
					if(t == valA[v])
						{
						PlaySound("se_Die4",60);
						Obj_SetVisible(flash,true);
						}
					else if(t == valA[v]+8)
						{
						Obj_SetVisible(flash,false);
						}	
					}
				}
			}
		yield;
		}
	ObjRender_SetPosition(obj,0,0,0);
	Obj_Delete(flash);
}
function CreateFumaRing(x,y,dist,time,dir)
	{
	let innerrad = dist-3;
	let outerrad = dist+3;
	let count = 46;
	count = trunc(count/2)*2;
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetTexture(obj, img_line);
	Obj_SetRenderPriority(obj, 0.3);
	ObjPrim_SetVertexCount(obj, count);
	ObjRender_SetPosition(obj,x,y,0);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetAlpha(obj,225);
	TGlow;
	return obj;
	task TGlow
		{
		let mov = trunc(rand(0,8));
		let d = 0;
		while(!Obj_IsDeleted(objEnemy) && !Obj_IsDeleted(obj))
			{
			mov+=1;	
			d+=(1-d)/(time*0.5);
			ascent(i in 0..count/2)
				{
				let indexvert = i*2;
				let angle = i*360/(count/2-1)-120;

				ObjPrim_SetVertexUVT(obj, indexvert, (count*0.5/4)*64/(count/2-1)*i+dir*mov*5, 0);
				ObjPrim_SetVertexUVT(obj, indexvert+1, (count*0.5/4)*64/(count/2-1)*i+dir*mov*5, 32);

				ObjPrim_SetVertexPosition(obj,indexvert+0,d*innerrad*cos(angle),d*innerrad*sin(angle),0);
				ObjPrim_SetVertexPosition(obj,indexvert+1,d*outerrad*cos(angle),d*outerrad*sin(angle),0);
				}
			yield;
			}
		Obj_Delete(obj);
		}
	}

task CreateFumaLine(x,y,xs,ys,aa,d,a,dir,time)
	{
	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, img_line);
	Obj_SetRenderPriority(obj, 0.3);
	
	let shot = CreateLaserA2(x,y,100000,a,d,12,time,ST_LASER,0,SHOT_BLUE);
	Obj_SetVisible(shot,false);
	ObjShot_SetAutoDelete(shot,false);
	let c = -1;
	while(!Obj_IsDeleted(objEnemy) && !Obj_IsDeleted(obj) && c < time+4)
		{
		c+=1;
		ObjSpriteList2D_ClearVertexCount(obj);
		let spread = min(c,4);
		let spreadA = max(0,c-time);
		ascent(i in spreadA..spread+1)
			{
			let aP = a-i*2.5*dir;
			ObjSpriteList2D_SetSourceRect(obj,-c*2-i*c,0,d-c*2-i*c,32);
			ObjRender_SetAlpha(obj,(4-i)*225/4);
			ObjRender_SetPosition(obj,x-i*xs*cos(aa)+0.5*d*cos(aP),y-i*ys*sin(aa)+0.5*d*sin(aP),0);
			ObjRender_SetScaleXYZ(obj,min(c,10)/10,6/32,0);
			ObjRender_SetAngleZ(obj,aP);
			ObjSpriteList2D_SetDestCenter(obj);
			ObjSpriteList2D_AddVertex(obj);
			}
		a+=dir;
		x+=xs*cos(aa);
		y+=ys*sin(aa);
		ObjMove_SetPosition(shot,x,y);
		ObjStLaser_SetAngle(shot,a);
		yield;
		}
	Obj_Delete(obj);
	Obj_Delete(shot);
	}

function CreateFumaFam(x,y,scM,type,alphM)
	{
	let obj = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(obj);	
	TMove;
	return obj;
	task TMove
		{
		ObjPrim_SetTexture(obj,img_fam);
		ObjSprite2D_SetSourceRect(obj,0,128,128,256);
		ObjSprite2D_SetDestRect(obj,0,0,1,1);
		ObjMove_SetPosition(obj,x,y);
		ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
		ObjRender_SetColorHSV(obj,300,255,255);
		let t = -1;
		let sc = 0;
		let a = -90;
		let alph = 1;
		let alphA = 180;
		if(type != 0)
			{
			sc = scM;
			a=0;
			alph = 0;
			}
		while(!Obj_IsDeleted(obj) && !Obj_IsDeleted(objEnemy))
			{
			if(sc < scM)
				{
				sc+=scM/30;
				a+=90/30+0.33*30;
				}
			else
				{
				a+=0.33;
				}
			if(Obj_GetValueD(obj,"FADE",false) == true)
				{
				if(alphA%360 < 180)
					{
					alphA += 2;
					}
				if(alph > 0)
					{
					alph-=1/20;
					}
				else
					{
					Obj_Delete(obj);
					}
				}
			else if(alph < 1)
				{
				alph+=1/10;
				}
			else
				{
				alphA+=2;
				}
			ObjRender_SetAngleZ(obj,a);
			ObjRender_SetScaleXYZ(obj,sc,sc,0);
			ObjRender_SetAlpha(obj,(alph+0.105*cos(alphA))*alphM);
			yield;
			}
		Obj_Delete(obj);
		}	
	}

//x,y,angle,scale,letter value,graphic,shot count,delay,linger time,delete time,color,alpha,delete type
task CreateHebrewShot(x,y,a,sc,n,g,spread,del,wait,delet,color,alph,type)
{
	let str;
	
	alternative(n)
	case(0)
	{
		str = [
			[ 0.0,   1,  0.25,-0.8,  0.5,0.5,  1,-0.8 ],
			[ 0.3,   0,  -0.8,-0.8,  0.8,0.8,  0,0 ],
			[ 0.7,   1,  -0.5,-0.5,  -0.6,0.8,  -0.8,-0.5 ],
			[ 0.95,   0,  -0.6,0.8,  -0.4,0.8,  0,0 ],
		];
	}
	case(1)
	{
		str = [
			[ 0.0,   0,  -0.5,-0.65,  0.4,-0.75,  0,0 ],
			[ 0.25,   1,  0.4,-0.75,  0.3,1,  0,0.5 ],
			[ 0.7,   0,  -0.6,1,  0.6,1,  0,0 ],
		];
	}
	case(2)
	{
		str = [
			[ 0.0,   1,  -0.5,-0.85,  0,0,  0.25,-1 ],
			[ 0.4,   1,  0,0,  0.5,1,  0,1 ],
			[ 0.8,   0,  0,0.5,  -0.6,1,  0,0 ],
		];
	}
	case(3)
	{
		str = [
			[ 0.0,   1,  -0.5,-1,  -0.25,-0.8,  -0.8,-0.8 ],
			[ 0.25,   0,  -0.25,-0.8,  0.65,-0.8,  0,0 ],
			[ 0.55,   0,  0.35,-0.8,  0.35,1,  0,0 ],
		];
	}
	case(4)
	{
		str = [
			[ 0.0,   0,  -0.6,-0.75,  0,-0.8, 0,0 ],
			[ 0.15,   1,  0,-0.8,  0.6,1, 1,-0.8 ],
			[ 0.65,   0,  -0.35,-0.5,  -0.5,1, 0,0 ],
		];
	}
	case(5)
	{
		str = [
			[ 0.0,   1,  -0.2,-0.85,  0.2,-0.5, 0,-0.5 ],
			[ 0.35,   1,  0.2,-0.5,  0.2,1, 0,1 ],
		];
	}
	case(6)
	{
		str = [
			[ 0.0,   1,  -0.35,-0.85,  0.25,-0.5, 0,-0.5 ],
			[ 0.35,   1,  0,-0.35,  0.2,1, -0.25,1 ],
		];
	}
	case(7)
	{
		str = [
			[ 0.0,   1,  -0.6,-0.85,  0,-0.65, -0.5,-0.65 ],
			[ 0.2,   1,  0,-0.65,  0.6,1, 1,-0.8 ],
			[ 0.7,   0,  -0.45,-0.75,  -0.5,1, 0,0 ],
		];
	}
	case(8)
	{
		str = [
			[ 0.0,   0,  -0.6,-0.6,  -0.1,1, 0,0 ],
			[ 0.35,   1,  -0.1,1,  0.75,0, 0.75,0.5 ],
			[ 0.7,   1,  0.75,0,  0,-0.6, 0.75,-0.75 ],
		];
	}
	case(9)
	{
		str = [
			[ 0.0,   1,  -0.25,-0.5,  0,0.5, 0.5,-0.5 ],
		];
	}
	case(10)
	{
		str = [
			[ 0.0,   1,  -0.5,-0.5,  0.3,1,  1,-1 ],
			[ 0.65,   0,  -0.6,1,  0.6,1,  0,0 ],
		];
	}
	case(11)
	{
		str = [
			[ 0.0,  1,  -0.4,-0.75,  -0.6,-0.25,  -0.4,-0.25 ],
			[ 0.25,  0,  -0.6,-0.25,  0.5,-0.25,  0,0 ],
			[ 0.55,  1,  0.5,-0.25,  -0.4,1,  0.5,0.5 ],
		];
	}
	case(12)
	{
		str = [
			[ 0.0,   1,  -0.3,-0.4,  0.6,1, 1,-1 ],
			[ 0.4,   1,  -0.6,-0.7,  -0.5,1, 0,-0.5 ],
			[ 0.85,   0,  -0.25,1,  0.5,1, 0,0 ],
		];
	}
	case(13)
	{
		str = [
			[ 0.0,   1,  -0.2,-0.85,  0.2,-0.5, 0,-0.5 ],
			[ 0.3,   1,  0.2,-0.5,  0.2,1, 0,1 ],
			[ 0.85,   0,  0.2,1,  -0.25,1, 0,0 ],
		];
	}
	case(14)
	{
		str = [
			[ 0.0,   0,  -0.6,-0.6,  -0.1,1, 0,0 ],
			[ 0.35,   1,  -0.1,1,  0.75,-0.35, 0.75,0.5 ],
			[ 0.7,   1,  0.75,-0.35,  -0.5,-0.4, 0.75,-0.75 ],
		];
	}
	case(15)
	{
		str = [
			[ 0.0,   0,  -0.5,-0.5,  0,0.75, 0,0 ],
			[ 0.3,   1,  0.6,-0.75,  0.1,0.75, 0.75,0.6 ],
			[ 0.75,   0,  0.1,0.75,  -0.5,1, 0,0.75 ],
		];
	}
	case(16)
	{
		str = [
			[ 0.0,   1,  0,0.25,  -0.3,-0.3, -0.5,0 ],
			[ 0.2,   1,  -0.3,-0.3,  0.6,1, 1,-1 ],
			[ 0.75,   0,  -0.25,1,  0.5,1, 0,0 ],
		];
	}
	case(17)
	{
		str = [
			[ 0.0,   1,  -0.5,-0.6,  0.5,0.8, 0.5,0.5 ],
			[ 0.4,   1,  0.5,0.8,  -0.5,1, 0.5,1 ],
			[ 0.75,   1,  0.5,-0.75,  0.25,0, 0.35,0 ],
		];
	}
	case(18)
	{
		str = [
			[ 0.0,   0,  -0.5,-0.6,  0.5,-0.6, 0,0 ],
			[ 0.3,   1,  0.5,-0.6,  -0.15,0.4, 0.5,0.4 ],
			[ 0.6,   0,  -0.4,-0.5,  -0.2,1, 0,0 ],
		];
	}
	case(19)
	{
		str = [
			[ 0.0,   0,  -0.5,-0.65,  0.4,-0.75,  0,0 ],
			[ 0.4,   1,  0.4,-0.75,  0.3,1,  0,0.5 ],
		];
	}
	case(20)
	{
		str = [
			[ 0.0,   0,  -0.6,-0.6,  -0.1,1, 0,0 ],
			[ 0.35,   1,  -0.1,1,  0.75,-0.5, 0.75,0.75 ],
			[ 0.75,   1,  -0.2,0.4,  0.25,-0.5, 0.25,0.25 ],
		];
	}
	case(21)
	{
		str = [
			[ 0.0,   0,  -0.5,-0.65,  0.4,-0.75,  0,0 ],
			[ 0.25,   1,  0.4,-0.75,  0.65,1,  0.2,0.5 ],
			[ 0.6,   1,  -0.25,-0.75,  -0.6,1,  -0.25,0.75 ],
		];
	}	
	
	let st = 0;
	ascent(i in 0..spread)
		{
		let c = i/(spread-1);

		let strA = [];
		let cA = 1;
		descent(i in 0 .. length(str))
			{
			if (str[i][0] <= c)
				{
				strA = str[i];
				break;
				}
			cA = str[i][0];
			}

		let lt = (c-strA[0])/(cA-strA[0]);
		let pos;
		if(strA[1] == 0)
			{
			pos = [strA[2]*(1-lt) + strA[4]*lt,
			strA[3]*(1-lt) + strA[5]*lt];
			}
		else
			{
			pos = [strA[2]*(1-lt)^2 + strA[4]*lt^2 + strA[6]*2*lt*(1-lt),
				strA[3]*(1-lt)^2 + strA[5]*lt^2 + strA[7]*2*lt*(1-lt)];
			}
		pos[0] = pos[0]*sc;
		pos[1] = pos[1]*sc;
		local
			{
			let aA = atan2(-pos[1],-pos[0]);
			let d = GetDistance(0,0,pos[0],pos[1]);
			pos[0] = d*cos(aA+a);
			pos[1] = d*sin(aA+a);
			} 	
		TSpam(pos[0],pos[1],i);
		st += del/spread;
		while(st > 0) { st--; yield; }
		}
	task TSpam(xA,yA,t)
		{
		let s = 0.2*GetDistance(0,0,xA,yA)/sc;
		let shot = CreateBulletA2(x+xA,y+yA,s,atan2(yA,xA),-s/30,0,0,g,4,0.25,color);
		ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
		ObjRender_SetColorHSV(shot,0,0,205);
		ObjRender_SetAlpha(shot,alph);
		ObjShot_SetSpellResist(shot,true);
		Obj_SetRenderPriority(shot,0.4);
		ObjShot_SetIntersectionEnable(shot,false);
		loop((spread-t)*del/spread+wait){yield;if(Obj_IsDeleted(shot)){return;}}	
		if(type == 0)
			{	
			let aA = atan2(ObjMove_GetY(shot)-y,ObjMove_GetX(shot)-x);
			let dA = GetDistance(x,y,ObjMove_GetX(shot),ObjMove_GetY(shot));
			ascent(t in 0..delet)
				{
				let d = (1-(t^2)/(delet^2));
				ObjMove_SetDestAtFrame(shot,x+d*dA*cos(aA),y+d*dA*sin(aA),1);
				ObjRender_SetAlpha(shot,alph*d);
				aA+=d*5;
				yield;
				}
			Obj_Delete(shot);
			return;
			}
		}
}