#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell02"]
#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;

@Loading {
	LoadSound(CSD ~ "SE/timestop.wav");
}

@Event {
	alternative(GetEventType)
	case(EV_REQUEST_LIFE){ SetScriptResult(700); }
	case(EV_REQUEST_TIMER){	SetScriptResult(60); }
	case(EV_REQUEST_SPELL_SCORE){ SetScriptResult(6000000); }
}

@Initialize {
	objEnemyMain = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	objScene = GetEnemyBossSceneObjectID;
	ObjEnemy_Regist(objEnemyMain);
	
	let spl = "Silver Bound";
	Obj_SetValue(objEnemyMain,"Boss Name","Sakuya Izayoi");
	Obj_SetValue(objEnemyMain,"Aura Color",[1,10,10]);
	Obj_SetValue(objEnemyMain,"Spell ID","001");
	Obj_SetValue(objEnemyMain,"Spell Name","Silver Sign \""~spl~"\"");
	Obj_SetValue(objEnemyMain,"Cut Image",[CSD ~ "IMG/Enemy/SakuyaCut.png"]);
	PlayEnemyHitSound(objEnemyMain, 1);
	InitializeAction(objEnemyMain, CSD ~ "LIB/Animation/img/dot_sakuya.png");
	DrawBoss_Task(objEnemyMain);
	
	ObjEnemyBossScene_StartSpell(objScene);
	
	SetInvincibilityDamageRate(objEnemyMain, 240, 32, 20);
	SetAction(objEnemyMain,ACT_MOVE,60);
	ObjMove_SetDestAtWeight(objEnemyMain, GetCenterX, GetCenterY-110, 10, 5);
	
	mainTask;
	End;
	
}

@MainLoop {
	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);
	// ObjEnemy_SetIntersectionCircleToPlayer(objEnemyMain, GetX, GetY, 16);
	yield;
}

task mainTask {
	wait(150);
	let dir = 0;
	loop {
		SetAction(objEnemyMain,ACT_SHOT_B,120);
		wait(10);
		dir = rand(0,360);
		PlaySoundEffect(shot2,100);
		BounceRing(22,1.6,dir);
		BounceRing(22,1.0,dir+360/50);
		wait(90);
		PlaySoundEffect(charge2,100);
		ConcentrationA1(objEnemyMain,30,5,170,[255,96,96]);
		wait(60);
		PlaySE(CSD ~ "SE/timestop.wav");
		TimeStopB1(objEnemyMain,120);
		SetAction(objEnemyMain,ACT_MOVE,90);
		ObjMove_SetDestAtWeight(objEnemyMain, GetPlayerX, GetPlayerY, 10, 4);
		Fire;
		waitTimeStop(90);
	}
}
task Fire {
	let dir = GetAngleToPlayer(objEnemyMain);
	loop(90/15) {
		if(GetDistance(GetPlayerX,GetPlayerY,GetX,GetY)>70) {
			CreateShot02(GetX,GetY,0,dir,1.2/30,1.2,KNIFE_BLUE,20);
			CreateShot02(GetX,GetY,0,dir+90,1.2/30,1.2,KNIFE_BLUE,20);
			CreateShot02(GetX,GetY,0,dir+180,1.2/30,1.2,KNIFE_BLUE,20);
			CreateShot02(GetX,GetY,0,dir+270,1.2/30,1.2,KNIFE_BLUE,20);
			
			CreateShot02(GetX,GetY,0,dir+45,0.6/30,0.6,KNIFE_BLUE,20);
			CreateShot02(GetX,GetY,0,dir+135,0.6/30,0.6,KNIFE_BLUE,20);
			CreateShot02(GetX,GetY,0,dir+225,0.6/30,0.6,KNIFE_BLUE,20);
			CreateShot02(GetX,GetY,0,dir+315,0.6/30,0.6,KNIFE_BLUE,20);
		}
		dir+=360/15;
		wait(15);
	}
}
task BounceRing(num,speed,dir) {
	let objRing = CreateShotRing02(num,GetX,GetY,5,dir,-(5-speed)/20,speed,KNIFE_PURPLE,10);
	ascent(i in 0..length(objRing)) {
		Bounce(objRing[i]);
		CheckStop(objRing[i]);
	}
	task CheckStop(obj) {
		while(!Obj_IsDeleted(obj)) {
			if(GetCommonData("TimeStop",false)) { 
				ObjMove_SetAcceleration(obj,0);
			}
			else {
				ObjMove_SetAcceleration(obj,-(5-speed)/20);
			}
			yield;
		}
	}
	task Bounce(obj) {
		while(!Obj_IsDeleted(obj)) {
			if(ObjMove_GetX(obj)>GetCenterX*2) {
				PlaySoundEffect(wave,90);
				ObjMove_SetAngle(obj,180-ObjMove_GetAngle(obj));
				break;
			}
			if(ObjMove_GetX(obj)<0) {
				PlaySoundEffect(wave,90);
				ObjMove_SetAngle(obj,180-ObjMove_GetAngle(obj));
				break;
			}
			if(ObjMove_GetY(obj)<0) {
				PlaySoundEffect(wave,90);
				ObjMove_SetAngle(obj,-ObjMove_GetAngle(obj));
				break;
			}
			if(ObjMove_GetY(obj)>GetCenterY*2) {
				PlaySoundEffect(wave,90);
				ObjMove_SetAngle(obj,-ObjMove_GetAngle(obj));
				break;
			}
			waitTimeStop(1);
		}
	}
}
task End {
	while(ObjEnemy_GetInfo(objEnemyMain, INFO_LIFE) > 0) { yield; }
	PlaySoundEffect(shot,100);
	CreateItems01(25,2,GetX,GetY,60);
	Obj_Delete(objEnemyMain);
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	yield;
	CloseScript(GetOwnScriptID);
}