#include "./AnimLib.dnh"

let F_SMALL = 1;
let F_MEDIUM = 2;
let F_MEDIUM2 = 3;
let F_LARGE = 4;

function AnimateFairy(obj, type, color){ //Basically the same as boss animations
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory ~ "../IMG/fairy_" ~ color ~ ".png");
	alternative(type)
	case(F_SMALL){ //Small fairy
		//Idle
		ObjAnim_AddFrameA1(obj, ANIM_IDLE, 224, 0, 256, 32);
		ObjAnim_AddFrameA1(obj, ANIM_IDLE, 256, 0, 288, 32);
		ObjAnim_AddFrameA1(obj, ANIM_IDLE, 288, 0, 320, 32);
		ObjAnim_AddFrameA1(obj, ANIM_IDLE, 256, 0, 288, 32);

		ObjAnim_SetLoopPointA1(obj, ANIM_IDLE, 0);
		ObjAnim_SetSpeedA1(obj, ANIM_IDLE, 5);


		//Move
		ObjAnim_AddFrameA1(obj, ANIM_MOVE, 320, 0, 352, 32);
		ObjAnim_AddFrameA1(obj, ANIM_MOVE, 352, 0, 384, 32);
		ObjAnim_AddFrameA1(obj, ANIM_MOVE, 384, 0, 416, 32);
		ObjAnim_AddFrameA1(obj, ANIM_MOVE, 416, 0, 448, 32);
		ObjAnim_AddFrameA1(obj, ANIM_MOVE, 384, 0, 416, 32);

		ObjAnim_AddFrameA1(obj, ANIM_MOVEEND, 320, 0, 352, 32);

		ObjAnim_SetLoopPointA1(obj, ANIM_MOVE, 1);
		ObjAnim_SetSpeedA1(obj, ANIM_MOVE, 5);

		ObjAnim_StartAnimationA1(obj, 6, 3);
	}
	case(F_MEDIUM){ //Medium fairy
		//Idle
		ObjAnim_AddFrameA1(obj, ANIM_IDLE, 0, 80, 48, 128);
		ObjAnim_AddFrameA1(obj, ANIM_IDLE, 48, 80, 96, 128);
		ObjAnim_AddFrameA1(obj, ANIM_IDLE, 96, 80, 144, 128);
		ObjAnim_AddFrameA1(obj, ANIM_IDLE, 48, 80, 96, 128);

		ObjAnim_SetLoopPointA1(obj, ANIM_IDLE, 0);
		ObjAnim_SetSpeedA1(obj, ANIM_IDLE, 5);


		//Move
		ObjAnim_AddFrameA1(obj, ANIM_MOVE, 144, 80, 192, 128);
		ObjAnim_AddFrameA1(obj, ANIM_MOVE, 192, 80, 240, 128);
		ObjAnim_AddFrameA1(obj, ANIM_MOVE, 240, 80, 288, 128);
		ObjAnim_AddFrameA1(obj, ANIM_MOVE, 288, 80, 336, 128);
		ObjAnim_AddFrameA1(obj, ANIM_MOVE, 240, 80, 288, 128);

		ObjAnim_AddFrameA1(obj, ANIM_MOVEEND, 144, 80, 192, 128);

		ObjAnim_SetLoopPointA1(obj, ANIM_MOVE, 1);
		ObjAnim_SetSpeedA1(obj, ANIM_MOVE, 5);

		ObjAnim_StartAnimationA1(obj, 6, 3);
	}
	case(F_MEDIUM2){ //Another medium fairy
		//Idle
		ObjAnim_AddFrameA1(obj, ANIM_IDLE, 0, 176, 48, 224);
		ObjAnim_AddFrameA1(obj, ANIM_IDLE, 48, 176, 96, 224);
		ObjAnim_AddFrameA1(obj, ANIM_IDLE, 96, 176, 144, 224);
		ObjAnim_AddFrameA1(obj, ANIM_IDLE, 48, 176, 96, 224);

		ObjAnim_SetLoopPointA1(obj, ANIM_IDLE, 0);
		ObjAnim_SetSpeedA1(obj, ANIM_IDLE, 5);


		//Move
		ObjAnim_AddFrameA1(obj, ANIM_MOVE, 144, 176, 192, 224);
		ObjAnim_AddFrameA1(obj, ANIM_MOVE, 192, 176, 240, 224);
		ObjAnim_AddFrameA1(obj, ANIM_MOVE, 240, 176, 288, 224);
		ObjAnim_AddFrameA1(obj, ANIM_MOVE, 288, 176, 336, 224);
		ObjAnim_AddFrameA1(obj, ANIM_MOVE, 240, 176, 288, 224);

		ObjAnim_AddFrameA1(obj, ANIM_MOVEEND, 144, 176, 192, 224);

		ObjAnim_SetLoopPointA1(obj, ANIM_MOVE, 1);
		ObjAnim_SetSpeedA1(obj, ANIM_MOVE, 5);

		ObjAnim_StartAnimationA1(obj, 6, 3);
	}
	case(F_LARGE){ //Sunflower fairy
		//Idle
		ObjAnim_AddFrameA1(obj, ANIM_IDLE, 0, 288, 64, 352);
		ObjAnim_AddFrameA1(obj, ANIM_IDLE, 64, 288, 128, 352);
		ObjAnim_AddFrameA1(obj, ANIM_IDLE, 128, 288, 192, 352);
		ObjAnim_AddFrameA1(obj, ANIM_IDLE, 64, 288, 128, 352);

		ObjAnim_SetLoopPointA1(obj, ANIM_IDLE, 0);
		ObjAnim_SetSpeedA1(obj, ANIM_IDLE, 5);


		//Move
		ObjAnim_AddFrameA1(obj, ANIM_MOVE, 192, 288, 256, 352);
		ObjAnim_AddFrameA1(obj, ANIM_MOVE, 256, 288, 320, 352);
		ObjAnim_AddFrameA1(obj, ANIM_MOVE, 320, 288, 384, 352);
		ObjAnim_AddFrameA1(obj, ANIM_MOVE, 384, 288, 448, 352);
		ObjAnim_AddFrameA1(obj, ANIM_MOVE, 320, 288, 384, 352);

		ObjAnim_AddFrameA1(obj, ANIM_MOVEEND, 192, 288, 256, 352);

		ObjAnim_SetLoopPointA1(obj, ANIM_MOVE, 1);
		ObjAnim_SetSpeedA1(obj, ANIM_MOVE, 5);

		ObjAnim_StartAnimationA1(obj, 6, 3);
	}
}



