#TouhouDanmakufu
#ScriptVersion[3]
#System["./../tricky_system/TrickySystem.dnh"]
#Title["Shokora Midboss Spell 3"]
#Background["./NamelessPath.dnh"]
#Player["./../lib/Okina_player_by_Python/Okina Matara/Okina.dnh"]

#include "../lib/UNL_Shot/lib_ShotConstants.dnh"
#include "./../lib/includes/BossIncludes.dnh"
#include "./../lib/includes/EffectFunctions.dnh"

@Event{
	alternative(GetEventType())
		case(EV_REQUEST_LIFE){ SetScriptResult(1000); }
		case(EV_REQUEST_TIMER){ SetScriptResult(999); }
		case(EV_REQUEST_SPELL_SCORE){ SetScriptResult(1000000); }
		case(EV_GAIN_SPELL){ AddScore(ObjEnemyBossScene_GetInfo(objScene, INFO_SPELL_SCORE)); }
		case(EV_REQUEST_IS_DURABLE_SPELL) { SetScriptResult(true); }
}

@Initialize{
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	ObjMove_SetPosition(objEnemy, -1500, -1500);
	SetAutoDeleteObject(true);
	delayedstart;
	End;
}

task End {
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0) { yield; }
	SetShotAutoDeleteClip(32, 40, 32, 32);
	NotifyEventAll(EV_SPELLBGEND, 1);
	CreateItems02(22, ITEM_POINT_S, GetStgFrameWidth/2, -40, 0, -50, 150, 35, 10000);
	//DeleteShotWave(1,GetX,GetY,20);
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	//PlaySoundEffect("end",60,GetCenterX);
	//NotifyEventAll(EV_SPAWN_ENEMY,[99.1,GetEnemyX,GetEnemyY]);
	Obj_Delete(objEnemy);
	CloseScript(GetOwnScriptID);
}

task delayedstart{
	ObjEnemyBossScene_StartSpell(objScene);
	ObjEnemyBossScene_SetSpellTimer(objScene, 60*24.75);
	PlaySoundEffect("cardcall", 90, GetCenterX);
	
	let lbcut = GetCurrentScriptDirectory~"sprites/shokorasprite.png";
	ObjCutin_SetSpellcardS4("Fire Tome &quot;Meteor&quot;", lbcut, "AYA", "255", "0", "0");
	ObjCutin_LaunchS3("AYA", "0", "Hard");
	NotifyEventAll(EV_SPELLBG, 1);
	SetShotAutoDeleteClip(32, 64, 32, 32);
	attack1;
}

task attack1{
	wait(240);
	let color = 0;
	angle = 270;
	let time = 115;
	let radius = 35;
	let offset = 0;
	let x = 0;
	let y = 0;
	angle = rand(0, 360);
	let wavevariable = 0.42;
	
	loop(2){
		PlaySoundEffect("sagecast", 90, GetCenterX);
		wait(30);
		PlaySoundEffect("fire2", 90, GetCenterX+90);
		let obj = CreateLooseLaserA1(GetCenterX+105, -60, 5, 90+rand(-1, 1), 150, 150, FIRE[RED], 0);
		ManageFireball(obj, wavevariable);
		wavevariable+=0.02;
		wait(195);
		PlaySoundEffect("sagecast", 90, GetCenterX);
		wait(30);
		PlaySoundEffect("fire2", 90, GetCenterX-90);
		let obj2 = CreateLooseLaserA1(GetCenterX-105, -60, 5, 90+rand(-1, 1), 150, 150, FIRE[RED], 0);
		ManageFireball(obj2, wavevariable);
		wait(195);
		wavevariable+=0.02;
	}
	
	PlaySoundEffect("sagecast", 90, GetCenterX);
	wait(30);
	PlaySoundEffect("fire2", 90, GetCenterX);
	let obj = CreateLooseLaserA1(GetCenterX, -60, 5, 90+rand(-1, 1), 150, 150, FIRE[RED], 0);
	ManageFireball(obj, wavevariable);
}

task ManageFireball(obj, wave){
	ObjMove_SetMaxSpeed(obj, 1.5*1.16666666667);
	ObjShot_SetIntersectionEnable(obj, false);
	ObjMove_SetAcceleration(obj, -3/45);
	ObjShot_SetSpellResist(obj, true);
	let rad = 45;
	let time = 0;
	let dir = 0;
	
	while(!Obj_IsDeleted(obj)){
		if(time%9==0 && time>45){
		PlaySoundEffect("shot5", 85, ObjMove_GetX(obj));
		loop(3){
			let obj2 = CreateMDNHShotA2(ObjMove_GetX(obj)+(rad*1.25)*cos(-90+dir), (ObjMove_GetY(obj)-70)+rad*sin(-90+dir), 3, -90+dir, -0.05, 0.95, FIRE[RED], 5);
			let obj3 = CreateMDNHShotA2(ObjMove_GetX(obj)+(rad*1.25)*cos(-90-dir), (ObjMove_GetY(obj)-70)+rad*sin(-90-dir), 3, -90-dir, -0.05, 0.95, FIRE[RED], 5);
			Obj_SetRenderPriorityI(obj2, Obj_GetRenderPriorityI(obj2)-1);
			Obj_SetRenderPriorityI(obj3, Obj_GetRenderPriorityI(obj3)-1);
			ObjRender_SetBlendType(obj2, BLEND_ADD_ARGB);
			ObjRender_SetBlendType(obj3, BLEND_ADD_ARGB);
			dir+=360/3;
		}
		dir+=180 * (3 - 5^wave);
		}
		time++;
		yield;
	}
}