#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Lamp Sign \"Colour-Controlling Light\""]

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

let bossObj;
let bossX;
let bossY;
let count = 0;
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(50);}
	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, 10, 0);}

	ObjEnemy_SetIntersectionCircleToShot(bossObj,bossX,bossY,24);
	ObjEnemy_SetIntersectionCircleToPlayer(bossObj,bossX,bossY,32);
	
	if(count >= 80){
		SetCommonData("ATTACK",1);}
	if(count >= 130){
		SetCommonData("ATTACK",0);
		count = 0;}
	count++;
	yield;}

@Finalize{}

task mainTask{
	ObjMove_SetDestAtWeight(bossObj,CenterX,120,8,3);
	TEtama;wait(70);attack=1;wait(20);SE(se_spell);
	if(GetCommonData("Replay",0) == 2){
	ObjCutin_SetSpellcardS4("Lamp Sign \"Colour-Controlling Light\"", cutin, NAZRIN, 240, 104, 104);
	ObjCutin_LaunchS3(MOKOU, cutin, "Practice");}
	else{
	ObjCutin_SetSpellcardS4("Lamp Sign \"Colour-Controlling Light\"", cutin, NAZRIN, 240, 104, 104);
	ObjCutin_LaunchS3(NAZRIN, cutin, "Extra");}
	TSpellBG;SetCommonData("BG",1);
	wait(90);attack=0;SE(se_ch2);
	MakeConcentrationB1(bossObj,"SAKURA",255,255,255,120);
	wait(90);damage=1;let t = 160;
	while(ObjEnemy_GetInfo(bossObj,INFO_LIFE) > 0){
		tArrow(rand(50,334),519,364,3);wait(t);
		tArrow(rand(50,334),531,364,15);wait(t);
		tArrow(rand(50,334),530,369,14);wait(t);
		tArrow(rand(50,334),527,368,11);wait(t);
		tArrow(rand(50,334),524,367,9);wait(t);
		tArrow(rand(50,334),523,366,7);wait(t);
		tArrow(rand(50,334),520,365,5);wait(t);
	}}

task tArrow(x,gr1,gr2,gr3){count = 0;
	let obj = CreateShotA1(x,0,1,90,gr2,60);
	ObjRender_SetScaleXYZ(obj,1.5,1.5,0);
	tShot;
	
	ascent(i in 0..15){
		let bullet = CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),0.5,i*24,gr1,0);
		ObjMove_AddPatternA1(bullet,60,1,90);}

	while(!Obj_IsDeleted(obj)){
		ObjRender_SetAngleZ(obj,GetAngleToPlayer(obj)-90);
		yield;}
		
	task tShot{
		SE(se_kira2);wait(60);
		while(!Obj_IsDeleted(obj)){
			if(GetCommonData("ATTACK",0) == 1){
				let arrow = CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),4,GetAngleToPlayer(obj),gr2,10);
				tDanmaku(arrow,gr3); SE(se_tan2); wait(4);}
			yield;}}}

task tDanmaku(obj,g){
	while(!Obj_IsDeleted(obj)){
		if(ObjMove_GetX(obj) > 384){
			ObjMove_SetAngle(obj,180-ObjMove_GetAngle(obj));
			ObjShot_SetGraphic(obj,g);
			ObjShot_SetDelay(obj,22);
			ObjMove_SetSpeed(obj,1);
			break;}
		if(ObjMove_GetX(obj) < 0){
			ObjMove_SetAngle(obj,180-ObjMove_GetAngle(obj));
			ObjShot_SetGraphic(obj,g);
			ObjShot_SetDelay(obj,22);
			ObjMove_SetSpeed(obj,1);
			break;}
		if(ObjMove_GetY(obj) > 448){
			ObjMove_SetAngle(obj,-ObjMove_GetAngle(obj));
			ObjShot_SetGraphic(obj,g);
			ObjShot_SetDelay(obj,22);
			ObjMove_SetSpeed(obj,1);
			break;}
		if(ObjMove_GetY(obj) < 0){
			ObjMove_SetAngle(obj,-ObjMove_GetAngle(obj));
			ObjShot_SetGraphic(obj,g);
			ObjShot_SetDelay(obj,22);
			ObjMove_SetSpeed(obj,1);
			break;}
	yield;}}