-
Notifications
You must be signed in to change notification settings - Fork 1
Int Extensions
IntExtensions provides a comprehensive set of extension methods for int values, making common mathematical and game development operations more intuitive and readable.
| Feature | Description |
|---|---|
| Clamping | Clamp values between ranges |
| Wrapping | Wrap values within ranges |
| Angle Conversion | Convert between degrees and radians |
| Even/Odd Checks | Check if a number is even or odd |
| Power of Two | Check and round to powers of two |
| Time Conversion | Convert between seconds, milliseconds, and frames |
| Tile Conversion | Convert between pixels and tiles |
| Sign and Absolute | Get sign or absolute value |
Clamps the value between a minimum and maximum.
int value = 15;
int clamped = value.Clamp(0, 10); // 10
int value2 = -5;
int clamped2 = value2.Clamp(0, 10); // 0Wraps the value within the range [0, max).
int value = 7;
int wrapped = value.Wrap(5); // 2
int value2 = -1;
int wrapped2 = value2.Wrap(5); // 4Wraps the value within the range [min, max).
int value = 7;
int wrapped = value.Wrap(0, 5); // 2
int value2 = -2;
int wrapped2 = value2.Wrap(0, 5); // 3Converts degrees to radians.
float radians = 90.ToRadians(); // 1.5708 (PI/2)
float radians2 = 180.ToRadians(); // 3.14159 (PI)Converts radians to degrees.
float degrees = 3.ToDegrees(); // ~171.887
float degrees2 = 1.ToDegrees(); // ~57.296Determines whether the value is even.
bool isEven = 4.IsEven(); // true
bool isEven2 = 5.IsEven(); // falseDetermines whether the value is odd.
bool isOdd = 5.IsOdd(); // true
bool isOdd2 = 4.IsOdd(); // falseDetermines whether the value is a power of two.
bool isPower = 8.IsPowerOfTwo(); // true
bool isPower2 = 6.IsPowerOfTwo(); // false
bool isPower3 = 1.IsPowerOfTwo(); // true
bool isPower4 = 0.IsPowerOfTwo(); // falseRounds up to the nearest power of two.
int next = 5.NextPowerOfTwo(); // 8
int next2 = 12.NextPowerOfTwo(); // 16
int next3 = 8.NextPowerOfTwo(); // 8Gets the sign of the value (-1, 0, or 1).
int sign = 5.Sign(); // 1
int sign2 = (-3).Sign(); // -1
int sign3 = 0.Sign(); // 0Gets the absolute value.
int abs = (-5).Abs(); // 5
int abs2 = 3.Abs(); // 3Converts seconds to milliseconds.
int ms = 2.SecondsToMilliseconds(); // 2000
int ms2 = 5.SecondsToMilliseconds(); // 5000Converts milliseconds to seconds.
float seconds = 1500.MillisecondsToSeconds(); // 1.5f
float seconds2 = 2000.MillisecondsToSeconds(); // 2.0fConverts frames to seconds at the specified FPS.
float seconds = 120.FramesToSeconds(60); // 2.0f
float seconds2 = 30.FramesToSeconds(60); // 0.5fConverts seconds to frames at the specified FPS.
int frames = 3.SecondsToFrames(60); // 180
int frames2 = 1.SecondsToFrames(30); // 30Converts pixels to tile coordinates.
int tileX = 128.PixelsToTiles(32); // 4
int tileY = 64.PixelsToTiles(32); // 2Converts tile coordinates to pixels.
int pixelX = 5.TilesToPixels(32); // 160
int pixelY = 3.TilesToPixels(32); // 96Converts the integer value to a boolean.
bool isTrue = 1.ToBool(); // true
bool isTrue2 = (-5).ToBool(); // true
bool isFalse = 0.ToBool(); // falsepublic int SnapToGrid(int value, int gridSize)
{
return (value / gridSize).PixelsToTiles(gridSize);
}public int GetAnimationFrame(float elapsedTime, int fps)
{
int frames = ((int)elapsedTime).SecondsToFrames(fps);
return frames % _totalFrames;
}public void MovePlayer(int pixelsX, int pixelsY)
{
int tileX = pixelsX.PixelsToTiles(Globals.TileSize);
int tileY = pixelsY.PixelsToTiles(Globals.TileSize);
if (IsWalkable(tileX, tileY))
{
_playerTileX = tileX;
_playerTileY = tileY;
}
}public int GetOptimalTextureSize(int requestedSize)
{
return requestedSize.NextPowerOfTwo();
}public int CalculateDamage(int baseDamage, int defense)
{
int effectiveDamage = baseDamage - defense;
// If negative, return 0
if (effectiveDamage < 0)
effectiveDamage = 0;
return effectiveDamage;
}| Method | Description |
|---|---|
Clamp |
Clamps between min and max |
Wrap |
Wraps within a range |
ToRadians |
Converts degrees to radians |
ToDegrees |
Converts radians to degrees |
IsEven |
Checks if the value is even |
IsOdd |
Checks if the value is odd |
IsPowerOfTwo |
Checks if the value is a power of two |
NextPowerOfTwo |
Rounds up to the nearest power of two |
Sign |
Gets the sign of the value |
Abs |
Gets the absolute value |
SecondsToMilliseconds |
Converts seconds to milliseconds |
MillisecondsToSeconds |
Converts milliseconds to seconds |
FramesToSeconds |
Converts frames to seconds |
SecondsToFrames |
Converts seconds to frames |
PixelsToTiles |
Converts pixels to tiles |
TilesToPixels |
Converts tiles to pixels |
ToBool |
Converts to boolean |
- MathHelper
- FileHelper
- HashHelper
- JsonHelper
- MapHelper
- TextHelper
- SoundHelper
- AlignHelpers
- Instance Helper
- Enum Extensions
- String Extensions
- Int Extensions
- Float Extensions
- IEnumerable Extensions
- Random Extensions
- Sound Extensions
- Font Extensions