#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Chroma \"Treasure Lamp Histogram\" - Ultra"]

#include"./../../Functions/Effects/Effects.txt"
#include"./../../System/Miyako_Effect.txt"
#include"./LoadGraphics.txt"
#include"./LoadSounds.txt"
#include"./Cutin.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);
	SetCommonData("ForceLifebar",1);
	SetCommonData("bossAURA",1);
	ObjEnemy_Regist(bossObj);
	SetAutoDeleteObject(true);

	animateBossM;
	LoadShotData;
	TEnemyMarker;
	mainTask;
	TEnd;}

@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){SetScriptResult(500);}
	case(EV_REQUEST_TIMER){SetScriptResult(48);}
	case(EV_REQUEST_SPELL_SCORE){SetScriptResult(10000000);}
	case(EV_GAIN_SPELL){SetScriptResult(10000000);}
	case(EV_REQUEST_IS_DURABLE_SPELL){SetScriptResult(true);}
	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);
	ObjEnemy_SetDamageRate(bossObj, 0, 0);
	ObjEnemy_SetIntersectionCircleToShot(bossObj,bossX,bossY,24);
	yield;}

@Finalize{}

task mainTask{
	ObjMove_SetDestAtWeight(bossObj,CenterX,120,8,3);
	TEtama;wait(90);SE(se_spell);
	TSpellBG;SetCommonData("BG",1);TSurvival;
	if(GetCommonData("Replay",0) == 2){
	ObjCutin_SetSpellcardS4("Chroma \"Treasure Lamp Histogram\"", cutin, NAZRIN, 240, 104, 104);
	ObjCutin_LaunchS3(MOKOU, cutin, "Practice");}
	else{
	ObjCutin_SetSpellcardS4("Chroma \"Treasure Lamp Histogram\"", cutin, NAZRIN, 240, 104, 104);
	ObjCutin_LaunchS3(NAZRIN, cutin, "Ultra");}
	wait(120);SE(se_ch2);
	MakeConcentrationB1(bossObj,"SAKURA",64,255,64,75);
	wait(60);ObjRender_SetColor(bossObj,64,255,64);ObjMove_SetDestAtWeight(bossObj,CenterX,CenterY,8,3);
	wait(90);TLaser;wait(150);SE(se_enep3);
	MakeExplosionA1(bossObj,"SAKURA",64,255,64);
	TSpawnBullet; TLaserCircle;}

task TLaserCircle{ wait(120);
	while(ObjEnemy_GetInfo(bossObj,INFO_LIFE) > 0){
	ascent(i in 0..45){
		let laser = CreateStraightLaserA1(bossX,bossY,i*8,380,10,30,1011,60);
		ObjRender_SetColorHSV(laser,120,100,255);}
	wait(120);}}

task TLaser{
	ascent(i in 0..4){
		let A1 = CreateStraightLaserA1(bossX,bossY,i*90,380,20,3000,1011,120);
		let P1 = PagodarLaser(bossX,bossY,i*90,480,120,3000,"GREEN");
	TMovement(A1,P1);Obj_SetVisible(A1,false);
	TLaserEffect(A1);}wait(120);SE(se_boon2);}

task TMovement(obj,p){
	let angle = ObjStLaser_GetAngle(obj);
	let angle2 = ObjMove_GetAngle(p);
	let fast = 0;
	loop(1200){
		ObjStLaser_SetAngle(obj,angle);
		ObjMove_SetAngle(p,angle);
		angle+=0.4;yield;}
	loop(100){
		ObjStLaser_SetAngle(obj,angle);
		ObjMove_SetAngle(p,angle);
		angle+=0.4-fast;fast+=0.004;yield;}
	wait(120);fast=0;
	while(ObjEnemy_GetInfo(bossObj,INFO_LIFE) > 0){
		ObjStLaser_SetAngle(obj,angle);
		ObjMove_SetAngle(p,angle);
		angle-=fast;fast+=0.0008;yield;}
	Obj_Delete(p);}

task TLaserEffect(object){let time = 10;
	while(!Obj_IsDeleted(object)){
		let A1 = CreateStraightLaserA1(bossX,bossY,ObjStLaser_GetAngle(object),380,10,60,1011,0);
		TFadeDelete(A1);ObjShot_SetIntersectionEnable(A1,false);wait(time);time-=0.02;}
	task TFadeDelete(objLaser){
		let alpha = 255;
		loop(60){
			if(ObjEnemy_GetInfo(bossObj,INFO_LIFE) < 0){break;}
			ObjRender_SetAlpha(objLaser,alpha);
			alpha-=255/60;yield;}
		Obj_Delete(objLaser);}}

task TSpawnBullet{
	let spawn = 0;
	let angle = 0;
	loop(130){SE(se_kira);
	ascent(i in 0..4){
		let obj = CreateShotA1(bossX+rand(50,360)*sin(spawn+i*90),bossY+rand(50,360)*cos(spawn+i*90),0,i*90,532,60);
		ObjShot_SetSpellResist(obj,true);
		ObjMove_AddPatternA1(obj,1140,0,atan2(ObjMove_GetY(obj)-bossY,ObjMove_GetX(obj)-bossX));
		TColor(obj);}spawn+=3.2;wait(8);}}

task TColor(object){
	let red = 255; wait(1000);
	loop(60){
		ObjRender_SetColor(object,red,255,red);
	red-=3; yield;}
	wait(150);
	ObjMove_SetAngularVelocity(object,-1);
	loop(30){
		ObjMove_SetSpeed(object,ObjMove_GetSpeed(object)+0.01);
	//	if(ObjEnemy_GetInfo(bossObj,INFO_LIFE) < 0){break;}
	yield;}
	ObjMove_SetAngularVelocity(object,-0.1);}