#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Training: Spell Card 2-3"]
#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 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(750);
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(120);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(11000000);
	}
}

@MainLoop{
	bossX = ObjMove_GetX(bossObj);
	bossY = ObjMove_GetY(bossObj);
	ObjEnemy_SetDamageRate(bossObj, 10, 0);
	ObjEnemy_SetIntersectionCircleToShot(bossObj, bossX, bossY, 32);
	ObjEnemy_SetIntersectionCircleToPlayer(bossObj, bossX, bossY, 24);
	yield;
}

@Finalize{
}

task MainTask{
	loop(15){yield;}
	ObjMove_SetDestAtWeight(bossObj,192,180,9,2);
	loop(45){yield;}
	TEtama;
	loop(45){yield;}
		TShotMain;
	loop(45){yield;}
	ObjMove_SetDestAtWeight(bossObj,192,180-60,9,2);
	loop(45){yield;}
		TShotFlowers(bossObj);
	loop(45){yield;}
	ObjMove_SetDestAtWeight(bossObj,192-60,180+20,9,2);
	loop(90){yield;}
		TShotButterfly(bossObj);
	loop(45){yield;}
	ObjMove_SetDestAtWeight(bossObj,192+60,180+20,9,2);
	loop(90){yield;}
		TShotHeart(bossObj);
	loop(120){yield;}
	ObjMove_SetDestAtWeight(bossObj,192,180,9,2);
	if(!Obj_IsDeleted(bossObj)){ SE(se_spell); }
	CutIn_Right(cutin,"Feast \"Spring in Three Days\"");
	loop(60){yield;}
	if(!Obj_IsDeleted(bossObj)){ SE(se_ch);
	MakeConcentrationA1(bossObj,"SAKURA",255,64,128,45); }
		let enemyObjA = CreateFairy(0,0);
		let enemyObjB = CreateFairy(0,0);
		let enemyObjC = CreateFairy(0,0);
	descent(i in 30..120){
		ObjMove_SetPosition(enemyObjA,bossX+i*4*cos(30),bossY+i*4*sin(30));
		ObjMove_SetPosition(enemyObjB,bossX+i*4*cos(150),bossY+i*4*sin(150));
		ObjMove_SetPosition(enemyObjC,bossX+i*4*cos(270),bossY+i*4*sin(270));
		yield;
	}
	let move = 0;
	let distR = GetObjectDistance(enemyObjA,bossObj);
	loop(60){yield;}
	TShot;
	TMovement;
	
	let count = 0;
	while(!Obj_IsDeleted(bossObj)){
		ObjMove_SetPosition(enemyObjA,bossX+distR*cos(30+move),bossY+distR*sin(30+move));
		ObjMove_SetPosition(enemyObjB,bossX+distR*cos(150+move),bossY+distR*sin(150+move));
		ObjMove_SetPosition(enemyObjC,bossX+distR*cos(270+move),bossY+distR*sin(270+move));
		if(count%150 == 0){TShotSun(bossObj);}
		count++;
		move++;
		yield;
	}
	Obj_Delete(enemyObjA);
	Obj_Delete(enemyObjB);
	Obj_Delete(enemyObjC);
	
	task TShot{
		while(!Obj_IsDeleted(bossObj)){
			TShotFlowers(enemyObjA);
			loop(30){yield;}
			TShotButterfly(enemyObjB);
			loop(120){yield;}
			TShotHeart(enemyObjC);
			loop(120){yield;}
		}
	}
}

task TMovement{
	while(!Obj_IsDeleted(bossObj)){
		ObjMove_SetDestAtWeight(bossObj,rand(80,384-80),rand(80,180),1,1);
		loop(120){yield;}
	}
}

function TShotMain{
		let t = 15;
		ascent(i in 0..45){if(!Obj_IsDeleted(bossObj)){
			let shotObj = CreateShotA2(bossX,bossY,5,i*8,-0.08,rand(1,2),19,15);
			ObjMove_AddPatternA2(shotObj,60,NO_CHANGE,NO_CHANGE,0,rand(-0.2,0.2),0);}}
		loop(t){yield;} SE(se_tan2);
		ascent(i in 0..45){if(!Obj_IsDeleted(bossObj)){
			let shotObj = CreateShotA2(bossX,bossY,5,i*8,-0.08,rand(1,2),31,15);
			ObjMove_AddPatternA2(shotObj,60,NO_CHANGE,NO_CHANGE,0,rand(-0.2,0.2),0);}}
		loop(t){yield;} SE(se_tan2);
		ascent(i in 0..45){if(!Obj_IsDeleted(bossObj)){
			let shotObj = CreateShotA2(bossX,bossY,5,i*8,-0.08,rand(1,2),30,15);
			ObjMove_AddPatternA2(shotObj,60,NO_CHANGE,NO_CHANGE,0,rand(-0.2,0.2),0);}}
		loop(t){yield;} SE(se_tan2);
		ascent(i in 0..45){if(!Obj_IsDeleted(bossObj)){
			let shotObj = CreateShotA2(bossX,bossY,5,i*8,-0.08,rand(1,2),27,15);
			ObjMove_AddPatternA2(shotObj,60,NO_CHANGE,NO_CHANGE,0,rand(-0.2,0.2),0);}}
		loop(t){yield;} SE(se_tan2);
		ascent(i in 0..45){if(!Obj_IsDeleted(bossObj)){
			let shotObj = CreateShotA2(bossX,bossY,5,i*8,-0.08,rand(1,2),24,15);
			ObjMove_AddPatternA2(shotObj,60,NO_CHANGE,NO_CHANGE,0,rand(-0.2,0.2),0);}}
		loop(t){yield;} SE(se_tan2);
		ascent(i in 0..45){if(!Obj_IsDeleted(bossObj)){
			let shotObj = CreateShotA2(bossX,bossY,5,i*8,-0.08,rand(1,2),23,15);
			ObjMove_AddPatternA2(shotObj,60,NO_CHANGE,NO_CHANGE,0,rand(-0.2,0.2),0);}}
		loop(t){yield;} SE(se_tan2);
		ascent(i in 0..45){if(!Obj_IsDeleted(bossObj)){
			let shotObj = CreateShotA2(bossX,bossY,5,i*8,-0.08,rand(1,2),21,15);
			ObjMove_AddPatternA2(shotObj,60,NO_CHANGE,NO_CHANGE,0,rand(-0.2,0.2),0);}}
}

