using Microsoft.Xna.Framework;
using MLEM.Extensions;
using MonoGame.Extended;
namespace MLEM.Extended.Extensions {
///
/// A set of extension methods that convert MLEM types to MonoGame.Extended types and vice versa.
///
public static class NumberExtensions {
///
/// Converts a MLEM to a MonoGame.Extended .
///
/// The rectangle to convert
/// The converted rectangle
public static RectangleF ToExtended(this Misc.RectangleF rect) {
return new RectangleF(rect.X, rect.Y, rect.Width, rect.Height);
}
///
/// Converts a MonoGame.Extended to a MLEM .
///
/// The rectangle to convert
/// The converted rectangle
public static Misc.RectangleF ToMlem(this RectangleF rect) {
return new Misc.RectangleF(rect.X, rect.Y, rect.Width, rect.Height);
}
///
/// Calculates the amount that the rectangle is penetrating the rectangle by.
/// If a penetration on both axes is occuring, the one with the lower value is returned.
/// This is useful for collision detection, as it can be used to push colliding objects out of each other.
///
/// The rectangle to do the penetration
/// The rectangle that should be penetrated
/// The direction that the penetration occured in
/// The amount that the penetration occured by, in the direction of
/// Whether or not a penetration occured
public static bool Penetrate(this RectangleF rect, RectangleF other, out Vector2 normal, out float penetration) {
return rect.ToMlem().Penetrate(other.ToMlem(), out normal, out penetration);
}
///
/// Returns how far between the given 's and value the given is, as a number between 0 and 1.
/// Note that, if the is outside the given , a correct proportional value outside the 0 to 1 range will still be returned.
/// This method is the reverse action of .
///
/// The range to query.
/// The value to query.
/// The percentage.
public static float GetPercentage(this Range range, float value) {
return (value - range.Min) / (range.Max - range.Min);
}
///
/// Returns how far between the given 's and value the given is, as a number between 0 and 1.
/// Note that, if the is outside the given , a correct proportional value outside the 0 to 1 range will still be returned.
/// This method is the reverse action of .
///
/// The range to query.
/// The value to query.
/// The percentage.
public static float GetPercentage(this Range range, float value) {
return (value - range.Min) / (range.Max - range.Min);
}
///
/// Returns a value within the given 's and values based on the given into the range.
/// Note that, if the is outside the 0 to 1 range, a correct value outside the will still be returned.
/// This method is the reverse action of .
///
/// The range to query.
/// The percentage to query.
/// The value.
public static float FromPercentage(this Range range, float percentage) {
return (range.Max - range.Min) * percentage + range.Min;
}
///
/// Returns a value within the given 's and values based on the given into the range.
/// Note that, if the is outside the 0 to 1 range, a correct value outside the will still be returned.
/// This method is the reverse action of .
///
/// The range to query.
/// The percentage to query.
/// The value.
public static float FromPercentage(this Range range, float percentage) {
return (range.Max - range.Min) * percentage + range.Min;
}
}
}