#TouhouDanmakufu
#ScriptVersion[3]
#System["./../tricky_system/TrickySystem.dnh"]
#Title["Kosuke Midboss Spell"]
#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"

@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)); }
}

@Initialize{

	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	PlayEnemyHitSound(objEnemy, 1);
	Invuln(objEnemy, 300, 18.1, 25);
	
	let Youmu = GetCurrentScriptDirectory~"sprites/Youmu.png";
	ObjPrim_SetTexture(objEnemy, Youmu); 
	ObjSprite2D_SetSourceRect(objEnemy, 0, 0, 128, 128);
	ascent(x in 0..3){ObjAnim_AddFrameA1(objEnemy, ANIM_IDLE, 0, 128*x, 128, 128*x+128);}
	ascent(x in 0..2){ObjAnim_AddFrameA1(objEnemy, ANIM_IDLE, 128, 128*x, 256, 128*x+128);}
	ascent(x in 0..2){ObjAnim_AddFrameA1(objEnemy, ANIM_MOVE_LEFT, 256, 128*x, 256+128, 128*x+128);}
	ObjAnim_AddFrameA1(objEnemy, ANIM_MOVEEND_LEFT, 0, 0, 128, 128);
	ascent(x in 2..4){ObjAnim_AddFrameA1(objEnemy, ANIM_MOVE_RIGHT, 256, 128*x, 256+128, 128*x+128);}
	ObjAnim_AddFrameA1(objEnemy, ANIM_MOVEEND_RIGHT, 0, 0, 128, 128);
	ascent(x in 0..4){ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACK, 128*4, 128*x, 128*5, 128*x+128);}
	ascent(x in 2..4){ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACKEND, 128*5, 128*x, 128*6, 128*x+128);}
	loop(4){ ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACKEND, 128*5, 128*7, 128*6, 128*7+128); }
	descent(x in 2..4){ ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACKEND, 128*5, 128*x, 128*6, 128*x+128);}
	descent(x in 0..3){ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACKEND, 128*4, 128*x, 128*5, 128*x+128);}
	ObjAnim_SetSpeedA1(objEnemy, ANIM_IDLE, 12);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_LEFT, 6);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_RIGHT, 6);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_ATTACK, 10);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_RIGHT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_LEFT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_ATTACK, 3);
	ObjAnim_StartAnimationA2(objEnemy, 0, 0);
	ObjSprite2D_SetDestCenter(objEnemy); 
	
	SetAutoDeleteObject(true);
	delayedstart;
	End;

}

task End {
	let itemx = 0;
	let itemy = 0;
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0) { yield; }
	SetShotAutoDeleteClip(32, 40, 32, 32);
	NotifyEventAll(EV_SPELLBGEND, 1);
	//DeleteShotWave(1,GetX,GetY,20);
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	PlaySoundEffect("end",60,GetCenterX);
	NotifyEventAll(EV_SPAWN_ENEMY,[99.1,GetEnemyX,GetEnemyY]);
	loop(10){
		itemx = GetEnemyX+rand(-25, 25);
		itemy = GetEnemyY+rand(-25, 25);
		CreateItemA2(ITEM_POINT_S, itemx, itemy, itemx, itemy-50, 10000);
		}
	Obj_Delete(objEnemy);
	CloseScript(GetOwnScriptID);
}

task delayedstart{
	ObjMove_SetDestAtFrame(objEnemy, GetCenterX, 120, 60);
	wait(60);
	
	ObjEnemyBossScene_StartSpell(objScene);
	ObjEnemyBossScene_SetSpellTimer(objScene, 60*38.75);
	PlaySoundEffect("cardcall", 90, GetCenterX);
	
	let lbcut = GetCurrentScriptDirectory~"sprites/kosukesprite.png";
	ObjCutin_SetSpellcardS4("Counter-Magic &quot;Recursive&quot;", lbcut, "NAZRIN", "255", "165", "0");
	ObjCutin_LaunchS3("NAZRIN", "0", "Hard");
	NotifyEventAll(EV_SPELLBG, 0);
	
	wait(120);
	SetShotAutoDeleteClip(175, 175, 175, 175);
	attack1;
}

task attack1{	
	let color = 0;
	angle = 270;
	let radius = 50;
	let offset = 0;
	wait(30);
	
	loop{
	
		offset = [45, 60, 75, 60][color%2];
		PlaySoundEffect("boon", 90, GetPlayerX);
		ascent(i in 0..50){
			if(i%10==0){
				Recurshot(GetPlayerX+radius*cos(angle), GetPlayerY+radius*sin(angle), -2.1, angle+180, 2.1/45, 1, [AMULET[ORANGE], AMULET[YELLOW]][color%2], 45, 3, offset);
			}
			else{
				CreateMDNHShotA2(GetPlayerX+radius*cos(angle), GetPlayerY+radius*sin(angle), -2.1, angle+180, 2.1/45, -0.985, [AMULET[ORANGE], AMULET[YELLOW]][color%2], 45);
			}
			angle+=360/50;
		}
		color++;
		angle = rand(0, 360);
		wait(240);
		
		ExMove(objEnemy, GetPlayerX, absolute(GetEnemyX-GetPlayerX)/2+rand(-5, 5), 120+rand(-5, 5), 20, 3, 15);
		wait(60);
	}
}

task Recurshot(x, y, v, angle, accel, minmax, graphic, delay, splittime, offset){
	let obj = CreateMDNHShotA2(x,y,v,angle,accel,minmax,graphic,delay);
	ObjShot_SetSpellResist(obj, true); 
	Manage(obj);
	
	task Manage(obj){
		let time = 0;
		while(!Obj_IsDeleted(obj)){
			if(time>=45 && splittime>0 && !Obj_IsDeleted(objEnemy)){
				PlaySoundEffect("shot2", 90, GetCenterX);
				ascent(i in -1..2){
					Recurshot(ObjMove_GetX(obj), ObjMove_GetY(obj), v, angle+(i*(offset)), accel, minmax, graphic, delay, splittime-1, offset);
				}
				Obj_Delete(obj);
			}
			time++;
			if(Obj_IsDeleted(objEnemy)){ Obj_Delete(obj); }
			yield;
		}
	}
}