#include "./../../../system/functions/functions_boss.txt"

let imgB = GetCurrentScriptDirectory ~ "img/Sprite_Boss.png";
let Spell = false;

@MainLoop{yield;}
@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
		{
		SetScriptResult(life);
		}
	case(EV_REQUEST_TIMER)
		{
		SetScriptResult(-1);
		}
	case(EV_REQUEST_SPELL_SCORE)
		{
		SetScriptResult(30000);
		}
}
@Finalize{DeleteShotAll(TYPE_ALL,TYPE_IMMEDIATE);}
task TBackground
	{
	}


let KOI_MAIN = 0;
let KOI_HAT = 1;
let KOI_ARM = 2;
let KOI_SPIN1 = 3;
let KOI_SPIN2 = 4;
let KOI_BALL1 =5;
let KOI_BALL2 = 6;

task ObjEnemy_Render(boss)
{
	let img = GetCurrentScriptDirectory ~ "img/Sprite_Boss.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj,img);
	Obj_SetRenderPriority(obj,0.35);
	let x;
	let y;
	let xA = 1024/8;
	let yA = 512/4;	
	let count = 1/10;
	let counter = -1;

	let animM = [];
	ascent(i in 0..7)
		{
		animM = animM ~ [0];
		}
	let move = 0;

	while(!Obj_IsDeleted(obj))
		{		
		counter+=1;
		let type = ObjEnemy_GetRender(boss);
		ascent(i in 0..length(animM))
			{
			if(type != i)
				{
				animM[i] = 0;
				}
			}
		animM[type]=animM[type]+count;
		if(type != KOI_MAIN)
			{
			move =  0;
			}
		alternative(type)
			case(KOI_MAIN)
				{
				if(cos(ObjMove_GetAngle(obj)) > cos(25) && ObjMove_GetSpeed(obj) > 0)
					{
					animM[type] = 0;
					if(move < 1)
						{
						move+=count;
						}
					x = 1;
					y = floor(move);
					}
				else if(cos(ObjMove_GetAngle(obj)) < cos(155) && ObjMove_GetSpeed(obj) > 0)
					{
					animM[type] = 0;
					if(move < 1)
						{
						move+=count;
						}
					x = 2;
					y = floor(move);
					}
				else
					{	
					move = 0;
					x = 0;		
					let anim = floor(animM[type]);
					y = anim%2;
					}
				}
			case(KOI_HAT)
				{
				x = 4;
				let anim = floor(animM[type]);
				if(anim < 3)
					{
					y = anim;
					}
				else if(anim < 5)
					{
					y = 4-anim;
					}
				else
					{
					ObjEnemy_SetRender(boss,KOI_MAIN);
					}
				}
			case(KOI_ARM)
				{
				x = 5;
				let anim = floor(animM[type]);
				if(anim < 2)
					{
					y = anim;
					}
				else if(anim < 3)
					{
					y = 1;
					}
				else
					{
					ObjEnemy_SetRender(boss,KOI_MAIN);
					}
				}
			case(KOI_SPIN1)
				{
				let anim = floor(animM[type]);
				if(anim < 4)
					{
					x = 6;
					y = anim;
					}
				else if(anim < 7)
					{
					x = 4;
					y = 6-anim;
					}
				else
					{
					ObjEnemy_SetRender(boss,KOI_MAIN);
					}
				}
			case(KOI_SPIN2)
				{
				let anim = floor(animM[type]);
				if(anim < 4)
					{
					x = 6;
					y = anim;
					}
				else
					{
					animM[type] = 4;
					x = 4;
					y = 2;
					}
				}
			case(KOI_BALL1)
				{
				x = 7;
				let anim = floor(animM[type]);
				if(anim < 2)
					{
					y = anim;
					}
				else if(anim < 3)
					{
					y = 1;
					}
				else
					{
					ObjEnemy_SetRender(boss,KOI_MAIN);
					}
				}
			case(KOI_BALL2)
				{
				let anim = floor(animM[type]);
				if(anim < 4)
					{
					y = anim;
					x = 7;
					}
				else if(anim < 7)
					{
					y = 6-anim;
					x = 4;
					}
				else
					{
					ObjEnemy_SetRender(boss,KOI_MAIN);
					}
				}
		ObjRender_SetAlpha(obj,ObjRender_GetAlpha(boss));
		Obj_SetRenderPriority(obj,Obj_GetRenderPriorityI(boss)/100);
		ObjSprite2D_SetSourceRect(obj,x*xA,y*yA,(x+1)*xA,(y+1)*yA);
		ObjRender_SetPosition(obj,ObjMove_GetX(boss),ObjMove_GetY(boss)+2*sin(counter*3),0);
		let g = ObjRender_GetColor(boss);
		ObjRender_SetColorHSV(obj,g[0],g[1],g[2]);
		ObjSprite2D_SetDestCenter(obj);
		if(ObjEnemy_GetGlow(boss) == true && counter%3 == 0)
			{
			TGlow;
			}	
		if(Obj_IsDeleted(boss))
			{
			Obj_Delete(obj);
			}
		yield;
		}
	task TGlow
		{
		let glw = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(glw,img);
		ObjSprite2D_SetSourceRect(glw,x*xA,y*yA,(x+1)*xA,(y+1)*yA);
		ObjSprite2D_SetDestCenter(glw);
		ObjRender_SetPosition(glw,ObjRender_GetX(obj),ObjRender_GetY(obj),0);
		Obj_SetRenderPriority(glw,0.28);
		ObjRender_SetBlendType(glw,BLEND_ADD_ARGB);
		let time = 20;
		ascent(t in 0..time)
			{
			let g = t/time;
			let gA = 1-g;
			ObjRender_SetColorHSV(glw,45*gA+120*g,255,255);
			ObjRender_SetAlpha(glw,(time-t)*ObjRender_GetAlpha(boss)/time);
			ObjRender_SetScaleXYZ(glw,1+t*1.25/time,1+t*1.25/time,0);
			if(Obj_IsDeleted(boss))
				{
				Obj_Delete(glw);
				}
			yield;
			}
		Obj_Delete(glw);
		}
}

task ObjEnemy_SetRender(obj,type)
{
	Obj_SetValue(obj,"IMG",type);
}
function ObjEnemy_GetRender(obj)
{
	return Obj_GetValueD(obj,"IMG",0);
}
task ObjEnemy_SetGlow(obj,type)
{
	Obj_SetValue(obj,"GLOW",type);
}
function ObjEnemy_GetGlow(obj)
{
	return Obj_GetValueD(obj,"GLOW",true);
}

task ObjRender_SetAlpha2(obj,alpha)
{
	ObjRender_SetAlpha(obj,alpha);
	Obj_SetValue(obj,"ALPHA",alpha);
}
function ObjRender_GetAlpha(obj)
{
	return Obj_GetValueD(obj,"ALPHA",255);
}

task ObjRender_SetColor2(obj,h,s,v)
{
	ObjRender_SetColorHSV(obj,h,s,v);
	Obj_SetValue(obj,"COLOR",[h,s,v]);
}
function ObjRender_GetColor(obj)
{
	return Obj_GetValueD(obj,"COLOR",[0,0,255]);
}