task AnimateYinyang(obj, color){
	let path = GetCurrentScriptDirectory ~ "./../IMG/yinyang.png";
	ObjPrim_SetTexture(obj, path);
	ObjRender_SetScaleXYZ(obj, 0.5, 0.5, 0);

	let x = 0;
	let y = 0;

	alternative(color) //Use color to determine which rects to pick
	case("RED"){
		x = 0;
		y = 0;
	}
	case("ORANGE"){
		x = 32;
		y = 0;
	}
	case("YELLOW"){
		x = 64;
		y = 0;
	}
	case("GREEN"){
		x = 96;
		y = 0;
	}
	case("AQUA"){
		x = 0;
		y = 32;
	}
	case("BLUE"){
		x = 32;
		y = 32;
	}
	case("PURPLE"){
		x = 64;
		y = 32;
	}
	case("WHITE"){
		x = 96;
		y = 32;
	}

	ObjSprite2D_SetSourceRect(obj, x, y, x + 32, y + 32);
	ObjSprite2D_SetDestCenter(obj);

	let wheel1 = ObjPrim_Create(OBJ_SPRITE_2D); //Inner alpha wheel
	ObjRender_SetPosition(wheel1, ObjMove_GetX(obj), ObjMove_GetY(obj), 0);
	ObjPrim_SetTexture(wheel1, path);
	ObjSprite2D_SetSourceRect(wheel1, x, y + 64, x + 32, y + 96);
	ObjSprite2D_SetDestCenter(wheel1);

	let wheel2 = ObjPrim_Create(OBJ_SPRITE_2D); //Outer additive wheel
	ObjRender_SetPosition(wheel2, ObjMove_GetX(obj), ObjMove_GetY(obj), 0);
	ObjPrim_SetTexture(wheel2, path);
	ObjRender_SetBlendType(wheel2, BLEND_ADD_RGB);
	ObjSprite2D_SetSourceRect(wheel2, x, y + 128, x + 32, y + 160);
	ObjSprite2D_SetDestCenter(wheel2);
	ObjRender_SetScaleXYZ(wheel2, 1.3, 1.3, 0);

	while(!Obj_IsDeleted(obj)){
		if(Obj_GetValueD(obj, "IsSpawning", false)){
			ObjRender_SetColor(obj, 128, 128, 128);
			ObjRender_SetColor(wheel1, 128, 128, 128);
			ObjRender_SetColor(wheel2, 128, 128, 128);
		}else{
			ObjRender_SetColor(obj, 255, 255, 255);
			ObjRender_SetColor(wheel1, 255, 255, 255);
			ObjRender_SetColor(wheel2, 255, 255, 255);
		}
		ObjRender_SetPosition(wheel1, ObjMove_GetX(obj) + ObjMove_GetSpeed(obj)*cos(ObjMove_GetAngle(obj)), ObjMove_GetY(obj) + ObjMove_GetSpeed(obj)*sin(ObjMove_GetAngle(obj)), 0);
		ObjRender_SetPosition(wheel2, ObjMove_GetX(obj) + ObjMove_GetSpeed(obj)*cos(ObjMove_GetAngle(obj)), ObjMove_GetY(obj) + ObjMove_GetSpeed(obj)*sin(ObjMove_GetAngle(obj)), 0);

		ObjRender_SetAngleZ(wheel1, ObjRender_GetAngleZ(wheel1) + 6);
		ObjRender_SetAngleZ(wheel2, ObjRender_GetAngleZ(wheel2) - 6);

		ObjRender_SetScaleXYZ(wheel1, ObjRender_GetScaleX(obj)*2, ObjRender_GetScaleY(obj)*2, 0);
		ObjRender_SetScaleXYZ(wheel2, ObjRender_GetScaleX(obj)*2.6, ObjRender_GetScaleY(obj)*2.6, 0);
		yield;
	}
	Obj_Delete(wheel1);
	Obj_Delete(wheel2);
}



