
let objEnemy;
let DropItem=[0,0,0];
let DamageRate=[100,100];
let DamageRateRatio=1;

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

function SetItem(x,y,z)
{
	DropItem=[x,y,z];
}

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 ex = ObjMove_GetX(objEnemy);
	let ey = ObjMove_GetY(objEnemy);

	SetScriptArgument(idScript,0,DropItem[0]);
	SetScriptArgument(idScript,1,DropItem[1]);
	SetScriptArgument(idScript,2,DropItem[2]);
	SetScriptArgument(idScript,3,ex);
	if(ey>=0)
	{
	SetScriptArgument(idScript,4,ey);
	}
	else
	{
	SetScriptArgument(idScript,4,64);
	}
	StartScript(idScript);

	StartSlow(TARGET_ALL,60);

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

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);}
/*
	let ChallengeArray=GetAreaCommonData("SPELL_RECORD","Challenge",[0,0,0,0,0,0,0,0,0,0,0,0,0,0]);
	ChallengeArray[GetCommonData("SpellNumber",0)]=ChallengeArray[GetCommonData("SpellNumber",0)]+1;
	SetAreaCommonData("SPELL_RECORD","Challenge",ChallengeArray);
*/
}
if(GetCommonData("SpellNumber",0)<22)
{
SetCommonData("BombBarrierLevel",2);
}
else
{
SetCommonData("BombBarrierLevel",5);
}

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)
	{
	if(GetCommonData("SpecialStage",false)==true){x+=256}
	let color=[255,255,255];
	color=SetSpellCardColor(Name);

	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,"ＭＳ 明朝");

	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);
	}
}

}

function SetSpellCardColor(Name)
{
	alternative(Name)
	case("死符「ザ・ペイルホース」"){return([150,150,150]);}
	case("光符「ブリーシンガリング」"){return([150,150,150]);}
	case("戦符「ミストルティンブレード」"){return([150,150,150]);}

	case("摸法「シルフィホルン」"){return([150,255,150]);}
	case("摸法「アグニシャイン」"){return([255,150,150]);}
	case("摸法「レイジィトリリトン」"){return([200,100,50]);}
	case("摸法「メタルファティーグ」"){return([255,255,150]);}
	case("摸法「プリンセスウンディネ」"){return([150,150,255]);}

	case("木符「ウインドミキサー」"){return([150,255,150]);}
	case("火符「フレイムチェイサー」"){return([255,150,150]);}
	case("土符「リストリクトウォール」"){return([200,100,50]);}
	case("金符「オーリキャルクギア」"){return([255,255,150]);}
	case("水符「ナイアスキャタラクト」"){return([150,150,255]);}

	case("木火符「ミニットワイルドファイア」"){return([202,202,150]);}
	case("火土符「イグニシャルフロー」"){return([202,125,100]);}
	case("土金符「ダイヤモンドレイド」"){return([202,177,100]);}
	case("金水符「メタルハイドライド」"){return([202,202,202]);}
	case("水木符「サンダーコンダクター」"){return([150,202,202]);}

	case("月符「アルテミスヴェイパー」"){return([255,255,200]);}
	case("日符「イマジナリフレア」"){return([255,200,150]);}
	case("日符「ロイヤルプロミネンス」"){return([255,200,150]);}
	case("日符「ロイヤルスピキュール」"){return([255,200,150]);}
	case("火水木金土符「リバースエレメンツ」"){return([255,255,255]);}
	case("火水木金土符「エレメンタルコメット」"){return([255,255,255]);}

	return([255,255,255]);
}

#include".\SpellList.txt"
