#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Training: Spell Card 2-6"]
#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/cutinstg04.png";
	
@Initialize{
	SetCommonData("BOSS","Lily");
	InstallFont(csd ~ "./resource/blogger sans.ttf");

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

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

	ObjMove_SetDestAtSpeed(bossObj,192,-120,50);
	animateBossLily;
	LoadShot;
	MainTask;
	TEndScript;
	AddGraze(-GetGraze);
	
	SetPlayerLife(1);
	SetAutoDeleteObject(true);
	SetForbidPlayerSpell(true);
}

@Loading{
	LoadTexture(cutin);}

@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(900);
	}
	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{
	let scale = 0;
	ObjRender_SetScaleXYZ(bossObj,scale,2+scale,0);
	ObjMove_SetDestAtFrame(bossObj,192,180,1);
	loop(10){yield;}
	loop(15){
		ObjRender_SetScaleXYZ(bossObj,sin(scale),2-sin(scale),0);
		scale+=90/15;
		yield;
	}
	loop(5){yield;}
	if(!Obj_IsDeleted(bossObj)){ SE(se_spell); }
	CutIn_Right(cutin,"\"Waltz of the Flowers\"");
	loop(60){yield;}
	if(!Obj_IsDeleted(bossObj)){ SE(se_ch);
	MakeConcentrationA1(bossObj,"SAKURA",255,64,128,45); }
	loop(30){yield;}
	if(!Obj_IsDeleted(bossObj)){
		CircleWave(50,120,1,50);
		CircleWave(380,-120,-1,50);}
	loop(90){yield;}
	damage = 1;
while(!Obj_IsDeleted(bossObj)){
	ascent(i in 0..20){
		if(!Obj_IsDeleted(bossObj)){SE(se_kira2);
		ascent(a in 0..20){
			let shotA = CreateShotA1(bossX,bossY,1,a*18,19,15);
			ObjMove_AddPatternA2(shotA,0,NO_CHANGE,NO_CHANGE,0,0.4,0);
			ObjMove_AddPatternA2(shotA,300,NO_CHANGE,NO_CHANGE,0,0,0);}}
		loop(40){yield;}
	}
	ascent(i in 0..20){
		if(!Obj_IsDeleted(bossObj)){SE(se_kira2);
		ascent(a in 0..20){
			let shotA = CreateShotA1(bossX,bossY,1,a*18,23,15);
			ObjMove_AddPatternA2(shotA,0,NO_CHANGE,NO_CHANGE,0,-0.4,0);
			ObjMove_AddPatternA2(shotA,300,NO_CHANGE,NO_CHANGE,0,0,0);}}
		loop(40){yield;}
	}
	let v = 0.4;
	ascent(i in 0..20){
		if(!Obj_IsDeleted(bossObj)){SE(se_kira2);
		ascent(a in 0..20){ 
			let shotA = CreateShotA1(bossX,bossY,1,a*18,21,15);
			ObjMove_AddPatternA2(shotA,0,NO_CHANGE,NO_CHANGE,0,v,0);
			ObjMove_AddPatternA2(shotA,300,NO_CHANGE,NO_CHANGE,0,0,0);}}
		loop(40){yield;}
		v = negative(v);
	}
	loop(3){ SE(se_tan2);
		TRed(bossX+rand(-50,50),bossY+rand(-50,50));
		loop(60){yield;}
	}
}
	task TRed(x,y){
		let move = 0;
		ascent(s in 0..8){
			ascent(a in 0..20){ if(!Obj_IsDeleted(bossObj)){
				let objA = CreateShotA2(x,y,s/4,a*18+move,-0.01,0,3,30);
				TManage(objA); }
			}
			move+=9;
		}
		task TManage(obj){
			loop(150){yield;} if(!Obj_IsDeleted(bossObj)){
			let objB = CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),1,0,3,0);
			ObjMove_AddPatternA1(objB,0,NO_CHANGE,GetAngleToPlayer(objB));}
			Obj_Delete(obj);
		}
	}
}

task CircleWave(anfR,borR,chR,beug){
	ascent(i in 0..180){
		let r = anfR+beug*sin(i*5*2);
		let waveA = CreateShotA1(bossX+r*cos(i*2), bossY+r*sin(i*2), 0, i*2, 294, 0);
		ObjShot_SetAutoDelete(waveA, false);
		TMoving(waveA);
	}
	
	task TMoving(obj){ yield;
		let move = 0;
		let unr = GetObjectDistance(obj,bossObj);
		let r = GetObjectDistance(obj,bossObj);
		let angle = ObjMove_GetAngle(obj);
		while(!Obj_IsDeleted(bossObj)){
			if(chR > 0){ if(r < unr+borR){r+=1*chR;} }
			if(chR < 0){ if(r > unr+borR){r+=1*chR;} }
			ObjMove_SetPosition(obj,bossX+r*cos(angle),bossY+r*sin(angle));
			ObjRender_SetAngleZ(obj,move);
			angle+=0.2;
			move+=0.2;
			yield;
		}
	}
}