#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Noncard03"]
#Text[""]
#Background["./Background.dnh"]
#System["./System/System Prototype.dnh"]
#Player["./Player/Rin Satsuki/Rin.dnh","./Player/Tokiko/Tokiko.dnh"]

#include"./Shot/ZUNShot_Const.dnh"
#include"./LIB/Function_Misc.dnh"
#include"./LIB/Function_Audio.dnh"
#include"./LIB/Function_Effect.dnh"
#include"./LIB/Function_Enemy.dnh"
#include"./LIB/Function_Shot.dnh"
#include"./LIB/SE_Const.dnh"
#include"./LIB/Animation/lib_anime_Sakuya.txt"

let CSD = GetCurrentScriptDirectory;
let MD = GetModuleDirectory;

let objEnemyMain;
let objScene;
let objList = [];
let Anim_Attack = 0;
let GetCenterX = GetStgFrameWidth/2;
let GetCenterY = GetStgFrameHeight/2;
let GetLife = 0;
let GetTimer = 0;
let GetX = 0;
let GetY = 0;

let t = 0;
let famCount = 0;

@Loading {
}

@Event {
	alternative(GetEventType)
	case(EV_REQUEST_LIFE){ SetScriptResult(7000); }
	case(EV_REQUEST_TIMER){	SetScriptResult(35); }
}

@Initialize {
	objEnemyMain = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	objScene = GetEnemyBossSceneObjectID;
	ObjEnemy_Regist(objEnemyMain);

	Obj_SetValue(objEnemyMain,"Boss Name","Sakuya Izayoi");
	Obj_SetValue(objEnemyMain,"Aura Color",[1,10,10]);
	PlayEnemyHitSound(objEnemyMain, 1);
	InitializeAction(objEnemyMain, CSD ~ "LIB/Animation/img/dot_sakuya.png");
	DrawBoss_Task(objEnemyMain);
	
	SetInvincibilityDamageRate(objEnemyMain, 120, 250, 240);
	SetAction(objEnemyMain,ACT_MOVE,60);
	ObjMove_SetDestAtWeight(objEnemyMain, GetCenterX, GetCenterY-110, 10, 5);
	
	mainTask;
	End;
	
}

@MainLoop {
	if(!Obj_IsDeleted(objEnemyMain)) {
		GetX = ObjMove_GetX(objEnemyMain);
		GetY = ObjMove_GetY(objEnemyMain);
		GetLife = ObjEnemy_GetInfo(objEnemyMain,INFO_LIFE);
		GetTimer = ObjEnemyBossScene_GetInfo(objScene,INFO_TIMER);
	}
	
	ObjEnemy_SetIntersectionCircleToShot(objEnemyMain, GetX, GetY, 24);
	if(t>150) { ObjEnemy_SetIntersectionCircleToPlayer(objEnemyMain, GetX, GetY, 16); }
	t++;
	yield;
}

