/*#TouhouDanmakufu[Single]
#Title ["NS1"]
#Text [""]
#ScriptVersion [3]*/

#include "./Functions/Index.txt"

let objBoss;
let BossX = 0;
let BossY = 0;
let frame =0;
let frame2=0;
let attframe = 0;
let arr = [1,6,4,2,3,5,0];

#include "script/default_system/Default_Effect.txt"

let imgBoss = GetCurrentScriptDirectory~"./img/MinorikoSprite.png";

@Initialize {
	SetAutoDeleteObject(true);
	
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	DrawName("Minoriko Aki",175,0,0);
	
	ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2,125,9,5);
	
	FlameAura(objBoss,[255,255,0],2);
	RenderBoss;
	
	TMain;
}

@Event {
	// Setting boss timer and life
	alternative(GetEventType())
	case(EV_REQUEST_LIFE) {
		SetScriptResult(4500);
	}
	case(EV_REQUEST_TIMER) {
		SetScriptResult(99);
		
	}

}



@MainLoop {
	BossX = ObjMove_GetX(objBoss);
	BossY = ObjMove_GetY(objBoss);
	ObjEnemy_SetIntersectionCircleToShot(objBoss,BossX,BossY,24);
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss,BossX,BossY,24);
	yield;
}

@Finalize {

}

task TMain {
	wait(120);
	TEnd;
	TShot02;
}

task TShot01 {
	let angle = 0;
	loop (12) {
		PlaySound("Shot1",60,0);
		let obj = CreateShotA1(BossX,BossY,2,angle,SMALL_RED,5);
		CustomShot1(obj);
		angle+=360/12;
	}
	task CustomShot1(obj) {
		let angle = 0;
		wait(30);
		loop (6) {
			PlaySound("Shot5",60,0);
			let shot = CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),2,angle,PELLET_RED,0);
			CustomShot2(shot);
			angle+=360/6;
		}
	}
	task CustomShot2(obj) {
		wait(30);
		let angle = 0;
		loop (4) {
			PlaySound("Shot5",60,0);
			CreateShotA1(ObjMove_GetX(obj),ObjMove_GetY(obj),2,angle,PELLET_GREEN,0);
			angle+=360/4;
		}
	}
	wait(90);
	TShot02;
}

task TShot02 {
	let angle = 0;
	loop (45) {
	PlaySound("Shot2",60,0);
	loop (4) {
		ascent(i in 0..4) {
			CreateShotA1(BossX,BossY,1+i/2,angle,PELLET_RED,5);
		}
		angle+=360/4;
	}
	angle+=9;
	yield;
	}
	wait (60);
	TShot01;
}


// Bezt frend
function wait(w) { loop(w) { yield; } }

function angleToPlayer(obj) {
	let dir = atan2(GetPlayerY-ObjMove_GetY(obj),GetPlayerX-ObjMove_GetX(obj));
	return dir;
}

let attack = false;

task RenderBoss {
	let hover = 0;
	let hovertrue = true;
	let dir = 0;
	let speed = 0;
	let imgCreate = ObjPrim_Create(OBJ_SPRITE_2D);
	
	while(!Obj_IsDeleted(objBoss)) {
		ObjPrim_SetTexture(imgCreate,imgBoss);
		ObjSprite2D_SetDestCenter(imgCreate);
		ObjRender_SetScaleXYZ(imgCreate,1,1,0);
		Obj_SetRenderPriorityI(imgCreate,25);
		
		ObjRender_SetPosition(imgCreate,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),0);
		
		dir = ObjMove_GetAngle(objBoss);
		speed = ObjMove_GetSpeed(objBoss);
		
		if(speed == 0 && attack == false) {ObjRender_SetAngleXYZ(imgCreate,0,0,0);
		if(frame < 4) {ObjSprite2D_SetSourceRect(imgCreate,0,128,64,192);}
		if(frame >= 4 && frame < 8) {ObjSprite2D_SetSourceRect(imgCreate,64,128,128,192);}
		frame2 = 0;
		hovertrue = true;
		}
		else if(cos(dir) > 0) {ObjRender_SetAngleXYZ(imgCreate,0,0,0);
		if(frame2 < 4) {ObjSprite2D_SetSourceRect(imgCreate,0,192,64,256);}
		if(frame2 >= 4 && frame2 < 8) {ObjSprite2D_SetSourceRect(imgCreate,64,192,128,256);}
		if(frame2 >= 8) {ObjSprite2D_SetSourceRect(imgCreate,128,192,192,256);}
		hovertrue = false;
		}
		else if(cos(dir) < 0) {ObjRender_SetAngleXYZ(imgCreate,0,180,0);
		if(frame2 < 4) {ObjSprite2D_SetSourceRect(imgCreate,0,192,64,256);}
		if(frame2 >= 4 && frame2 < 8) {ObjSprite2D_SetSourceRect(imgCreate,64,192,128,256);}
		if(frame2 >= 8) {ObjSprite2D_SetSourceRect(imgCreate,128,192,192,256);}
		hovertrue = false;
		}
		
		if(attack == true && speed == 0) {ObjRender_SetAngleXYZ(imgCreate,0,0,0);
		if(attframe >= 1){ObjSprite2D_SetSourceRect(imgCreate,128,128,192,192);}
		hovertrue = true;
		}
		
		frame+=0.5;
		attframe++;
		frame2+=1;
		if(hovertrue==true){hover++;}
		if(hovertrue==false){}
		
		if(attframe >= 1) {attframe = 1;}
		ObjRender_SetPosition(imgCreate,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss)+7*sin(hover*3),0);
		if(frame > 8) {frame = 0;}
		yield;
	}
}

task TEnd {
	
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE) > 0) {
		yield;
	}
	//PlaySE(bossdeathSFX);
	TExplosionA(BossX,BossY,10,0.5);
	wait(15);
	Obj_Delete(objBoss);
	DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	CloseScript(GetOwnScriptID);
	return;
}