/*#TouhouDanmakufu[Single]
#Title ["S1"]
#Text ["Was this too simple? Hard? Easy?	I dont know."]
#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];
let imgBoss = GetCurrentScriptDirectory~"./img/MinorikoSprite.png";
let CutIn = GetCurrentScriptDirectory~"img/Minoriko.png";
#include "script/default_system/Default_Effect.txt"

@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 {
	alternative(GetEventType())
	case(EV_REQUEST_LIFE) {
		SetScriptResult(4500);
	}
	case(EV_REQUEST_TIMER) {
		SetScriptResult(60);
		
	}
	case(EV_REQUEST_SPELL_SCORE) {
		SetScriptResult(1000000);
	}
}

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

@Finalize {

}

task SpellCall {
	bspell = true;
	PlaySound("cardcall",100,0);
	TDrawSpell;
}

task TMain {
	SpellCall;
	TEnd;
	CutInS1(objBoss,"KANAKO","Harvest sign &quot;Strange cherry fruit&quot;",CutIn,0,0,248,306);
	wait(165);
	TFire1;
	TShot02;
}

task TShot02 {
	wait(60*5);
	let angle = 0;
	loop {
	loop (12) {
		ascent(i in 0..3) {
		let obj = CreateShotA1(BossX,BossY,3,angle,SMALL_RED,0);
		RunReflectCode(obj);
		ObjMove_AddPatternA3(obj,15,1+i/2,NO_CHANGE,0,9,0,NO_CHANGE);
		ObjMove_AddPatternA3(obj,30,NO_CHANGE,NO_CHANGE,0,0,0,NO_CHANGE);
		}
		angle+=360/12;
	}
	wait(60*2);
	}
}

task TFire1 {
	attack = true;
	TMove;
	wait(30);
	let angle = 0;
	loop {
		PlaySound("BulletWave",50,0);
		let obj = CreateShotA1(BossX,BossY,1,270+rand(-45,45),rand(50,51),5);
		ObjMove_AddPatternA3(obj,15,NO_CHANGE,NO_CHANGE,0,5,0,NO_CHANGE);
		ObjMove_AddPatternA3(obj,50,NO_CHANGE,NO_CHANGE,0,0,0,NO_CHANGE);
		wait(2);
	}
}

task TMove {
	let angle = 0;
	ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2+125,BossY,5,8);
	wait(30);
	loop {
		loop (360) {
			ObjMove_SetPosition(objBoss,(GetStgFrameWidth/2)+125*cos(angle),ObjMove_GetY(objBoss));
			angle+=360/360;
			yield;
		}
		yield;
	}
}

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

function angleToPlayer {
	let dir = atan2(GetPlayerY-BossY,GetPlayerX-BossX);
	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;
}