#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell01"]
#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 time = false;

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

@Event {
	alternative(GetEventType)
	case(EV_REQUEST_LIFE){ SetScriptResult(1100); }
	case(EV_REQUEST_TIMER){	SetScriptResult(60); }
	case(EV_REQUEST_SPELL_SCORE){ SetScriptResult(4000000); }
}

@Initialize {
	objEnemyMain = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	objScene = GetEnemyBossSceneObjectID;
	ObjEnemy_Regist(objEnemyMain);
	
	let spl = "C. Ricochet";
	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","Light Speed \""~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, 270, 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);
	
	if(!GetCommonData("TimeStop",false) && t >270) { ObjEnemy_SetDamageRate(objEnemyMain,32,20); }
	
	ObjEnemy_SetIntersectionCircleToShot(objEnemyMain, GetX, GetY, 24);
	ObjEnemy_SetIntersectionCircleToPlayer(objEnemyMain, GetX, GetY, 16);
	t++;
	yield;
}

task mainTask {
	wait(90);
	let dir = 0;
	let rand_int = 0;
	Fire;
	loop {
		PlaySoundEffect(charge,100);
		ConcentrationA2(objEnemyMain,75,7,200,[255,96,96]);
		wait(90);
		SetAction(objEnemyMain,ACT_SHOT_A,90);
		waitTimeStop(15);
		rand_int = rand(0,100);
		if(rand_int<=50) { dir = rand(230,250); }
		else { dir = rand(290,310); }
		RicochetKnife(dir);
		RicochetKnife(270+(270-dir));
		waitTimeStop(390);
	}
}
task Fire {
	wait(180);
	loop {
		CurveRing(24,rand(0,360),1);
		waitTimeStop(13);
		CurveRing(24,rand(0,360),-1);
		waitTimeStop(13);
	}
}
task CurveRing(num,dir,rot) {
	let obj = CreateShotRing01(num,GetX,GetY,2.3,dir,KUNAI_BLUE,15);
	PlaySoundEffect(shot,90);
	CheckStop;
	waitTimeStop(20);
	ascent(i in 0..length(obj)) {
		ObjMove_SetAngularVelocity(obj[i],0.6*rot);
	}
	waitTimeStop(90);
	ascent(i in 0..length(obj)) {
		ObjMove_SetAngularVelocity(obj[i],0);
	}
	task CheckStop {
		wait(15);
		if(GetCommonData("TimeStop",false)) {
			ascent(i in 0..length(obj)) {
				Obj_Delete(obj[i]);
			}
		}
	}
}
task RicochetKnife(dir) {
	let obj = CreateLaser01(GetX,GetY,35,dir,130,30,KNIFE_RED,0);
	ObjShot_SetIntersectionEnable(obj, false);
	loop(360) {
		if(ObjMove_GetX(obj)>GetCenterX*2) {
			PlaySE(CSD ~ "SE/knife.wav");
			ObjMove_SetAngle(obj,180-ObjMove_GetAngle(obj)+rand(-2,2));
			ObjMove_SetX(obj,ObjMove_GetX(obj)-5);
		}
		if(ObjMove_GetX(obj)<0) {
			PlaySE(CSD ~ "SE/knife.wav");
			ObjMove_SetAngle(obj,180-ObjMove_GetAngle(obj)+rand(-2,2));
			ObjMove_SetX(obj,ObjMove_GetX(obj)+5);
		}
		if(ObjMove_GetY(obj)>GetCenterY*2) {
			PlaySE(CSD ~ "SE/knife.wav");
			ObjMove_SetAngle(obj,-ObjMove_GetAngle(obj)+rand(-2,2));
			ObjMove_SetY(obj,ObjMove_GetY(obj)-5);
		}
		if(ObjMove_GetY(obj)<0) {
			PlaySE(CSD ~ "SE/knife.wav");
			ObjMove_SetAngle(obj,-ObjMove_GetAngle(obj)+rand(-2,2));
			ObjMove_SetY(obj,ObjMove_GetY(obj)+5);
		}
		waitTimeStop(1);
	}
	loop {
		if(ObjMove_GetX(obj)>GetCenterX*2) {
			PlaySE(CSD ~ "SE/knife.wav");
			ObjMove_SetAngle(obj,180-ObjMove_GetAngle(obj)+rand(-2,2));
			ObjMove_SetX(obj,ObjMove_GetX(obj)-5);
			if(ObjMove_GetY(obj)<GetCenterY) { break; }
		}
		if(ObjMove_GetX(obj)<0) {
			PlaySE(CSD ~ "SE/knife.wav");
			ObjMove_SetAngle(obj,180-ObjMove_GetAngle(obj)+rand(-2,2));
			ObjMove_SetX(obj,ObjMove_GetX(obj)+5);
			if(ObjMove_GetY(obj)<GetCenterY) { break; }
		}
		if(ObjMove_GetY(obj)>GetCenterY*2) {
			PlaySE(CSD ~ "SE/knife.wav");
			ObjMove_SetAngle(obj,-ObjMove_GetAngle(obj)+rand(-2,2));
			ObjMove_SetY(obj,ObjMove_GetY(obj)-5);
		}
		if(ObjMove_GetY(obj)<0) {
			PlaySE(CSD ~ "SE/knife.wav");
			ObjMove_SetAngle(obj,-ObjMove_GetAngle(obj)+rand(-2,2));
			ObjMove_SetY(obj,ObjMove_GetY(obj)+5);
			if(ObjMove_GetY(obj)<GetCenterY) { break; }
		}
		waitTimeStop(1);
	}
	Hitbox;
	
	ObjMove_SetAngle(obj,GetAngleToPlayer(obj));
	ObjMove_SetSpeed(obj,15);
	waitTimeStop(5);
	
	if(!time) {
		time = true;
		PlaySE(CSD ~ "SE/metal.wav");
		TimeStopB1(objEnemyMain,45);
	}
	waitTimeStop(1);
	time = false;
	// waitTimeStop(45);
	// while(GetDistance(ObjMove_GetX(obj),ObjMove_GetY(obj),GetPlayerX,GetPlayerY)>130) { yield; }
	// StartSlow(TARGET_ALL,30);
	// wait(45);
	// StopSlow(TARGET_ALL);
	
	task Hitbox {
		while(!Obj_IsDeleted(obj)) {
			SetShotIntersectionLine(ObjMove_GetX(obj),ObjMove_GetY(obj),ObjMove_GetX(obj)-130*cos(ObjMove_GetAngle(obj)),ObjMove_GetY(obj)-130*sin(ObjMove_GetAngle(obj)),7);
			yield;
		}
	}
}
task End {
	while(ObjEnemy_GetInfo(objEnemyMain, INFO_LIFE) > 0) { yield; }
	PlaySoundEffect(shot,100);
	CreateItems01(20,2,GetX,GetY,60);
	Obj_Delete(objEnemyMain);
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	CloseScript(GetOwnScriptID);
}