#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell03"]
#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(3000); }
	case(EV_REQUEST_TIMER){	SetScriptResult(75); }
	case(EV_REQUEST_SPELL_SCORE){ SetScriptResult(8000000); }
}

@Initialize {
	objEnemyMain = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	objScene = GetEnemyBossSceneObjectID;
	ObjEnemy_Regist(objEnemyMain);
	
	let spl = "Mastery of Time and Space";
	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","\""~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, 150, 60, 30);
	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(90);
	PlaySoundEffect(charge,100);
	ConcentrationA2(objEnemyMain,75,7,200,[255,96,96]);
	Walls;
	wait(240);
	Wave1;
	wait(360);
	Wave2;
	
	// Wave3;
}
task Wave1 {
	let dir = 0;
	loop {
		Flash;
		dir = rand(0,360);
		loop(12) {
			Shot1(dir,1);
			Shot1(dir+360/24,-1);
			dir+=360/12;
		}
		wait(360);
	}
}
task Wave2 {
	let dir = 0;
	loop {
		Flash;
		dir = rand(0,360);
		loop(16) {
			Shot2(GetX+80,dir,10);
			Shot2(GetX+80,dir+360/32,3);
			
			Shot2(GetX-80,dir,10);
			Shot2(GetX-80,dir+360/32,3);
			
			dir+=360/16;
		}
		if(GetLife>1600) { wait(60); }
		if(GetLife>800) { wait(30); }
		wait(60);
	}
}
task Wave3 {

}
task Shot2(x,dir,w) {
	let obj = CreateShot01(x+40*cos(dir),GetY+40*sin(dir),6,dir,KNIFE_PURPLE,0);
	wait(w);
	ObjMove_SetAcceleration(obj,-4.5/5);
	ObjMove_SetMaxSpeed(obj,1.5);
}
task Shot1(dir,rot) {
	let r = 90;
	let obj = CreateShot01(GetPlayerX+r*cos(dir),GetPlayerY+r*sin(dir),1.0,0,KNIFE_BLUE,0);
	ObjMove_SetAngle(obj,GetAngleToPlayer(obj));
	// wait(20);
	ObjMove_SetAngularVelocity(obj, 2.0*rot);
	wait(240);
	ObjMove_SetAngularVelocity(obj, 0);
}
task Walls {
	loop {
		ascent(j in -5..6) {
			ascent(i in -3..2) {
				PlaySoundEffect(shot,90);
				CreateShot01(GetCenterX*2,GetCenterY+j*90+i*9,1.4,180,KUNAI_GREEN,20);
				CreateShot01(0,GetCenterY+j*90+i*9+45,1.4,0,KUNAI_GREEN,20);
			}
		}
		wait(120);
	}
}
task Flash {
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	
	let alpha = 255;
	
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	Obj_SetRenderPriorityI(obj,60);
	ObjPrim_SetTexture(obj, CSD ~ "IMG/Enemy/white.png");
	ObjSprite2D_SetSourceRect(obj, 0, 0, 512, 512);
	ObjSprite2D_SetDestCenter(obj);
	
	ObjRender_SetPosition(obj,GetCenterX,GetCenterY,0);

	PlaySoundEffect(slash,90);
	
	loop(15) {
		ObjRender_SetAlpha(obj,alpha);
		alpha-=255/15;
		if(Obj_IsDeleted(objEnemyMain)) { Obj_Delete(obj); }
		yield;
	}
	Obj_Delete(obj);
}
task End {
	while(ObjEnemy_GetInfo(objEnemyMain, INFO_LIFE) > 0) { yield; }
	PlaySoundEffect(shot,100);
	CreateItems02(objEnemyMain,25,2,0,0,60);
	ExplosionB3(objEnemyMain,[255,96,96],[255,96,96]);
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	yield;
	CloseScript(GetOwnScriptID);
}