#TouhouDanmakufu
#ScriptVersion[3]
#System["./../tricky_system/TrickySystem.dnh"]
#Title["Tricky Nonspell 3"]
#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;
let leftx = 20;
let rightx = GetStgFrameWidth-20;

@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, 45, 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;
	attack3;
	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);
	SetShotAutoDeleteClip(32, 40, 32, 32);
	Obj_Delete(objEnemy);
	CloseScript(GetOwnScriptID);
}

task attack1{
	SetShotAutoDeleteClip(150, 90, 150, 20);
	let color = 0;
	let angle = 90;
	let angle2 = 0;
	wait(75);
	PlaySoundEffect("charge2", 100, GetEnemyX);
	TCharge01(GetEnemyX, GetEnemyY, 3, 45, 173, 255, 47, BLEND_ADD_ARGB, 25);
	wait(60);
	loop{
		PlaySoundEffect("shot5", 75, GetEnemyX);
		loop(6){
			let obj = CreateShotA2(leftx, 0+40, 2, angle, 0.025, 5, RICE[GREEN], 0);
			ObjMove_AddPatternA2(obj, 0, 3.65, NO_CHANGE, rand(0.0070,0.0080), 3.05+(0.30*sin(color)), 6);
			ObjMove_AddPatternA2(obj, 60, NO_CHANGE, NO_CHANGE, -0.05, 0, 2.725); 
			ObjRender_SetColor(obj, 153, 255, 27);
			ManipulateColor(obj);
			angle+=360/6;
		}
		color+=2.5;
		loop(6){
			let obj = CreateShotA2(rightx, 0+40, 2, angle2, 0.025, 5, RICE[GREEN], 0);
			ObjMove_AddPatternA2(obj, 0, 3.65, NO_CHANGE, rand(0.0070,0.0080), -3.05-(0.30*sin(color)), 6);
			ObjMove_AddPatternA2(obj, 60, NO_CHANGE, NO_CHANGE, -0.05, 0, 2.75); 
			ObjRender_SetColor(obj, 153, 255, 27);
			ManipulateColor(obj);
			angle2-=360/6;
		}
	color+=2.5;
	angle2-=360/5.0576;
	angle+=360/5.0576;
	wait(4);
	}
}

task ManipulateColor(obj){
	let color = 153;
	let color2 = 27;
	loop(180){
		color+=(255-153)/180;
		color2+=(255-27)/180;
		ObjRender_SetColor(obj, color, 255, color2);
		yield;
	}
	yield;
}

task attack2{
	wait(180);
	loop{
		ExMove(objEnemy, GetCenterX, rand(20, 30), rand(110, 130), 20, 3, 15);
		//ExMove(120, 5, rand(20, 30));
		wait(180);
	}
}

task attack3{
	let time = 0;
	loop(240){
		if(time%4==0 && time>75+60){
			let obj = CreateShotA1(leftx, 40, 0, 90, DOT[GREEN], 15);
			ObjShot_SetDeleteFrame(obj, 0);
			let obj2 = CreateShotA1(rightx, 40, 0, 90, DOT[GREEN], 15);
			ObjShot_SetDeleteFrame(obj2, 0);
		}
		time++;
		yield;
	}
	loop{
		loop(180){
		leftx+=1.9111111111111111111111111111111;
		rightx-=1.9111111111111111111111111111111;
		if(time%4==0){
			let obj = CreateShotA1(leftx, 40, 0, 90, DOT[GREEN], 15);
			ObjShot_SetDeleteFrame(obj, 0);
			let obj2 = CreateShotA1(rightx, 40, 0, 90, DOT[GREEN], 15);
			ObjShot_SetDeleteFrame(obj2, 0);
		}
		time++;
		yield;
		}
		loop(90){ 
			if(time%4==0){
			let obj = CreateShotA1(leftx, 40, 0, 90, DOT[GREEN], 15);
			ObjShot_SetDeleteFrame(obj, 0);
			let obj2 = CreateShotA1(rightx, 40, 0, 90, DOT[GREEN], 15);
			ObjShot_SetDeleteFrame(obj2, 0);
			}
			time++;
			yield; 
		}
		loop(180){
			leftx-=1.9111111111111111111111111111111;
			rightx+=1.9111111111111111111111111111111;
			if(time%4==0){
				let obj = CreateShotA1(leftx, 40, 0, 90, DOT[GREEN], 15);
				ObjShot_SetDeleteFrame(obj, 0);
				let obj2 = CreateShotA1(rightx, 40, 0, 90, DOT[GREEN], 15);
				ObjShot_SetDeleteFrame(obj2, 0);
			}
			time++;
			yield;
		}
		loop(90){
			if(time%4==0){
				let obj = CreateShotA1(leftx, 40, 0, 90, DOT[GREEN], 15);
				ObjShot_SetDeleteFrame(obj, 0);
				let obj2 = CreateShotA1(rightx, 40, 0, 90, DOT[GREEN], 15);
				ObjShot_SetDeleteFrame(obj2, 0);
			}
			time++;
			yield;
		}
	}
}