task AnimateSpirit(obj, y, s){
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory ~ "./../IMG/hitodama.png");
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	ObjRender_SetScaleXYZ(obj, s, s, s);
	loopanimspirit(obj, y, s);
	while(!Obj_IsDeleted(obj)){
		ascent(x in 0..8){
			ObjSprite2D_SetSourceRect(obj, 32*x, y, 32*(x+1), 31+y);
			ObjSprite2D_SetDestCenter(obj);
			loop(3){yield;}
		}
		yield;
	}	
}

task loopanimspirit(obj, y, s){
	while(!Obj_IsDeleted(obj)){
		AnimateSpiritTrail(obj, ObjMove_GetAngle(obj), y, s);
		loop(6){yield;}
	}
}

task AnimateSpiritTrail(obj, a, y, s){
	let objs = ObjPrim_Create(OBJ_SPRITE_2D);

	Move(obj, objs, a, s);
	Obj_SetRenderPriorityI(objs, Obj_GetRenderPriorityI(obj)-1);
	ObjPrim_SetTexture(objs, GetCurrentScriptDirectory ~ "./../IMG/hitodama.png");
	ObjRender_SetBlendType(objs, BLEND_ADD_ARGB);
	ObjRender_SetPosition(objs,ObjMove_GetX(obj)+round(rand(-3,3)),ObjMove_GetY(obj),0);

	while(!Obj_IsDeleted(objs)){
		ascent(x in 0..8){
			ObjSprite2D_SetSourceRect(objs,32*x,y,32*(x+1),32+y);
			ObjSprite2D_SetDestCenter(objs);
			loop(3){yield;}
		}
		yield;
	}
}

task Move(obj, objs, a, s){
	let si = s;
	let speed = 3;
	let angle = a-180;

	if(ObjMove_GetSpeed(obj) == 0){angle = 270;}

	let mx = speed*cos(angle);
	let my = speed*sin(angle);

	while(si >0){
		si-=0.07;
		if(si < 0){Obj_Delete(objs);}
		ObjRender_SetPosition(objs,ObjRender_GetX(objs)+mx,ObjRender_GetY(objs)+my,0);

		ObjRender_SetScaleXYZ(objs,si,si,0);
		yield;
	}
}


task AnimateBall(obj){
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory ~ "./../IMG/enm_ball.png");
	ObjSprite2D_SetSourceRect(obj, 0, 0, 127, 127);
	ObjSprite2D_SetDestRect(obj, -63, -63, 65, 65);

	let c = 0;
	let cc = 0;
	while(!Obj_IsDeleted(obj)){
		ObjRender_SetAngleZ(obj, ObjRender_GetAngleZ(obj) + 12);
		ObjRender_SetScaleXYZ(obj, 1 + c, 1 + c, 0);
		c += 0.005*sin(cc);
		cc++;
		yield;
	}
}

