//Enemy Script Function library
//
////I. Basic Functions
//1. InField(x, y) - Returns true if (x, y) is on the screen
//2. AngleFromPlayer(x, y) - Returns the angle from the player to (x, y)
//3. AngleToPlayer(x, y) - Returns the angle to the player from (x, y)
//4. Distance(x1, y1, x2, y2) - Returns the distance from (x1, y1) to (x2, y2)
//5. DistanceFromPlayer(x, y) - Returns the distance from the player to (x, y)
//6. NormalizeAngle(angle) - Returns an angle between 0° and 359°
//7. IsSameAngle(angle1, angle2) - Returns true if the 2 given angles are equal
//8. AngularDistance(angle1, angle2) - Returns the difference between angle1 and angle2
//
////II. Enemy Functions
//1. ItemDrop(type, x, y) - Creates an item type at point (x, y)
//2. EnemyInit(life, d) - Initializes the enemy with life and a damage rate of d (MUST BE USED AT THE START OF EVERY ENEMY)
//3. ChangeDamageRate(d) - Changes damage rate (from EnemyInit)

///////////////////////////////BASIC FUNCTIONS//////////////////////////////

function InField(x, y)
{
	if(x<GetClipMaxX && x>GetClipMinX && y<GetClipMaxY && y>GetClipMinY)
	{
	return true;
	}else{
	return false;
	}
}

function AngleFromPlayer(x, y)
{
	return atan2(y-GetPlayerY, x-GetPlayerX);
}

function AngleToPlayer(x, y)
{
	return atan2(GetPlayerY-y, GetPlayerX-x);
}

function Distance(x1, y1, x2, y2)
{
	return ((x1-x2)^2 + (y1-y2)^2)^0.5;
}

function DistanceFromPlayer(x, y)
{
	return Distance(x, y, GetPlayerX, GetPlayerY);
}

function NormalizeAngle(angle)
{
  angle%=360;
  if(angle<0){angle+=360;}
  return angle;
}

function IsSameAngle(angle1, angle2)
{
  angle1=NormalizeAngle(angle1);
  angle2=NormalizeAngle(angle2);
  return angle1==angle2;
}

function AngularDistance(angle1, angle2)
{
  let distance=NormalizeAngle(angle2 - angle1);
  if(distance>180){distance-=360;}
  return distance;
}

///////////////////////////////ENEMY FUNCTIONS//////////////////////////////

function ItemDrop(type, x, y)
{
	let temparray=[];
	temparray=GetCommonDataDefault("ITEMDROP", [ [], [], [] ]);
	temparray[0]=temparray[0]~[type];
	temparray[1]=temparray[1]~[x];
	temparray[2]=temparray[2]~[y];
	SetCommonData("ITEMDROP", temparray);
}

let enemydamagerate=0;
task EnemyInit(life, d)
{
	SetLife(life);
	enemydamagerate=d;
	LoadUserShotData(GetCurrentScriptDirectory~"supershot.txt");
	loop{
		SetDamageRate(enemydamagerate*GetCommonDataDefault("DAMAGERATE", 1), enemydamagerate*GetCommonDataDefault("DAMAGERATE", 1));
		yield;
	}
}

function ChangeDamageRate(d){
	enemydamagerate=d;
}