#TouhouDanmakufu
#ScriptVersion[3]
#System["./../tricky_system/TrickySystem.dnh"]
#Title["Tricky Nonspell 2"]
#Background["./NamelessPath.dnh"]
#Player["./../lib/Okina_player_by_Python/Okina Matara/Okina.dnh"]
#BGM["./../sfx/Cold Whirlwind of the Forest.ogg"]

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

let GetDifficulty = 2;
let GetStage = 4;

@Event{
	alternative(GetEventType())
		case(EV_REQUEST_LIFE){ SetScriptResult(3000); }
		case(EV_REQUEST_TIMER){ SetScriptResult(40); }
}

@Initialize{

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

	let Eirin = GetCurrentScriptDirectory~"sprites/eirin.png";
	ObjPrim_SetTexture(objEnemy, Eirin); 
	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, 256, 0, 256+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, 256, 128*2, 256+128, 128*3);
	ascent(x in 0..3){ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACK, 128*5, 128*x, 128*6, 128*x+128);}
	descent(x in 0..3){ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACKEND, 128*5, 128*x, 128*6, 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, 2);
	ObjAnim_StartAnimationA2(objEnemy, 0, 0);
	ObjSprite2D_SetDestCenter(objEnemy); 
	
	ObjMove_SetDestAtFrame(objEnemy, GetCenterX, 120, 60);

	SetAutoDeleteObject(true);
	attack1;
	attack2;
	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{
	let angle = 90;
	let way = [8, 10, 14, 16][GetDifficulty];
	wait(120);
	loop{
		PlaySoundEffect("shot5", 95, GetEnemyX);
		loop(way){
			let obj = CreateMDNHShotA2(ObjMove_GetX(objEnemy), ObjMove_GetY(objEnemy), 5, angle, -0.15, 2, BUBBLE[BLUE], 20);
			ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
			angle+=360/way;
		}
		angle+=360/way/2;
		wait([55, 55, 50, 50, 45, 40][GetStage]);
	}
}

task attack2{
	wait(60);
	let left = 0;
	let right = 0;
	let radius = 0;
	let color = 0;
	let angle2 = 0;
	let angle3 = 0;
	let distance = 0;
	let heightdiff = 0;
	let waittime = [60, 60, 40, 25][GetDifficulty] - [0, 5, 10, 15, 20, 25][GetStage];
	let way = [7, 8, 12, 14][GetDifficulty] + [0, 0, 1, 1, 2, 2][GetStage];
	let rate = [36, 38, 40, 42][GetDifficulty] + [0, 2, 4, 6, 8, 10][GetStage];
	wait(120);
	ObjAnim_SetAttackA1(objEnemy, true);
	PlaySoundEffect("charge2", 100, GetEnemyX);
	TCharge01(GetEnemyX, GetEnemyY, 3, 45, 130, 144, 255, BLEND_ADD_ARGB, 25);
	wait(45);
	loop{
		ObjAnim_SetAttackA1(objEnemy, true);
		left = rand(20, 40);
		right = rand(20, 40);
		distance = GetDistance(0-20, left, GetStgFrameWidth+20, right);
		angle2 = AngleBetweenPoints(0-20, left, GetStgFrameWidth+20, right);
		radius = 0;
		loop(rate){
			PlaySoundEffect("shot", 90, (0-20+radius*cos(angle2)));
			loop(way){
				CreateShotA2(0-20+radius*cos(angle2), left+radius*sin(angle2)+heightdiff*cos(radius*2), 4, angle3, -0.05, [2, 2.15, 2.35, 2.55][GetDifficulty] + [0, 0, 0.1, 0.15, 0.25, 0.4][GetStage], [ARROWHEAD[BLUE], ARROWHEAD[CYAN]][color%2], 15);
				angle3+=360/way;
			}
			radius+=(distance/rate);
			wait(4);
			color++;
			angle3+=360/way/2.977667493796526054590570719603;
			}
		ObjAnim_SetAttackA1(objEnemy, false);
		wait(30);
		ExMove(objEnemy, ObjMove_GetX(GetPlayerObjectID), rand(30, 50), rand(110, 130), 40, 3, 15);
		wait(waittime);
		ObjAnim_SetAttackA1(objEnemy, true);
		left = rand(20, 40);
		right = rand(20, 40);
		distance = GetDistance(GetStgFrameWidth+20, right, 0-20, left);
		angle2 = AngleBetweenPoints(GetStgFrameWidth+20, right, 0-20, left);
		radius = 0;
		loop(rate){
			PlaySoundEffect("shot", 90, (GetStgFrameWidth+20+radius*cos(angle2)));
			loop(way){
				CreateShotA2(GetStgFrameWidth+20+radius*cos(angle2), right+radius*sin(angle2)+heightdiff*cos(radius*2), 4, angle3, -0.05, [2.1, 2.2, 2.45, 2.55][GetDifficulty] + [0, 0.05, 0.1, 0.15, 0.25, 0.4][GetStage], [ARROWHEAD[BLUE], ARROWHEAD[CYAN]][color%2], 15);
				angle3+=360/way;
			}
			radius+=(distance/rate);
			wait(4);
			color++;
				angle3-=360/way/2.977667493796526054590570719603;
		}
		ObjAnim_SetAttackA1(objEnemy, false);
		wait(waittime);
		ExMove(objEnemy, ObjMove_GetX(GetPlayerObjectID), rand(30, 50), rand(110, 130), 40, 3, 15);
		wait(30);
		
	}
}