task AnimateBoss_Usami(obj){
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory ~ "../IMG/Sumireko.png");

	//Idle
	ascent(i in 0 .. 8){
		ObjAnim_AddFrameA1(obj, ANIM_IDLE, 64*i, 0, 64*(i+1), 64);
	}
	ObjAnim_SetLoopPointA1(obj, ANIM_IDLE, 0);
	ObjAnim_SetSpeedA1(obj, ANIM_IDLE, 5);

	//Move
	ascent(i in 0 .. 8){
		ObjAnim_AddFrameA1(obj, ANIM_MOVE_RIGHT, 64*i, 64, 64*(i+1), 128);
		ObjAnim_AddFrameA1(obj, ANIM_MOVE_LEFT, 64*i, 128, 64*(i+1), 192);
	}
	descent(i in 0 .. 4){
		ObjAnim_AddFrameA1(obj, ANIM_MOVEEND_RIGHT, 64*i, 64, 64*(i+1), 128);
		ObjAnim_AddFrameA1(obj, ANIM_MOVEEND_LEFT, 64*i, 128, 64*(i+1), 192);
	}
	ObjAnim_SetLoopPointA1(obj, ANIM_MOVE_RIGHT, 4);
	ObjAnim_SetSpeedA1(obj, ANIM_MOVE_RIGHT, 5);

	ObjAnim_SetLoopPointA1(obj, ANIM_MOVE_LEFT, 4);
	ObjAnim_SetSpeedA1(obj, ANIM_MOVE_LEFT, 5);

	//Attack
	ascent(i in 0 .. 6){
		ObjAnim_AddFrameA1(obj, ANIM_ATTACK, 64*i, 192, 64*(i+1), 256);
	}
	descent(i in 0 .. 2){
		ObjAnim_AddFrameA1(obj, ANIM_ATTACKEND, 64*i, 192, 64*(i+1), 256);
	}
	ObjAnim_SetLoopPointA1(obj, ANIM_ATTACK, 2);
	ObjAnim_SetSpeedA1(obj, ANIM_ATTACK, 5);

	ObjAnim_StartAnimationA2(obj, 6, 2);
}

task AnimateBoss_Doremy(obj){ 
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory ~ "../IMG/Doremy.png");

	//Idle
	loop(2){
		ascent(i in 0 .. 3){
			ObjAnim_AddFrameA1(obj, ANIM_IDLE, 64*i, 0, 64*(i+1), 96);
		}
		ObjAnim_AddFrameA1(obj, ANIM_IDLE, 64*1, 0, 64*2, 96);
	}
	ascent(i in 0 .. 4){
		ObjAnim_AddFrameA1(obj, ANIM_IDLE, 64*i, 0, 64*(i+1), 96);
	}
	ObjAnim_SetLoopPointA1(obj, ANIM_IDLE, 0);
	ObjAnim_SetSpeedA1(obj, ANIM_IDLE, 5);

	//Move
	ascent(i in 0 .. 6){
		ObjAnim_AddFrameA1(obj, ANIM_MOVE_RIGHT, 64*i, 96, 64*(i+1), 192);
		ObjAnim_AddFrameA1(obj, ANIM_MOVE_LEFT, 64*i, 192, 64*(i+1), 288);
	}
	descent(i in 0 .. 2){
		ObjAnim_AddFrameA1(obj, ANIM_MOVEEND_RIGHT, 64*i, 96, 64*(i+1), 192);
		ObjAnim_AddFrameA1(obj, ANIM_MOVEEND_LEFT, 64*i, 192, 64*(i+1), 288);
	}
	ObjAnim_SetLoopPointA1(obj, ANIM_MOVE_RIGHT, 2);
	ObjAnim_SetSpeedA1(obj, ANIM_MOVE_RIGHT, 5);

	ObjAnim_SetLoopPointA1(obj, ANIM_MOVE_LEFT, 2);
	ObjAnim_SetSpeedA1(obj, ANIM_MOVE_LEFT, 5);

	//Attack
	ascent(i in 0 .. 6){
		ObjAnim_AddFrameA1(obj, ANIM_ATTACK, 64*i, 288, 64*(i+1), 384);
	}
	descent(i in 0 .. 2){
		ObjAnim_AddFrameA1(obj, ANIM_ATTACKEND, 64*i, 288, 64*(i+1), 384);
	}
	ObjAnim_SetLoopPointA1(obj, ANIM_ATTACK, 2);
	ObjAnim_SetSpeedA1(obj, ANIM_ATTACK, 5);

	ObjAnim_StartAnimationA2(obj, 6, 2);
}