#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Training: Spell Card 2-1"]
#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";
let face01 = csd ~ "./resource/img/face/enemy3/face01.png";
let face02 = csd ~ "./resource/img/face/enemy3/face02.png";
let face03 = csd ~ "./resource/img/face/enemy3/face03.png";
let dialog = csd ~ "./resource/img/stg02dialogue.png";
let dataFile = csd ~ "./resource/data/dialogue.dat";
	
@Initialize{
	SetCommonData("BOSS","Aunn");
	CreateCommonDataArea("Dialogue");
	LoadCommonDataAreaA2("Dialogue",dataFile);
	InstallFont(csd ~ "./resource/blogger sans.ttf");

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

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

	animateBossAunn(bossObj);
	if(GetAreaCommonData("Dialogue","Aunn",0) != 0){
		TEtama;}
	LoadShot;
	MainTask;
	TEndScript;
	AddGraze(-GetGraze);
	
	SetPlayerLife(1);
	SetAutoDeleteObject(true);
	SetForbidPlayerSpell(true);
}

@Loading{
	LoadTexture(cutin);
	LoadTexture(face01);
	LoadTexture(face02);
	LoadTexture(face03);
	LoadTexture(dialog);}

@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(900);
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(150);
	}
	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(75){yield;}

	if(GetAreaCommonData("Dialogue","Aunn",0) == 0){
		SetAreaCommonData("Dialogue","Aunn",1);
		SaveCommonDataAreaA2("Dialogue",dataFile);
	
	let objF = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objF,face01);
	Obj_SetRenderPriorityI(objF,100);
	ObjSprite2D_SetSourceRect(objF,0,0,340,720);
	descent(move in 0..45){
		if(Obj_IsDeleted(bossObj)){break;}
		ObjRender_SetAlpha(objF, 255 - move*(255/45));
		ObjSprite2D_SetDestRect(objF,260+move, 80+move, 260+340/1.6+move, 80+720/1.6+move);
		yield;}
	
	let objD = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objD,dialog);
	Obj_SetRenderPriorityI(objD,100);
	ObjSprite2D_SetSourceRect(objD,0,0,640,120);
	TManage();
	
	task TManage(){
		while(!Obj_IsDeleted(bossObj)){yield;}
		Obj_Delete(objF);Obj_Delete(objD);}
	
	ascent(move in 0..15){
		ObjSprite2D_SetDestRect(objD, 30, 210+80/1.8*cos(move*6), 30+640/1.8, 210+120/1.8-40/1.8*cos(move*6));
		yield;}
	loop(120){yield;}
	descent(move in 0..15){
		ObjSprite2D_SetDestRect(objD, 30, 210+80/1.8*cos(move*6), 30+640/1.8, 210+120/1.8-40/1.8*cos(move*6));
		yield;}
	ObjSprite2D_SetSourceRect(objD,0,120,640,240);
	ObjPrim_SetTexture(objF,face03);
	ascent(move in 0..15){
		ObjSprite2D_SetDestRect(objD, 30, 210+80/1.8*cos(move*6), 30+640/1.8, 210+120/1.8-40/1.8*cos(move*6));
		yield;}
	loop(120){yield;}
	descent(move in 0..15){
		ObjSprite2D_SetDestRect(objD, 30, 210+80/1.8*cos(move*6), 30+640/1.8, 210+120/1.8-40/1.8*cos(move*6));
		yield;}
	ObjSprite2D_SetSourceRect(objD,0,240,640,360);
	ObjPrim_SetTexture(objF,face02); 
	if(!Obj_IsDeleted(bossObj)){ TEtama; }
	ascent(move in 0..15){
		ObjSprite2D_SetDestRect(objD, 30, 210+80/1.8*cos(move*6), 30+640/1.8, 210+120/1.8-40/1.8*cos(move*6));
		yield;}
	loop(120){yield;}
	descent(move in 0..15){
		ObjSprite2D_SetDestRect(objD, 30, 210+80/1.8*cos(move*6), 30+640/1.8, 210+120/1.8-40/1.8*cos(move*6));
		yield;}
	ObjSprite2D_SetSourceRect(objD,0,360,640,480);
	ascent(move in 0..15){
		ObjSprite2D_SetDestRect(objD, 30, 210+80/1.8*cos(move*6), 30+640/1.8, 210+120/1.8-40/1.8*cos(move*6));
		yield;}
	loop(120){yield;}
	descent(move in 0..15){
		ObjSprite2D_SetDestRect(objD, 30, 210+80/1.8*cos(move*6), 30+640/1.8, 210+120/1.8-40/1.8*cos(move*6));
		yield;}

	Obj_Delete(objD);
	ascent(move in 0..45){
		ObjRender_SetAlpha(objF, 255 - move*(255/45));
		ObjSprite2D_SetDestRect(objF, 260+move, 80+move, 260+340/1.6+move, 80+720/1.6+move);
		yield;}
	Obj_Delete(objF);
	}
	SetCommonData("MUSIC",1);
	attack = 1;
	damage = 1;
		TNonspell(1,101); 
		TNonspell(-1,112);
		TNonspell(1,101); 
		TNonspell(-1,112);	
	if(!Obj_IsDeleted(bossObj)){ SE(se_spell); }
	CutIn_Left(cutin,"Sacred Place \"A Place where Dogs' Souls Wander\"");
	loop(60){yield;}
	if(!Obj_IsDeleted(bossObj)){ SE(se_ch);
	MakeConcentrationA1(bossObj,"SAKURA",255,64,128,45); }
	loop(120){yield;}
	if(!Obj_IsDeleted(bossObj)){ SE(se_enep3);
	MakeExplosionA1(bossObj,"SAKURA",255,64,128); }
	if(!Obj_IsDeleted(bossObj)){ SE(se_water);
	ascent(i in 0..90){
		CreateFlowerShot(bossX,bossY,1+rand(-0.5,0.5),i*4+rand(-2,2));
	}}
	loop(90){yield;}
	TWave;
	while(!Obj_IsDeleted(bossObj)){
		if(!Obj_IsDeleted(bossObj)){
		SE(se_water);
		loop(5){
			CreateFlowerShot(bossX,bossY,1+rand(-0.5,0.5),GetAngleToPlayer(bossObj)+rand(-45,45));}}
		loop(90){yield;}
	}
}

