#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Training: Spell Card 2-4"]
#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(750);
	}
	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{
	DeleteCommonData("START");
}

task MainTask{
	TBarrier;
	ObjMove_SetDestAtWeight(bossObj,192,120,6,3);
	loop(30){yield;}
	if(!Obj_IsDeleted(bossObj)){ SE(se_spell); }
	CutIn_Left(cutin,"Protection Sign \"Barrier Labyrinth\"");}

task TBarrier{
	loop(60){yield;}
	if(!Obj_IsDeleted(bossObj)){ SE(se_ch);
	MakeConcentrationA1(bossObj,"SAKURA",255,64,128,45); }
	ascent(i in 0..11){
		let laserA = CreateStraightLaserA1(0, -360+i*75, 45, 550, 18, 90, 1005, 120);
		let laserB = CreateStraightLaserA1(384, -360+i*75, 135, 550, 18, 90, 1005, 120);
		Obj_SetRenderPriorityI(laserA,51);
		Obj_SetRenderPriorityI(laserB,51);
		loop(3){yield;}
	}
	loop(80){yield;}
	SE(se_boon2);
	loop(40){yield;}
	ascent(i in 0..11){
		let laserA = CreateStraightLaserA1(0, -360+i*75, 45, 550, 18, 0, 1005, 4000);
		let laserB = CreateStraightLaserA1(384, -360+i*75, 135, 550, 18, 0, 1005, 4000);
		TMovement(laserA,true); TMovement(laserB,true);
		ascent(a in 0..28){
			let shotA = CreateShotA1(0+a*15, -360+i*75+a*15, 0, 45, 103, 0);
			let shotB = CreateShotA1(384-a*15, -360+i*75+a*15, 0, 135, 103, 0);
			TMovement(shotA,true); TMovement(shotB,true);
		}
	}
	damage = 1;
	TRings;
	loop(300){yield;}
	SetCommonData("START",1);
	TCheckers;
	if(!Obj_IsDeleted(bossObj)){ SE(se_enep3);
	MakeExplosionA1(bossObj,"SAKURA",64,64,255); }
	while(!Obj_IsDeleted(bossObj)){
		TLine;
		loop(75*2){yield;}
		let laserA = CreateStraightLaserA1(0, -360, 45, 550, 18, 0, 1005, 4000);
		let laserB = CreateStraightLaserA1(384, -360, 135, 550, 18, 0, 1005, 4000);
		TMovement(laserA,true); TMovement(laserB,true);
	}
}

task TLine(){
	ascent(i in 0..28){
		if(!Obj_IsDeleted(bossObj)){
		let shotA = CreateShotA1(0+i*15, -50, 0, 135, 103, 0);
		let shotB = CreateShotA1(384-i*15, -50, 0, 45, 103, 0);
		TMovement(shotA,true); TMovement(shotB,true);}
		loop(30){yield;}
	}
}

task TCheckers{
	loop(500-75*4){yield;}
	while(!Obj_IsDeleted(bossObj)){
		if(!Obj_IsDeleted(bossObj)){
			let shotA = CreateShotA1(5+75*round(rand(-2,10)),0,0.5,90,324,30);
			Obj_SetRenderPriorityI(shotA,49);}
		loop(75){yield;}
		if(!Obj_IsDeleted(bossObj)){
			let shotB = CreateShotA1(5+75/2+75*round(rand(-2,10)),0,0.5,90,324,30);
			Obj_SetRenderPriorityI(shotB,49);}
		loop(75){yield;}
	}
}

task TRings{
	loop(60){yield;}
	while(!Obj_IsDeleted(bossObj)){
		ascent(a in 0..15){
			ascent(i in -4..5){
				CreateShotOA1(bossObj,1.2,GetAngleToPlayer(bossObj)+i*1.5+a*24,373,30);
			}
		}
		loop(30){yield;}
		if(!Obj_IsDeleted(bossObj)){SE(se_tan2);}
		loop(60){yield;}
	}
}

task TMovement(obj,wait){
	ObjShot_SetAutoDelete(obj, false);
	while(GetCommonData("START",0) == 0){yield;}
	let acc = 0;
	let count = 0;
	while(!Obj_IsDeleted(obj)){
		if(Obj_IsDeleted(bossObj)){break;}
		ObjMove_SetPosition(obj,ObjMove_GetX(obj),ObjMove_GetY(obj)+acc);
		if(acc >= 0.5){acc = 0.5;}
		if(count >= 660){
			ObjShot_SetAutoDelete(obj, true);}
		acc+=0.005;
		count++;
		yield;
	}
	Obj_Delete(obj);
}