using Godot;

/**
 * Speed of a game item, expressed as its components on the two axes.
 *
 * Speed values are expressed in pixels/millisecond, direction in radians.
 */
public class Velocity
{
	public float vx;
	public float vy;

	public Velocity(float vx, float vy)
	{
		this.vx = vx;
		this.vy = vy;
	}

	public float speed => Mathf.Sqrt(vx * vx + vy * vy);

	public float direction => Mathf.Atan2(vy, vx);

	public void LimitSpeed(float maxSpeed)
	{
		if (speed > maxSpeed)
		{
			float dir = direction;

			vx = Mathf.Cos(dir) * maxSpeed;
			vy = Mathf.Sin(dir) * maxSpeed;
		}
	}
}
