#TouhouDanmakufu
#ScriptVersion[3]
#System["./../tricky_system/TrickySystem.dnh"]
#Title["Kosuke Midboss Nonspell"]
#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(3000); }
		case(EV_REQUEST_TIMER){ SetScriptResult(28); }
}

@Initialize{

	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(objEnemy, 180, 70, 75);
	PlayEnemyHitSound(objEnemy, 1);

	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(8){ 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); 
	
	ObjMove_SetPosition(objEnemy, GetCenterX+rand(-40, 40), -115);
	ObjMove_SetDestAtFrame(objEnemy, GetCenterX, GetClipMinY+120, 45);
	
	SetAutoDeleteObject(true);
	attack1;
	End;
}

task End {
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0) { yield; }
	//DeleteShotWave(1,GetX,GetY,20);
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	PlaySoundEffect("end",60,GetCenterX);
	Obj_Delete(objEnemy);
	CloseScript(GetOwnScriptID);
}

task attack1{
	color = 0;
	wait(45);
	ObjAnim_SetAttackA1(objEnemy, true);
	wait(15);
	
	loop{
	
		PlaySoundEffect("charge2", 100, GetEnemyX);
		TCharge01(GetEnemyX, GetEnemyY, 3, 45, 255, 0, 0, BLEND_ADD_ARGB, 25);
		wait(15);
		
		FlameVortex(switch, [FIRE[RED], FIRE[ORANGE]][color%2]);
		switch*=-1;
		wait(175);
	}	
}

	task FlameVortex(dir, color){
		let shottotal = 120; //if needs to be nerfed, try shot total of 90 and wait time of 4 and change radiusacc accordingly maybe???
		let way = 3;
		let speed = 5;
		let maxspeed = 1.5;
		let radius = 1.5 * 80;
		let radiusacc = ( 11 - radius ) / ( shottotal - 1 );
		let whirlangle = rand( 0, 360 );
		let whirlangleacc = 18.15 * dir; //original difficulty is way = 4, whirlangleacc = 19.15

		loop(shottotal){
			if(!Obj_IsDeleted(objEnemy)){
			let _nX = GetEnemyX;
			let _nY = GetEnemyY;
			let whirlangle2 = whirlangle;
			loop(way){
				let nX = GetEnemyX+radius*cos(whirlangle2);
				let nY = GetEnemyY+radius*sin(whirlangle2);
				let shotangle = whirlangle2 - 90 * dir;
				let obj = CreateMDNHShotA2(nX, nY, 4.85, shotangle, -3.5/30, 2.70, color, 30);
				ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
				whirlangle2+= 360/way;
			}
			radius += radiusacc;
			whirlangle+=whirlangleacc;
			wait(3);
			}
		}
	}