Skip to content

Commit

Permalink
Merge pull request #52 from Exouxas/51-add-world-wrapping
Browse files Browse the repository at this point in the history
51 add world wrapping
  • Loading branch information
Exouxas authored Oct 15, 2023
2 parents ee1777d + fe44d05 commit 1e096de
Showing 1 changed file with 57 additions and 2 deletions.
59 changes: 57 additions & 2 deletions src/Core/Worlds/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,37 @@ public abstract class World : ISerializable
/// </summary>
public event NotifyActorMoved? ActorMoved;

/// <summary>
/// The world bounds. If this value is null, the world is unbounded. If any of the values are 0 or lower, they will be set to a very small value.
/// </summary>
public Vector3? WorldBounds
{
get => _worldBounds;
set
{
if (value != null)
{
float minimum = 0.000000001f;

_worldBounds = new Vector3(
Math.Max(value.Value.X, minimum),
Math.Max(value.Value.Y, minimum),
Math.Max(value.Value.Z, minimum)
);
}
else
{
_worldBounds = null;
}
}
}
private Vector3? _worldBounds = null;

/// <summary>
/// Whether the world wraps around.
/// </summary>
public bool WorldWrap { get; set; } = false;

/// <summary>
/// Get the current step.
/// </summary>
Expand Down Expand Up @@ -254,17 +285,41 @@ public void MoveTo(Actor a, Vector3 v)
throw new ArgumentException("Vector3 cannot contain NaN values");
}

Vector3 newPosition = v;

if (WorldBounds != null)
{
float x = newPosition.X;
float y = newPosition.Y;
float z = newPosition.Z;

if (WorldWrap)
{
x -= (float)Math.Floor(x / WorldBounds.Value.X) * WorldBounds.Value.X;
y -= (float)Math.Floor(y / WorldBounds.Value.Y) * WorldBounds.Value.Y;
z -= (float)Math.Floor(z / WorldBounds.Value.Z) * WorldBounds.Value.Z;
}
else
{
x = Math.Clamp(x, 0, WorldBounds.Value.X);
y = Math.Clamp(y, 0, WorldBounds.Value.Y);
z = Math.Clamp(z, 0, WorldBounds.Value.Z);
}

newPosition = new Vector3(x, y, z);
}

Vector3 oldPosition = GetPosition(a);

lock (_positionsLock)
{
if (positions.ContainsKey(a))
{
positions[a] = v;
positions[a] = newPosition;
}
}

ActorMoved?.Invoke(a, oldPosition, v);
ActorMoved?.Invoke(a, oldPosition, newPosition);
}

/// <summary>
Expand Down

0 comments on commit 1e096de

Please sign in to comment.