task Attack(time){
SetCommonData("BossAttack", true);
loop(time){yield;}
SetCommonData("BossAttack", false);
}

task RenderCharacter(tar){
	let aframe = 0;
	let sframe = 0;
	let mframe = 0;
	let nframe = 0;
	let x = 64;
	let y = 64;
	let bob = 0;
	
	Obj_SetValue(tar, "State", "Idle");
	
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "./img/Ingame/stg2enm.png");
	Obj_SetRenderPriorityI(obj, 35);
	
	ObjSprite2D_SetSourceRect(obj,0,0,x,y);
	ObjSprite2D_SetDestCenter(obj);	
	
	while(!Obj_IsDeleted(obj)){
	let BossX = ObjMove_GetX(tar);
	let BossY = ObjMove_GetY(tar);
	let dir = ObjMove_GetAngle(tar);
	let speed = ObjMove_GetSpeed(tar);
	
	ObjRender_SetAlpha(obj, GetCommonData("BossAlpha", 255));
	if(Obj_GetValueD(tar, "State", 0) == "MoveLeft" || Obj_GetValueD(tar, "State", 0) == "MoveRight" && speed == 0) {Obj_SetValue(tar, "State", "StopMoving");}
	if(speed == 0 && Obj_GetValueD(tar, "State", 0) != "StopMoving") {Obj_SetValue(tar, "State", "Idle"); ObjRender_SetAngleXYZ(obj,0,0,GetCommonData("BossAngle",0)) }
	if(GetCommonData("BossAttack", false) == true){Obj_SetValue(tar, "State", "Attack");}
	if(speed != 0){
		if(cos(dir) > 0) {Obj_SetValue(tar, "State", "MoveLeft"); ObjRender_SetAngleXYZ(obj, 0, 0, GetCommonData("BossAngle", 0));}
		if(cos(dir) < 0) {Obj_SetValue(tar, "State", "MoveRight"); ObjRender_SetAngleXYZ(obj, 0, 180, GetCommonData("BossAngle", 0));}
	}
	
	
	if(Obj_GetValueD(tar, "State", 0) == "Idle" && sframe <= 8) {ObjSprite2D_SetSourceRect(obj,0,0,x,y);}
	if(Obj_GetValueD(tar, "State", 0) == "Idle" && sframe >= 8) {sframe = 0;}
	
	if(Obj_GetValueD(tar, "State", 0) == "MoveLeft" && mframe <= 6) {ObjSprite2D_SetSourceRect(obj,0,y*1,x,y*2);}
	if(Obj_GetValueD(tar, "State", 0) == "MoveLeft" && mframe >= 6) {ObjSprite2D_SetSourceRect(obj,x,y*1,x*2,y*2);}
	if(Obj_GetValueD(tar, "State", 0) == "MoveLeft" && mframe >= 12) {ObjSprite2D_SetSourceRect(obj,x*2,y*1,x*3,y*2);}
	if(Obj_GetValueD(tar, "State", 0) == "MoveLeft" && mframe >= 24) {ObjSprite2D_SetSourceRect(obj,x*3,y*1,x*4,y*2);}
	
	if(Obj_GetValueD(tar, "State", 0) == "MoveRight" && mframe <= 6) {ObjSprite2D_SetSourceRect(obj,0,y*1,x,y*2);}
	if(Obj_GetValueD(tar, "State", 0) == "MoveRight" && mframe >= 6) {ObjSprite2D_SetSourceRect(obj,x,y*1,x*2,y*2);}
	if(Obj_GetValueD(tar, "State", 0) == "MoveRight" && mframe >= 12) {ObjSprite2D_SetSourceRect(obj,x*2,y*1,x*3,y*2);}
	if(Obj_GetValueD(tar, "State", 0) == "MoveRight" && mframe >= 24) {ObjSprite2D_SetSourceRect(obj,x*3,y*1,x*4,y*2);}
	
	if(Obj_GetValueD(tar, "State", 0) == "StopMoving" && nframe <= 3) {ObjSprite2D_SetSourceRect(obj,x*3,y*1,x*4,y*2);}
	if(Obj_GetValueD(tar, "State", 0) == "StopMoving" && nframe >= 3) {ObjSprite2D_SetSourceRect(obj,x*2,y*1,x*3,y*2);}
	if(Obj_GetValueD(tar, "State", 0) == "StopMoving" && nframe >= 6) {ObjSprite2D_SetSourceRect(obj,x,y*1,x*2,y*2);}
	if(Obj_GetValueD(tar, "State", 0) == "StopMoving" && nframe >= 9) {ObjSprite2D_SetSourceRect(obj,0,y*1,x,y*2);}
	if(Obj_GetValueD(tar, "State", 0) == "StopMoving" && nframe >= 12) {Obj_SetValue(tar, "State", "Idle")}
	
	if(Obj_GetValueD(tar, "State", 0) == "Attack" && aframe <= 8) {ObjSprite2D_SetSourceRect(obj,0,0,x,y);}
	if(Obj_GetValueD(tar, "State", 0) == "Attack" && aframe >= 8) {ObjSprite2D_SetSourceRect(obj,x,0,x*2,y);}
	if(Obj_GetValueD(tar, "State", 0) == "Attack" && aframe >= 16) {ObjSprite2D_SetSourceRect(obj,x*2,0,x*3,y);}
	if(Obj_GetValueD(tar, "State", 0) == "Attack" && aframe >= 24) {ObjSprite2D_SetSourceRect(obj,x*3,0,x*4,y);}
	
	if(Obj_GetValueD(tar, "State", 0) == "Idle") {sframe++; mframe = 0; nframe = 0; aframe = 0;}
	if(Obj_GetValueD(tar, "State", 0) == "StopMoving") {nframe++;}
	if(Obj_GetValueD(tar, "State", 0) == "Attack") {aframe++;}
	if(Obj_GetValueD(tar, "State", 0) == "MoveLeft" || Obj_GetValueD(tar, "State", 0) == "MoveRight") {mframe++;}
	
	bob++;
	
	ObjRender_SetPosition(obj,BossX,BossY+3*sin(bob*3),0);
	yield;
	}
}



