function ObjStLaser_GetEndX(objL){
	return ObjMove_GetX(objL) +ObjLaser_GetLength(objL) *cos(ObjStLaser_GetAngle(objL))
}

function GetCenterX{
	return (GetStgFrameWidth() / 2)
}

function GetCenterY{
	return (GetStgFrameHeight() / 2)
}

function ObjStLaser_GetEndY(objL){
	return ObjMove_GetY(objL) +ObjLaser_GetLength(objL) *sin(ObjStLaser_GetAngle(objL))
}

function rand_int(Min, Max){
	let i = truncate(rand(Min, Max+0.999));
	return i;
}

function InCurcleToPlayer(x, y, d){
	if((GetPlayerX-x)^2 + (GetPlayerY-y)^2 < d^2){
		return true;
	}else{return false;}
}

function Horming(toAngle, angle, dAngle){
	toAngle %= 360;
	angle %= 360;

	let ret = toAngle-angle;
	if(ret >= 180){ret -= 360;}//180°以上なら-180〜180内に補正
	if(ret < -180){ret += 360;}//-180°未満なら-180〜180内に補正

	if((|ret|) < dAngle){
		angle = toAngle;
	}else{
		if(sin(ret) > 0){
			angle += dAngle;
		}else{
			angle -= dAngle;
		}
	}
	if(angle >= 360){angle -= 360;}
	if(angle < 0){angle += 360;}

	return angle;
}

function IsOut(x, y, d){
	if(absolute(GetCenterX-x) < (GetCenterX+d) &&
		absolute(GetCenterY-y) < (GetCenterY+d)){
		return false;
	}else{return true;}
}

function getDotAngle(x1,y1,x2,y2){
	let angle=atan2(y1-y2,x1-x2);
	return angle;
}
function getAngleFromObj(obj1, obj2){
	let angle=atan2(ObjMove_GetY(obj1)-ObjMove_GetY(obj2),ObjMove_GetX(obj1)-ObjMove_GetX(obj2));
	return angle;
}

function GetDistance(x, y, x2, y2){
	let d = ((x2-x)^2 + (y2-y)^2)^0.5;
	return d;
}

function GetDistance2(x, y, x2, y2){
	let d = ((x2-x)^2 + (y2-y)^2);
	return d;
}

task ObjMove_SetDestAtFrameWeight(objEnemy, fin_x, fin_y, flame){
	let x = ObjMove_GetX(objEnemy);
	let y = ObjMove_GetY(objEnemy);
	let rx = fin_x - x;
	let ry = fin_y - y;
	let t_rate = 90/flame;
	let s = 0.1;
	ObjMove_SetAngle(objEnemy, getDotAngle(fin_x,fin_y,ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy)));
	ascent(time in 1 .. flame+1){
		s-=0.1/(flame-1);
		ObjMove_SetSpeed(objEnemy, s);
		ObjMove_SetX(objEnemy, x + rx * sin(t_rate * time));
		ObjMove_SetY(objEnemy, y + ry * sin(t_rate * time));
		yield;
	}
	ObjMove_SetSpeed(objEnemy, 0);
}

task ObjMove_SetDestAtFrameWeight02(objEnemy, fin_x, fin_y, flame){
	let cx = (ObjMove_GetX(objEnemy)+fin_x)/2;
	let cy = (ObjMove_GetY(objEnemy)+fin_y)/2;
	let rx = (fin_x-ObjMove_GetX(objEnemy))/2;
	let ry = (fin_y-ObjMove_GetY(objEnemy))/2;
	let t_rate = 180/flame;
	let s = 0.1;
	ObjMove_SetAngle(objEnemy, getDotAngle(fin_x,fin_y,ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy)));
	ascent(time in 1 .. flame+1){
		s-=0.1/(flame-1);
		ObjMove_SetSpeed(objEnemy, s);
		ObjMove_SetX(objEnemy, cx - rx * cos(t_rate * time));
		ObjMove_SetY(objEnemy, cy - ry * cos(t_rate * time));
		yield;
	}
	ObjMove_SetSpeed(objEnemy, 0);
}

