#TouhouDanmakufu
#ScriptVersion[3]
#System["./../tricky_system/TrickySystem.dnh"]
#Title["Kotohime 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, 80, 75);
	PlayEnemyHitSound(objEnemy, 1);

	let Kaguya = GetCurrentScriptDirectory~"sprites/Kaguya.png";
	ObjPrim_SetTexture(objEnemy, Kaguya); 
	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); 
	//BossShadow;
	ObjMove_SetPosition(objEnemy, -40, 40);

	SetAutoDeleteObject(true);
	attack1;
	Movement(25, 65, 359, 140);
	SetShotAutoDeleteClip(64, 64, 64, 64);
	End;
}

task End {
	SetCommonData("MidbossDead", false);
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0) { yield; }
	//DeleteShotWave(1,GetEnemyX,GetEnemyY,20);
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	loop(16){
		CreateItems02(1, ITEM_POINT_S, GetEnemyX+50*cos(angle), GetEnemyY+50*sin(angle), 0, -25, 0, 0, 10000);
		angle+=360/16;
	}
	CreateItems02(1, ITEM_SPELL, GetEnemyX, GetEnemyY, 0, -25, 0, 0, 69);
	SetCommonData("BossHittable", false);
	PlaySoundEffect("end",60,GetCenterX);
	SetShotAutoDeleteClip(32, 40, 32, 32);
	SetCommonData("MidbossDead", true);
	ObjMove_SetAcceleration(objEnemy, 0.025);
	ObjMove_SetMaxSpeed(objEnemy, 4);
	while(GetEnemyX>-25 && GetEnemyX<GetStgFrameWidth+25 && GetEnemyY>-25 && GetEnemyY<GetStgFrameHeight+25){ yield; }
	SetCommonData("BossHittable", true);
	Obj_Delete(objEnemy);
	CloseScript(GetOwnScriptID);
}

task Movement(left, top, right, bottom){
	let time = 7;
	let dir = 90;
	ObjMove_SetSpeed(objEnemy, 2);
	ObjMove_SetAngle(objEnemy, 12.5);
	wait(60);
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0){
		if(trunc(ObjMove_GetX(objEnemy))<left){
			ObjMove_SetX(objEnemy,left-ObjMove_GetX(objEnemy)+left);
			ObjMove_SetAngle(objEnemy,180 - ObjMove_GetAngle(objEnemy));
		}
		if(trunc(ObjMove_GetY(objEnemy))<top){
			ObjMove_SetY(objEnemy,top-(ObjMove_GetY(objEnemy)-top));
			ObjMove_SetAngle(objEnemy,360-ObjMove_GetAngle(objEnemy));
		}
		if(trunc(ObjMove_GetX(objEnemy))>right){
			ObjMove_SetX(objEnemy,right-ObjMove_GetX(objEnemy)+right);
			ObjMove_SetAngle(objEnemy,180 - ObjMove_GetAngle(objEnemy));
		}
		if(trunc(ObjMove_GetY(objEnemy))>bottom){
			ObjMove_SetY(objEnemy,bottom-(ObjMove_GetY(objEnemy)-bottom));
			ObjMove_SetAngle(objEnemy,360-ObjMove_GetAngle(objEnemy));
		}
		if(time%22==0 && ObjMove_GetSpeed(objEnemy)>-1 && (ObjEnemy_GetInfo(objEnemy, INFO_LIFE)>0)){
			PlaySoundEffect("shot", 90, GetEnemyX);
			loop(4){
				ascent(i in -4..5){
					let obj = CreateDNHShotA1(GetEnemyX+40*cos(90)+(i*14.5), GetEnemyY+40*sin(90)-(absolute(i)*8.5), 2.4, dir, DOT[GRAY], 10);
					Obj_SetRenderPriorityI(obj, Obj_GetRenderPriorityI(obj)+1);
				}
				dir+=360/4;
			}
			dir+=360/4/2.5;
		}
		
		time++;
		yield;
	}
}

task attack1{
	let dir = rand(0, 360);
	wait(90);
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0){
		if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE)>0){
			wait(180);
			if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE)>0){
				PlaySoundEffect("shadow", 90, GetEnemyX);
			}
		}
		if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE)>0){
		loop(14){
			let obj = CreateShotA1(GetEnemyX, GetEnemyY, 0, dir, CIRCLE_L[ORANGE], 0);
			ManageBomb(obj);
			dir+=360/14;
		}
		}
		if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE)>0){
		wait(30);	
		ObjAnim_SetAttackA1(objEnemy, true);
		}
		if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE)>0){
		wait(10);
		ObjMove_SetSpeed(objEnemy, 0);
		}
		if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE)>0){
		wait(45);
		}
		ObjAnim_SetAttackA1(objEnemy, false);
		if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE)>0){
		wait(35);
		}
		ObjMove_SetSpeed(objEnemy, 2);
	}	
}

task ManageBomb(obj){
	ObjShot_SetSpellResist(obj, true);
	ObjShot_SetAutoDelete(obj, false);
	let dir = ObjMove_GetAngle(obj);
	let dir2 = dir;
	let rad = 0;
	let exploding = false;
	let time = 0;
	let bulcolor = 0;
	
	while(!Obj_IsDeleted(obj)){
		if(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) <= 0){ Obj_Delete(obj); }
		if(rad>=60){ rad+=60/300; }
		if(rad<60){ rad+=60/45; }
		if(rad<60){ dir+=4; } else if(rad>60 && exploding == false){ dir+=42; }
		ObjMove_SetPosition(obj, GetEnemyX+rad*cos(dir), GetEnemyY+rad*sin(dir));
		if(time>=45){
			if(time%8==0){
				ObjShot_SetGraphic(obj, [CIRCLE_L[RED], CIRCLE_L[ORANGE]][bulcolor%2]);
				bulcolor++;
			}
		}
		if(time>=90){
			PlaySoundEffect("se_enep02", 90, GetEnemyX);
			loop(14){
				ascent(i in 0..2){
					CreateShotA1(ObjMove_GetX(obj), ObjMove_GetY(obj), 2.25-i+rand(-i/10, i/10), dir2, [CIRCLE_S[RED], DOT[RED]][i%2], 10);
				}
				dir2+=360/14;
			}
		Obj_Delete(obj);
		}
		dir2+=rand(-time, time);
		time++;
		yield;
	}
}