diff --git a/Content.Server/GameObjects/Components/Chemistry/ChemMasterComponent.cs b/Content.Server/GameObjects/Components/Chemistry/ChemMasterComponent.cs index 208392e4c6..52285bf67d 100644 --- a/Content.Server/GameObjects/Components/Chemistry/ChemMasterComponent.cs +++ b/Content.Server/GameObjects/Components/Chemistry/ChemMasterComponent.cs @@ -14,6 +14,7 @@ using Content.Shared.GameObjects.Components.Chemistry.ChemMaster; using Content.Shared.GameObjects.EntitySystems; using Content.Shared.Interfaces; using Content.Shared.Interfaces.GameObjects.Components; +using Content.Shared.Utility; using Robust.Server.GameObjects.Components.Container; using Robust.Server.GameObjects.Components.UserInterface; using Robust.Server.GameObjects.EntitySystems; @@ -300,15 +301,12 @@ namespace Content.Server.GameObjects.Components.Chemistry hands.PutInHand(item); continue; } - } //Put it on the floor bottle.Transform.GridPosition = user.Transform.GridPosition; //Give it an offset - var x_negative = random.Prob(0.5f) ? -1 : 1; - var y_negative = random.Prob(0.5f) ? -1 : 1; - bottle.Transform.LocalPosition += new Vector2(random.NextFloat() * 0.2f * x_negative, random.NextFloat() * 0.2f * y_negative); + bottle.RandomOffset(0.2f); } } diff --git a/Content.Server/GameObjects/Components/Conveyor/ConveyorComponent.cs b/Content.Server/GameObjects/Components/Conveyor/ConveyorComponent.cs index 01a29b4a05..bfab8543f6 100644 --- a/Content.Server/GameObjects/Components/Conveyor/ConveyorComponent.cs +++ b/Content.Server/GameObjects/Components/Conveyor/ConveyorComponent.cs @@ -10,6 +10,7 @@ using Content.Shared.GameObjects.Components.Conveyor; using Content.Shared.GameObjects.Components.Interactable; using Content.Shared.Interfaces.GameObjects.Components; using Content.Shared.Physics; +using Content.Shared.Utility; using Robust.Server.GameObjects; using Robust.Shared.Containers; using Robust.Shared.GameObjects; @@ -170,7 +171,7 @@ namespace Content.Server.GameObjects.Components.Conveyor Owner.AddComponent(); _group?.RemoveConveyor(this); - Owner.Transform.WorldPosition += (_random.NextFloat() * 0.4f - 0.2f, _random.NextFloat() * 0.4f - 0.2f); + Owner.RandomOffset(0.2f); return true; } diff --git a/Content.Shared/Utility/SharedEntityExtensions.cs b/Content.Shared/Utility/SharedEntityExtensions.cs new file mode 100644 index 0000000000..8ad47700e2 --- /dev/null +++ b/Content.Shared/Utility/SharedEntityExtensions.cs @@ -0,0 +1,41 @@ +using System; +using Robust.Shared.Interfaces.GameObjects; +using Robust.Shared.Interfaces.Random; +using Robust.Shared.IoC; +using Robust.Shared.Maths; +using Robust.Shared.Random; +using Robust.Shared.Utility; + +namespace Content.Shared.Utility +{ + public static class SharedEntityExtensions + { + public static void RandomOffset(this IEntity entity, float minX, float maxX, float minY, float maxY) + { + DebugTools.AssertNotNull(entity); + DebugTools.Assert(minX <= maxX, $"Minimum X value ({minX}) must be smaller than or equal to the maximum X value ({maxX})"); + DebugTools.Assert(minY <= maxY, $"Minimum Y value ({minY}) must be smaller than or equal to the maximum Y value ({maxY})"); + + var random = IoCManager.Resolve(); + var randomX = random.NextFloat() * (maxX - minX) + minX; + var randomY = random.NextFloat() * (maxY - minY) + minY; + var offset = new Vector2(randomX, randomY); + + entity.Transform.LocalPosition += offset; + } + + public static void RandomOffset(this IEntity entity, float min, float max) + { + DebugTools.AssertNotNull(entity); + DebugTools.Assert(min <= max, $"Minimum value ({min}) must be smaller than or equal to the maximum value ({max})"); + + entity.RandomOffset(min, max, min, max); + } + + public static void RandomOffset(this IEntity entity, float value) + { + value = Math.Abs(value); + entity.RandomOffset(-value, value); + } + } +}