47 lines
1.9 KiB
C#
47 lines
1.9 KiB
C#
namespace Pong.Scripts.Data;
|
|
|
|
/// <summary>
|
|
/// a record class that represents the game area.
|
|
/// </summary>
|
|
public record GameArea
|
|
{
|
|
/// <summary>
|
|
/// the X coordinate of the GameArea wrapped in a struct that makes operating on it easier. this is divided
|
|
/// by 2 in the constructor to represent both sides of the screen by either negative or positive of the number.
|
|
/// </summary>
|
|
public GameCoordinate X { get; }
|
|
|
|
/// <summary>
|
|
/// the Y coordinate of the GameArea wrapped in a struct that makes operating on it easier. this is divided
|
|
/// by 2 in the constructor to represent both sides of the screen by either negative or positive of the number.
|
|
/// </summary>
|
|
public GameCoordinate Y { get; }
|
|
|
|
/// <summary>
|
|
/// take a global position inside the game area and normalize it to a value between 0 and 1 which represents the game area.
|
|
/// </summary>
|
|
/// <param name="globalPosition">a Vector2 containing a global position inside the game area.</param>
|
|
public Vector2 NormalizePosition(Vector2 globalPosition)
|
|
{
|
|
float normalizedX = (float)Mathf.InverseLerp(-X.ByWallSizeExtents, X.ByWallSizeExtents, globalPosition.X);
|
|
float normalizedY = (float)Mathf.InverseLerp(-Y.ByWallSizeExtents, Y.ByWallSizeExtents, globalPosition.Y);
|
|
return new Vector2(normalizedX, normalizedY);
|
|
}
|
|
|
|
/// <summary>
|
|
/// take a normalized position and convert it back into a globalized position.
|
|
/// </summary>
|
|
/// <param name="normalizedPosition">a Vector2 containing a normalized position.</param>
|
|
public Vector2 GlobalizePosition(Vector2 normalizedPosition)
|
|
{
|
|
float globalizedX = (float)Mathf.Lerp(-X.ByWallSizeExtents, X.ByWallSizeExtents, normalizedPosition.X);
|
|
float globalizedY = (float)Mathf.Lerp(-Y.ByWallSizeExtents, Y.ByWallSizeExtents, normalizedPosition.Y);
|
|
return new Vector2(globalizedX, globalizedY);
|
|
}
|
|
|
|
public GameArea(Vector2 area)
|
|
{
|
|
X = new GameCoordinate(area.X / 2);
|
|
Y = new GameCoordinate(area.Y / 2);
|
|
}
|
|
}
|