#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Training: Spell Card 2-7"]
#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 speedN = GetPlayerSpeed[0];
let speedF = GetPlayerSpeed[1];
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(700);
	}
	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{
	TPath;
	ObjMove_SetDestAtWeight(bossObj,192,120,6,3);
	loop(30){yield;}
	if(!Obj_IsDeleted(bossObj)){ SE(se_spell); }
	CutIn_Left(cutin,"Protection of the Miyago Shrine \"Blue Demon\"");
	loop(60){yield;}
	TSnow;
	damage = 1;
	loop(210){yield;}
	ascent(i in 0..24){
		if(!Obj_IsDeleted(bossObj)){
		CreateShotA1(bossX,bossY,1.5,i*15,279,15);}
	}
	TSnow;
	loop(4){ SE(se_tan2);
		ascent(i in 0..24){
			if(!Obj_IsDeleted(bossObj)){
			CreateShotA1(bossX,bossY,1.5,i*15,279,15);}
		}
		loop(30){yield;}
	}
	loop(4){ SE(se_tan2);
		let r = rand(0,15);
		ascent(i in 0..24){
			if(!Obj_IsDeleted(bossObj)){
			CreateShotA1(bossX,bossY,1.5,i*15+r,279,15);}
		}
		loop(30){yield;}
	}
	TSnow;
	loop(180){yield;}
	SE(se_tan2);
	ascent(i in 0..12){
		ascent(a in -4..5){
			if(!Obj_IsDeleted(bossObj)){
			CreateShotA1(bossX,bossY,1.2,i*30+a*2+GetAngleToPlayer(bossObj),375,15);}
		}
	}
	loop(60){yield;}
	let count = 4;
	while(!Obj_IsDeleted(bossObj)){
		if(count % 4 == 0){TSnow;}
		if(count > 8){ SE(se_tan2);
			ascent(i in 0..10){
				ascent(a in 0..3){
					if(!Obj_IsDeleted(bossObj)){
					CreateShotA1(bossX,bossY,0.5+a/3,i*36+GetAngleToPlayer(bossObj),279,15);}
				}
			}
		}
		if(count >= 15){
			if(count % 2 == 0){TAunn;}
		}
		loop(90){yield;}
		SE(se_tan2);
		ascent(i in 0..12){
			ascent(a in -4..5){
				if(!Obj_IsDeleted(bossObj)){
				CreateShotA1(bossX,bossY,1.2,i*30+a*2+GetAngleToPlayer(bossObj),375,15);}
			}
		}
		count++;
	}
}

task TAunn{
	ascent(i in 0..20){
		CreateShotN(i*18);
		loop(2){yield;}
	}
	task CreateShotN(ang){
		if(!Obj_IsDeleted(bossObj)){
		let obj = CreateShotA2(bossX,bossY,3,ang,-0.1,0,277,15);
		loop(15){yield;}
		SE(se_kira);
		loop(75){yield;}
		ObjMove_AddPatternA2(obj,0,0,GetAngleToPlayer(obj),0.05,0,1);}
	}
}

task TPath{
	while(!Obj_IsDeleted(bossObj)){
		let x = rand(-8,-1);
		let y = rand(-2,-5);
		ascent(t in 0..round(rand(4,7))){
			let objA = CreateShotA1(x+t*18,y,1,90,102,0);
			ObjRender_SetAngleZ(objA,90);
		}
		ascent(t in 0..round(rand(4,7))){
			let objB = CreateShotA1(x*(-1)+384-t*18,y,1,90,102,0);
			ObjRender_SetAngleZ(objB,90);
		}
		loop(9){yield;}
	}
}

task TSnow{ SE(se_kira2);
	ascent(a in 0..24){
		if(!Obj_IsDeleted(bossObj)){
		let snow = CreateShotA1(bossX,bossY,1.5,a*15,384,0);
		TManage(snow);}
	}
	task TManage(obj){
		let color = 64;
		let player = GetPlayerObjectID;
		while(GetObjectDistance(obj,player) >= 90){yield;}
		if(!Obj_IsDeleted(obj)){
		SE(se_ice);
			SetForbidPlayerShot(true);
			ObjShot_SetIntersectionEnable(obj,false);
			ObjRender_SetColor(player,color,color,255);
			SetPlayerSpeed(0,0);
			ascent(i in 0..15){
				ObjRender_SetScaleXYZ(obj,1+i/3,1+i/3,0);
				ObjRender_SetAlpha(obj,255-i*(255/14));
				yield;
			}
		Obj_Delete(obj);
		loop(90){
			ObjRender_SetColor(player,color,color,255);
			color+=(255-64)/90;
			yield;}
		SetForbidPlayerShot(false);
		SetPlayerSpeed(speedN,speedF);}
	}
}