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

let GetDifficulty = 2;
let GetStage = 4;

phase = 0;

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

@Initialize{

	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(objEnemy, 420, 6, 0.1);
	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); 
	
	SetCommonData("pausebeforebonus", true);
	SetAutoDeleteObject(true);
	delayedstart;
	End;
}

task End {
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0) { yield; }
	BossExplosionTricky(objEnemy, 3, GetOwnScriptID);
	
}

task delayedstart{

	ObjMove_SetDestAtFrame(objEnemy, GetCenterX, 120, 60);
	wait(90);
	PlaySoundEffect("charge", 100, GetEnemyX);
	TCharge01(GetEnemyX, GetEnemyY, 3, 90, 255, 255, 255, BLEND_ADD_ARGB, 25);
	wait(90);
	
	ObjEnemyBossScene_StartSpell(objScene);
	ObjEnemyBossScene_SetSpellTimer(objScene, 120*60);
	PlaySoundEffect("cardcall", 90, GetCenterX);
	
	let shokocut = GetCurrentScriptDirectory~"sprites/trickysprite.png";
	ObjCutin_SetSpellcardS4("Woven Elements &quot;Components of Nature&quot;", shokocut, "NAZRIN", "255", "255", "255");
	ObjCutin_LaunchS3("NAZRIN", "0", "Hard");
	NotifyEventAll(EV_SPELLBG, 3);
	
	wait(120);
	attack1;
	attack2;
	attack3; 
	ManagePhase;
	SetShotAutoDeleteClip(32, 40, 32, 32);
}

task ManagePhase{
	ObjAnim_SetAttackA1(objEnemy, true);
	loop{
		if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE)<=700 || ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_TIMER)<=60 && phase == 0){
			phase = 1; 
			PlaySoundEffect("charge2", 85, GetCenterX);
			TCharge01(GetEnemyX, GetEnemyY, 3, 45, 255, 255, 255, BLEND_ADD_ARGB, 25);
			wait(60);
		}
		if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE)<=400 || ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_TIMER)<=30 && phase == 1){
			phase = 2; 
			PlaySoundEffect("charge2", 85, GetCenterX);
			TCharge01(GetEnemyX, GetEnemyY, 3, 45, 255, 255, 255, BLEND_ADD_ARGB, 25);
			wait(60);
		}
	yield;
	}
}

task attack1{
	let x1 = GetCenterX;
	let y1 = 0;
	let loc = 0;
	wait(60);
	loop{
		y1 = rand(50, 110);
		PlaySoundEffect("shot5", 85, x1+(180*sin(loc))+rand(-12.5, 12.5));
		let obj = CreateDNHShotA1(x1+(180*sin(loc))+rand(-12.5, 12.5), y1, rand(1.85, 2.75), 90+rand(-2, 2), CIRCLE_L[ORANGE], 20);
		wait(10-phase);
		loc+=31.235;
		if(absolute(sin(loc))>0.9){
		loc+=10;
		}
	}
}

task attack2{
	let loc = 0;
	let angle = 0;
	wait(60);
	loop{
		PlaySoundEffect("shot2", 85, GetEnemyX);
		loop(5+floor(phase/2)){
			let obj = CreateDNHShotA2(GetEnemyX, GetEnemyY, 4, angle+45*sin(loc), -0.05, 2.25, PELLET[BLUE], 10);
			ObjShot_SetSpellResist(obj, true);
			let obj2 = CreateDNHShotA2(GetEnemyX, GetEnemyY, 4, angle+45*sin(loc)+360/10, -0.05, 1.5, PELLET[CYAN], 10);
			ObjShot_SetSpellResist(obj2, true);
			angle+=360/(5+floor(phase/2));
			}
		angle--;
		wait(8-phase);
		loc+=17;
	}
}

task attack3{
	let loc = 0;
	let angle2 = 0;
	let tar = AngleBetweenPoints(0+20, 40, GetCenterX, GetCenterY);
	let tar2 = AngleBetweenPoints(GetStgFrameWidth-20, 40, GetCenterX, GetCenterY);
	wait(60);
	loop{
		loop(10){
			PlaySoundEffect("shot", 85, GetEnemyX);
			loop(13){
				if(NormalizeAngle(tar+angle2)<=160 || NormalizeAngle(tar+angle2) >=290){
					CreateDNHShotA2(0+20, 40, 1, tar+angle2, 0.035, 3, ARROWHEAD[GREEN], 10);
				}
				if(NormalizeAngle(tar-angle2)>=290 || NormalizeAngle(tar-angle2)<=160){
					CreateDNHShotA2(GetStgFrameWidth-20, 40, 1, tar2-angle2, 0.035, 3, ARROWHEAD[GREEN], 10);
				}
			angle2+=360/13;
			}
		wait(4);
		}
		angle2+=360/13/(3+(phase/4));
		wait(25-phase*5);
		loc++;
	}
}