task mainTask {
	let dir = 0;
	wait(80);
	PlaySoundEffect(charge,100);
	ConcentrationA2(objEnemyMain,90,7,170,[255,96,96]);
	wait(100);
	PlaySoundEffect(fam,100);
	loop(4) {
		CreateFamiliar(45,dir,1.0,1,KUNAI_RED,[255,96,96]);
		CreateFamiliar(75,dir+360/8,2.5,-1,KUNAI_BLUE,[96,96,255]);
		CreateFamiliar(105,dir,5.0,1,KUNAI_GREEN,[96,255,96]);
		dir+=360/4;
	}
	wait(20);
	loop {
		SetAction(objEnemyMain,ACT_SHOT_A,90);
		wait(10);
		ascent(i in 1..7) {
			PlaySoundEffect(shot,100);
			CreateShot01(GetX,GetY,0.8+0.6*i,GetAngleToPlayer(objEnemyMain),KNIFE_YELLOW,20);
			CreateShot01(GetX,GetY,0.8+0.6*i,GetAngleToPlayer(objEnemyMain)+2.0,KNIFE_YELLOW,20);
			CreateShot01(GetX,GetY,0.8+0.6*i,GetAngleToPlayer(objEnemyMain)-2.0,KNIFE_YELLOW,20);
			CreateShot01(GetX,GetY,0.8+0.6*i,GetAngleToPlayer(objEnemyMain)+4.0,KNIFE_YELLOW,20);
			CreateShot01(GetX,GetY,0.8+0.6*i,GetAngleToPlayer(objEnemyMain)-4.0,KNIFE_YELLOW,20);
			
			CreateShot01(GetX,GetY,0.8+0.6*i,GetAngleToPlayer(objEnemyMain)+30,KNIFE_YELLOW,20);
			CreateShot01(GetX,GetY,0.8+0.6*i,GetAngleToPlayer(objEnemyMain)-30,KNIFE_YELLOW,20);
			CreateShot01(GetX,GetY,0.8+0.6*i,GetAngleToPlayer(objEnemyMain)+60,KNIFE_YELLOW,20);
			CreateShot01(GetX,GetY,0.8+0.6*i,GetAngleToPlayer(objEnemyMain)-60,KNIFE_YELLOW,20);
			CreateShot01(GetX,GetY,0.8+0.6*i,GetAngleToPlayer(objEnemyMain)+90,KNIFE_YELLOW,20);
			CreateShot01(GetX,GetY,0.8+0.6*i,GetAngleToPlayer(objEnemyMain)-90,KNIFE_YELLOW,20);
			CreateShot01(GetX,GetY,0.8+0.6*i,GetAngleToPlayer(objEnemyMain)+120,KNIFE_YELLOW,20);
			CreateShot01(GetX,GetY,0.8+0.6*i,GetAngleToPlayer(objEnemyMain)-120,KNIFE_YELLOW,20);
			CreateShot01(GetX,GetY,0.8+0.6*i,GetAngleToPlayer(objEnemyMain)+150,KNIFE_YELLOW,20);
			CreateShot01(GetX,GetY,0.8+0.6*i,GetAngleToPlayer(objEnemyMain)-150,KNIFE_YELLOW,20);
			CreateShot01(GetX,GetY,0.8+0.6*i,GetAngleToPlayer(objEnemyMain)+180,KNIFE_YELLOW,20);
			CreateShot01(GetX,GetY,0.8+0.6*i,GetAngleToPlayer(objEnemyMain)-180,KNIFE_YELLOW,20);
			wait(4);
		}
		wait(170);
	}
}
task CreateFamiliar(r,angle,revspeed,rot,graphic,color) {
	let objfam = ObjPrim_Create(OBJ_SPRITE_2D);
	let spin = 0;
	
	let path = CSD ~ "IMG/Enemy/fam.png";
	ObjPrim_SetTexture(objfam,path);
	ObjRender_SetBlendType(objfam,BLEND_ADD_RGB);
	ObjSprite2D_SetSourceRect(objfam,0,0,GetTextureWidth(path),GetTextureHeight(path));
	ObjSprite2D_SetDestCenter(objfam);
	Obj_SetRenderPriorityI(objfam,40);
	ObjRender_SetScaleXYZ(objfam,0.35,0.35,1);
	ObjRender_SetColor(objfam,220,220,220);
	
	let count = 0;
	let dir = angle;
	
	ObjRender_SetPosition(objfam, GetX, GetY, 0);
	
	let x = GetX+r*cos(angle);
	let y = GetY+r*sin(angle);
	let distance = GetDistance(ObjRender_GetX(objfam),ObjRender_GetY(objfam),x,y);
	let destangle = 0;
	let max_speed = 3;
	let weight = 10;
	while(distance > 1){
		let speed = distance/weight;
		if(speed > max_speed) { speed = max_speed; }
		x = GetX+r*cos(angle);
		y = GetY+r*sin(angle);
		destangle = atan2(y-ObjRender_GetY(objfam), x-ObjRender_GetX(objfam));
		
		ObjRender_SetX(objfam, ObjRender_GetX(objfam) + cos(destangle)*speed);
		ObjRender_SetY(objfam, ObjRender_GetY(objfam) + sin(destangle)*speed);
		
		distance = GetDistance(ObjRender_GetX(objfam),ObjRender_GetY(objfam),x,y);
		
		ObjRender_SetAngleXYZ(objfam, 0, 0, spin);
		spin+=1.2*rot;
		yield;
	}
	
	ObjRender_SetPosition(objfam,x,y,0);
	famCount++;
	while(famCount<12) { yield; }
	// SetMovePosition(objfam, GetX+r*cos(angle), GetY+r*sin(angle), 10, 3);
	
	while(!Obj_IsDeleted(objfam)) {
		if(count==15) {
			PlaySoundEffect(shot3,90);
			ascent(i in -1..2) {
				CreateShot01(ObjRender_GetX(objfam),ObjRender_GetY(objfam),2.7,dir+i*1.5,graphic,10);
				CreateShot01(ObjRender_GetX(objfam),ObjRender_GetY(objfam),2.7,180+dir+i*1.5,graphic,10);
			}
			dir+=360/15;
			count = 0;
		}
		ObjRender_SetPosition(objfam, GetX+r*cos(angle), GetY+r*sin(angle),0);
		ObjRender_SetAngleXYZ(objfam, 0, 0, spin);
		spin+=1.2*rot;
		count++;
		angle+=revspeed*rot;
		if(Obj_IsDeleted(objEnemyMain)) { ExplosionA1(objfam,color,color); Obj_Delete(objfam); }
		yield;
	}
}
task End {
	while(ObjEnemy_GetInfo(objEnemyMain, INFO_LIFE) > 0) { yield; }
	DeleteShotWave(1,GetX,GetY,15);
	PlaySoundEffect(shot,100);
	Obj_Delete(objEnemyMain);
	yield;
	CloseScript(GetOwnScriptID);
}