Fixed wrapping
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -185,7 +185,9 @@ namespace Marro.PacManUdon
|
|||||||
|
|
||||||
public static Vector2 GetNextPosition(Vector2 currentPosition, Vector2 directionVector, float speed, float deltaTime)
|
public static Vector2 GetNextPosition(Vector2 currentPosition, Vector2 directionVector, float speed, float deltaTime)
|
||||||
{
|
{
|
||||||
return currentPosition + deltaTime * speed * directionVector;
|
var nextPosition = currentPosition + deltaTime * speed * directionVector;
|
||||||
|
nextPosition = new Vector2((nextPosition.x + 32) % 32, (nextPosition.y + 32) % 32);
|
||||||
|
return nextPosition;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Vector2 PositionToGrid(Vector2 position)
|
public static Vector2 PositionToGrid(Vector2 position)
|
||||||
|
|||||||
@@ -154,8 +154,10 @@ namespace Marro.PacManUdon
|
|||||||
|
|
||||||
internal static int GetTilemapIndex(Vector2 position)
|
internal static int GetTilemapIndex(Vector2 position)
|
||||||
{
|
{
|
||||||
position = Clamp(position, 0, mazeWidth - 1, 0, mazeHeight - 1);
|
// (int)position.x %% mazeWidth + (int)position.y %% mazeHeight * mazeWidth, where %% is an absolute modulo
|
||||||
var index = (int)position.x + (int)position.y * mazeWidth;
|
// 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;
|
return index;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
@@ -234,32 +236,5 @@ namespace Marro.PacManUdon
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
#endregion
|
#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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user