From 27fcf690e227aa113a5608af997167b40e495ac4 Mon Sep 17 00:00:00 2001 From: Exouxas Date: Sun, 15 Oct 2023 11:16:33 +0200 Subject: [PATCH 1/2] Add bounds to World --- src/Core/Worlds/World.cs | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/src/Core/Worlds/World.cs b/src/Core/Worlds/World.cs index d88a12b..26039c0 100644 --- a/src/Core/Worlds/World.cs +++ b/src/Core/Worlds/World.cs @@ -40,6 +40,16 @@ public abstract class World : ISerializable /// public event NotifyActorMoved? ActorMoved; + /// + /// The world bounds. If this value is null, the world is unbounded. + /// + public Vector3? WorldBounds { get; set; } = null; + + /// + /// Whether the world wraps around. + /// + public bool WorldWrap { get; set; } = false; + /// /// Get the current step. /// @@ -254,17 +264,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); } /// From fe44d05b48eeb6125159738ab2212c8c93045d5c Mon Sep 17 00:00:00 2001 From: Exouxas Date: Sun, 15 Oct 2023 12:58:36 +0200 Subject: [PATCH 2/2] Fix boundary value limit --- src/Core/Worlds/World.cs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Core/Worlds/World.cs b/src/Core/Worlds/World.cs index 26039c0..ba52cec 100644 --- a/src/Core/Worlds/World.cs +++ b/src/Core/Worlds/World.cs @@ -41,9 +41,30 @@ public abstract class World : ISerializable public event NotifyActorMoved? ActorMoved; /// - /// The world bounds. If this value is null, the world is unbounded. + /// 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. /// - public Vector3? WorldBounds { get; set; } = null; + 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; /// /// Whether the world wraps around.