#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["NS1"]
#Text["Baaaaaaaaaap"]
#System["./default_system/Default_System.txt"]
#include "./default_system/MS_Index.txt"


let BossX;
let BossY;
let objBoss;
let appear = false; 


@Initialize {
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	HitSound(objBoss, 75, 450);
	
	if(GetCommonData("IsStage", false)==true){
	SetCommonData("ForceLifebar", 1);
	SetCommonData("BossAlpha", 0);
	}
	else{
	MagicRing(objBoss,true);
	SetCommonData("BossAlpha", 255);
	StageBackground;
	appear=true;
	}
	
	RenderCharacter(objBoss);
	ObjMove_SetDestAtWeight(objBoss, GetStgFrameWidth/2, GetStgFrameHeight/2-100, 10, 4);
	mainTask;
	end;
} 

@Event { 
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(4000);
     	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(105);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(1000000);
	}
}

@MainLoop {
	BossX = ObjMove_GetX(objBoss);
	BossY = ObjMove_GetY(objBoss);
	if(appear == true){
	ObjEnemy_SetIntersectionCircleToShot(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),21);}
	yield;
} 

@Finalize {
} 

task mainTask {
 
 	if(GetCommonData("IsStage", false)==true){
	wait(180);
	ascent(i in 0..45){
	let ang = rand(0,360);
	let obj = Bat(BossX+300*cos(ang), BossY+300*sin(ang), 1000, [rand(100, 150), rand(15, 30), rand(100, 150)], false);
	BatFly(obj);
	wait(2);
	}
	wait(130);
	PlaySound(GetCurrentScriptDirectory ~ "Default_System/se/charge.wav", 255);
	loop(8){
	Concentrate(BossX, BossY, 0, 4, 4, 1, "Cherry", 200, -2, [125, 15, 125]);
	Concentrate(BossX, BossY, 0, 4, 4, 1, "Cherry", 200, 2, [255, 255, 255]);
	wait(5);
	}
	Attack(31);
	wait(30);
	PlaySound(GetCurrentScriptDirectory ~ "Default_System/se/die.wav", 255);
	appear=true;
	SetCommonData("ForceLifebar", 0);
	SetCommonData("BossAlpha", 255);
	MagicRing(objBoss,true);
	wait(10);
	GetCommonData("ForceTimer", 1);}
	wait(50);
	
	
	SetCommonData("MusicProgress", 1);
	
	
	wait(90);
	loop{
	Attack(60);
	PlaySound(GetCurrentScriptDirectory ~ "Default_System/se/charge.wav", 255);
	loop(10){
	Concentrate(BossX, BossY, 0, 4, 3, 0.8, "Cherry", 150, -2, [125, 15, 125]);
	Concentrate(BossX, BossY, 0, 4, 3, 0.8, "Cherry", 150, 2, [255, 255, 255]);
	wait(6);
	}
	ascent(j in 0..3){
		ascent(i in 0..32){
		let obj = CreateShotA2(BossX, BossY, 4, i*360/32+j*360/64, -0.05, 1, RICE_M_PURPLE, 2);
		ObjMove_AddPatternA2(obj, 50, 1+(j/2), NO_CHANGE, 0, 0, 5);
		}
	}
	PlaySound(GetCurrentScriptDirectory ~ "Default_System/se/spirit.wav", 80);
	wait(10);
		loop(3){
		let obj = Bat(GetStgFrameWidth/2+rand(-100, 100), GetStgFrameHeight/2+rand(-200, 0), 300, [122, 15, 122], true);
		Shoot(obj);
		wait(10);
		}
	wait(90);
	ObjMove_SetDestAtWeight(objBoss, GetStgFrameWidth/2+rand(-50, 50), GetStgFrameHeight/3+rand(-50, 50), 10, 5);
	wait(180);
	}
	
	task Shoot(obj){
		while(!Obj_IsDeleted(obj)){
		let angle = GetAngleToPlayer(obj);
			loop(3){
			if(Obj_IsDeleted(obj)){break;}
			CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 3, angle, DOT, 10);
			PlaySound(GetCurrentScriptDirectory ~ "Default_System/se/smbullet.wav", 50);
			wait(3);
			}
			wait(30);
			let random = rand(-1, 1);
			loop(3){
			if(Obj_IsDeleted(obj)){break;}
			CreateShotB2(ObjMove_GetX(obj), ObjMove_GetY(obj), random, -2, 0, 0.02, 5, 5, DOT, 10);
			PlaySound(GetCurrentScriptDirectory ~ "Default_System/se/smbullet.wav", 50);
			wait(3);
			}
			wait(30);
		}
	}
}

task BatFly(obj){
wait(30);
let angle = rand(0,360);
ObjMove_SetDestAtWeight(obj, BossX+rand(0, 50)*cos(angle), BossY+rand(0, 50)*sin(angle), 10, 4);
while(appear != true){yield}
ObjMove_SetDestAtWeight(obj, BossX+600*cos(angle+180), BossY+600*sin(angle+180), 10, 6);
wait(180);
Obj_Delete(obj);
}

task end{
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){yield;}
    Obj_Delete(objBoss);
    DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	SetCommonData("deathsound",1);
    SetAutoDeleteObject(true);
    CloseScript(GetOwnScriptID());
} 