#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Training: Spell Card 1-5"]
#Player["./resource/player/Marisa by plasmaflame22/Marisa_A.txt","./resource/player/Marisa by plasmaflame22/Marisa_B.txt","./resource/player/Marisa by plasmaflame22/Marisa_C.txt"]
#Background["./resource/bg/stg01.txt"]
#System["./system/system.txt"]

#include"./resource/cutin/cutin.txt"
#include"./system/effects.txt"
#include"./resource/[library] effects.txt"
#include"./resource/[library] boss.txt"
#include"./resource/[library] se.txt"
let bossObj = 0;
let bossX = 0;
let bossY = 0;
let attack = 0;
let damage = 0;
let csd = GetCurrentScriptDirectory;
let cutin = csd ~ "./resource/img/cutinstg01.png";

@Initialize{
	SetCommonData("BOSS","Eternity");
	InstallFont(GetCurrentScriptDirectory ~ "./resource/blogger sans.ttf");

	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);

	bossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(bossObj);

	animateBossEternity;
	TEtama;
	LoadShot;
	MainTask;
	TEndScript;
	AddGraze(-GetGraze);
	
	SetPlayerLife(1);
	SetAutoDeleteObject(true);
	SetForbidPlayerSpell(true);
}

@Loading{
	LoadTexture(cutin);}

@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(600);
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(120);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(11000000);
	}
}

@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, 32);
	ObjEnemy_SetIntersectionCircleToPlayer(bossObj, bossX, bossY, 24);
	yield;
}

@Finalize{
}

task MainTask{
	ObjMove_SetDestAtWeight(bossObj,192,192,6,4);
	loop(90){yield;} if(!Obj_IsDeleted(bossObj)){SE(se_spell);}
	CutIn_Left(cutin,"Rebirth Sign \"Choir of Cicadas\"");
	damage = 1;
	TMovement;
	loop(150){yield;}
	TChrizantema;
	while(!Obj_IsDeleted(bossObj)){
		attack = 1;
		loop(8){
			if(!Obj_IsDeleted(bossObj)){
			TCicada;}
			loop(80){yield;}
		}
		attack = 0;
		loop(150){yield;}
		SetCommonData("START",1);
		loop(600){yield;}
	}
}

task TChrizantema{
	while(!Obj_IsDeleted(bossObj)){
		while(GetCommonData("START",0) == 0){yield;}
		ascent(i in 0..10){
			let objM = CreateShotA1(bossX,bossY,1,i*36,281,30);
			TReflection(objM);
			TChri(objM);
		}
		loop(240){yield;}
	}
	task TReflection(shot){
	while(!Obj_IsDeleted(shot)){
		if(ObjMove_GetX(shot) <= 0){
			ObjMove_SetAngle(shot,180-ObjMove_GetAngle(shot));
			break;}
		if(ObjMove_GetX(shot) >= 384){
			ObjMove_SetAngle(shot,180-ObjMove_GetAngle(shot));
			break;}
		if(ObjMove_GetY(shot) <= 0){
			ObjMove_SetAngle(shot,-ObjMove_GetAngle(shot));
			break;}
		if(ObjMove_GetY(shot) >= 448){
			ObjMove_SetAngle(shot,-ObjMove_GetAngle(shot));
			break;}
	yield;}}
}

task TCicada(){
	let gr = 45;
	let speed = 3;
	let c = CreateShotA1(bossX,bossY,speed,rand(165,375),385,30);
	SE(se_tan2);
	while(ObjMove_GetX(c) > 0+gr && ObjMove_GetX(c) < 384-gr
			&& ObjMove_GetY(c) > 0+gr && ObjMove_GetY(c) < 440-gr){yield;}
		while(speed > -0.1){
			ObjMove_SetSpeed(c,speed);
		speed-=0.1;
		yield;
		}
		if(!Obj_IsDeleted(bossObj)){
		ascent(i in 0..45){
			CreateShotA2(ObjMove_GetX(c),ObjMove_GetY(c),0,i*8,0.05,6,775,30);
		}}
		loop(30){yield;}
			if(!Obj_IsDeleted(bossObj)){
			SE(se_kira);}
		loop(60){yield;}
		let r = rand(340,346);
		loop(30){
			if(!Obj_IsDeleted(bossObj)){
			SE(se_kira2);
			ascent(i in 0..45){
				CreateShotA1(ObjMove_GetX(c),ObjMove_GetY(c),6,i*8,r,0);
			}}
		loop(3){yield;}
		}
		ObjMove_SetAngle(c,GetAngleToPlayer(c));
		while(GetCommonData("START",0) == 0){yield;}
		let ang = ObjMove_GetAngle(c);
		let move = 0;
		loop(25){
			let x = ObjMove_GetX(c);
			let y = ObjMove_GetY(c);
			if(!Obj_IsDeleted(bossObj)){
			ObjMove_SetSpeed(c,ObjMove_GetSpeed(c)+0.15);
			CreateShotA2(x,y,0,rand(0,360),0.01,1,rand(340,346),15);}
			if(Obj_IsDeleted(c)){break;}
			move+=15;
			loop(8){yield;}
			SE(se_kira);
		}
		loop(10){yield;}
		
	SetCommonData("START",0);
}

task TChri(objMain){
	let move = 0;
	ascent(s in 0..4){
		ascent(a in 0..20){
			let shot = CreateShotA2(bossX,bossY,2+s/4,a*18+move,-0.1,0,3,0);
			TMove(shot,a*18+move);
		}
		move+=9;
	}
	task TMove(obj,ang){
		loop(30){yield;}
		let z = 0;
		let rad = GetObjectDistance(obj,objMain);
		while(!Obj_IsDeleted(objMain)){
			let x = ObjMove_GetX(objMain);
			let y = ObjMove_GetY(objMain);
			ObjMove_SetPosition(obj,x+rad*cos(ang),y+rad*sin(ang));
			ObjRender_SetAngleZ(obj,z);
			z++; ang++;
			yield;
		}
	}
}

task TMovement{loop(150){yield;}
	while(!Obj_IsDeleted(bossObj)){
		ObjMove_SetDestAtWeight(bossObj,192+rand(-90,90),100+rand(-30,30),15,3);
	loop(240){yield;}}
}