#TouhouDanmakufu
#ScriptVersion[3]
#System["./../tricky_system/TrickySystem.dnh"]
#Title["Tricky Spell 3"]
#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, 360, 12.5, 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;Aero III&quot;", shokocut, "NAZRIN", "173", "255", "47");
	ObjCutin_LaunchS3("NAZRIN", "0", "Hard");
	NotifyEventAll(EV_SPELLBG, 3);
	
	wait(120);
	attack1;
	SetShotAutoDeleteClip(32, 40, 32, 32);
}

task attack1{	
	let one = -1;
	wait(30);
	loop{
		
		PlaySoundEffect("charge2", 100, GetEnemyX);
		TCharge01(GetEnemyX, GetEnemyY, 3, 45, 173, 255, 47, BLEND_ADD_ARGB, 25);
		wait(45);
		loop(4){
			CreateOrbitars(GetCenterX, GetStgFrameHeight-140, angle, 150, one);
			angle+=360/4;
		}
		one*=-1;
		wait(90);
		ObjAnim_SetAttackA1(objEnemy, true);
		wait(30);
		loop(6){
			loop(10){
				PlaySoundEffect("shot", 75, GetEnemyX);
				loop(2){
					let obj = CreateShotA1(GetEnemyX, GetEnemyY, 3, angle, ARROWHEAD[YELLOW], 10);
					AimedStuff(obj);
					angle+=360/2;
				}
				wait(3);
			}
			wait(45);
		}
		ObjAnim_SetAttackA1(objEnemy, false);
	}
}

task AimedStuff(obj){
	ObjMove_SetAcceleration(obj, -0.0375);
	ObjMove_SetMaxSpeed(obj, 0);
	while(ObjMove_GetSpeed(obj)>0){ yield; }
	if(Obj_IsDeleted(obj)==true){ return; }
	PlaySoundEffect("ping", 75, GetEnemyX);
	ObjMove_SetAngle(obj, GetAngleToPlayer(obj));
	ObjMove_SetSpeed(obj, 1);
	ObjMove_SetAcceleration(obj, 0.05);
	ObjMove_SetMaxSpeed(obj, 4);
}

task CreateOrbitars(x, y, ang, minrad, rot){
	let radius = 400;
	let time = 0;
	let ang2 = ang;
	let ang3 = 0;
	let obj = CreateCurveLaserA1(x+radius*cos(ang2), y+radius*sin(ang2), 1, angle, 25, 10, LASER[GREEN], 0);
	ObjCrLaser_SetTipDecrement(obj, 0.5);
	ObjShot_SetAutoDelete(obj, false);
	while(!Obj_IsDeleted(obj)){
		ObjMove_SetPosition(obj, x+radius*cos(ang2), y+radius*sin(ang2));
		if(time%2==0){
			let bullet = CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 4, AngleBetweenPoints(x, y, ObjMove_GetX(obj)-45, ObjMove_GetY(obj)), PELLET[GRAY], 0); 
			ManipulateColor(bullet);
			let bullet2 = CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 4, AngleBetweenPoints(x, y, ObjMove_GetX(obj)+45, ObjMove_GetY(obj)), PELLET[GRAY], 0); 
			ManipulateColor(bullet2);
		}
		if(radius<=minrad){
			if(time%60==0){
			PlaySoundEffect("shot5", 90, GetCenterX);
				loop(10){
					CreateDNHShotA2(ObjMove_GetX(obj), ObjMove_GetY(obj), 2.5, AngleBetweenPoints(ObjMove_GetX(obj), ObjMove_GetY(obj), x, y)+ang3, -0.05, 1.25, CIRCLE_S[GREEN], 10);
					ang3+=360/10;
				}
			}
		}		
	if(radius>minrad && time<600){ radius-=2.5; }
	if(radius<=400 && time>600){ radius+=2.5; }
	ang2+=3.275*rot;
	time++;
	yield;
	}
Obj_Delete(obj);
}

task ManipulateColor(obj){
	let color2 = 0;
	loop(20){
		ObjRender_SetColor(obj, color2, 255, 0);
		color2+=13;
		yield;
	}
}