#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Training: Spell Card 1-1"]
#Player["./resource/player/Marisa by plasmaflame22/Marisa_A.txt","./resource/player/Marisa by plasmaflame22/Marisa_B.txt","./resource/player/Marisa by plasmaflame22/Marisa_C.txt"]
#Background["./resource/bg/stg01.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/cutinstg01.png";
let face01 = csd ~ "./resource/img/face/enemy1/face01.png";
let face02 = csd ~ "./resource/img/face/enemy1/face02.png";
let face03 = csd ~ "./resource/img/face/enemy1/face03.png";
let dialog = csd ~ "./resource/img/stg01dialogue.png";
let dataFile = csd ~ "./resource/data/dialogue.dat";

@Initialize{
	SetCommonData("BOSS","Eternity");
	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);

	animateBossEternity;
	if(GetAreaCommonData("Dialogue","Eternity",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(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(GetAreaCommonData("Dialogue","Eternity",0) == 0){
		SetAreaCommonData("Dialogue","Eternity",1);
		SaveCommonDataAreaA2("Dialogue",dataFile);
	
	let objF = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objF,face01);
	Obj_SetRenderPriorityI(objF,100);
	ObjSprite2D_SetSourceRect(objF,0,0,700,680);
	descent(move in 0..45){
		if(Obj_IsDeleted(bossObj)){break;}
		ObjRender_SetAlpha(objF, 255 - move*(255/45));
		ObjSprite2D_SetDestRect(objF,200+move, 80+move, 200+700/1.6+move, 80+680/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(90){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,face02);
	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(90){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,face03); 
	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(90){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,200+move, 80+move, 200+700/1.6+move, 80+680/1.6+move);
		yield;}
	Obj_Delete(objF);
	}
	if(!Obj_IsDeleted(bossObj)){ SE(se_spell); }
	CutIn_Left(cutin,"Cycle \"Awakening of the Blue Night Butterflies\"");
	loop(90){yield;} 
	if(!Obj_IsDeleted(bossObj)){ SE(se_ch);
	MakeConcentrationA1(bossObj,"PENTAGON",255,255,64,45); }
	loop(120){yield;}

	if(!Obj_IsDeleted(bossObj)){ SE(se_enep3);
	MakeExplosionA1(bossObj,"PENTAGON",255,255,64); }
	
	damage = 1;
	let macc = 1;
	let angle = 0;
	ascent(i in 0..30){
		angle = 0;
		ascent(a in 0..14){
			let objA = CreateShotOA1(bossObj,0.3+a*0.2,i*12+angle,517,10);
			ObjShot_SetAutoDelete(objA, false);
			ObjRender_SetAlpha(objA,100);
			TStop(objA);
		angle+=(a*0.54)*macc;
		}
	macc = negative(macc);
	}
	
	task TStop(obj){
		ObjShot_SetIntersectionEnable(obj,false);
		loop(160){yield;}
		let speed = ObjMove_GetSpeed(obj)/60;
		loop(60){
			ObjMove_SetSpeed(obj,ObjMove_GetSpeed(obj)-speed);
			yield;}			
		SE(se_kira);
		ascent(i in 0..15){
			ObjRender_SetAlpha(obj,100+i*(155/15));
			yield;
		}
		ObjShot_SetIntersectionEnable(obj,true);
		loop(120){
			ObjMove_SetSpeed(obj,ObjMove_GetSpeed(obj)-speed/2);
			yield;}
		loop(90){yield;}
		ObjShot_FadeDelete(obj);
	}
	
	loop(60*6){yield;}
	if(!Obj_IsDeleted(bossObj)){ SE(se_ch);
	MakeConcentrationA1(bossObj,"PENTAGON",255,255,255,45); }
	loop(100){yield;}
	TEffect;

	loop(10){yield;}
		if(!Obj_IsDeleted(bossObj)){ SE(se_enep3);
		MakeExplosionA1(bossObj,"PENTAGON",155,155,255);}
	
	if(!Obj_IsDeleted(bossObj)){
	ascent(i in 0..10){
		CreateButterflyA2(bossX+450*cos(i*36),bossY+450*sin(i*36),2,i*36+180,0,1,0);
		CreateButterflyA2(bossX+450*cos(i*36+18),bossY+450*sin(i*36+18),1.5,i*36+180+18,0,1,0);
	}}
	
	TButterflyCycle;
	loop(120){yield;}
	
	angle = 4;
	let rad = 1;
	let z = 0;
	while(!Obj_IsDeleted(bossObj)){
		ascent(i in 0..5){
			CreateShotA1(bossX+rad*cos(angle),bossY+rad*sin(angle),2+i/14,angle+z+90,876,10);
			CreateShotA1(bossX+rad*cos(angle+180),bossY+rad*sin(angle+180),2+i/14,angle+z-90,876,10);
			CreateShotA1(bossX+rad*cos(-angle),bossY+rad*sin(-angle),2+i/14,-angle-z-90,876,10);
			CreateShotA1(bossX+rad*cos(-angle+180),bossY+rad*sin(-angle+180),2+i/14,-angle-z+90,876,10);}
	loop(6){yield;}
	SE(se_tan2);
	rad+=1.5*sin((angle*2)/4);
	angle+=4;
	z+=6;
	if(z >= 360){z = 0;}
	}
}

task TButterflyCycle{
	loop(180){yield;}
	loop{ 
		if(!Obj_IsDeleted(bossObj)){SE(se_kira);}
		let r = rand(0,360);
		if(Obj_IsDeleted(bossObj)){break;}
		ascent(i in 0..10){
			CreateButterflyA2(bossX+450*cos(i*36+r),bossY+450*sin(i*36+r),2,i*36+180+r,0,1,0);
		}
	loop(180){yield;}
	}
}
	
task TEffect{
	while(!Obj_IsDeleted(bossObj)){
		let angle = rand(0,360);
		loop(12){ TLaser(angle); angle+=30; }
		loop(10){yield;}
	}
	task TLaser(ang){
		let laser = CreateStraightLaserA1(bossX,bossY,ang,0,60,180,38,0);
		ObjShot_SetIntersectionEnable(laser,false);
		let ro = rand(-0.2,0.2);
		let length = rand(30,180);
		ascent(i in 0..30){
			ObjRender_SetAlpha(laser,255*sin(i*6));
			ObjStLaser_SetAngle(laser,ObjStLaser_GetAngle(laser)+ro);
			ObjLaser_SetLength(laser,length+30*sin(i*6));
			yield;
		}
	}
}

function CreateButterflyA2(x,y,speed,angle,acs,max,delay){
	let obj = CreateShotA2(x,y,speed,angle,acs,max,380,delay);
	ObjShot_SetAutoDelete(obj,false);
	task TManage{
		let gr = 0;
		while(!Obj_IsDeleted(obj) && ObjEnemy_GetInfo(bossObj,INFO_LIFE) > 0){
			ObjShot_SetGraphic(obj,380 + gr);
			if(GetObjectDistance(obj,bossObj)<=10){Obj_Delete(obj);}
			if(GetObjectDistance(obj,GetPlayerObjectID())<=60 && GetObjectDistance(obj,bossObj)<=300){
				let time = 4;
				loop(5){
					loop(5){
						ObjShot_SetGraphic(obj,380 + gr);
						ObjMove_SetDestAtWeight(obj,GetPlayerX,GetPlayerY,1,0.1);
						gr+=1;
						if(gr>=4){gr = 0;}
						loop(time){yield;}
					}
				time++;
					if(time==8){
					let angle = rand(0,360);
					if(!Obj_IsDeleted(bossObj)){SE(se_tan2);
						ascent(i in 0..10){
							CreateShotOA1(obj,0.5,i*36,30,20);
						}}
					}
				}
				ObjMove_SetSpeed(obj,3);
				ObjMove_SetAngle(obj,GetAngleToPlayer(obj));
				while(!Obj_IsDeleted(obj)){
						ObjShot_SetGraphic(obj,380 + gr);
						gr+=1;
						if(gr>=4){gr = 0;}
				yield;}
				break;
			}
		gr+=1;
		if(gr>=4){gr = 0;}
		loop(3){yield;}
		}
	}
	TManage;
	return obj;
}