#TouhouDanmakufu
#ScriptVersion[3]
#System["./../tricky_system/TrickySystem.dnh"]
#Title["Shokora Nonspell 1"]
#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, 180, 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_SetPosition(objEnemy, GetCenterX, GetClipMinY+150);

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

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