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

@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, 240, 10, 25);
	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); 
	
	SetAutoDeleteObject(true);
	delayedstart;
	End;
}

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

task delayedstart{

	ObjMove_SetDestAtFrame(objEnemy, GetCenterX, 120, 60);
	wait(60);
	
	ObjEnemyBossScene_StartSpell(objScene);
	ObjEnemyBossScene_SetSpellTimer(objScene, 60*60);
	PlaySoundEffect("cardcall", 90, GetCenterX);
	
	let shokocut = GetCurrentScriptDirectory~"sprites/trickysprite.png";
	ObjCutin_SetSpellcardS4("Conjury &quot;Fluid Aura&quot;", shokocut, "NAZRIN", "30", "144", "255");
	ObjCutin_LaunchS3("NAZRIN", "0", "Hard");
	NotifyEventAll(EV_SPELLBG, 3);
	
	wait(120);
	attack1;
	attack2;
	SetShotAutoDeleteClip(32, 40, 32, 32);
}

task attack1{
	let one = -1;
	wait(0);
	let angle2 = 0;
	let color = 0;
	let x = 0;
	let y = GetCenterY;
	let angle3 = 0;
	let frame = 0;
	let randfactor = [1, 1.25, 1.75, 2, 2.5, 3][GetStage];
	let spacing = [70, 60, 50, 40][GetDifficulty];
	PlaySoundEffect("charge2", 100, GetEnemyX);
	TCharge01(GetEnemyX, GetEnemyY, 3, 45, 30, 144, 255, BLEND_ADD_ARGB, 25);
	wait(60);
	loop{
		if(y>60){ y = y%15; }
		y+=10;
		PlaySoundEffect("shot4", 100, GetEnemyX);
		ascent(i in -11..12){
			if(color%2==0){
				CreateDNHShotA2(0, y+(i*spacing)+rand(-spacing/4, spacing/4),  0, rand(-randfactor, randfactor), 0.02, rand(2.13, 2.27), ARROWHEAD[BLUE], 10);
			}
			else{
				CreateDNHShotA2(GetStgFrameWidth, y+(i*spacing)+rand(-spacing/4, spacing/4),  0, rand(-randfactor, randfactor)+180, 0.02, rand(2.13, 2.27), ARROWHEAD[CYAN], 10);
			}
			color++;
		}
		wait([25, 25, 22, 20][GetDifficulty] - [0, 1, 2, 3, 4, 5][GetStage]);
	}
}

task attack2{
	let angle = 0;
	let arcwidthmultiplier = [24, 22, 19, 17][GetDifficulty];
	let undulationmultiplier = [4.5, 5, 5.5, 6, 6.5, 7][GetStage];
	wait(270-30);
	ObjAnim_SetAttackA1(objEnemy, true);
	wait(30);
	loop{
		angle = 0;
		ascent(i in 0..34){
		PlaySoundEffect("shot3", 100, GetEnemyX);
			ascent(p in -6..6){
				let obj = CreateDNHShotA1(ObjMove_GetX(objEnemy)+40*cos(99+(p*arcwidthmultiplier)+undulationmultiplier*sin(i*20)), ObjMove_GetY(objEnemy)+60*sin(99+(p*arcwidthmultiplier)+undulationmultiplier*sin(i*20))-10, 3.75-(i/18), 99+(p*arcwidthmultiplier)+undulationmultiplier*sin(i*20), [OVAL[CYAN], OVAL[BLUE]][color%2], 10);
				Obj_SetRenderPriorityI(obj, Obj_GetRenderPriorityI(obj)-1);
			}
		wait(8-(2-(i/9)));
		}
	wait(30);
	ObjAnim_SetAttackA1(objEnemy, false);
	wait(30);
	color++;
	//ExMoveCenter(90, 10, rand(45, 55));
	ExMove(objEnemy, GetCenterX, rand(45, 45), rand(110, 130), 20, 3, 15);
	wait(105-30);
	ObjAnim_SetAttackA1(objEnemy, true);
	wait(30);
	}
}