task TWave{
	let move = 0;
	let count = 0;
	while(!Obj_IsDeleted(bossObj)){
		loop(5){
			ascent(i in 0..45){
				if(!Obj_IsDeleted(bossObj)){
				CreateShotS(i*9);}
			}
			loop(6){yield;}
			SE(se_tan2);
		}
		loop(60){yield;}
	}
	
	function CreateShotS(angle){
		let objShot = CreateShotA2(bossX,bossY,5,angle,-0.1,0,313,30);
		task TManage{
			loop(90){yield;}
			if(!Obj_IsDeleted(bossObj)){ SE(se_kira);
			let objShot2 = CreateShotA2(ObjMove_GetX(objShot),ObjMove_GetY(objShot),4.5,GetAngleToPlayer(objShot),-0.1,0,308,0);
			Obj_Delete(objShot);
			loop(45){yield;}
			if(!Obj_IsDeleted(bossObj)){ SE(se_tan2);
			CreateShotA1(ObjMove_GetX(objShot2),ObjMove_GetY(objShot2),2,angle,313,0);}
			Obj_Delete(objShot2);}
		}
		TManage;
		return objShot;
	}
}

function CreateFlowerShot(x,y,speed,angle){
	let objShot = CreateShotA1(x,y,speed,angle,277,0);
	Obj_SetRenderPriorityI(objShot,52);
	ascent(i in 0..10){
		let objPetal = CreateShotA1(bossX,bossY,0,0,833,0);
		ObjShot_SetIntersectionEnable(objPetal,false);
		Obj_SetRenderPriorityI(objPetal,51);
		TManage(objPetal,i*36);
	}
	task TManage(obj,angle){
		while(!Obj_IsDeleted(objShot)){
			ObjMove_SetPosition(obj,ObjMove_GetX(objShot)+10*cos(angle),ObjMove_GetY(objShot)+10*sin(angle));
			ObjRender_SetAngleZ(obj,angle+180);
			angle++;
			yield;
		}
		Obj_Delete(obj);
	}
	return objShot;
}

function TNonspell(dir,gr){
	let move = 0;
	loop(15){
		if(!Obj_IsDeleted(bossObj)){
		ascent(i in 0..25){
			let objA = CreateShotA2(bossX+75*cos(move+i*360/25),bossY+75*sin(move+i*360/25),1.5,move+i*360/25,-0.02,0.1,99,15);
			ObjMove_AddPatternA3(objA,60,4,atan2((GetPlayerY-90)-ObjMove_GetY(objA),GetPlayerX-ObjMove_GetX(objA)),-0.1,0,0,NO_CHANGE);
			TManage(objA,i*720/25);
		}}
		move+=3*dir;
		loop(15){yield;}
		if(!Obj_IsDeleted(bossObj)){
		SE(se_tan2); }
	}
	loop(30){yield;}
	
	task TManage(obj,angle){
		loop(120){yield;}
		ObjMove_SetSpeed(obj,2.5);
		ObjShot_SetDelay(obj,30);
		ObjShot_SetGraphic(obj,gr);
		ObjMove_SetAngle(obj,angle-50);
		ObjMove_SetAcceleration(obj,0);
	}
}