task BossLifeSingleA1(EnemyObj,maxRadius){
	let img_lifebar = GetCurrentScriptDirectory~"./lib/img/lifebar.png";
	let path = img_lifebar;
	let countVertex = 26; //+14*2;
	let objScene = ID_INVALID;
	maxRadius = maxRadius/2;
	
	let objLife = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(objLife, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(objLife, countVertex);
	ObjRender_SetBlendType(objLife, BLEND_ALPHA);
	Obj_SetRenderPriority(objLife, 0.69);
	ObjPrim_SetTexture(objLife, path);
	
	let objOutline = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(objOutline, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(objOutline, countVertex);
	ObjRender_SetBlendType(objOutline, BLEND_ALPHA);
	Obj_SetRenderPriority(objOutline, 0.68);
	ObjPrim_SetTexture(objOutline, path);
	
	ascent (iVert in 0..countVertex/2){
		let vertex = iVert * 2;
		ObjPrim_SetVertexUVT(objLife, vertex+0, 0, 0);
		ObjPrim_SetVertexUVT(objLife, vertex+1, 10, 32);	
		ObjPrim_SetVertexUVT(objOutline, vertex+0, 10, 0);
		ObjPrim_SetVertexUVT(objOutline, vertex+1, 19, 32);		
	}
	
	let mx = 0;
	let my = 0;
	let fade = 255;
	let fadec = 255;
	
	let lifeRateRender = 0;
	let outlineRender = 0;
	
	let lifeTotalMax = ObjEnemy_GetInfo(EnemyObj, INFO_LIFE);
	let lifeTotal = ObjEnemy_GetInfo(EnemyObj, INFO_LIFE);
	let lifeRate = min(lifeTotal / lifeTotalMax, lifeRateRender);
	
	
	
	ObjRender_SetScaleXYZ(objLife, -1, 1, 1);
	ObjRender_SetScaleXYZ(objOutline, -1, 1, 1);
	
	
	while(lifeTotal / lifeTotalMax>0){	
		
		mx = ObjRender_GetX(EnemyObj);
		my = ObjRender_GetY(EnemyObj);
		RefreshVertex(objLife);

		if(GetDistance(GetPlayerX,GetPlayerY,mx,my)<maxRadius*1.75){
			fadec = max(fadec-10, 85);
		}else{fadec = min(fadec+10, 255);}
		ObjRender_SetAlpha(objOutline, fadec);
		ObjRender_SetAlpha(objLife, fadec);
		
		if(GetCommonData("ForceLifebar", 0) != 0){
			outlineRender = 0;
			lifeRateRender = 0;
		}
		
		yield;
	}
	Obj_Delete(objOutline);
	Obj_Delete(objLife);

	task RefreshVertex(objid){	
		lifeTotal = ObjEnemy_GetInfo(EnemyObj, INFO_LIFE);
		lifeRate = min(lifeTotal / lifeTotalMax, lifeRateRender);
		
		ascent(iVert in 0..countVertex/2){
			let indexVert = iVert * 2;
			let angle = (360 / (countVertex/2 - 1) * iVert);
			angle = min(360*(outlineRender), angle);
			let x = (maxRadius)*cos(angle-90);
			let y = (maxRadius)*sin(angle-90);
			ObjPrim_SetVertexPosition(objOutline, indexVert + 0 , x, y, 0);
			x = (maxRadius*1.1)*cos(angle-90);
			y = (maxRadius*1.1)*sin(angle-90);
			ObjPrim_SetVertexPosition(objOutline, indexVert + 1 , x, y, 0);
			
			angle = min(360*(lifeRate), angle);
			x = (maxRadius)*cos(angle-90);
			y = (maxRadius)*sin(angle-90);
			ObjPrim_SetVertexPosition(objLife, indexVert + 0 , x, y, 0);			
			x = (maxRadius*1.1)*cos(angle-90);
			y = (maxRadius*1.1)*sin(angle-90);
			ObjPrim_SetVertexPosition(objLife, indexVert + 1 , x, y, 0);
		}
		ObjRender_SetPosition(objLife, mx, my, 0);
		ObjRender_SetPosition(objOutline, mx, my, 0);
		
		lifeRateRender = min(1, lifeRateRender+0.02);
		outlineRender = min(1, outlineRender+0.016);
		
	}
}


function CreateEffectObject(px,py,img,blendtype,left,top,right,bottom){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	LoadTexture(img);
	ObjPrim_SetTexture(obj,img);
	ObjSprite2D_SetSourceRect(obj,left,top,right,bottom);
	ObjSprite2D_SetDestCenter(obj);
	if(blendtype=="ALPHA"){
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	}
	if(blendtype=="RGB"){
	ObjRender_SetBlendType(obj,BLEND_ADD_RGB);
	}
	if(blendtype=="ARGB"){
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	}
	if(blendtype=="INVERT"){
	ObjRender_SetBlendType(obj,BLEND_INV_DESTRGB);
	}
	ObjRender_SetPosition(obj,px,py,0);
	ObjMove_SetPosition(obj,px,py,);
	SetAutoDeleteObject(true);
	return obj;
}




