Skip to content

Int Extensions

Shmellyorc edited this page Aug 31, 2026 · 1 revision

IntExtensions provides a comprehensive set of extension methods for int values, making common mathematical and game development operations more intuitive and readable.


Overview

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

Clamping

Clamp

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);  // 0

Wrapping

Wrap (single parameter)

Wraps the value within the range [0, max).

int value = 7;
int wrapped = value.Wrap(5);  // 2

int value2 = -1;
int wrapped2 = value2.Wrap(5);  // 4

Wrap (min and max)

Wraps 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);  // 3

Angle Conversion

ToRadians

Converts degrees to radians.

float radians = 90.ToRadians();  // 1.5708 (PI/2)
float radians2 = 180.ToRadians();  // 3.14159 (PI)

ToDegrees

Converts radians to degrees.

float degrees = 3.ToDegrees();  // ~171.887
float degrees2 = 1.ToDegrees();  // ~57.296

Even/Odd Checks

IsEven

Determines whether the value is even.

bool isEven = 4.IsEven();  // true
bool isEven2 = 5.IsEven(); // false

IsOdd

Determines whether the value is odd.

bool isOdd = 5.IsOdd();  // true
bool isOdd2 = 4.IsOdd(); // false

Power of Two

IsPowerOfTwo

Determines 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(); // false

NextPowerOfTwo

Rounds up to the nearest power of two.

int next = 5.NextPowerOfTwo();  // 8
int next2 = 12.NextPowerOfTwo(); // 16
int next3 = 8.NextPowerOfTwo();  // 8

Sign and Absolute

Sign

Gets the sign of the value (-1, 0, or 1).

int sign = 5.Sign();   // 1
int sign2 = (-3).Sign(); // -1
int sign3 = 0.Sign();  // 0

Abs

Gets the absolute value.

int abs = (-5).Abs();  // 5
int abs2 = 3.Abs();    // 3

Time Conversion

SecondsToMilliseconds

Converts seconds to milliseconds.

int ms = 2.SecondsToMilliseconds();  // 2000
int ms2 = 5.SecondsToMilliseconds(); // 5000

MillisecondsToSeconds

Converts milliseconds to seconds.

float seconds = 1500.MillisecondsToSeconds();  // 1.5f
float seconds2 = 2000.MillisecondsToSeconds(); // 2.0f

FramesToSeconds

Converts frames to seconds at the specified FPS.

float seconds = 120.FramesToSeconds(60);  // 2.0f
float seconds2 = 30.FramesToSeconds(60);  // 0.5f

SecondsToFrames

Converts seconds to frames at the specified FPS.

int frames = 3.SecondsToFrames(60);  // 180
int frames2 = 1.SecondsToFrames(30); // 30

Tile Conversion

PixelsToTiles

Converts pixels to tile coordinates.

int tileX = 128.PixelsToTiles(32);  // 4
int tileY = 64.PixelsToTiles(32);   // 2

TilesToPixels

Converts tile coordinates to pixels.

int pixelX = 5.TilesToPixels(32);  // 160
int pixelY = 3.TilesToPixels(32);  // 96

Boolean Conversion

ToBool

Converts the integer value to a boolean.

bool isTrue = 1.ToBool();   // true
bool isTrue2 = (-5).ToBool(); // true
bool isFalse = 0.ToBool();  // false

Examples

Grid Snapping

public int SnapToGrid(int value, int gridSize)
{
    return (value / gridSize).PixelsToTiles(gridSize);
}

Animation Frame Calculation

public int GetAnimationFrame(float elapsedTime, int fps)
{
    int frames = ((int)elapsedTime).SecondsToFrames(fps);
    return frames % _totalFrames;
}

Tile-based Movement

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;
    }
}

Power of Two Texture Sizes

public int GetOptimalTextureSize(int requestedSize)
{
    return requestedSize.NextPowerOfTwo();
}

Damage Calculation with Sign

public int CalculateDamage(int baseDamage, int defense)
{
    int effectiveDamage = baseDamage - defense;
    
    // If negative, return 0
    if (effectiveDamage < 0)
        effectiveDamage = 0;
    
    return effectiveDamage;
}

Summary

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

Back to Home

Clone this wiki locally