#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Training: Spell Card 2-5"]
#Player["./resource/player/Reimu by plasmaflame22/Reimu_A.txt","./resource/player/Reimu by plasmaflame22/Reimu_B.txt","./resource/player/Reimu by plasmaflame22/Reimu_C.txt"]
#Background["./resource/bg/stg02.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/cutinstg03.png";
	
@Initialize{
	SetCommonData("BOSS","Aunn");
	SetCommonData("START",0);
	InstallFont(csd ~ "./resource/blogger sans.ttf");

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

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

	animateBossAunn(bossObj);
	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,120,6,3);
	loop(30){yield;}
	if(!Obj_IsDeleted(bossObj)){ SE(se_spell); }
	CutIn_Left(cutin,"Komainu's Power \"Energy of the Flower of Life\"");
	loop(60){yield;}
	ObjMove_SetDestAtWeight(bossObj,192,60,6,3);
	loop(60){yield;}
	if(!Obj_IsDeleted(bossObj)){ SE(se_ch); 
	MakeConcentrationA1(bossObj,"SAKURA",255,64,128,120); }
	ascent(i in 0..7){
		TRainbow(192,224,20,40+i*6,8,371+i,0.01,3);
	}
	loop(240){yield;}
	SetCommonData("START",1);
	if(!Obj_IsDeleted(bossObj)){ SE(se_enep3);
	MakeExplosionA1(bossObj,"SAKURA",255,64,128); }
	loop(240){yield;}
	TMovement;
	damage = 1;
	let count = 0;
	let graphic = 371;
	while(!Obj_IsDeleted(bossObj)){
		TFlower(rand(50,384-50),50);
		if(graphic >= 378){graphic = 371;}
		if(count % 3 == 0){
		if(!Obj_IsDeleted(bossObj)){
			TRainbow(bossX,bossY,10,30,6,graphic,0.01,5);
			graphic++;
		}}
		loop(90){yield;}
		count++;
	}
}

task TFlower(x,y){
	let d = 15;
	let r = 6;
	let R = 5*r;
	
	let r1 = rand(-0.5,0.5);
	let r2 = rand(0.5,2);
	SE(se_kira3);
	let shotB = CreateShotA1(x,y,0,0,46,60);
	ascent(i in 0..120){
		let x1 = x+(R + r)*cos(i*3)-d*cos(((R + r)/r)*i*3);
		let y1 = y+(R + r)*sin(i*3)-d*sin(((R + r)/r)*i*3);
		let shotA = CreateShotA1(x,y,0,i*15,378,15);
		ObjMove_SetDestAtWeight(shotA,x1,y1,1,0.5);
		TMove();
			
		task TMove{
			loop(120){yield;}
			let hold = 0;
			let move = ObjMove_GetAngle(shotA);
			let xPos = ObjMove_GetX(shotB);
			let yPos = ObjMove_GetY(shotB);
			let rad = GetObjectDistance(shotA,shotB);
			while(!Obj_IsDeleted(shotA)){
				ObjMove_SetPosition(shotA,xPos+rad*cos(move),yPos+rad*sin(move));
				ObjMove_SetPosition(shotB,xPos,yPos);
				ObjRender_SetAngleZ(shotA,hold);
				xPos+=r1; yPos+=r2;
				move+=1; hold+=1;
				yield;
			}
		}
	}
}

task TRainbow(x,y,r,d,n,gr,sp,t){
	let R = n*r;
	let count = 0;
	ascent(i in -90..360-90-180+1){
		let x1 = (R + r)*cos(i)-d*cos(((R + r)/r)*i);
		let y1 = (R + r)*sin(i)-d*sin(((R + r)/r)*i);
		let x2 = (R + r)*cos(180-i)-d*cos(180-((R + r)/r)*i);
		let y2 = (R + r)*sin(180-i)-d*sin(180-((R + r)/r)*i);
		if(!Obj_IsDeleted(bossObj)){
			let shotA = CreateShotA1(x+x1,y+y1, 0,i*9, gr,15);
			let shotB = CreateShotA1(x+x2,y+y2, 0,180-i*9, gr,15);
		TManage(shotA);TManage(shotB);
		if(count%t == 0){SE(se_tan2);}}
		count++;
		yield;
	}
	task TManage(obj){
		while(GetCommonData("START",0) == 0){yield;}
		ascent(i in 0..90){
			ObjMove_SetSpeed(obj,ObjMove_GetSpeed(obj)+sp);
			yield;
		}
	}
}

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