Fixed wrapping

This commit is contained in:
2026-06-17 13:09:34 +02:00
parent 97a3d90f14
commit e3b626f3d6
3 changed files with 7 additions and 988 deletions

View File

@@ -154,8 +154,10 @@ namespace Marro.PacManUdon
internal static int GetTilemapIndex(Vector2 position)
{
position = Clamp(position, 0, mazeWidth - 1, 0, mazeHeight - 1);
var index = (int)position.x + (int)position.y * mazeWidth;
// (int)position.x %% mazeWidth + (int)position.y %% mazeHeight * mazeWidth, where %% is an absolute modulo
// Absoliute modulo in this case is implemented by just adding the divisor, works good enough and is performant.
var index = ((int)position.x + mazeWidth) % mazeWidth + ((int)position.y + mazeHeight) % mazeHeight * mazeWidth;
return index;
}
#endregion
@@ -234,32 +236,5 @@ namespace Marro.PacManUdon
return true;
}
#endregion
#region Utils
private static Vector2 Clamp(Vector2 vector, float xMin, float xMax, float yMin, float yMax)
{
if (vector.x < xMin)
{
vector.x = xMin;
}
if (vector.x > xMax)
{
vector.x = xMax;
}
if (vector.y < yMin)
{
vector.y = yMin;
}
if (vector.y > yMax)
{
vector.y = yMax;
}
return vector;
}
#endregion
}
}