#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Training: Spell Card 2-2"]
#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");
	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,"Search \"A Pack of Hunting Souls\"");
	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); }
	TWave; damage = 1;
}

task TWave{
	let move = 270;
	let count = 0;
	let graphic = 0;
	while(!Obj_IsDeleted(bossObj)){
		loop(90){
			if(count % 4){ SE(se_tan2);
				graphic = 313;
			}
			else{
				graphic = 281;
			}
			if(!Obj_IsDeleted(bossObj)){
			ascent(s in 0..3){
				let shotA = CreateShotA1(bossX, bossY, 1+s/4, move, graphic, 30);
				TManageA(shotA,graphic);
				if(graphic == 281){
					ascent(i in 0..10){
						let objPetal = CreateShotA1(bossX,bossY,0,0,837,0);
						ObjShot_SetIntersectionEnable(objPetal,false);
						TManage(objPetal,shotA,i*36);
					}
				}
			}}
			move+=4;
			count++;
			yield;
		}
		loop(90){yield;}
		loop(90){
			if(count % 4){ SE(se_tan2);
				graphic = 309;
			}
			else{
				graphic = 277;
			}
			if(!Obj_IsDeleted(bossObj)){
			let r = rand(0,360);
			ascent(s in 0..3){
				let shotA = CreateShotA1(bossX, bossY, 1+s/4, move, graphic, 30);
				TManageB(shotA,graphic,r,1,45*s);
				if(graphic == 277){
					ascent(i in 0..10){
						let objPetal = CreateShotA1(bossX,bossY,0,0,833,0);
						ObjShot_SetIntersectionEnable(objPetal,false);
						TManage(objPetal,shotA,i*36);
					}
				}
				if(graphic == 309){TManageO(shotA);}
			}}
			move-=4;
			count++;
			yield;
		}
		loop(360){yield;}
	}

	task TManage(obj,obj2,angle){
		while(!Obj_IsDeleted(obj2)){
			ObjMove_SetPosition(obj,ObjMove_GetX(obj2)+10*cos(angle),ObjMove_GetY(obj2)+10*sin(angle));
			ObjRender_SetAngleZ(obj,angle+180);
			angle++;
			yield;
		}
		Obj_Delete(obj);
	}
	
	task TManageO(object){
		loop(90){yield;}
		while(GetObjectDistance(object,GetPlayerObjectID())>=150){yield;}
		if(!Obj_IsDeleted(bossObj)){
		if(!Obj_IsDeleted(object)){
			SE(se_kira);
			CreateShotA1(ObjMove_GetX(object), ObjMove_GetY(object), 2, GetAngleToPlayer(object), 313, 15);
			Obj_Delete(object);
		}}
	}
	
	task TManageA(object,check){
		loop(90){yield;}
		while(GetObjectDistance(object,GetPlayerObjectID())>=150){yield;}
		if(!Obj_IsDeleted(bossObj)){
		if(!Obj_IsDeleted(object)){
		if(check == 281){ SE(se_kira);
			let objA = CreateShotA1(ObjMove_GetX(object), ObjMove_GetY(object), 1, GetAngleToPlayer(object), 277, 15);
			ascent(i in 0..10){
				let objPetal = CreateShotA1(bossX,bossY,0,0,833,15);
				ObjShot_SetIntersectionEnable(objPetal,false);
				TManage(objPetal,objA,i*36);
			}
			Obj_Delete(object);
		}}}
	}
	
	task TManageB(object,check,r,speed,delay){
		while(ObjMove_GetX(object) >= 0 && ObjMove_GetX(object) <= 384 &&
		ObjMove_GetY(object) >= 0 && ObjMove_GetY(object) <= 448){yield;}
		if(!Obj_IsDeleted(bossObj)){
		if(check == 277){ SE(se_kira);
			ascent(i in 0..8){
			CreateShotA1(ObjMove_GetX(object), ObjMove_GetY(object), speed, i*45+r, 337, delay);}
			Obj_Delete(object);
		}}
	}
}