#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Coloring \"Dance of the Rare Man\""]

#include"./Cutin.txt"
#include"./../../Functions/Effects/Effects.txt"
#include"./LoadGraphics.txt"
#include"./LoadSounds.txt"

let bossObj;
let bossX;
let bossY;
let damage = 0;

#include"./MiyakoDrawBoss.txt"

@Initialize{	
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);
	bossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(bossObj);
	SetAutoDeleteObject(true);

	animateBossM;
	LoadShotData;
	TEnemyMarker;
	mainTask;
	TEnd;}

@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){SetScriptResult(500);}
	case(EV_REQUEST_TIMER){SetScriptResult(60);}
	case(EV_REQUEST_SPELL_SCORE){SetScriptResult(10000000);}
	case(EV_START_BOSS_SPELL){
		let path = GetCurrentScriptDirectory ~ "./../../System/Miyako_System_MagicCircle.txt";
		let id = LoadScript(path);
		StartScript(id);yield;}}

@MainLoop{
	bossX = ObjMove_GetX(bossObj);
	bossY = ObjMove_GetY(bossObj);

	if(damage==0){ObjEnemy_SetDamageRate(bossObj, 0, 0);}
	if(damage==1){ObjEnemy_SetDamageRate(bossObj, 25, 0);}

	ObjEnemy_SetIntersectionCircleToShot(bossObj,bossX,bossY,24);
	ObjEnemy_SetIntersectionCircleToPlayer(bossObj,bossX,bossY,32);
	yield;}

@Finalize{}

task mainTask{
	ObjMove_SetDestAtWeight(bossObj,CenterX,120,8,3);
	TEtama;wait(90);SE(se_spell);
	if(GetCommonData("Replay",0) == 2){
	ObjCutin_SetSpellcardS4("Coloring \"Dance of the Rare Man\"", cutin, NAZRIN, 240, 104, 104);
	ObjCutin_LaunchS3(MOKOU, cutin, "Practice");}
	else{
	ObjCutin_SetSpellcardS4("Coloring \"Dance of the Rare Man\"", cutin, NAZRIN, 240, 104, 104);
	ObjCutin_LaunchS3(NAZRIN, cutin, "Extra");}
	TSpellBG;SetCommonData("BG",1);
	wait(120);TDance;}

task TDance{TBubble;
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0){
	ObjMove_SetDestAtWeight(bossObj,CenterX+100,120,8,6);
	wait(60);SE(se_kira2);
		TShot(272,228);
	ObjMove_SetDestAtWeight(bossObj,CenterX-100,120,8,6);
	wait(60);SE(se_kira2);
		TShot(269,227);
		damage=1;}}

task TBubble{wait(630);
	while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0){SE(se_tan2);
		CreateShotA1(bossX,bossY,2,GetAngleToPlayer(bossObj),823,15);
	wait(120);}}

task TShot(gr1,gr2){
	Fire(bossX,bossY,90,2,1,gr1,gr2);
	ascent(i in -16..17){
		CreateShotA1(bossX+30*cos(270+i*7),bossY+30*sin(270+i*7),3,270+i*7,gr1,10);}}

task Fire(x,y,angle,sp1,sp2,gr1,gr2){
	let ang = 0;
	let obj = CreateShotA1(x,y,1,angle,226,10);
	ObjShot_SetIntersectionEnable(obj,false);
	Obj_SetVisible(obj,false);
	loop(6){
		let objshot = CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj),2,ang,gr1,10);
		ObjShot_SetAutoDelete(obj,true);
		Delay(objshot,gr2); Hokai(objshot,obj,sp1,sp2); 
		ang+=60;}}

task Hokai(object,objmove,sp1,sp2){
	loop(90){
		ObjMove_SetAngle(object,ObjMove_GetAngle(object)+sp1);
		ObjMove_SetSpeed(object,ObjMove_GetSpeed(object)+sp2);
		ObjMove_SetX(object,ObjMove_GetX(objmove));
		ObjMove_SetY(object,ObjMove_GetY(objmove));
	if(ObjMove_GetX(object)<-50||ObjMove_GetX(object)>GetStgFrameWidth+50||
		ObjMove_GetY(object)<-50||ObjMove_GetY(object)>GetStgFrameHeight+50||
		ObjEnemy_GetInfo(bossObj,INFO_LIFE) < 0){Obj_Delete(object);break;}
		yield;}
	loop(1200){
		ObjMove_SetAngle(object,ObjMove_GetAngle(object)+sp1);
		ObjMove_SetSpeed(object,ObjMove_GetSpeed(object)+0.001);
		ObjMove_SetX(object,ObjMove_GetX(objmove));
		ObjMove_SetY(object,ObjMove_GetY(objmove));
	if(ObjMove_GetX(object)<-50||ObjMove_GetX(object)>GetStgFrameWidth+50||
		ObjMove_GetY(object)<-50||ObjMove_GetY(object)>GetStgFrameHeight+50||
		ObjEnemy_GetInfo(bossObj,INFO_LIFE) < 0){Obj_Delete(object);break;}
		yield;}
	Obj_Delete(object);}

task Delay(object,gr2){ 
	let obj = 1;
	loop{
	obj = CreateShotA1(ObjMove_GetX(object),ObjMove_GetY(object),0,0,gr2,10);
	ObjShot_SetIntersectionEnable(obj,false);
	ObjShot_SetAutoDelete(obj,true);
	ObjShot_FadeDelete(obj);
		if(ObjMove_GetX(obj)==0&&ObjMove_GetY(obj)==0){Obj_Delete(obj);}
		wait(4);}}