
let objEnemy;
let DamageRate=[100,100];
let DamageRateRatio=1;
let MoveClip=[64,64,320,160];

function GetX
{
	return(ObjRender_GetX(objEnemy));
}

function GetY
{
	return(ObjRender_GetY(objEnemy));
}

function GetLife
{
	return(ObjEnemy_GetInfo(objEnemy,INFO_LIFE));
}

function SetDamageRate(Shot_R,Bomb_R)
{
	ObjEnemy_SetDamageRate(objEnemy,Shot_R,Bomb_R);
}

function BossInit(X,Y)
{
	SetCommonData("DurableSpell", false);
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjMove_SetPosition(objEnemy,X,Y);
	ObjEnemy_Regist(objEnemy);
	BossDamageRate;
	BossRender;
	BossWork;
	BossEnd;
}

task BossDamageRate
{
	DamageRate=[5,5];
	while(!Obj_IsDeleted(objEnemy))
	{
		DamageRateRatio=GetCommonData("DamageRateRatio",1);
		ObjEnemy_SetDamageRate(objEnemy,DamageRate[0]*DamageRateRatio,DamageRate[1]*DamageRateRatio);
		yield;
	}
}

task BossRender
{
	InitializeAction();
	BossShadowAnimation(imgBoss,0,255,255,255);
	if(GetCommonData("Name","None")=="Chen")
	{
		while(!Obj_IsDeleted(objEnemy))
		{
			DrawBossRoll(imgBoss);
			yield;
		}
	}
	else
	{
		while(!Obj_IsDeleted(objEnemy))
		{
			DrawBoss(imgBoss);
			yield;
		}
	}
}

task BossEnd
{
	let path = GetCurrentScriptDirectory ~ "BossEnd.txt";
	let idScript = LoadScriptInThread(path);
	SetAutoDeleteObject(true);

	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0)
	{
		yield;
	}

	let ObjBullet_Array=GetShotIdInCircleA2(240,320,400,TARGET_ENEMY);
	let ObjBulletPosition_Array=[];
	ascent(i in 0..length(ObjBullet_Array))
	{
		ObjBulletPosition_Array=ObjBulletPosition_Array~[[ObjRender_GetX(ObjBullet_Array[i]),ObjRender_GetY(ObjBullet_Array[i]),ObjShot_GetImageID(ObjBullet_Array[i])]];
	}
	SetCommonData("ObjBulletPosition_Array",ObjBulletPosition_Array);

	StartSlow(TARGET_ALL,60);

	Obj_Delete(objEnemy);
	CloseScript(GetOwnScriptID());
}

task Move1(minMoveX,minMoveY,maxMoveX,maxMoveY,weight,speed)
{
	let ex = ObjMove_GetX(objEnemy);
	let ey = ObjMove_GetY(objEnemy);
	let MoveX= ex+rand(-maxMoveX,maxMoveX);
	let MoveY= ey+rand(-maxMoveY,maxMoveY);
	while(MoveX<MoveClip[0] || MoveX>MoveClip[2] || absolute(ex-MoveX)<minMoveX || absolute(ex-MoveX)>maxMoveX){MoveX=ex+rand(-maxMoveX,maxMoveX);}
	while(MoveY<MoveClip[1] || MoveY>MoveClip[3] || absolute(ey-MoveY)<minMoveY || absolute(ey-MoveY)>maxMoveY){MoveY=ey+rand(-maxMoveY,maxMoveY);}
	ObjMove_SetDestAtWeight(objEnemy,MoveX, MoveY,weight,speed);
}

task ObjRender_SetMovePositionBezier(obj,ControlX,ControlY,EndX,EndY,frame)
{
	let X=[ObjRender_GetX(obj)]~ControlX~[EndX];
	let Y=[ObjRender_GetY(obj)]~ControlY~[EndY];

	let num=length(X);

	let C=[];
	ascent(i in 0..num)
	{
		C=C~[factorial(num-1)/(factorial(num-1-i)*factorial(i))];
	}

	let x=0;
	let y=0;
	let k;
	let count=0;

	loop(frame)
	{
		k=count/frame;
		ascent(i in 0..num)
		{
			x+=C[i]*(1-k)^(num-1-i)*k^i*X[i];
			y+=C[i]*(1-k)^(num-1-i)*k^i*Y[i];
		}
		ObjRender_SetX(obj,x);
		ObjRender_SetY(obj,y);
		x=0;
		y=0;
		count+=1;
		yield;
    	}
}

function factorial(num)
{
	num=absolute(num);
	if(num==0){num=1;}
	let answer=num;
	while(num>1)
	{
		num-=1;
		answer*=num;
	}
	return(answer);
}


task CallSpellCard(Name)
{
	SetSpellCardNumber(Name);
	if(!IsReplay)
	{
		let ChallengeArray=[];
		alternative(GetAreaCommonData("TUNING","PlayerCharacter",0))
		case(0){ChallengeArray=GetAreaCommonData("SPELL_RECORD","Reimu_Challenge",[]);}
		case(1){ChallengeArray=GetAreaCommonData("SPELL_RECORD","Marisa_Challenge",[]);}
		ChallengeArray[GetCommonData("SpellNumber",0)]=ChallengeArray[GetCommonData("SpellNumber",0)]+1;
		alternative(GetAreaCommonData("TUNING","PlayerCharacter",0))
		case(0){SetAreaCommonData("SPELL_RECORD","Reimu_Challenge",ChallengeArray);}
		case(1){SetAreaCommonData("SPELL_RECORD","Marisa_Challenge",ChallengeArray);}
	}

	EV_DrawText(Name);

	task EV_DrawText(text)
	{
		ascent(i in 0..floor(length(text)/2))
		{
			EV_DrawLetter(372-19/2*length(text)+19*i,4,text[i*2..i*2+2]);
			loop(1){yield;}
		}

		task EV_DrawLetter(x,y,letter)
		{
			let color=[255,255,255];

			let obj = ObjText_Create();
			ObjText_SetText(obj, letter);
			ObjText_SetFontSize(obj, 18);
			ObjText_SetFontBold(obj, true);
			ObjText_SetFontColorTop(obj, 255, 255, 255);
			ObjText_SetFontColorBottom(obj, color[0], color[1], color[2]);
			ObjText_SetFontBorderType(obj, BORDER_NONE);
			ObjText_SetFontBorderColor(obj,0, 255, 0);
			ObjText_SetFontBorderWidth(obj, 0);
			Obj_SetRenderPriorityI(obj, 72);
			ObjRender_SetX(obj,16+x);
			ObjRender_SetY(obj, y);
			ObjText_SetFontType(obj,"‚l‚r –¾’©");

			ObjText_SetMaxWidth(obj,360);
			let Alpha=255;
			let scale=3;
			loop(15)
			{
				scale-=2/15;
				ObjRender_SetScaleXYZ(obj,scale,scale,scale);
				if(GetPlayerY<=160)
				{		
					Alpha=(GetPlayerY/160)*255;
				}
				else{Alpha=255;}
				ObjRender_SetAlpha(obj,Alpha);
				yield;
			}
			loop
			{
				if(GetPlayerY<=128)
				{		
					Alpha=(GetPlayerY/128)*255;
				}
				else{Alpha=255;}
				ObjRender_SetAlpha(obj,Alpha);
				yield;
			}
			Obj_Delete(obj);
		}
	}

}

#include".\SpellList.txt"
