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

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

@Initialize{

	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(objEnemy, 245, 65, 75);
	PlayEnemyHitSound(objEnemy, 1);

	let Patchouli = GetCurrentScriptDirectory~"sprites/Patchouli.png";
	ObjPrim_SetTexture(objEnemy, Patchouli); 
	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, 150, 60);

	SetAutoDeleteObject(true);
	attack1;
	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 offset = 0;
	switch = 1;
	wait(90);
	
	loop{
	
		PlaySoundEffect("charge2", 100, GetEnemyX);
		TCharge01(GetEnemyX, GetEnemyY, 3, 45, 255, 0, 255, BLEND_ADD_ARGB, 25);
		ObjAnim_SetAttackA1(objEnemy, true);
		wait(30);
		
		ascent(i in 9..16){
			switch*=-1;
			offset = 0;
			PlaySoundEffect("shot3", 100, GetEnemyX);
			
			ascent(j in -3-(i/6)..(i/6)+4){
				loop(2){
					let obj = CreateDNHShotA2(ObjMove_GetX(objEnemy)+35*cos(0+(j*12.25)+offset), ObjMove_GetY(objEnemy)+35*sin(0+(j*12.25)+offset), 1.25, 0+(j*12.25)+offset, 0.05, 4, OVAL[PURPLE], 10);
					ManageShot(obj, switch);
					offset+=180;
				}
			}
			wait(20);
		}
		switch*=-1;
		ObjAnim_SetAttackA1(objEnemy, false);
		wait(30);
		ExMove(objEnemy, GetCenterX, rand(20, 30), 150+rand(-5, 5), 20, 3, 15);
		wait(75);
	}	
}

task ManageShot(obj, switch){
	let randangle = rand(90-5, 90+5);
	while(!Obj_IsDeleted(obj)){
		if((ObjMove_GetX(obj)>384 || ObjMove_GetX(obj)<0) && ObjMove_GetY(obj)<448){
			PlaySoundEffect("shot2", 85, GetCenterX);
			if(ObjMove_GetX(obj)<0){
				ascent(i in 0..2){
					CreateDNHShotA2(ObjMove_GetX(obj), ObjMove_GetY(obj), 0, 0+(i*13.5*switch), 0.025, 1.75, CIRCLE_S[PURPLE], 20);
				}
			}
			else if(ObjMove_GetX(obj)>384){
				ascent(i in 0..2){
					CreateDNHShotA2(ObjMove_GetX(obj), ObjMove_GetY(obj), 0, 180+(i*13.5*switch), 0.025, 1.75, CIRCLE_S[PURPLE], 20);
				}
			}
			Obj_Delete(obj);
		}
		yield;
	}
}