
#include "./../../lib_familiar.txt"

let	boss;

task startBoss{
	boss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(boss);
	ObjMove_SetPosition(boss,-250,-250);
}

function createBoss(path,color){
	let bossA = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(bossA);

	let bossSpA = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(bossSpA,GetCurrentScriptDirectory ~ "./../../../img/enemy/boss/" ~ path ~ "_dot.png");
	Obj_SetRenderPriority(bossSpA, 0.47);
	ObjEnemy_SetLife(bossA,1000);
	ObjEnemy_CreateHitbox(bossA,32,32);
	ObjEnemy_CreateMarker(bossA);
 	ObjEnemy_SetHitScore(bossA);
 	ObjEnemy_SetDamageRate(bossA,0,0);

	EverythingElseFromTheBoss;

	return bossA;

	task EverythingElseFromTheBoss{

		let animFrame = 0;
		let A = 0;
		let B = 0;
		let uny = 0;
		let maxX = 128;
		let maxY = 128;
		let dir;
		let aur = 0;
		let hp = 0;

		while(!Obj_IsDeleted(boss) && !Obj_IsDeleted(bossA)){
			hp = ObjEnemy_GetInfo(bossA,INFO_LIFE); 
			dir =   ObjMove_GetAngle(bossA);
			 
			if(Obj_GetValueD(bossA,"IsPose",[0,0]) == [0,0]){
				if(Obj_GetValueD(bossA,"DesTime",0) == 0) {
					if(cos(dir)<0 && (animFrame) < 4)  {A=2;B=0;}
					else if((animFrame) < 4) 			{A=2;B=2;}
					else if((animFrame-4)%(8*6) < 8)   {A=0;B=0;}
					else if((animFrame-4)%(8*6) < 8*2) {A=0;B=1;}
					else if((animFrame-4)%(8*6) < 8*3) {A=0;B=2;}
					else if((animFrame-4)%(8*6) < 8*4) {A=0;B=0;}
					else if((animFrame-4)%(8*6) < 8*5) {A=1;B=0;}
					else if((animFrame-4)%(8*6) < 8*6) {A=1;B=1;}
					uny = 5*cos(animFrame*2);
				}
				
				else if(Obj_GetValueD(bossA,"DesTime",0) > 0){
					if(cos(dir) < 0){
						if(Obj_GetValueD(bossA,"DesTime",0) > Obj_GetValueD(bossA,"DesMaxTime",0) - 4){A=2;B=0;}
						else if(Obj_GetValueD(bossA,"DesTime",0) > 4){A=2;B=1;}
						else{A=2;B=0;}
					}
					else if(cos(dir) > 0){
						if(Obj_GetValueD(bossA,"DesTime",0) > Obj_GetValueD(bossA,"DesMaxTime",0) - 4){A=2;B=2;}
						else if(Obj_GetValueD(bossA,"DesTime",0) > 4){A=2;B=3;}
						else{A=2;B=2;}
					}
					uny = 0;
				}
			}
			else{
				A = Obj_GetValueD(bossA,"IsPose",[0,0])[0]; B = Obj_GetValueD(bossA,"IsPose",[0,0])[1];
			}
			
			ObjSprite2D_SetSourceRect(bossSpA,A*maxX,B*maxY,(A+1)*maxX,(B+1)*maxY);
			ObjSprite2D_SetDestCenter(bossSpA);   
			ObjRender_SetPosition(bossSpA,ObjMove_GetX(bossA),ObjMove_GetY(bossA)+uny,0);
			
			animFrame++;
			aur++;
			
			if(aur%4 == 0 && GetAreaCommonData("Boss","AuraEnable",0) ){AfterImage(bossSpA, A, B , 30, GetAreaCommonData("Boss","AuraScale",0), CR
			(color)[0], CR(color)[1], CR(color)[2]);
			}

			yield;
			if(Obj_IsDeleted(bossA)){Obj_Delete(bossSpA);return;}
			hp = hp-ObjEnemy_GetInfo(bossA,INFO_LIFE);
			ObjEnemy_AddLife(boss,-hp);
			ObjEnemy_SetLife(bossA,1000);
		}
		Obj_Delete(bossSpA);

		task AfterImage(obj2,A,B, frame, rate, R, G, Bl){
			let scale = 1.0;
			let scale_rate = (rate-1.0)/frame;
			let alpha = 255;
			let alpha_rate = 255/frame;

			let obj=ObjPrim_Create(OBJ_SPRITE_2D);
			ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "./../../../img/enemy/boss/" ~ path ~ "_dot.png");
			ObjRender_SetPosition(obj, ObjRender_GetX(obj2), ObjRender_GetY(obj2), 0);
			Obj_SetRenderPriority(obj, 0.40);
			ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
			ObjSprite2D_SetSourceRect(obj,A*maxX,B*maxY,(A+1)*maxX,(B+1)*maxY);
			ObjSprite2D_SetDestCenter(obj);  
			ObjRender_SetAngleZ(obj, ObjRender_GetAngleZ(obj2));
			ObjRender_SetAlpha(obj, 255);
			let sx = ObjRender_GetScaleX(obj2);
			let sy = ObjRender_GetScaleY(obj2);
			loop(frame){
				ObjRender_SetScaleXYZ(obj, sx * scale, sy * scale, 1.0);
				ObjRender_SetColor(obj, R * alpha/255, G * alpha/255, Bl * alpha/255);
				alpha -= alpha_rate;
				scale += scale_rate;
				yield;
			}
			Obj_Delete(obj);
		}

	}
}