function TShotFlowers(obj){
	loop(30){
		let objShot = CreateShotOA1(obj,rand(1,2),rand(0,360),30,15);
		Obj_SetRenderPriorityI(objShot,51);
		ascent(i in 0..10){
			let objH = CreateShotOA1(obj,0,i*36,310,15);
			TPetal(objShot,objH,i*36);
		}
		loop(2){yield;}
		SE(se_kira);
	}
	
	task TPetal(obj1,obj2,angle){
		while(!Obj_IsDeleted(obj1)){
			ObjMove_SetPosition(obj2,ObjMove_GetX(obj1)+10*cos(angle),ObjMove_GetY(obj1)+10*sin(angle));
			yield;
		}
	}
}

function TShotHeart(obj){
	let angle = 0;
	SE(se_kira2);
	ascent(i in 0..21){
		CreateShotOA1(obj,2.5-0.08*i+cos(150+i*13.5),90-i*9,236,15);
		CreateShotOA1(obj,2.5-0.08*i+cos(150+i*13.5),90+i*9,236,15);
	}
}

function TShotButterfly(obj){
	let r = truncate(rand(0,360));
	ascent(i in 0..10){
		ascent(a in 0..6){
			let shotObj = CreateShotOA1(obj,5,i*36+r,294,15);
			ObjMove_AddPatternA2(shotObj,0,NO_CHANGE,NO_CHANGE,-0.2,1,2);
			ObjMove_AddPatternA2(shotObj,30,NO_CHANGE,NO_CHANGE,0,3+a/6,0);
			ObjMove_AddPatternA2(shotObj,80,NO_CHANGE,NO_CHANGE,0,0,0);
		}
		ascent(a in 0..6){
			let shotObj = CreateShotOA1(obj,5,i*36+r,295,15);
			ObjMove_AddPatternA2(shotObj,0,NO_CHANGE,NO_CHANGE,-0.2,1,1.2);
			ObjMove_AddPatternA2(shotObj,30,NO_CHANGE,NO_CHANGE,0,-3-a/6,0);
			ObjMove_AddPatternA2(shotObj,80,NO_CHANGE,NO_CHANGE,0,0,0);
		}
	}
	loop(15){yield;}
	SE(se_tan2);
	SE(se_kira3);
	loop(3){yield;}
	SE(se_tan2);
}

function TShotSun(obj){
	ascent(i in 0..40){
		CreateShotOA1(obj,1,i*360/40,337,15);
	}
	ascent(i in 0..15){
		ascent(a in 0..15){
			CreateShotOA1(obj,1+a/15,i*24+5*sin(a*30),337,15);
		}
	}
	loop(15){yield;}
	SE(se_tan2);
}

function CreateFairy(x,y){
	let fam = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(fam);
	ObjEnemy_SetLife(fam,100);
	Obj_SetRenderPriorityI(fam,40);
	ObjRender_SetPosition(fam,x,y,0);
	animateEnemyFairy(fam);
	return fam;}

task animateEnemyFairy(id){
	let animFrame = 0;
	ObjPrim_SetTexture(id,GetCurrentScriptDirectory ~ "./resource/img/enemy.png");

	while(!Obj_IsDeleted(id)){
		ObjSprite2D_SetDestCenter(id);
			if(animFrame < 5){ObjSprite2D_SetSourceRect(id,0,384,64,448);}
			if(animFrame >= 5 && animFrame < 10){ObjSprite2D_SetSourceRect(id,64,384,128,448);}
			if(animFrame >= 10 && animFrame < 15){ObjSprite2D_SetSourceRect(id,128,384,192,448);}
			if(animFrame >= 15){ObjSprite2D_SetSourceRect(id,192,384,256,448);}
			if(animFrame >= 20){animFrame = 0;}
		animFrame++;
		yield;
	}
}


//balloon
function TShotBalloon{
	ascent(i in 0..31){
		CreateShotOA1(bossObj,1+0.5*cos(i*9),90-i*6,236,15);
		CreateShotOA1(bossObj,1+0.5*cos(i*9),90+i*6,236,15);
	}
}

//chapter
function TShotChapter{
	let angle = 0;
	ascent(i in 0..31){
		angle = 150+i*14;
		CreateShotOA1(bossObj,1+0.5*cos(angle),90-i*6,236,15);
		CreateShotOA1(bossObj,1+0.5*cos(angle),90+i*6,236,15);
	}
}