diff --git a/Robust.Client/Audio/Midi/MidiManager.cs b/Robust.Client/Audio/Midi/MidiManager.cs index 2d1da28a0..c98d73991 100644 --- a/Robust.Client/Audio/Midi/MidiManager.cs +++ b/Robust.Client/Audio/Midi/MidiManager.cs @@ -1,15 +1,13 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; using System.Runtime.InteropServices; using System.Threading; -using JetBrains.Annotations; using NFluidsynth; using Robust.Client.Interfaces.Graphics.ClientEye; using Robust.Client.Interfaces.ResourceManagement; +using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.Log; -using Robust.Shared.Interfaces.Map; using Robust.Shared.Interfaces.Physics; using Robust.Shared.Interfaces.Resources; using Robust.Shared.IoC; @@ -68,9 +66,9 @@ namespace Robust.Client.Audio.Midi internal class MidiManager : IDisposable, IMidiManager { - [Dependency] private readonly IMapManager _mapManager = default!; [Dependency] private readonly IEyeManager _eyeManager = default!; [Dependency] private readonly IResourceManager _resourceManager = default!; + [Dependency] private readonly IEntityManager _entityManager = default!; public bool IsAvailable { @@ -280,7 +278,7 @@ namespace Robust.Client.Audio.Midi MapCoordinates? mapPos = null; if (renderer.TrackingCoordinates != null) { - mapPos = renderer.TrackingCoordinates.Value.ToMap(_mapManager); + mapPos = renderer.TrackingCoordinates.Value.ToMap(_entityManager); } else if (renderer.TrackingEntity != null) { diff --git a/Robust.Client/Audio/Midi/MidiRenderer.cs b/Robust.Client/Audio/Midi/MidiRenderer.cs index ddd12633a..d63d32f0f 100644 --- a/Robust.Client/Audio/Midi/MidiRenderer.cs +++ b/Robust.Client/Audio/Midi/MidiRenderer.cs @@ -139,7 +139,7 @@ namespace Robust.Client.Audio.Midi /// This is only used if is set to True /// and is null. /// - GridCoordinates? TrackingCoordinates { get; set; } + EntityCoordinates? TrackingCoordinates { get; set; } /// /// Send a midi event for the renderer to play. @@ -255,7 +255,7 @@ namespace Robust.Client.Audio.Midi } public IEntity? TrackingEntity { get; set; } = null; - public GridCoordinates? TrackingCoordinates { get; set; } = null; + public EntityCoordinates? TrackingCoordinates { get; set; } = null; internal bool Free { get; set; } = false; diff --git a/Robust.Client/Console/Commands/ClientSpawnCommand.cs b/Robust.Client/Console/Commands/ClientSpawnCommand.cs index bfaf58e1f..e174bf7c1 100644 --- a/Robust.Client/Console/Commands/ClientSpawnCommand.cs +++ b/Robust.Client/Console/Commands/ClientSpawnCommand.cs @@ -23,7 +23,7 @@ namespace Robust.Client.Console.Commands } var entityManager = IoCManager.Resolve(); - entityManager.SpawnEntity(args[0], player.ControlledEntity.Transform.GridPosition); + entityManager.SpawnEntity(args[0], player.ControlledEntity.Transform.Coordinates); return false; } } diff --git a/Robust.Client/Console/Commands/Debug.cs b/Robust.Client/Console/Commands/Debug.cs index 7e03c06df..f594f201f 100644 --- a/Robust.Client/Console/Commands/Debug.cs +++ b/Robust.Client/Console/Commands/Debug.cs @@ -55,7 +55,7 @@ namespace Robust.Client.Console.Commands foreach (var e in entityManager.GetEntities().OrderBy(e => e.Uid)) { - console.AddLine($"entity {e.Uid}, {e.Prototype?.ID}, {e.Transform.GridPosition}.", Color.White); + console.AddLine($"entity {e.Uid}, {e.Prototype?.ID}, {e.Transform.Coordinates}.", Color.White); } return false; diff --git a/Robust.Client/GameObjects/ClientEntityManager.cs b/Robust.Client/GameObjects/ClientEntityManager.cs index 6ac88935e..5241fbae7 100644 --- a/Robust.Client/GameObjects/ClientEntityManager.cs +++ b/Robust.Client/GameObjects/ClientEntityManager.cs @@ -175,12 +175,14 @@ namespace Robust.Client.GameObjects } /// - public override IEntity CreateEntityUninitialized(string? prototypeName, GridCoordinates coordinates) + public override IEntity CreateEntityUninitialized(string? prototypeName, EntityCoordinates coordinates) { var newEntity = CreateEntity(prototypeName, GenerateEntityUid()); - if (coordinates.GridID != GridId.Invalid) + var gridId = coordinates.GetGridId(this); + + if (coordinates.GetGridId(this) != GridId.Invalid) { - var gridEntityId = _mapManager.GetGrid(coordinates.GridID).GridEntityId; + var gridEntityId = _mapManager.GetGrid(gridId).GridEntityId; newEntity.Transform.AttachParent(GetEntity(gridEntityId)); newEntity.Transform.LocalPosition = coordinates.Position; } @@ -198,7 +200,7 @@ namespace Robust.Client.GameObjects } /// - public override IEntity SpawnEntity(string? protoName, GridCoordinates coordinates) + public override IEntity SpawnEntity(string? protoName, EntityCoordinates coordinates) { var newEnt = CreateEntityUninitialized(protoName, coordinates); InitializeAndStartEntity((Entity) newEnt); @@ -216,12 +218,11 @@ namespace Robust.Client.GameObjects } /// - public override IEntity SpawnEntityNoMapInit(string? protoName, GridCoordinates coordinates) + public override IEntity SpawnEntityNoMapInit(string? protoName, EntityCoordinates coordinates) { return SpawnEntity(protoName, coordinates); } - protected override EntityUid GenerateEntityUid() { return new EntityUid(_nextClientEntityUid++); diff --git a/Robust.Client/GameObjects/EntitySystems/AudioSystem.cs b/Robust.Client/GameObjects/EntitySystems/AudioSystem.cs index 0bb268f4c..6a5f43b92 100644 --- a/Robust.Client/GameObjects/EntitySystems/AudioSystem.cs +++ b/Robust.Client/GameObjects/EntitySystems/AudioSystem.cs @@ -27,6 +27,7 @@ namespace Robust.Client.GameObjects.EntitySystems [Dependency] private readonly IMapManager _mapManager = default!; [Dependency] private readonly IClydeAudio _clyde = default!; [Dependency] private readonly IEyeManager _eyeManager = default!; + [Dependency] private readonly IEntityManager _entityManager = default!; private readonly List _playingClydeStreams = new List(); @@ -55,10 +56,12 @@ namespace Robust.Client.GameObjects.EntitySystems private void PlayAudioPositionalHandler(PlayAudioPositionalMessage ev) { - if (!_mapManager.GridExists(ev.Coordinates.GridID)) + var gridId = ev.Coordinates.GetGridId(_entityManager); + + if (!_mapManager.GridExists(gridId)) { Logger.Error( - $"Server tried to play sound on grid {ev.Coordinates.GridID.Value}, which does not exist. Ignoring."); + $"Server tried to play sound on grid {gridId}, which does not exist. Ignoring."); return; } @@ -108,9 +111,9 @@ namespace Robust.Client.GameObjects.EntitySystems if (stream.TrackingCoordinates != null) { var coords = stream.TrackingCoordinates.Value; - if (_mapManager.GridExists(coords.GridID)) + if (_mapManager.GridExists(coords.GetGridId(_entityManager))) { - mapPos = stream.TrackingCoordinates.Value.ToMap(_mapManager); + mapPos = stream.TrackingCoordinates.Value.ToMap(_entityManager); } else { @@ -270,7 +273,7 @@ namespace Robust.Client.GameObjects.EntitySystems /// The resource path to the OGG Vorbis file to play. /// The coordinates at which to play the audio. /// - public IPlayingAudioStream? Play(string filename, GridCoordinates coordinates, AudioParams? audioParams = null) + public IPlayingAudioStream? Play(string filename, EntityCoordinates coordinates, AudioParams? audioParams = null) { if (_resourceCache.TryGetResource(new ResourcePath(filename), out var audio)) { @@ -287,11 +290,11 @@ namespace Robust.Client.GameObjects.EntitySystems /// The audio stream to play. /// The coordinates at which to play the audio. /// - public IPlayingAudioStream? Play(AudioStream stream, GridCoordinates coordinates, + public IPlayingAudioStream? Play(AudioStream stream, EntityCoordinates coordinates, AudioParams? audioParams = null) { var source = _clyde.CreateAudioSource(stream); - if (!source.SetPosition(coordinates.ToMapPos(_mapManager))) + if (!source.SetPosition(coordinates.ToMapPos(EntityManager))) { source.Dispose(); Logger.Warning("Can't play positional audio, can't set position."); @@ -329,7 +332,7 @@ namespace Robust.Client.GameObjects.EntitySystems public uint? NetIdentifier; public IClydeAudioSource Source = default!; public IEntity TrackingEntity = default!; - public GridCoordinates? TrackingCoordinates; + public EntityCoordinates? TrackingCoordinates; public bool Done; public float Volume; diff --git a/Robust.Client/GameObjects/EntitySystems/EffectSystem.cs b/Robust.Client/GameObjects/EntitySystems/EffectSystem.cs index 2585234c2..7deffcae7 100644 --- a/Robust.Client/GameObjects/EntitySystems/EffectSystem.cs +++ b/Robust.Client/GameObjects/EntitySystems/EffectSystem.cs @@ -44,7 +44,7 @@ namespace Robust.Client.GameObjects SubscribeNetworkEvent(CreateEffect); SubscribeLocalEvent(CreateEffect); - var overlay = new EffectOverlay(this, prototypeManager, _mapManager, _playerManager); + var overlay = new EffectOverlay(this, prototypeManager, _mapManager, _playerManager, _entityManager); overlayManager.AddOverlay(overlay); } @@ -59,9 +59,9 @@ namespace Robust.Client.GameObjects { if (message.AttachedEntityUid != null && message.Coordinates != default) { - Logger.Warning("Set both an AttachedEntityUid and GridCoordinates on an EffectSystemMessage for sprite {0} which is not supported!", message.EffectSprite); + Logger.Warning("Set both an AttachedEntityUid and EntityCoordinates on an EffectSystemMessage for sprite {0} which is not supported!", message.EffectSprite); } - + var gameTime = gameTiming.CurTime; if (gameTime > message.DeathTime) //Did we already die in transit? That's pretty troubling isn't it { @@ -70,7 +70,7 @@ namespace Robust.Client.GameObjects } //Create effect from creation message - var effect = new Effect(message, resourceCache, _mapManager); + var effect = new Effect(message, resourceCache, _mapManager, _entityManager); if (effect.AttachedEntityUid != null) { effect.AttachedEntity = _entityManager.GetEntity(effect.AttachedEntityUid.Value); @@ -121,33 +121,33 @@ namespace Robust.Client.GameObjects /// Entity that the effect is attached to /// public IEntity? AttachedEntity { get; set; } - + public EntityUid? AttachedEntityUid { get; } - + /// /// Offset relative to the attached entity /// public Vector2 AttachedOffset { get; } - + /// /// Effect position relative to the emit position /// - public GridCoordinates Coordinates; + public EntityCoordinates Coordinates; /// /// Where the emitter was when the effect was first emitted /// - public GridCoordinates EmitterCoordinates; + public EntityCoordinates EmitterCoordinates; /// /// Effect's x/y velocity /// - public Vector2 Velocity = new Vector2(0, 0); + public Vector2 Velocity = Vector2.Zero; /// /// Effect's x/y acceleration /// - public Vector2 Acceleration = new Vector2(0, 0); + public Vector2 Acceleration = Vector2.Zero; /// /// Effect's radial velocity - relative to EmitterPosition @@ -215,8 +215,9 @@ namespace Robust.Client.GameObjects public TimeSpan Deathtime = TimeSpan.FromSeconds(1); private readonly IMapManager _mapManager; + private readonly IEntityManager _entityManager; - public Effect(EffectSystemMessage effectcreation, IResourceCache resourceCache, IMapManager mapManager) + public Effect(EffectSystemMessage effectcreation, IResourceCache resourceCache, IMapManager mapManager, IEntityManager entityManager) { if (effectcreation.RsiState != null) { @@ -254,6 +255,7 @@ namespace Robust.Client.GameObjects ColorDelta = effectcreation.ColorDelta; Shaded = effectcreation.Shaded; _mapManager = mapManager; + _entityManager = entityManager; } public void Update(float frameTime) @@ -269,11 +271,11 @@ namespace Robust.Client.GameObjects var deltaPosition = new Vector2(0f, 0f); //If we have an emitter we can do special effects around that emitter position - if (_mapManager.GridExists(EmitterCoordinates.GridID)) + if (_mapManager.GridExists(EmitterCoordinates.GetGridId(_entityManager))) { //Calculate delta p due to radial velocity var positionRelativeToEmitter = - Coordinates.ToMapPos(_mapManager) - EmitterCoordinates.ToMapPos(_mapManager); + Coordinates.ToMapPos(_entityManager) - EmitterCoordinates.ToMapPos(_entityManager); var deltaRadial = RadialVelocity * frameTime; deltaPosition = positionRelativeToEmitter * (deltaRadial / positionRelativeToEmitter.Length); @@ -290,7 +292,7 @@ namespace Robust.Client.GameObjects //Calculate new position from our velocity as well as possible rotation/movement around emitter deltaPosition += Velocity * frameTime; - Coordinates = new GridCoordinates(Coordinates.Position + deltaPosition, Coordinates.GridID); + Coordinates = Coordinates.Offset(deltaPosition); //Finish calculating new rotation, size, color Rotation += RotationRate * frameTime; @@ -340,21 +342,23 @@ namespace Robust.Client.GameObjects private sealed class EffectOverlay : Overlay { private readonly IPlayerManager _playerManager; - + public override bool AlwaysDirty => true; public override OverlaySpace Space => OverlaySpace.WorldSpace; private readonly ShaderInstance _unshadedShader; private readonly EffectSystem _owner; private readonly IMapManager _mapManager; + private readonly IEntityManager _entityManager; - public EffectOverlay(EffectSystem owner, IPrototypeManager protoMan, IMapManager mapMan, IPlayerManager playerMan) : base( + public EffectOverlay(EffectSystem owner, IPrototypeManager protoMan, IMapManager mapMan, IPlayerManager playerMan, IEntityManager entityManager) : base( "EffectSystem") { _owner = owner; _unshadedShader = protoMan.Index("unshaded").Instance(); _mapManager = mapMan; _playerManager = playerMan; + _entityManager = entityManager; } protected override void Draw(DrawingHandleBase handle, OverlaySpace currentSpace) @@ -367,8 +371,8 @@ namespace Robust.Client.GameObjects foreach (var effect in _owner._Effects) { - if (effect.AttachedEntity?.Transform.MapID != player?.Transform.MapID && - _mapManager.GetGrid(effect.Coordinates.GridID).ParentMapId != map) + if (effect.AttachedEntity?.Transform.MapID != player?.Transform.MapID && + _mapManager.GetGrid(effect.Coordinates.GetGridId(_entityManager)).ParentMapId != map) { continue; } @@ -382,9 +386,9 @@ namespace Robust.Client.GameObjects } var effectSprite = effect.EffectSprite; - var effectOrigin = effect.AttachedEntity?.Transform.MapPosition.Position + effect.AttachedOffset ?? - effect.Coordinates.ToMapPos(_mapManager); - + var effectOrigin = effect.AttachedEntity?.Transform.MapPosition.Position + effect.AttachedOffset ?? + effect.Coordinates.ToMapPos(_entityManager); + var effectArea = Box2.CenteredAround(effectOrigin, effect.Size); var rotatedBox = new Box2Rotated(effectArea, effect.Rotation, effectOrigin); diff --git a/Robust.Client/Graphics/ClientEye/EyeManager.cs b/Robust.Client/Graphics/ClientEye/EyeManager.cs index f25846c1a..5093f25c4 100644 --- a/Robust.Client/Graphics/ClientEye/EyeManager.cs +++ b/Robust.Client/Graphics/ClientEye/EyeManager.cs @@ -1,7 +1,6 @@ -using System; -using Robust.Client.Interfaces.Graphics; +using Robust.Client.Interfaces.Graphics; using Robust.Client.Interfaces.Graphics.ClientEye; -using Robust.Shared.Interfaces.Map; +using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.IoC; using Robust.Shared.Map; using Robust.Shared.Maths; @@ -20,11 +19,8 @@ namespace Robust.Client.Graphics.ClientEye /// public const int PixelsPerMeter = 32; -#pragma warning disable 649, CS8618 - // ReSharper disable twice NotNullMemberIsNotInitialized - [Dependency] private readonly IMapManager _mapManager; - [Dependency] private readonly IClyde _displayManager; -#pragma warning restore 649, CS8618 + [Dependency] private readonly IClyde _displayManager = default!; + [Dependency] private readonly IEntityManager _entityManager = default!; // We default to this when we get set to a null eye. private readonly FixedEye _defaultEye = new FixedEye(); @@ -102,10 +98,9 @@ namespace Robust.Client.Graphics.ClientEye } /// - public ScreenCoordinates WorldToScreen(GridCoordinates point) + public ScreenCoordinates CoordinatesToScreen(EntityCoordinates point) { - var worldCoords = _mapManager.GetGrid(point.GridID).LocalToWorld(point); - return new ScreenCoordinates(WorldToScreen(worldCoords.Position)); + return MapToScreen(point.ToMap(_entityManager)); } public ScreenCoordinates MapToScreen(MapCoordinates point) diff --git a/Robust.Client/Interfaces/Graphics/ClientEye/IEyeManager.cs b/Robust.Client/Interfaces/Graphics/ClientEye/IEyeManager.cs index 7f5aea88b..9b43556d0 100644 --- a/Robust.Client/Interfaces/Graphics/ClientEye/IEyeManager.cs +++ b/Robust.Client/Interfaces/Graphics/ClientEye/IEyeManager.cs @@ -47,7 +47,7 @@ namespace Robust.Client.Interfaces.Graphics.ClientEye /// /// Point in world to transform. /// Corresponding point in UI screen space. - ScreenCoordinates WorldToScreen(GridCoordinates point); + ScreenCoordinates CoordinatesToScreen(EntityCoordinates point); /// /// Unprojects a point from UI screen space to world space using the current camera. diff --git a/Robust.Client/Placement/Modes/AlignSimilar.cs b/Robust.Client/Placement/Modes/AlignSimilar.cs index 962c9a7a9..deebc277a 100644 --- a/Robust.Client/Placement/Modes/AlignSimilar.cs +++ b/Robust.Client/Placement/Modes/AlignSimilar.cs @@ -16,7 +16,8 @@ namespace Robust.Client.Placement.Modes public override void AlignPlacementMode(ScreenCoordinates mouseScreen) { MouseCoords = ScreenToCursorGrid(mouseScreen); - var mapGrid = pManager.MapManager.GetGrid(MouseCoords.GridID); + var gridId = MouseCoords.GetGridId(pManager.EntityManager); + var mapGrid = pManager.MapManager.GetGrid(gridId); CurrentTile = mapGrid.GetTileRef(MouseCoords); if (pManager.CurrentPermission!.IsTile) @@ -29,11 +30,9 @@ namespace Robust.Client.Placement.Modes return; } - var manager = IoCManager.Resolve(); - - var snapToEntities = manager.GetEntitiesInRange(MouseCoords, SnapToRange) + var snapToEntities = pManager.EntityManager.GetEntitiesInRange(MouseCoords, SnapToRange) .Where(entity => entity.Prototype == pManager.CurrentPrototype && entity.Transform.MapID == mapGrid.ParentMapId) - .OrderBy(entity => (entity.Transform.WorldPosition - MouseCoords.ToMapPos(pManager.MapManager)).LengthSquared) + .OrderBy(entity => (entity.Transform.WorldPosition - MouseCoords.ToMapPos(pManager.EntityManager)).LengthSquared) .ToList(); if (snapToEntities.Count == 0) @@ -66,10 +65,10 @@ namespace Robust.Client.Placement.Modes var closestSide = (from Vector2 side in sides orderby (side - MouseCoords.Position).LengthSquared select side).First(); - MouseCoords = new GridCoordinates(closestSide, MouseCoords.GridID); + MouseCoords = new EntityCoordinates(MouseCoords.EntityId, MouseCoords.Position); } - public override bool IsValidPosition(GridCoordinates position) + public override bool IsValidPosition(EntityCoordinates position) { if (pManager.CurrentPermission!.IsTile) { diff --git a/Robust.Client/Placement/Modes/AlignSnapgridBorder.cs b/Robust.Client/Placement/Modes/AlignSnapgridBorder.cs index 639084969..722ac5800 100644 --- a/Robust.Client/Placement/Modes/AlignSnapgridBorder.cs +++ b/Robust.Client/Placement/Modes/AlignSnapgridBorder.cs @@ -1,6 +1,8 @@ using System; using Robust.Client.Graphics.ClientEye; using Robust.Client.Graphics.Drawing; +using Robust.Shared.Interfaces.GameObjects; +using Robust.Shared.IoC; using Robust.Shared.Map; using Robust.Shared.Maths; using Robust.Shared.Utility; @@ -59,7 +61,7 @@ namespace Robust.Client.Placement.Modes { MouseCoords = ScreenToCursorGrid(mouseScreen); - snapSize = pManager.MapManager.GetGrid(MouseCoords.GridID).SnapSize; //Find snap size. + snapSize = pManager.MapManager.GetGrid(MouseCoords.GetGridId(pManager.EntityManager)).SnapSize; //Find snap size. GridDistancing = snapSize; onGrid = true; @@ -69,11 +71,11 @@ namespace Robust.Client.Placement.Modes //Convert back to original world and screen coordinates after applying offset MouseCoords = - new GridCoordinates( - mouselocal + new Vector2(pManager.PlacementOffset.X, pManager.PlacementOffset.Y), MouseCoords.GridID); + new EntityCoordinates( + MouseCoords.EntityId, mouselocal + new Vector2(pManager.PlacementOffset.X, pManager.PlacementOffset.Y)); } - public override bool IsValidPosition(GridCoordinates position) + public override bool IsValidPosition(EntityCoordinates position) { if (!RangeCheck(position)) { diff --git a/Robust.Client/Placement/Modes/AlignSnapgridCenter.cs b/Robust.Client/Placement/Modes/AlignSnapgridCenter.cs index 02ddcd742..90261e19e 100644 --- a/Robust.Client/Placement/Modes/AlignSnapgridCenter.cs +++ b/Robust.Client/Placement/Modes/AlignSnapgridCenter.cs @@ -2,7 +2,6 @@ using Robust.Shared.Map; using Robust.Shared.Maths; using Robust.Client.Graphics.Drawing; -using Robust.Shared.Utility; namespace Robust.Client.Placement.Modes { @@ -49,7 +48,8 @@ namespace Robust.Client.Placement.Modes { MouseCoords = ScreenToCursorGrid(mouseScreen); - snapSize = pManager.MapManager.GetGrid(MouseCoords.GridID).SnapSize; //Find snap size. + var gridId = MouseCoords.GetGridId(pManager.EntityManager); + snapSize = pManager.MapManager.GetGrid(gridId).SnapSize; //Find snap size. GridDistancing = snapSize; onGrid = true; @@ -58,10 +58,10 @@ namespace Robust.Client.Placement.Modes (float)(MathF.Round((MouseCoords.Position.Y / snapSize - 0.5f), MidpointRounding.AwayFromZero) + 0.5) * snapSize); //Adjust mouseCoords to new calculated position - MouseCoords = new GridCoordinates(mouseLocal + new Vector2(pManager.PlacementOffset.X, pManager.PlacementOffset.Y), MouseCoords.GridID); + MouseCoords = new EntityCoordinates(MouseCoords.EntityId, mouseLocal + new Vector2(pManager.PlacementOffset.X, pManager.PlacementOffset.Y)); } - public override bool IsValidPosition(GridCoordinates position) + public override bool IsValidPosition(EntityCoordinates position) { if (!RangeCheck(position)) { diff --git a/Robust.Client/Placement/Modes/AlignTileAny.cs b/Robust.Client/Placement/Modes/AlignTileAny.cs index 6687a05a6..6f64f1e77 100644 --- a/Robust.Client/Placement/Modes/AlignTileAny.cs +++ b/Robust.Client/Placement/Modes/AlignTileAny.cs @@ -1,5 +1,4 @@ -using System.CodeDom; -using Robust.Shared.Map; +using Robust.Shared.Map; using Robust.Shared.Maths; using Robust.Shared.Utility; @@ -18,7 +17,8 @@ namespace Robust.Client.Placement.Modes MouseCoords = ScreenToCursorGrid(mouseScreen); - var mapGrid = pManager.MapManager.GetGrid(MouseCoords.GridID); + var gridId = MouseCoords.GetGridId(pManager.EntityManager); + var mapGrid = pManager.MapManager.GetGrid(gridId); if (mapGrid.IsDefaultGrid) { @@ -59,7 +59,8 @@ namespace Robust.Client.Placement.Modes // move mouse one tile out along normal var newTilePos = tileCenterWorld + normal * closest.TileSize; - MouseCoords = new GridCoordinates(closest.WorldToLocal(newTilePos), closest.Index); + + MouseCoords = new EntityCoordinates(closest.GridEntityId, closest.WorldToLocal(newTilePos)); } //else free place } @@ -73,21 +74,19 @@ namespace Robust.Client.Placement.Modes { if(!mapGrid.IsDefaultGrid) { - MouseCoords = new GridCoordinates(CurrentTile.X + tileSize / 2, - CurrentTile.Y + tileSize / 2, - MouseCoords.GridID); + MouseCoords = new EntityCoordinates(MouseCoords.EntityId, (CurrentTile.X + tileSize / 2, + CurrentTile.Y + tileSize / 2)); } // else we don't modify coords } else { - MouseCoords = new GridCoordinates(CurrentTile.X + tileSize / 2 + pManager.PlacementOffset.X, - CurrentTile.Y + tileSize / 2 + pManager.PlacementOffset.Y, - MouseCoords.GridID); + MouseCoords = new EntityCoordinates(MouseCoords.EntityId, (CurrentTile.X + tileSize / 2 + pManager.PlacementOffset.X, + CurrentTile.Y + tileSize / 2 + pManager.PlacementOffset.Y)); } } - public override bool IsValidPosition(GridCoordinates position) + public override bool IsValidPosition(EntityCoordinates position) { if (!RangeCheck(position)) { diff --git a/Robust.Client/Placement/Modes/AlignTileDense.cs b/Robust.Client/Placement/Modes/AlignTileDense.cs index b2b32520f..8bee4ed2c 100644 --- a/Robust.Client/Placement/Modes/AlignTileDense.cs +++ b/Robust.Client/Placement/Modes/AlignTileDense.cs @@ -1,4 +1,6 @@ -using Robust.Shared.Map; +using Robust.Shared.Interfaces.GameObjects; +using Robust.Shared.IoC; +using Robust.Shared.Map; namespace Robust.Client.Placement.Modes { @@ -13,26 +15,25 @@ namespace Robust.Client.Placement.Modes { MouseCoords = ScreenToCursorGrid(mouseScreen); - var mapGrid = pManager.MapManager.GetGrid(MouseCoords.GridID); + var mapGrid = pManager.MapManager.GetGrid(MouseCoords.GetGridId(pManager.EntityManager)); CurrentTile = mapGrid.GetTileRef(MouseCoords); float tileSize = mapGrid.TileSize; //convert from ushort to float GridDistancing = tileSize; if (pManager.CurrentPermission!.IsTile) { - MouseCoords = new GridCoordinates(CurrentTile.X + tileSize / 2, - CurrentTile.Y + tileSize / 2, - MouseCoords.GridID); + MouseCoords = new EntityCoordinates(MouseCoords.EntityId, + (CurrentTile.X + tileSize / 2, CurrentTile.Y + tileSize / 2)); } else { - MouseCoords = new GridCoordinates(CurrentTile.X + tileSize / 2 + pManager.PlacementOffset.X, - CurrentTile.Y + tileSize / 2 + pManager.PlacementOffset.Y, - MouseCoords.GridID); + MouseCoords = new EntityCoordinates(MouseCoords.EntityId, + (CurrentTile.X + tileSize / 2 + pManager.PlacementOffset.X, + CurrentTile.Y + tileSize / 2 + pManager.PlacementOffset.Y)); } } - public override bool IsValidPosition(GridCoordinates position) + public override bool IsValidPosition(EntityCoordinates position) { if (!RangeCheck(position)) { diff --git a/Robust.Client/Placement/Modes/AlignTileEmpty.cs b/Robust.Client/Placement/Modes/AlignTileEmpty.cs index 9d43b2c92..63c1fa3a8 100644 --- a/Robust.Client/Placement/Modes/AlignTileEmpty.cs +++ b/Robust.Client/Placement/Modes/AlignTileEmpty.cs @@ -1,5 +1,7 @@ using Robust.Shared.IoC; using Robust.Client.Interfaces.GameObjects; +using Robust.Shared.Interfaces.GameObjects; +using Robust.Shared.Interfaces.Map; using Robust.Shared.Map; using Robust.Shared.Maths; @@ -16,35 +18,38 @@ namespace Robust.Client.Placement.Modes { MouseCoords = ScreenToCursorGrid(mouseScreen); - var mapGrid = pManager.MapManager.GetGrid(MouseCoords.GridID); + var mapGrid = pManager.MapManager.GetGrid(MouseCoords.GetGridId(pManager.EntityManager)); CurrentTile = mapGrid.GetTileRef(MouseCoords); float tileSize = mapGrid.TileSize; //convert from ushort to float GridDistancing = tileSize; if (pManager.CurrentPermission!.IsTile) { - MouseCoords = new GridCoordinates(CurrentTile.X + tileSize / 2, - CurrentTile.Y + tileSize / 2, - MouseCoords.GridID); + MouseCoords = new EntityCoordinates(MouseCoords.EntityId, + (CurrentTile.X + tileSize / 2, CurrentTile.Y + tileSize / 2)); } else { - MouseCoords = new GridCoordinates(CurrentTile.X + tileSize / 2 + pManager.PlacementOffset.X, - CurrentTile.Y + tileSize / 2 + pManager.PlacementOffset.Y, - MouseCoords.GridID); + MouseCoords = new EntityCoordinates(MouseCoords.EntityId, + (CurrentTile.X + tileSize / 2 + pManager.PlacementOffset.X, + CurrentTile.Y + tileSize / 2 + pManager.PlacementOffset.Y)); } } - public override bool IsValidPosition(GridCoordinates position) + public override bool IsValidPosition(EntityCoordinates position) { if (!RangeCheck(position)) { return false; } - var entitymanager = IoCManager.Resolve(); - return !(entitymanager.AnyEntitiesIntersecting(pManager.MapManager.GetGrid(MouseCoords.GridID).ParentMapId, - new Box2(new Vector2(CurrentTile.X, CurrentTile.Y), new Vector2(CurrentTile.X + 0.99f, CurrentTile.Y + 0.99f)))); + var gridId = MouseCoords.GetGridId(pManager.EntityManager); + var map = pManager.MapManager.GetGrid(gridId).ParentMapId; + var bottomLeft = new Vector2(CurrentTile.X, CurrentTile.Y); + var topRight = new Vector2(CurrentTile.X + 0.99f, CurrentTile.Y + 0.99f); + var box = new Box2(bottomLeft, topRight); + + return !pManager.EntityManager.AnyEntitiesIntersecting(map, box); } } } diff --git a/Robust.Client/Placement/Modes/AlignTileNonDense.cs b/Robust.Client/Placement/Modes/AlignTileNonDense.cs index e9241eaab..8a7dadb73 100644 --- a/Robust.Client/Placement/Modes/AlignTileNonDense.cs +++ b/Robust.Client/Placement/Modes/AlignTileNonDense.cs @@ -13,26 +13,26 @@ namespace Robust.Client.Placement.Modes { MouseCoords = ScreenToCursorGrid(mouseScreen); - var mapGrid = pManager.MapManager.GetGrid(MouseCoords.GridID); + var gridId = MouseCoords.GetGridId(pManager.EntityManager); + var mapGrid = pManager.MapManager.GetGrid(gridId); CurrentTile = mapGrid.GetTileRef(MouseCoords); float tileSize = mapGrid.TileSize; //convert from ushort to float GridDistancing = tileSize; if (pManager.CurrentPermission!.IsTile) { - MouseCoords = new GridCoordinates(CurrentTile.X + tileSize / 2, - CurrentTile.Y + tileSize / 2, - MouseCoords.GridID); + MouseCoords = new EntityCoordinates(MouseCoords.EntityId, + (CurrentTile.X + tileSize / 2, CurrentTile.Y + tileSize / 2)); } else { - MouseCoords = new GridCoordinates(CurrentTile.X + tileSize / 2 + pManager.PlacementOffset.X, - CurrentTile.Y + tileSize / 2 + pManager.PlacementOffset.Y, - MouseCoords.GridID); + MouseCoords = new EntityCoordinates(MouseCoords.EntityId, + (CurrentTile.X + tileSize / 2 + pManager.PlacementOffset.X, + CurrentTile.Y + tileSize / 2 + pManager.PlacementOffset.Y)); } } - public override bool IsValidPosition(GridCoordinates position) + public override bool IsValidPosition(EntityCoordinates position) { if (!RangeCheck(position)) { diff --git a/Robust.Client/Placement/Modes/AlignWall.cs b/Robust.Client/Placement/Modes/AlignWall.cs index 2e300b5cb..cdb73f904 100644 --- a/Robust.Client/Placement/Modes/AlignWall.cs +++ b/Robust.Client/Placement/Modes/AlignWall.cs @@ -12,7 +12,8 @@ namespace Robust.Client.Placement.Modes public override void AlignPlacementMode(ScreenCoordinates mouseScreen) { MouseCoords = ScreenToCursorGrid(mouseScreen); - CurrentTile = pManager.MapManager.GetGrid(MouseCoords.GridID).GetTileRef(MouseCoords); + var gridId = MouseCoords.GetGridId(pManager.EntityManager); + CurrentTile = pManager.MapManager.GetGrid(gridId).GetTileRef(MouseCoords); if (pManager.CurrentPermission!.IsTile) { @@ -38,12 +39,11 @@ namespace Robust.Client.Placement.Modes orderby (node - MouseCoords.Position).LengthSquared ascending select node).First(); - MouseCoords = new GridCoordinates(closestNode + new Vector2(pManager.PlacementOffset.X, - pManager.PlacementOffset.Y), - MouseCoords.GridID); + MouseCoords = new EntityCoordinates(MouseCoords.EntityId, + closestNode + (pManager.PlacementOffset.X, pManager.PlacementOffset.Y)); } - public override bool IsValidPosition(GridCoordinates position) + public override bool IsValidPosition(EntityCoordinates position) { if (pManager.CurrentPermission!.IsTile) { diff --git a/Robust.Client/Placement/Modes/AlignWallProper.cs b/Robust.Client/Placement/Modes/AlignWallProper.cs index 5eba753eb..29a08267b 100644 --- a/Robust.Client/Placement/Modes/AlignWallProper.cs +++ b/Robust.Client/Placement/Modes/AlignWallProper.cs @@ -16,7 +16,8 @@ namespace Robust.Client.Placement.Modes public override void AlignPlacementMode(ScreenCoordinates mouseScreen) { MouseCoords = ScreenToCursorGrid(mouseScreen); - var grid = pManager.MapManager.GetGrid(MouseCoords.GridID); + var gridId = MouseCoords.GetGridId(pManager.EntityManager); + var grid = pManager.MapManager.GetGrid(gridId); CurrentTile = grid.GetTileRef(MouseCoords); if (pManager.CurrentPermission!.IsTile) @@ -24,7 +25,7 @@ namespace Robust.Client.Placement.Modes return; } - var tileGridCoordinates = grid.GridTileToLocal(CurrentTile.GridIndices); + var tileCoordinates = grid.GridTileToLocal(CurrentTile.GridIndices); var offsets = new Vector2[] { @@ -34,13 +35,15 @@ namespace Robust.Client.Placement.Modes (-0.5f, 0f) }; - var closestNode = offsets.Select(o => tileGridCoordinates.Offset(o)) - .OrderBy(node => node.Distance(pManager.MapManager, MouseCoords)).First(); + var closestNode = offsets + .Select(o => tileCoordinates.Offset(o)) + .OrderBy(node => node.TryDistance(pManager.EntityManager, MouseCoords, out var distance) ? distance : (float?) null) + .First(f => f != null); MouseCoords = closestNode; } - public override bool IsValidPosition(GridCoordinates position) + public override bool IsValidPosition(EntityCoordinates position) { if (pManager.CurrentPermission!.IsTile) { diff --git a/Robust.Client/Placement/Modes/PlaceFree.cs b/Robust.Client/Placement/Modes/PlaceFree.cs index 54096da6f..fc5fe0e86 100644 --- a/Robust.Client/Placement/Modes/PlaceFree.cs +++ b/Robust.Client/Placement/Modes/PlaceFree.cs @@ -1,4 +1,6 @@ -using Robust.Shared.Map; +using Robust.Shared.Interfaces.GameObjects; +using Robust.Shared.IoC; +using Robust.Shared.Map; namespace Robust.Client.Placement.Modes { @@ -9,10 +11,11 @@ namespace Robust.Client.Placement.Modes public override void AlignPlacementMode(ScreenCoordinates mouseScreen) { MouseCoords = ScreenToCursorGrid(mouseScreen); - CurrentTile = pManager.MapManager.GetGrid(MouseCoords.GridID).GetTileRef(MouseCoords); + var gridId = MouseCoords.GetGridId(pManager.EntityManager); + CurrentTile = pManager.MapManager.GetGrid(gridId).GetTileRef(MouseCoords); } - public override bool IsValidPosition(GridCoordinates position) + public override bool IsValidPosition(EntityCoordinates position) { return true; } diff --git a/Robust.Client/Placement/Modes/PlaceNearby.cs b/Robust.Client/Placement/Modes/PlaceNearby.cs index 011f257d0..03d798e0f 100644 --- a/Robust.Client/Placement/Modes/PlaceNearby.cs +++ b/Robust.Client/Placement/Modes/PlaceNearby.cs @@ -1,4 +1,6 @@ -using Robust.Shared.Map; +using Robust.Shared.Interfaces.GameObjects; +using Robust.Shared.IoC; +using Robust.Shared.Map; namespace Robust.Client.Placement.Modes { @@ -11,10 +13,11 @@ namespace Robust.Client.Placement.Modes public override void AlignPlacementMode(ScreenCoordinates mouseScreen) { MouseCoords = ScreenToCursorGrid(mouseScreen); - CurrentTile = pManager.MapManager.GetGrid(MouseCoords.GridID).GetTileRef(MouseCoords); + var gridId = MouseCoords.GetGridId(pManager.EntityManager); + CurrentTile = pManager.MapManager.GetGrid(gridId).GetTileRef(MouseCoords); } - public override bool IsValidPosition(GridCoordinates position) + public override bool IsValidPosition(EntityCoordinates position) { if (pManager.CurrentPermission!.IsTile) { diff --git a/Robust.Client/Placement/PlacementHijack.cs b/Robust.Client/Placement/PlacementHijack.cs index 658809e65..f60368912 100644 --- a/Robust.Client/Placement/PlacementHijack.cs +++ b/Robust.Client/Placement/PlacementHijack.cs @@ -7,12 +7,12 @@ namespace Robust.Client.Placement { public PlacementManager Manager { get; internal set; } = default!; - public virtual bool HijackPlacementRequest(GridCoordinates coordinates) + public virtual bool HijackPlacementRequest(EntityCoordinates coordinates) { return false; } - public virtual bool HijackDeletion(GridCoordinates coordinates) + public virtual bool HijackDeletion(EntityCoordinates coordinates) { return false; } diff --git a/Robust.Client/Placement/PlacementManager.cs b/Robust.Client/Placement/PlacementManager.cs index 7278685f5..7e9584926 100644 --- a/Robust.Client/Placement/PlacementManager.cs +++ b/Robust.Client/Placement/PlacementManager.cs @@ -47,7 +47,7 @@ namespace Robust.Client.Placement [Dependency] public readonly IEyeManager eyeManager = default!; [Dependency] private readonly IInputManager _inputManager = default!; [Dependency] private readonly IEntitySystemManager _entitySystemManager = default!; - [Dependency] private readonly IEntityManager _entityManager = default!; + [Dependency] public readonly IEntityManager EntityManager = default!; [Dependency] private readonly IPrototypeManager _prototypeManager = default!; [Dependency] private readonly IBaseClient _baseClient = default!; [Dependency] private readonly IOverlayManager _overlayManager = default!; @@ -62,7 +62,7 @@ namespace Robust.Client.Placement /// Dictionary of all placement mode types /// private readonly Dictionary _modeDictionary = new Dictionary(); - private readonly List> _pendingTileChanges = new List>(); + private readonly List> _pendingTileChanges = new List>(); /// /// Tells this system to try to handle placement of an entity during the next frame @@ -77,7 +77,7 @@ namespace Robust.Client.Placement /// /// Holds the anchor that we can try to spawn in a line or a grid from /// - public GridCoordinates StartPoint { get; set; } + public EntityCoordinates StartPoint { get; set; } /// /// Whether the placement manager is currently in a mode where it accepts actions @@ -211,7 +211,7 @@ namespace Robust.Client.Placement return false; } - HandleDeletion(_entityManager.GetEntity(uid)); + HandleDeletion(EntityManager.GetEntity(uid)); } else { @@ -372,7 +372,7 @@ namespace Robust.Client.Placement } } - public bool HandleDeletion(GridCoordinates coordinates) + public bool HandleDeletion(EntityCoordinates coordinates) { if (!IsActive || !Eraser) return false; if (Hijack != null) @@ -566,16 +566,17 @@ namespace Robust.Client.Placement IsActive = true; } - private void RequestPlacement(GridCoordinates coordinates) + private void RequestPlacement(EntityCoordinates coordinates) { - if (MapManager.GetGrid(coordinates.GridID).ParentMapId == MapId.Nullspace) return; + var gridId = coordinates.GetGridId(EntityManager); + if (MapManager.GetGrid(gridId).ParentMapId == MapId.Nullspace) return; if (CurrentPermission == null) return; if (!CurrentMode!.IsValidPosition(coordinates)) return; if (Hijack != null && Hijack.HijackPlacementRequest(coordinates)) return; if (CurrentPermission.IsTile) { - var grid = MapManager.GetGrid(coordinates.GridID); + var grid = MapManager.GetGrid(gridId); // no point changing the tile to the same thing. if (grid.GetTileRef(coordinates).Tile.TypeId == CurrentPermission.TileType) @@ -588,7 +589,7 @@ namespace Robust.Client.Placement return; } - var tuple = new Tuple(coordinates, _time.RealTime + _pendingTileTimeout); + var tuple = new Tuple(coordinates, _time.RealTime + _pendingTileTimeout); _pendingTileChanges.Add(tuple); } @@ -604,7 +605,7 @@ namespace Robust.Client.Placement message.EntityTemplateName = CurrentPermission.EntityType; // world x and y - message.GridCoordinates = coordinates; + message.EntityCoordinates = coordinates; message.DirRcv = Direction; diff --git a/Robust.Client/Placement/PlacementMode.cs b/Robust.Client/Placement/PlacementMode.cs index 898769200..8010145ca 100644 --- a/Robust.Client/Placement/PlacementMode.cs +++ b/Robust.Client/Placement/PlacementMode.cs @@ -5,13 +5,15 @@ using Robust.Client.Graphics; using Robust.Client.Graphics.ClientEye; using Robust.Client.Graphics.Drawing; using Robust.Client.ResourceManagement; +using Robust.Shared.Interfaces.GameObjects; +using Robust.Shared.IoC; using Robust.Shared.Map; using Robust.Shared.Maths; using Robust.Shared.Utility; namespace Robust.Client.Placement { - abstract public class PlacementMode + public abstract class PlacementMode { public readonly PlacementManager pManager; @@ -23,7 +25,7 @@ namespace Robust.Client.Placement /// /// Local coordinates of our cursor on the map /// - public GridCoordinates MouseCoords { get; set; } + public EntityCoordinates MouseCoords { get; set; } /// /// Texture resource to draw to represent the entity we are tryign to spawn @@ -79,7 +81,7 @@ namespace Robust.Client.Placement /// /// /// - public abstract bool IsValidPosition(GridCoordinates position); + public abstract bool IsValidPosition(EntityCoordinates position); public virtual void Render(DrawingHandleWorld handle) { @@ -89,7 +91,7 @@ namespace Robust.Client.Placement DebugTools.AssertNotNull(SpriteToDraw); } - IEnumerable locationcollection; + IEnumerable locationcollection; switch (pManager.PlacementType) { case PlacementManager.PlacementTypes.None: @@ -109,21 +111,21 @@ namespace Robust.Client.Placement var size = SpriteToDraw!.Size; foreach (var coordinate in locationcollection) { - var worldPos = pManager.MapManager.GetGrid(coordinate.GridID).LocalToWorld(coordinate).Position; + var worldPos = coordinate.ToMapPos(pManager.EntityManager); var pos = worldPos - (size/(float)EyeManager.PixelsPerMeter) / 2f; var color = IsValidPosition(coordinate) ? ValidPlaceColor : InvalidPlaceColor; handle.DrawTexture(SpriteToDraw, pos, color); } } - public IEnumerable SingleCoordinate() + public IEnumerable SingleCoordinate() { yield return MouseCoords; } - public IEnumerable LineCoordinates() + public IEnumerable LineCoordinates() { - var (x, y) = MouseCoords.ToMapPos(pManager.MapManager) - pManager.StartPoint.ToMapPos(pManager.MapManager); + var (x, y) = MouseCoords.ToMapPos(pManager.EntityManager) - pManager.StartPoint.ToMapPos(pManager.EntityManager); float iterations; Vector2 distance; if (Math.Abs(x) > Math.Abs(y)) @@ -139,13 +141,14 @@ namespace Robust.Client.Placement for (var i = 0; i <= iterations; i++) { - yield return new GridCoordinates(pManager.StartPoint.Position + distance * i, pManager.StartPoint.GridID); + yield return new EntityCoordinates(pManager.StartPoint.EntityId, pManager.StartPoint.Position + distance * i); } } - public IEnumerable GridCoordinates() + // This name is a nice reminder of our origins. Never forget. + public IEnumerable GridCoordinates() { - var placementdiff = MouseCoords.ToMapPos(pManager.MapManager) - pManager.StartPoint.ToMapPos(pManager.MapManager); + var placementdiff = MouseCoords.ToMapPos(pManager.EntityManager) - pManager.StartPoint.ToMapPos(pManager.EntityManager); var distanceX = new Vector2(placementdiff.X > 0 ? 1 : -1, 0) * GridDistancing; var distanceY = new Vector2(0, placementdiff.Y > 0 ? 1 : -1) * GridDistancing; @@ -156,7 +159,7 @@ namespace Robust.Client.Placement { for (var y = 0; y <= iterationsY; y++) { - yield return new GridCoordinates(pManager.StartPoint.Position + distanceX * x + distanceY * y, pManager.StartPoint.GridID); + yield return new EntityCoordinates(pManager.StartPoint.EntityId, pManager.StartPoint.Position + distanceX * x + distanceY * y); } } } @@ -180,7 +183,7 @@ namespace Robust.Client.Placement /// Checks if the player is spawning within a certain range of his character if range is required on this mode /// /// - public bool RangeCheck(GridCoordinates coordinates) + public bool RangeCheck(EntityCoordinates coordinates) { if (!RangeRequired) return true; @@ -191,15 +194,15 @@ namespace Robust.Client.Placement } var range = pManager.CurrentPermission!.Range; - if (range > 0 && !pManager.PlayerManager.LocalPlayer.ControlledEntity.Transform.GridPosition.InRange(pManager.MapManager, coordinates, range)) + if (range > 0 && !pManager.PlayerManager.LocalPlayer.ControlledEntity.Transform.Coordinates.InRange(pManager.EntityManager, coordinates, range)) return false; return true; } - public bool IsColliding(GridCoordinates coordinates) + public bool IsColliding(EntityCoordinates coordinates) { var bounds = pManager.ColliderAABB; - var worldcoords = coordinates.ToMapPos(pManager.MapManager); + var worldcoords = coordinates.ToMapPos(pManager.EntityManager); var collisionbox = Box2.FromDimensions( bounds.Left + worldcoords.X, @@ -207,7 +210,7 @@ namespace Robust.Client.Placement bounds.Width, bounds.Height); - if (pManager.PhysicsManager.TryCollideRect(collisionbox, pManager.MapManager.GetGrid(coordinates.GridID).ParentMapId)) + if (pManager.PhysicsManager.TryCollideRect(collisionbox, pManager.MapManager.GetGrid(coordinates.GetGridId(pManager.EntityManager)).ParentMapId)) return true; return false; @@ -223,14 +226,15 @@ namespace Robust.Client.Placement return pManager.eyeManager.WorldToScreen(point); } - protected GridCoordinates ScreenToCursorGrid(ScreenCoordinates coords) + protected EntityCoordinates ScreenToCursorGrid(ScreenCoordinates coords) { var mapCoords = pManager.eyeManager.ScreenToMap(coords.Position); if (!pManager.MapManager.TryFindGridAt(mapCoords, out var grid)) { grid = pManager.MapManager.GetDefaultGrid(mapCoords.MapId); } - return grid.MapToGrid(mapCoords); + + return EntityCoordinates.FromMap(pManager.EntityManager, grid.GridEntityId, mapCoords); } } } diff --git a/Robust.Client/UserInterface/CustomControls/DebugCoordsPanel.cs b/Robust.Client/UserInterface/CustomControls/DebugCoordsPanel.cs index 8ff66fd89..bfcc53f69 100644 --- a/Robust.Client/UserInterface/CustomControls/DebugCoordsPanel.cs +++ b/Robust.Client/UserInterface/CustomControls/DebugCoordsPanel.cs @@ -8,6 +8,7 @@ using Robust.Client.Graphics.Drawing; using Robust.Client.Interfaces.Graphics; using Robust.Client.Interfaces.State; using Robust.Client.Player; +using Robust.Shared.GameObjects; using Robust.Shared.Interfaces.Map; using Robust.Shared.Timing; @@ -71,7 +72,7 @@ namespace Robust.Client.UserInterface.CustomControls var screenSize = _displayManager.ScreenSize; MapCoordinates mouseWorldMap; - GridCoordinates mouseGridPos; + EntityCoordinates mouseGridPos; TileRef tile; mouseWorldMap = eyeManager.ScreenToMap(mouseScreenPos); @@ -83,7 +84,7 @@ namespace Robust.Client.UserInterface.CustomControls } else { - mouseGridPos = new GridCoordinates(mouseWorldMap.Position, GridId.Invalid); + mouseGridPos = new EntityCoordinates(EntityUid.Invalid, mouseWorldMap.Position); tile = default; } diff --git a/Robust.Client/ViewVariables/Editors/ViewVariablesPropertyEditorGridLocalCoordinates.cs b/Robust.Client/ViewVariables/Editors/ViewVariablesPropertyEditorEntityCoordinates.cs similarity index 71% rename from Robust.Client/ViewVariables/Editors/ViewVariablesPropertyEditorGridLocalCoordinates.cs rename to Robust.Client/ViewVariables/Editors/ViewVariablesPropertyEditorEntityCoordinates.cs index 34f07db6a..d0e78d711 100644 --- a/Robust.Client/ViewVariables/Editors/ViewVariablesPropertyEditorGridLocalCoordinates.cs +++ b/Robust.Client/ViewVariables/Editors/ViewVariablesPropertyEditorEntityCoordinates.cs @@ -1,16 +1,20 @@ using System.Globalization; using Robust.Client.UserInterface; using Robust.Client.UserInterface.Controls; +using Robust.Shared.GameObjects; +using Robust.Shared.Interfaces.GameObjects; +using Robust.Shared.Interfaces.Map; +using Robust.Shared.IoC; using Robust.Shared.Map; using Robust.Shared.Maths; namespace Robust.Client.ViewVariables.Editors { - public class ViewVariablesPropertyEditorGridLocalCoordinates : ViewVariablesPropertyEditor + public class ViewVariablesPropertyEditorEntityCoordinates : ViewVariablesPropertyEditor { protected override Control MakeUI(object? value) { - var coords = (GridCoordinates) value!; + var coords = (EntityCoordinates) value!; var hBoxContainer = new HBoxContainer { CustomMinimumSize = new Vector2(240, 0), @@ -18,13 +22,15 @@ namespace Robust.Client.ViewVariables.Editors hBoxContainer.AddChild(new Label {Text = "grid: "}); + var entityManager = IoCManager.Resolve(); + var gridId = new LineEdit { Editable = !ReadOnly, SizeFlagsHorizontal = Control.SizeFlags.FillExpand, PlaceHolder = "Grid ID", ToolTip = "Grid ID", - Text = coords.GridID.ToString() + Text = coords.GetGridId(entityManager).ToString() }; hBoxContainer.AddChild(gridId); @@ -56,10 +62,17 @@ namespace Robust.Client.ViewVariables.Editors void OnEntered(LineEdit.LineEditEventArgs e) { var gridVal = int.Parse(gridId.Text, CultureInfo.InvariantCulture); + var mapManager = IoCManager.Resolve(); var xVal = float.Parse(x.Text, CultureInfo.InvariantCulture); var yVal = float.Parse(y.Text, CultureInfo.InvariantCulture); + + if (!mapManager.TryGetGrid(new GridId(gridVal), out var grid)) + { + ValueChanged(new EntityCoordinates(EntityUid.Invalid, (xVal, yVal))); + return; + } - ValueChanged(new GridCoordinates(xVal, yVal, new GridId(gridVal))); + ValueChanged(new EntityCoordinates(grid.GridEntityId, (xVal, yVal))); } if (!ReadOnly) diff --git a/Robust.Client/ViewVariables/ViewVariablesManager.cs b/Robust.Client/ViewVariables/ViewVariablesManager.cs index 8c100b9f5..d44842f3e 100644 --- a/Robust.Client/ViewVariables/ViewVariablesManager.cs +++ b/Robust.Client/ViewVariables/ViewVariablesManager.cs @@ -174,9 +174,9 @@ namespace Robust.Client.ViewVariables return new ViewVariablesPropertyEditorUIBox2(ViewVariablesPropertyEditorUIBox2.BoxType.UIBox2i); } - if (type == typeof(GridCoordinates)) + if (type == typeof(EntityCoordinates)) { - return new ViewVariablesPropertyEditorGridLocalCoordinates(); + return new ViewVariablesPropertyEditorEntityCoordinates(); } if (type == typeof(EntityUid)) diff --git a/Robust.Server/Console/Commands/MapCommands.cs b/Robust.Server/Console/Commands/MapCommands.cs index dea1fdf0e..fe2a4c568 100644 --- a/Robust.Server/Console/Commands/MapCommands.cs +++ b/Robust.Server/Console/Commands/MapCommands.cs @@ -5,6 +5,7 @@ using Robust.Server.Interfaces.Console; using Robust.Server.Interfaces.Maps; using Robust.Server.Interfaces.Player; using Robust.Server.Interfaces.Timing; +using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.Map; using Robust.Shared.IoC; using Robust.Shared.Map; @@ -228,9 +229,11 @@ namespace Robust.Server.Console.Commands if(player?.AttachedEntity == null) return; - var pos = player.AttachedEntity.Transform.GridPosition; + var pos = player.AttachedEntity.Transform.Coordinates; + var entityManager = IoCManager.Resolve(); + var gridId = pos.GetGridId(entityManager); - shell.SendText(player, $"MapID:{IoCManager.Resolve().GetGrid(pos.GridID).ParentMapId} GridID:{pos.GridID} X:{pos.X:N2} Y:{pos.Y:N2}"); + shell.SendText(player, $"MapID:{IoCManager.Resolve().GetGrid(gridId).ParentMapId} GridID:{gridId} X:{pos.X:N2} Y:{pos.Y:N2}"); } } diff --git a/Robust.Server/Console/Commands/PlayerCommands.cs b/Robust.Server/Console/Commands/PlayerCommands.cs index 019afaabf..61ccb144d 100644 --- a/Robust.Server/Console/Commands/PlayerCommands.cs +++ b/Robust.Server/Console/Commands/PlayerCommands.cs @@ -47,7 +47,7 @@ namespace Robust.Server.Console.Commands { var gridPos = grid.WorldToLocal(position); - transform.GridPosition = new GridCoordinates(gridPos, grid); + transform.Coordinates = new EntityCoordinates(grid.GridEntityId, gridPos); } else { diff --git a/Robust.Server/Console/Commands/SpawnCommand.cs b/Robust.Server/Console/Commands/SpawnCommand.cs index 4494ea396..7dd398fdd 100644 --- a/Robust.Server/Console/Commands/SpawnCommand.cs +++ b/Robust.Server/Console/Commands/SpawnCommand.cs @@ -17,7 +17,7 @@ namespace Robust.Server.Console.Commands var ent = IoCManager.Resolve(); if (player?.AttachedEntity != null) { - ent.SpawnEntity(args[0], player.AttachedEntity.Transform.GridPosition); + ent.SpawnEntity(args[0], player.AttachedEntity.Transform.Coordinates); } } } diff --git a/Robust.Server/GameObjects/EntitySystems/AudioSystem.cs b/Robust.Server/GameObjects/EntitySystems/AudioSystem.cs index e74f5bc0c..7a33ac2a5 100644 --- a/Robust.Server/GameObjects/EntitySystems/AudioSystem.cs +++ b/Robust.Server/GameObjects/EntitySystems/AudioSystem.cs @@ -113,7 +113,7 @@ namespace Robust.Server.GameObjects.EntitySystems var msg = new PlayAudioEntityMessage { FileName = filename, - Coordinates = entity.Transform.GridPosition, + Coordinates = entity.Transform.Coordinates, EntityUid = entity.Uid, AudioParams = audioParams ?? AudioParams.Default, Identifier = id, @@ -125,7 +125,7 @@ namespace Robust.Server.GameObjects.EntitySystems return new AudioSourceServer(this, id); } - var players = range > 0.0f ? _playerManager.GetPlayersInRange(entity.Transform.GridPosition, range) : _playerManager.GetAllPlayers(); + var players = range > 0.0f ? _playerManager.GetPlayersInRange(entity.Transform.Coordinates, range) : _playerManager.GetAllPlayers(); for (var i = players.Count - 1; i >= 0; i--) { @@ -150,7 +150,7 @@ namespace Robust.Server.GameObjects.EntitySystems /// /// The max range at which the audio will be heard. Less than or equal to 0 to send to every player. /// Session that won't receive the audio message. - public AudioSourceServer PlayAtCoords(string filename, GridCoordinates coordinates, AudioParams? audioParams = null, int range = AudioDistanceRange, IPlayerSession? excludedSession = null) + public AudioSourceServer PlayAtCoords(string filename, EntityCoordinates coordinates, AudioParams? audioParams = null, int range = AudioDistanceRange, IPlayerSession? excludedSession = null) { var id = CacheIdentifier(); var msg = new PlayAudioPositionalMessage @@ -216,7 +216,7 @@ namespace Robust.Server.GameObjects.EntitySystems /// The coordinates at which to play the audio. /// [Obsolete("Deprecated. Use PlayAtCoords instead.")] - public void Play(string filename, GridCoordinates coordinates, AudioParams? audioParams = null) + public void Play(string filename, EntityCoordinates coordinates, AudioParams? audioParams = null) { PlayAtCoords(filename, coordinates, audioParams); } diff --git a/Robust.Server/GameObjects/EntitySystems/TileLookup/GridTileLookupSystem.cs b/Robust.Server/GameObjects/EntitySystems/TileLookup/GridTileLookupSystem.cs index 904faa02e..cd198576f 100644 --- a/Robust.Server/GameObjects/EntitySystems/TileLookup/GridTileLookupSystem.cs +++ b/Robust.Server/GameObjects/EntitySystems/TileLookup/GridTileLookupSystem.cs @@ -150,11 +150,11 @@ namespace Robust.Server.GameObjects.EntitySystems.TileLookup return results; } - private HashSet GetOrCreateNodes(GridCoordinates coordinates, Box2 box) + private HashSet GetOrCreateNodes(EntityCoordinates coordinates, Box2 box) { var results = new HashSet(); - foreach (var grid in _mapManager.FindGridsIntersecting(_mapManager.GetGrid(coordinates.GridID).ParentMapId, box)) + foreach (var grid in _mapManager.FindGridsIntersecting(_mapManager.GetGrid(coordinates.GetGridId(EntityManager)).ParentMapId, box)) { foreach (var tile in grid.GetTilesIntersecting(box)) { @@ -210,7 +210,7 @@ namespace Robust.Server.GameObjects.EntitySystems.TileLookup return new Box2(collidableComponent.WorldAABB.BottomLeft + 0.01f, collidableComponent.WorldAABB.TopRight - 0.01f); // Don't want to accidentally get neighboring tiles unless we're near an edge - return Box2.CenteredAround(entity.Transform.GridPosition.Position, Vector2.One / 2); + return Box2.CenteredAround(entity.Transform.Coordinates.ToMapPos(EntityManager), Vector2.One / 2); } public override void Initialize() @@ -324,7 +324,7 @@ namespace Robust.Server.GameObjects.EntitySystems.TileLookup /// private void HandleEntityMove(MoveEvent moveEvent) { - if (moveEvent.Sender.Deleted || moveEvent.NewPosition.GridID == GridId.Invalid) + if (moveEvent.Sender.Deleted || moveEvent.NewPosition.GetGridId(EntityManager) == GridId.Invalid || !moveEvent.NewPosition.IsValid(EntityManager)) { HandleEntityRemove(moveEvent.Sender); return; diff --git a/Robust.Server/GameObjects/ServerEntityManager.cs b/Robust.Server/GameObjects/ServerEntityManager.cs index 857f0932c..64f1f963a 100644 --- a/Robust.Server/GameObjects/ServerEntityManager.cs +++ b/Robust.Server/GameObjects/ServerEntityManager.cs @@ -62,14 +62,14 @@ namespace Robust.Server.GameObjects } /// - public override IEntity CreateEntityUninitialized(string? prototypeName, GridCoordinates coordinates) + public override IEntity CreateEntityUninitialized(string? prototypeName, EntityCoordinates coordinates) { var newEntity = CreateEntityServer(prototypeName); - if (coordinates.GridID != GridId.Invalid) + + if (TryGetEntity(coordinates.EntityId, out var entity)) { - var gridEntityId = _mapManager.GetGrid(coordinates.GridID).GridEntityId; - newEntity.Transform.AttachParent(GetEntity(gridEntityId)); - newEntity.Transform.LocalPosition = coordinates.Position; + newEntity.Transform.AttachParent(entity); + newEntity.Transform.Coordinates = coordinates; } return newEntity; @@ -112,15 +112,16 @@ namespace Robust.Server.GameObjects } /// - public override IEntity SpawnEntity(string? protoName, GridCoordinates coordinates) + public override IEntity SpawnEntity(string? protoName, EntityCoordinates coordinates) { - if (coordinates.GridID == GridId.Invalid) - throw new InvalidOperationException($"Tried to spawn entity {protoName} onto invalid grid."); + if (!coordinates.IsValid(this)) + throw new InvalidOperationException($"Tried to spawn entity {protoName} on invalid coordinates {coordinates}."); var entity = CreateEntityUninitialized(protoName, coordinates); + InitializeAndStartEntity((Entity) entity); - var grid = _mapManager.GetGrid(coordinates.GridID); - if (_pauseManager.IsMapInitialized(grid.ParentMapId)) + + if (_pauseManager.IsMapInitialized(coordinates.GetMapId(this))) { entity.RunMapInit(); } @@ -137,7 +138,7 @@ namespace Robust.Server.GameObjects } /// - public override IEntity SpawnEntityNoMapInit(string? protoName, GridCoordinates coordinates) + public override IEntity SpawnEntityNoMapInit(string? protoName, EntityCoordinates coordinates) { var newEnt = CreateEntityUninitialized(protoName, coordinates); InitializeAndStartEntity((Entity) newEnt); diff --git a/Robust.Server/Interfaces/Player/IPlayerManager.cs b/Robust.Server/Interfaces/Player/IPlayerManager.cs index e591c4e07..70df73aed 100644 --- a/Robust.Server/Interfaces/Player/IPlayerManager.cs +++ b/Robust.Server/Interfaces/Player/IPlayerManager.cs @@ -66,7 +66,8 @@ namespace Robust.Server.Interfaces.Player IEnumerable GetAllPlayerData(); void DetachAll(); - List GetPlayersInRange(GridCoordinates worldPos, int range); + List GetPlayersInRange(MapCoordinates worldPos, int range); + List GetPlayersInRange(EntityCoordinates worldPos, int range); List GetPlayersBy(Func predicate); List GetAllPlayers(); List? GetPlayerStates(GameTick fromTick); diff --git a/Robust.Server/Placement/PlacementManager.cs b/Robust.Server/Placement/PlacementManager.cs index b2f407989..3cf75f8aa 100644 --- a/Robust.Server/Placement/PlacementManager.cs +++ b/Robust.Server/Placement/PlacementManager.cs @@ -86,7 +86,7 @@ namespace Robust.Server.Placement //TODO: Distance check, so you can't place things off of screen. - var coordinates = msg.GridCoordinates; + var coordinates = msg.EntityCoordinates; /* TODO: Redesign permission system, or document what this is supposed to be doing @@ -118,7 +118,7 @@ namespace Robust.Server.Placement } else { - var mapCoords = coordinates.ToMap(_mapManager); + var mapCoords = coordinates.ToMap(_entityManager); PlaceNewTile(tileType, mapCoords.MapId, mapCoords.Position); } } diff --git a/Robust.Server/Player/PlayerManager.cs b/Robust.Server/Player/PlayerManager.cs index 65a559100..63f5b8c0f 100644 --- a/Robust.Server/Player/PlayerManager.cs +++ b/Robust.Server/Player/PlayerManager.cs @@ -10,6 +10,7 @@ using Robust.Server.Interfaces.Player; using Robust.Shared.Enums; using Robust.Shared.GameStates; using Robust.Shared.Input; +using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.Map; using Robust.Shared.Interfaces.Network; using Robust.Shared.Interfaces.Reflection; @@ -37,6 +38,7 @@ namespace Robust.Server.Player [Dependency] private readonly IServerNetManager _network = default!; [Dependency] private readonly IReflectionManager _reflectionManager = default!; [Dependency] private readonly IMapManager _mapManager = default!; + [Dependency] private readonly IEntityManager _entityManager = default!; public BoundKeyMap KeyMap { get; private set; } = default!; @@ -223,15 +225,15 @@ namespace Robust.Server.Player /// Position of the circle in world-space. /// Radius of the circle in world units. /// - public List GetPlayersInRange(GridCoordinates worldPos, int range) + public List GetPlayersInRange(MapCoordinates worldPos, int range) { _sessionsLock.EnterReadLock(); //TODO: This needs to be moved to the PVS system. try { return - _sessions.Values.Where(x => x.AttachedEntity != null && - worldPos.InRange(_mapManager, x.AttachedEntity.Transform.GridPosition, range)) + _sessions.Values.Where(x => + x.AttachedEntity != null && worldPos.InRange(x.AttachedEntity.Transform.MapPosition, range)) .Cast() .ToList(); } @@ -240,6 +242,17 @@ namespace Robust.Server.Player _sessionsLock.ExitReadLock(); } } + + /// + /// Gets all players inside of a circle. + /// + /// Position of the circle in world-space. + /// Radius of the circle in world units. + /// + public List GetPlayersInRange(EntityCoordinates worldPos, int range) + { + return GetPlayersInRange(worldPos.ToMap(_entityManager), range); + } public List GetPlayersBy(Func predicate) { diff --git a/Robust.Shared.Scripting/ScriptGlobalsShared.cs b/Robust.Shared.Scripting/ScriptGlobalsShared.cs index 00e181089..de1db3a82 100644 --- a/Robust.Shared.Scripting/ScriptGlobalsShared.cs +++ b/Robust.Shared.Scripting/ScriptGlobalsShared.cs @@ -27,14 +27,19 @@ namespace Robust.Shared.Scripting public IEnumerable eprotos => prot.EnumeratePrototypes(); - public GridCoordinates gpos(double x, double y, int gridId) + public EntityCoordinates gpos(double x, double y, int gridId) { - return new GridCoordinates((float)x, (float)y, new GridId(gridId)); + return gpos(x, y, new GridId(gridId)); } - public GridCoordinates gpos(double x, double y, GridId gridId) + public EntityCoordinates gpos(double x, double y, GridId gridId) { - return new GridCoordinates((float)x, (float)y, gridId); + if (!map.TryGetGrid(gridId, out var grid)) + { + return new EntityCoordinates(EntityUid.Invalid, ((float) x, (float) y)); + } + + return new EntityCoordinates(grid.GridEntityId, ((float) x, (float) y)); } public EntityUid eid(int i) @@ -62,7 +67,7 @@ namespace Robust.Shared.Scripting return map.GetGrid(mapId); } - public IEntity spawn(string prototype, GridCoordinates position) + public IEntity spawn(string prototype, EntityCoordinates position) { return ent.SpawnEntity(prototype, position); } diff --git a/Robust.Shared/GameObjects/Components/Collidable/CollidableComponent.Physics.cs b/Robust.Shared/GameObjects/Components/Collidable/CollidableComponent.Physics.cs index 3ff4d546c..18b138a83 100644 --- a/Robust.Shared/GameObjects/Components/Collidable/CollidableComponent.Physics.cs +++ b/Robust.Shared/GameObjects/Components/Collidable/CollidableComponent.Physics.cs @@ -312,7 +312,7 @@ namespace Robust.Shared.GameObjects.Components /// public bool OnGround => Status == BodyStatus.OnGround && !IoCManager.Resolve() - .IsWeightless(Owner.Transform.GridPosition); + .IsWeightless(Owner.Transform.Coordinates); /// /// Whether or not the entity is anchored in place. diff --git a/Robust.Shared/GameObjects/Components/Transform/SnapGridComponent.cs b/Robust.Shared/GameObjects/Components/Transform/SnapGridComponent.cs index e6856bf15..b0773ea03 100644 --- a/Robust.Shared/GameObjects/Components/Transform/SnapGridComponent.cs +++ b/Robust.Shared/GameObjects/Components/Transform/SnapGridComponent.cs @@ -85,7 +85,7 @@ namespace Robust.Shared.GameObjects.Components.Transform return $"ofs/pos: {Offset}/{Position}"; } - public GridCoordinates DirectionToGrid(Direction direction) + public EntityCoordinates DirectionToGrid(Direction direction) { var ownerGrid = _mapManager.GetGrid(Owner.Transform.GridID); var grid = ownerGrid.GridTileToLocal(SnapGridPosAt(direction)); @@ -166,7 +166,7 @@ namespace Robust.Shared.GameObjects.Components.Transform IsSet = true; var oldPos = Position; - Position = grid.SnapGridCellFor(Owner.Transform.GridPosition, Offset); + Position = grid.SnapGridCellFor(Owner.Transform.Coordinates, Offset); _lastGrid = Owner.Transform.GridID; grid.AddToSnapGridCell(Position, Offset, this); diff --git a/Robust.Shared/GameObjects/Components/Transform/TransformComponent.cs b/Robust.Shared/GameObjects/Components/Transform/TransformComponent.cs index 98fdb1427..b34257e4f 100644 --- a/Robust.Shared/GameObjects/Components/Transform/TransformComponent.cs +++ b/Robust.Shared/GameObjects/Components/Transform/TransformComponent.cs @@ -181,71 +181,6 @@ namespace Robust.Shared.GameObjects.Components.Transform public bool IsMapTransform => !ContainerHelpers.IsInContainer(Owner); - /// - [ViewVariables(VVAccess.ReadWrite)] - public GridCoordinates GridPosition - { - get - { - if (_parent.IsValid()) - { - // transform _position from parent coords to world coords - var worldPos = Parent!.WorldMatrix.Transform(_localPosition); - return new GridCoordinates(worldPos, GridID); - } - else - { - return new GridCoordinates(_localPosition, GridID); - } - } - set - { - if (!_parent.IsValid()) - { - DebugTools.Assert("Tried to move root node."); - return; - } - - // grid coords to world coords - var worldCoords = value.ToMapPos(_mapManager); - - if (value.GridID != GridID) - { - if (value.GridID != GridId.Invalid) - { - var newGrid = _mapManager.GetGrid(value.GridID); - AttachParent(_entityManager.GetEntity(newGrid.GridEntityId)); - } - else - { - var map = _mapManager.GetMapEntity(MapID); - AttachParent(map); - } - } - - // world coords to parent coords - var newPos = Parent!.InvWorldMatrix.Transform(worldCoords); - - // float rounding error guard, if the offset is less than 1mm ignore it - if ((newPos - _localPosition).LengthSquared < 10.0E-3) - return; - - SetPosition(newPos); - - //TODO: This is a hack, look into WHY we can't call GridPosition before the comp is Running - if (Running) - { - RebuildMatrices(); - Owner.EntityManager.EventBus.RaiseEvent( - EventSource.Local, new MoveEvent(Owner, GridPosition, value)); - } - - Dirty(); - UpdateEntityTree(); - UpdatePhysicsTree(); - } - } - /// [ViewVariables(VVAccess.ReadWrite)] public Vector2 WorldPosition @@ -291,7 +226,7 @@ namespace Robust.Shared.GameObjects.Components.Transform } set { - var oldPosition = GridPosition; + var oldPosition = Coordinates; if (value.EntityId != _parent) { @@ -306,7 +241,7 @@ namespace Robust.Shared.GameObjects.Components.Transform { RebuildMatrices(); Owner.EntityManager.EventBus.RaiseEvent( - EventSource.Local, new MoveEvent(Owner, oldPosition, GridPosition)); + EventSource.Local, new MoveEvent(Owner, oldPosition, Coordinates)); } Dirty(); @@ -316,10 +251,7 @@ namespace Robust.Shared.GameObjects.Components.Transform } [ViewVariables(VVAccess.ReadWrite)] - public MapCoordinates MapPosition - { - get => new MapCoordinates(WorldPosition, MapID); - } + public MapCoordinates MapPosition => new MapCoordinates(WorldPosition, MapID); [ViewVariables(VVAccess.ReadWrite)] [Animatable] @@ -330,14 +262,14 @@ namespace Robust.Shared.GameObjects.Components.Transform { // Set _nextPosition to null to break any on-going lerps if this is done in a client side prediction. _nextPosition = null; - var oldGridPos = GridPosition; + var oldGridPos = Coordinates; SetPosition(value); RebuildMatrices(); Dirty(); UpdateEntityTree(); UpdatePhysicsTree(); Owner.EntityManager.EventBus.RaiseEvent( - EventSource.Local, new MoveEvent(Owner, oldGridPos, GridPosition)); + EventSource.Local, new MoveEvent(Owner, oldGridPos, Coordinates)); } } @@ -717,11 +649,11 @@ namespace Robust.Shared.GameObjects.Components.Transform if (_localPosition != newState.LocalPosition) { - var oldPos = GridPosition; + var oldPos = Coordinates; SetPosition(newState.LocalPosition); Owner.EntityManager.EventBus.RaiseEvent( - EventSource.Local, new MoveEvent(Owner, oldPos, GridPosition)); + EventSource.Local, new MoveEvent(Owner, oldPos, Coordinates)); rebuildMatrices = true; } @@ -808,7 +740,7 @@ namespace Robust.Shared.GameObjects.Components.Transform public string GetDebugString() { - return $"pos/rot/wpos/wrot: {GridPosition}/{LocalRotation}/{WorldPosition}/{WorldRotation}"; + return $"pos/rot/wpos/wrot: {Coordinates}/{LocalRotation}/{WorldPosition}/{WorldRotation}"; } private void ActivateLerp() @@ -861,7 +793,7 @@ namespace Robust.Shared.GameObjects.Components.Transform public class MoveEvent : EntitySystemMessage { - public MoveEvent(IEntity sender, GridCoordinates oldPos, GridCoordinates newPos) + public MoveEvent(IEntity sender, EntityCoordinates oldPos, EntityCoordinates newPos) { Sender = sender; OldPosition = oldPos; @@ -869,7 +801,7 @@ namespace Robust.Shared.GameObjects.Components.Transform } public IEntity Sender { get; } - public GridCoordinates OldPosition { get; } - public GridCoordinates NewPosition { get; } + public EntityCoordinates OldPosition { get; } + public EntityCoordinates NewPosition { get; } } } diff --git a/Robust.Shared/GameObjects/EntityManager.cs b/Robust.Shared/GameObjects/EntityManager.cs index 037345ade..fe6ecd6fe 100644 --- a/Robust.Shared/GameObjects/EntityManager.cs +++ b/Robust.Shared/GameObjects/EntityManager.cs @@ -101,19 +101,19 @@ namespace Robust.Shared.GameObjects public abstract IEntity CreateEntityUninitialized(string? prototypeName); /// - public abstract IEntity CreateEntityUninitialized(string? prototypeName, GridCoordinates coordinates); + public abstract IEntity CreateEntityUninitialized(string? prototypeName, EntityCoordinates coordinates); /// public abstract IEntity CreateEntityUninitialized(string? prototypeName, MapCoordinates coordinates); /// - public abstract IEntity SpawnEntity(string? protoName, GridCoordinates coordinates); + public abstract IEntity SpawnEntity(string? protoName, EntityCoordinates coordinates); /// public abstract IEntity SpawnEntity(string? protoName, MapCoordinates coordinates); /// - public abstract IEntity SpawnEntityNoMapInit(string? protoName, GridCoordinates coordinates); + public abstract IEntity SpawnEntityNoMapInit(string? protoName, EntityCoordinates coordinates); /// /// Returns an entity by id @@ -156,8 +156,8 @@ namespace Robust.Shared.GameObjects foreach (var entity in _entityTreesPerMap[mapId].Query(position, approximate)) { var transform = entity.Transform; - if (MathHelper.CloseTo(transform.GridPosition.X, position.X) && - MathHelper.CloseTo(transform.GridPosition.Y, position.Y)) + if (MathHelper.CloseTo(transform.Coordinates.X, position.X) && + MathHelper.CloseTo(transform.Coordinates.Y, position.Y)) { yield return entity; } @@ -424,9 +424,9 @@ namespace Robust.Shared.GameObjects } /// - public IEnumerable GetEntitiesIntersecting(GridCoordinates position, bool approximate = false) + public IEnumerable GetEntitiesIntersecting(EntityCoordinates position, bool approximate = false) { - var mapPos = position.ToMap(_mapManager); + var mapPos = position.ToMap(this); return GetEntitiesIntersecting(mapPos.MapId, mapPos.Position, approximate); } @@ -438,7 +438,7 @@ namespace Robust.Shared.GameObjects return GetEntitiesIntersecting(entity.Transform.MapID, component.WorldAABB, approximate); } - return GetEntitiesIntersecting(entity.Transform.GridPosition, approximate); + return GetEntitiesIntersecting(entity.Transform.Coordinates, approximate); } /// @@ -470,11 +470,12 @@ namespace Robust.Shared.GameObjects } /// - public IEnumerable GetEntitiesInRange(GridCoordinates position, float range, bool approximate = false) + public IEnumerable GetEntitiesInRange(EntityCoordinates position, float range, bool approximate = false) { - var aabb = new Box2(position.Position - new Vector2(range / 2, range / 2), - position.Position + new Vector2(range / 2, range / 2)); - return GetEntitiesIntersecting(_mapManager.GetGrid(position.GridID).ParentMapId, aabb, approximate); + var mapPosition = position.ToMapPos(this); + var aabb = new Box2(mapPosition - new Vector2(range / 2, range / 2), + mapPosition + new Vector2(range / 2, range / 2)); + return GetEntitiesIntersecting(_mapManager.GetGrid(position.GetGridId(this)).ParentMapId, aabb, approximate); } /// @@ -499,14 +500,14 @@ namespace Robust.Shared.GameObjects return GetEntitiesInRange(entity.Transform.MapID, component.WorldAABB, range, approximate); } - return GetEntitiesInRange(entity.Transform.GridPosition, range, approximate); + return GetEntitiesInRange(entity.Transform.Coordinates, range, approximate); } /// - public IEnumerable GetEntitiesInArc(GridCoordinates coordinates, float range, Angle direction, + public IEnumerable GetEntitiesInArc(EntityCoordinates coordinates, float range, Angle direction, float arcWidth, bool approximate = false) { - var position = coordinates.ToMap(_mapManager).Position; + var position = coordinates.ToMap(this).Position; foreach (var entity in GetEntitiesInRange(coordinates, range * 2, approximate)) { diff --git a/Robust.Shared/GameObjects/EntitySystemMessages/AudioMessages.cs b/Robust.Shared/GameObjects/EntitySystemMessages/AudioMessages.cs index 654ee8338..dbc9f7838 100644 --- a/Robust.Shared/GameObjects/EntitySystemMessages/AudioMessages.cs +++ b/Robust.Shared/GameObjects/EntitySystemMessages/AudioMessages.cs @@ -32,13 +32,13 @@ namespace Robust.Shared.GameObjects [Serializable, NetSerializable] public class PlayAudioPositionalMessage : AudioMessage { - public GridCoordinates Coordinates { get; set; } + public EntityCoordinates Coordinates { get; set; } } [Serializable, NetSerializable] public class PlayAudioEntityMessage : AudioMessage { - public GridCoordinates Coordinates { get; set; } + public EntityCoordinates Coordinates { get; set; } public EntityUid EntityUid { get; set; } } } diff --git a/Robust.Shared/GameObjects/EntitySystemMessages/EffectMessage.cs b/Robust.Shared/GameObjects/EntitySystemMessages/EffectMessage.cs index 0e7114c60..26232bcbc 100644 --- a/Robust.Shared/GameObjects/EntitySystemMessages/EffectMessage.cs +++ b/Robust.Shared/GameObjects/EntitySystemMessages/EffectMessage.cs @@ -38,12 +38,12 @@ namespace Robust.Shared.GameObjects.EntitySystemMessages /// /// Effect position relative to the emit position /// - public GridCoordinates Coordinates { get; set; } + public EntityCoordinates Coordinates { get; set; } /// /// Where the emitter was when the effect was first emitted /// - public GridCoordinates EmitterCoordinates { get; set; } + public EntityCoordinates EmitterCoordinates { get; set; } /// /// Effect's x/y velocity diff --git a/Robust.Shared/GameObjects/Systems/SharedPhysicsSystem.cs b/Robust.Shared/GameObjects/Systems/SharedPhysicsSystem.cs index 0a2404768..af9896a8b 100644 --- a/Robust.Shared/GameObjects/Systems/SharedPhysicsSystem.cs +++ b/Robust.Shared/GameObjects/Systems/SharedPhysicsSystem.cs @@ -341,8 +341,8 @@ namespace Robust.Shared.GameObjects.Systems return (0f, 0f); var location = body.Owner.Transform; - var grid = _mapManager.GetGrid(location.GridPosition.GridID); - var tile = grid.GetTileRef(location.GridPosition); + var grid = _mapManager.GetGrid(location.Coordinates.GetGridId(EntityManager)); + var tile = grid.GetTileRef(location.Coordinates); var tileDef = _tileDefinitionManager[tile.Tile.TypeId]; return (tileDef.Friction, grid.HasGravity ? 9.8f : 0f); } diff --git a/Robust.Shared/Input/Binding/InputCmdHandler.cs b/Robust.Shared/Input/Binding/InputCmdHandler.cs index 55171f85f..57cc737b4 100644 --- a/Robust.Shared/Input/Binding/InputCmdHandler.cs +++ b/Robust.Shared/Input/Binding/InputCmdHandler.cs @@ -72,7 +72,7 @@ namespace Robust.Shared.Input.Binding } } - public delegate bool PointerInputCmdDelegate(ICommonSession? session, GridCoordinates coords, EntityUid uid); + public delegate bool PointerInputCmdDelegate(ICommonSession? session, EntityCoordinates coords, EntityUid uid); public delegate bool PointerInputCmdDelegate2(in PointerInputCmdHandler.PointerInputCmdArgs args); @@ -117,13 +117,13 @@ namespace Robust.Shared.Input.Binding public readonly struct PointerInputCmdArgs { public readonly ICommonSession? Session; - public readonly GridCoordinates Coordinates; + public readonly EntityCoordinates Coordinates; public readonly ScreenCoordinates ScreenCoordinates; public readonly EntityUid EntityUid; public readonly BoundKeyState State; public readonly FullInputCmdMessage OriginalMessage; - public PointerInputCmdArgs(ICommonSession? session, GridCoordinates coordinates, + public PointerInputCmdArgs(ICommonSession? session, EntityCoordinates coordinates, ScreenCoordinates screenCoordinates, EntityUid entityUid, BoundKeyState state, FullInputCmdMessage originalMessage) { diff --git a/Robust.Shared/Input/InputCmdMessage.cs b/Robust.Shared/Input/InputCmdMessage.cs index 9948542c8..b368f9b9f 100644 --- a/Robust.Shared/Input/InputCmdMessage.cs +++ b/Robust.Shared/Input/InputCmdMessage.cs @@ -113,7 +113,7 @@ namespace Robust.Shared.Input /// /// Local Coordinates of the pointer when the command was created. /// - public GridCoordinates Coordinates { get; } + public EntityCoordinates Coordinates { get; } /// /// Entity that was under the pointer when the command was created (if any). @@ -126,7 +126,7 @@ namespace Robust.Shared.Input /// Client tick this was created. /// Function this command is changing. /// Local Coordinates of the pointer when the command was created. - public PointerInputCmdMessage(GameTick tick, ushort subTick, KeyFunctionId inputFunctionId, GridCoordinates coordinates) + public PointerInputCmdMessage(GameTick tick, ushort subTick, KeyFunctionId inputFunctionId, EntityCoordinates coordinates) : this(tick, subTick, inputFunctionId, coordinates, EntityUid.Invalid) { } /// @@ -136,7 +136,7 @@ namespace Robust.Shared.Input /// Function this command is changing. /// Local Coordinates of the pointer when the command was created. /// Entity that was under the pointer when the command was created. - public PointerInputCmdMessage(GameTick tick, ushort subTick, KeyFunctionId inputFunctionId, GridCoordinates coordinates, EntityUid uid) + public PointerInputCmdMessage(GameTick tick, ushort subTick, KeyFunctionId inputFunctionId, EntityCoordinates coordinates, EntityUid uid) : base(tick, subTick, inputFunctionId) { Coordinates = coordinates; @@ -158,7 +158,7 @@ namespace Robust.Shared.Input /// /// Local Coordinates of the pointer when the command was created. /// - public GridCoordinates Coordinates { get; } + public EntityCoordinates Coordinates { get; } /// /// Screen Coordinates of the pointer when the command was created. @@ -179,7 +179,7 @@ namespace Robust.Shared.Input /// New state of the Input Function. /// Local Coordinates of the pointer when the command was created. /// - public FullInputCmdMessage(GameTick tick, ushort subTick, int inputSequence, KeyFunctionId inputFunctionId, BoundKeyState state, GridCoordinates coordinates, ScreenCoordinates screenCoordinates) + public FullInputCmdMessage(GameTick tick, ushort subTick, int inputSequence, KeyFunctionId inputFunctionId, BoundKeyState state, EntityCoordinates coordinates, ScreenCoordinates screenCoordinates) : this(tick, subTick, inputFunctionId, state, coordinates, screenCoordinates, EntityUid.Invalid) { } /// @@ -191,7 +191,7 @@ namespace Robust.Shared.Input /// Local Coordinates of the pointer when the command was created. /// /// Entity that was under the pointer when the command was created. - public FullInputCmdMessage(GameTick tick, ushort subTick, KeyFunctionId inputFunctionId, BoundKeyState state, GridCoordinates coordinates, ScreenCoordinates screenCoordinates, EntityUid uid) + public FullInputCmdMessage(GameTick tick, ushort subTick, KeyFunctionId inputFunctionId, BoundKeyState state, EntityCoordinates coordinates, ScreenCoordinates screenCoordinates, EntityUid uid) : base(tick, subTick, inputFunctionId) { State = state; diff --git a/Robust.Shared/Interfaces/GameObjects/Components/ITransformComponent.cs b/Robust.Shared/Interfaces/GameObjects/Components/ITransformComponent.cs index 217a781ef..0707daee7 100644 --- a/Robust.Shared/Interfaces/GameObjects/Components/ITransformComponent.cs +++ b/Robust.Shared/Interfaces/GameObjects/Components/ITransformComponent.cs @@ -1,8 +1,6 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using JetBrains.Annotations; using Robust.Shared.Animations; -using Robust.Shared.Enums; using Robust.Shared.GameObjects; using Robust.Shared.Map; using Robust.Shared.Maths; @@ -22,11 +20,6 @@ namespace Robust.Shared.Interfaces.GameObjects.Components [Animatable] Vector2 LocalPosition { get; set; } - /// - /// Position offset of this entity relative to the grid it's on. - /// - GridCoordinates GridPosition { get; set; } - /// /// Position offset of this entity relative to its parent. /// diff --git a/Robust.Shared/Interfaces/GameObjects/IEntityManager.cs b/Robust.Shared/Interfaces/GameObjects/IEntityManager.cs index dcd4b8dda..7c4bd3758 100644 --- a/Robust.Shared/Interfaces/GameObjects/IEntityManager.cs +++ b/Robust.Shared/Interfaces/GameObjects/IEntityManager.cs @@ -33,7 +33,7 @@ namespace Robust.Shared.Interfaces.GameObjects IEntity CreateEntityUninitialized(string? prototypeName); - IEntity CreateEntityUninitialized(string? prototypeName, GridCoordinates coordinates); + IEntity CreateEntityUninitialized(string? prototypeName, EntityCoordinates coordinates); IEntity CreateEntityUninitialized(string? prototypeName, MapCoordinates coordinates); @@ -43,7 +43,7 @@ namespace Robust.Shared.Interfaces.GameObjects /// The prototype to clone. If this is null, the entity won't have a prototype. /// /// Newly created entity. - IEntity SpawnEntity(string? protoName, GridCoordinates coordinates); + IEntity SpawnEntity(string? protoName, EntityCoordinates coordinates); /// /// Spawns an entity at a specific position @@ -62,7 +62,7 @@ namespace Robust.Shared.Interfaces.GameObjects /// The prototype to clone. If this is null, the entity won't have a prototype. /// /// Newly created entity. - IEntity SpawnEntityNoMapInit(string? protoName, GridCoordinates coordinates); + IEntity SpawnEntityNoMapInit(string? protoName, EntityCoordinates coordinates); /// /// Returns an entity by id @@ -154,7 +154,7 @@ namespace Robust.Shared.Interfaces.GameObjects /// /// /// If true, will not recalculate precise entity AABBs, resulting in a perf increase. - IEnumerable GetEntitiesIntersecting(GridCoordinates position, bool approximate = false); + IEnumerable GetEntitiesIntersecting(EntityCoordinates position, bool approximate = false); /// /// Gets entities that intersect with this entity @@ -169,7 +169,7 @@ namespace Robust.Shared.Interfaces.GameObjects /// /// /// If true, will not recalculate precise entity AABBs, resulting in a perf increase. - IEnumerable GetEntitiesInRange(GridCoordinates position, float range, bool approximate = false); + IEnumerable GetEntitiesInRange(EntityCoordinates position, float range, bool approximate = false); /// /// Gets entities within a certain *square* range of this entity @@ -205,7 +205,7 @@ namespace Robust.Shared.Interfaces.GameObjects /// /// If true, will not recalculate precise entity AABBs, resulting in a perf increase. /// - IEnumerable GetEntitiesInArc(GridCoordinates coordinates, float range, Angle direction, float arcWidth, bool approximate = false); + IEnumerable GetEntitiesInArc(EntityCoordinates coordinates, float range, Angle direction, float arcWidth, bool approximate = false); #endregion diff --git a/Robust.Shared/Interfaces/Physics/IPhysicsManager.cs b/Robust.Shared/Interfaces/Physics/IPhysicsManager.cs index dd2637bfe..c711b5ee8 100644 --- a/Robust.Shared/Interfaces/Physics/IPhysicsManager.cs +++ b/Robust.Shared/Interfaces/Physics/IPhysicsManager.cs @@ -23,13 +23,12 @@ namespace Robust.Shared.Interfaces.Physics /// true if collides, false if not bool TryCollideRect(Box2 collider, MapId map); - /// /// Checks whether a certain grid position is weightless or not /// - /// + /// /// - bool IsWeightless(GridCoordinates gridPosition); + bool IsWeightless(EntityCoordinates coordinates); /// /// Get all entities colliding with a certain body. diff --git a/Robust.Shared/Map/Coordinates.cs b/Robust.Shared/Map/Coordinates.cs index 0f76eb2cb..436bf232d 100644 --- a/Robust.Shared/Map/Coordinates.cs +++ b/Robust.Shared/Map/Coordinates.cs @@ -5,216 +5,10 @@ using System; using System.Diagnostics.CodeAnalysis; using JetBrains.Annotations; using Robust.Shared.GameObjects; -using Robust.Shared.GameObjects.Components.Map; using Robust.Shared.Interfaces.GameObjects; namespace Robust.Shared.Map { - /// - /// Coordinates relative to a specific grid. - /// - [Obsolete("Use EntityCoordinates instead.")] - [PublicAPI] - [Serializable, NetSerializable] - public readonly struct GridCoordinates : IEquatable - { - /// - /// Map grid that this position is relative to. - /// - public readonly GridId GridID; - - /// - /// Local Position coordinates relative to the MapGrid. - /// - public readonly Vector2 Position; - - /// - /// Location on the X axis relative to the MapGrid. - /// - public float X => Position.X; - - /// - /// Location on the X axis relative to the MapGrid. - /// - public float Y => Position.Y; - - /// - /// A set of coordinates that is at the origin of an invalid grid. - /// This is also the values of an uninitialized struct. - /// - public static readonly GridCoordinates InvalidGrid = new GridCoordinates(0, 0, GridId.Invalid); - - /// - /// Constructs new grid local coordinates. - /// - /// Position relative to the grid. - /// Grid the position is relative to. - public GridCoordinates(Vector2 position, IMapGrid grid) - : this(position, grid.Index) { } - - /// - /// Constructs new grid local coordinates. - /// - /// Position relative to the grid. - /// ID of the Grid the position is relative to. - public GridCoordinates(Vector2 position, GridId gridId) - { - Position = position; - GridID = gridId; - } - - /// - /// Constructs new grid local coordinates. - /// - /// X axis of the position. - /// Y axis of the position. - /// Grid the position is relative to. - public GridCoordinates(float x, float y, IMapGrid grid) - : this(new Vector2(x, y), grid.Index) { } - - /// - /// Constructs new grid local coordinates. - /// - /// X axis of the position. - /// Y axis of the position. - /// ID of the Grid the position is relative to. - public GridCoordinates(float x, float y, GridId gridId) - : this(new Vector2(x, y), gridId) { } - - /// - /// Converts this set of coordinates to map coordinates. - /// - public MapCoordinates ToMap(IMapManager mapManager) - { - //TODO: Assert GridID is not invalid - - var grid = mapManager.GetGrid(GridID); - return new MapCoordinates(grid.LocalToWorld(Position), grid.ParentMapId); - } - - /// - /// Converts this set of coordinates to map coordinate position. - /// - public Vector2 ToMapPos(IMapManager mapManager) - { - //TODO: Assert GridID is not invalid - - return mapManager.GetGrid(GridID).LocalToWorld(Position); - } - - /// - /// Converts this set of coordinates to map indices. - /// - public MapIndices ToMapIndices(IMapManager mapManager) - { - return mapManager.GetGrid(GridID).GetTileRef(this).GridIndices; - } - - /// - /// Offsets the position by a given vector. - /// - public GridCoordinates Offset(Vector2 offset) - { - return new GridCoordinates(Position + offset, GridID); - } - - /// - /// Checks that these coordinates are within a certain distance of another set. - /// - /// Map manager containing the two GridIds. - /// Other set of coordinates to use. - /// maximum distance between the two sets of coordinates. - /// True if the two points are within a given range. - public bool InRange(IMapManager mapManager, GridCoordinates otherCoords, float range) - { - if (mapManager.GetGrid(otherCoords.GridID).ParentMapId != mapManager.GetGrid(GridID).ParentMapId) - { - return false; - } - - return ((otherCoords.ToMapPos(mapManager) - ToMapPos(mapManager)).LengthSquared < range * range); - } - - /// - /// Checks that these coordinates are within a certain distance of another set. - /// - /// Map manager containing the two GridIds. - /// Other set of coordinates to use. - /// maximum distance between the two sets of coordinates. - /// True if the two points are within a given range. - public bool InRange(IMapManager mapManager, GridCoordinates otherCoords, int range) - { - return InRange(mapManager, otherCoords, (float) range); - } - - /// - /// Calculates the distance between two GirdCoordinates. - /// - /// Map manager containing this GridId. - /// Other set of coordinates to use. - /// Distance between the two points. - public float Distance(IMapManager mapManager, GridCoordinates otherCoords) - { - return (ToMapPos(mapManager) - otherCoords.ToMapPos(mapManager)).Length; - } - - /// - /// Offsets the position by another vector. - /// - /// Vector to translate by. - /// Resulting translated coordinates. - public GridCoordinates Translated(Vector2 offset) - { - return new GridCoordinates(Position + offset, GridID); - } - - /// - public override string ToString() - { - return $"Grid={GridID}, X={Position.X:N2}, Y={Position.Y:N2}"; - } - - /// - public bool Equals(GridCoordinates other) - { - return GridID.Equals(other.GridID) && Position.Equals(other.Position); - } - - /// - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - return obj is GridCoordinates coords && Equals(coords); - } - - /// - public override int GetHashCode() - { - unchecked - { - var hashCode = GridID.GetHashCode(); - hashCode = (hashCode * 397) ^ Position.GetHashCode(); - return hashCode; - } - } - - /// - /// Tests for value equality between two LocalCoordinates. - /// - public static bool operator ==(GridCoordinates self, GridCoordinates other) - { - return self.Equals(other); - } - - /// - /// Tests for value inequality between two LocalCoordinates. - /// - public static bool operator !=(GridCoordinates self, GridCoordinates other) - { - return !self.Equals(other); - } - } - /// /// Contains the coordinates of a position on the rendering screen. /// @@ -579,48 +373,16 @@ namespace Robust.Shared.Map if(!IsValid(entityManager)) return new MapIndices(); - var gridCoords = ToGrid(entityManager, mapManager); + var gridId = GetGridId(entityManager); - if (gridCoords.GridID != GridId.Invalid) + if (gridId != GridId.Invalid) { - return mapManager.GetGrid(gridCoords.GridID).GetTileRef(gridCoords).GridIndices; + return mapManager.GetGrid(gridId).GetTileRef(this).GridIndices; } var (x, y) = ToMapPos(entityManager); - return new MapIndices((int)x, (int)y); - } - - /// - /// Converts a set of into a set of . - /// - /// Entity manager that contains the . - /// - public GridCoordinates ToGrid(IEntityManager entityManager, IMapManager mapManager) - { - if(!IsValid(entityManager)) - return GridCoordinates.InvalidGrid; - - var gridId = GetGridId(entityManager); - - if (gridId == GridId.Invalid) - return new GridCoordinates(ToMapPos(entityManager), GridId.Invalid); - - var pos = mapManager.GetGrid(GetGridId(entityManager)).WorldToLocal(ToMapPos(entityManager)); - return new GridCoordinates(pos, gridId); - - } - - /// - /// Creates a set of EntityCoordinates given some GridCoordinates. - /// - /// - /// - /// - public static EntityCoordinates FromGrid(IMapManager mapManager, GridCoordinates coordinates) - { - var grid = mapManager.GetGrid(coordinates.GridID); - return new EntityCoordinates(grid.GridEntityId, coordinates.Position); + return new MapIndices((int) x, (int) y); } /// @@ -634,6 +396,37 @@ namespace Robust.Shared.Map return new EntityCoordinates(EntityId, newPosition); } + /// + /// Returns a new set of EntityCoordinates local to a new entity. + /// + /// The Entity Manager holding this entity + /// The entity that the new coordinates will be local to + /// A new set of EntityCoordinates local to a new entity. + public EntityCoordinates WithEntityId(IEntityManager entityManager, EntityUid entityId) + { + if(!entityManager.TryGetEntity(entityId, out var entity)) + return new EntityCoordinates(entityId, Vector2.Zero); + + return WithEntityId(entity); + } + + /// + /// Returns a new set of EntityCoordinates local to a new entity. + /// + /// The entity that the new coordinates will be local to + /// A new set of EntityCoordinates local to a new entity. + public EntityCoordinates WithEntityId(IEntity entity) + { + var entityManager = entity.EntityManager; + var mapPos = ToMap(entity.EntityManager); + + if(!IsValid(entityManager) || entity.Transform.MapID != mapPos.MapId) + return new EntityCoordinates(entity.Uid, Vector2.Zero); + + var localPos = entity.Transform.InvWorldMatrix.Transform(mapPos.Position); + return new EntityCoordinates(entity.Uid, localPos); + } + /// /// Returns the Grid Id this entity is on. /// If none of the ancestors are a grid, returns grid instead. diff --git a/Robust.Shared/Map/IMapGrid.cs b/Robust.Shared/Map/IMapGrid.cs index 0f3a88962..97e684cf6 100644 --- a/Robust.Shared/Map/IMapGrid.cs +++ b/Robust.Shared/Map/IMapGrid.cs @@ -76,9 +76,9 @@ namespace Robust.Shared.Map /// /// Gets a tile a the given world coordinates. This will not create a new chunk. /// - /// The location of the tile in coordinates. + /// The location of the tile in coordinates. /// The tile at the world coordinates. - TileRef GetTileRef(GridCoordinates worldPos); + TileRef GetTileRef(EntityCoordinates coords); /// /// Gets a tile a the given grid indices. This will not create a new chunk. @@ -96,9 +96,9 @@ namespace Robust.Shared.Map /// /// Replaces a single tile inside of the grid. /// - /// + /// /// The tile to insert at the coordinates. - void SetTile(GridCoordinates worldPos, Tile tile); + void SetTile(EntityCoordinates coords, Tile tile); /// /// Modifies a single tile inside of the chunk. @@ -122,17 +122,17 @@ namespace Robust.Shared.Map #region SnapGridAccess - IEnumerable GetSnapGridCell(GridCoordinates worldPos, SnapGridOffset offset); + IEnumerable GetSnapGridCell(EntityCoordinates coords, SnapGridOffset offset); IEnumerable GetSnapGridCell(MapIndices pos, SnapGridOffset offset); - MapIndices SnapGridCellFor(GridCoordinates gridPos, SnapGridOffset offset); + MapIndices SnapGridCellFor(EntityCoordinates coords, SnapGridOffset offset); MapIndices SnapGridCellFor(MapCoordinates worldPos, SnapGridOffset offset); MapIndices SnapGridCellFor(Vector2 localPos, SnapGridOffset offset); void AddToSnapGridCell(MapIndices pos, SnapGridOffset offset, SnapGridComponent snap); - void AddToSnapGridCell(GridCoordinates worldPos, SnapGridOffset offset, SnapGridComponent snap); + void AddToSnapGridCell(EntityCoordinates coords, SnapGridOffset offset, SnapGridComponent snap); void RemoveFromSnapGridCell(MapIndices pos, SnapGridOffset offset, SnapGridComponent snap); - void RemoveFromSnapGridCell(GridCoordinates worldPos, SnapGridOffset offset, SnapGridComponent snap); + void RemoveFromSnapGridCell(EntityCoordinates coords, SnapGridOffset offset, SnapGridComponent snap); #endregion SnapGridAccess @@ -148,14 +148,7 @@ namespace Robust.Shared.Map /// /// Transforms map coordinates to grid coordinates. /// - GridCoordinates MapToGrid(MapCoordinates posWorld); - - /// - /// Transforms world-space coordinates from the local grid origin to the global origin. - /// - /// The world-space coordinates with local grid origin. - /// The world-space coordinates with global origin. - GridCoordinates LocalToWorld(GridCoordinates posLocal); + EntityCoordinates MapToGrid(MapCoordinates posWorld); /// /// Transforms local vectors into world space vectors @@ -177,7 +170,7 @@ namespace Robust.Shared.Map /// /// /// - GridCoordinates GridTileToLocal(MapIndices gridTile); + EntityCoordinates GridTileToLocal(MapIndices gridTile); /// /// Transforms grid-space tile indices to map coordinate position. @@ -207,7 +200,7 @@ namespace Robust.Shared.Map /// /// Transforms local grid coordinates to chunk indices. /// - MapIndices LocalToChunkIndices(GridCoordinates gridPos); + MapIndices LocalToChunkIndices(EntityCoordinates gridPos); #endregion Transforms diff --git a/Robust.Shared/Map/MapGrid.cs b/Robust.Shared/Map/MapGrid.cs index ff7f8be43..91a38b38e 100644 --- a/Robust.Shared/Map/MapGrid.cs +++ b/Robust.Shared/Map/MapGrid.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using Robust.Shared.GameObjects; using Robust.Shared.GameObjects.Components.Map; using Robust.Shared.GameObjects.Components.Transform; +using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Maths; using Robust.Shared.Timing; using Robust.Shared.Utility; @@ -55,6 +56,7 @@ namespace Robust.Shared.Map private readonly Dictionary _chunks = new Dictionary(); private readonly IMapManagerInternal _mapManager; + private readonly IEntityManager _entityManager; private bool _hasGravity; /// @@ -65,10 +67,11 @@ namespace Robust.Shared.Map /// The dimension of this square chunk. /// Distance in world units between the lines on the conceptual snap grid. /// Parent map identifier. - internal MapGrid(IMapManagerInternal mapManager, GridId gridIndex, ushort chunkSize, float snapSize, + internal MapGrid(IMapManagerInternal mapManager, IEntityManager entityManager, GridId gridIndex, ushort chunkSize, float snapSize, MapId parentMapId) { _mapManager = mapManager; + _entityManager = entityManager; Index = gridIndex; ChunkSize = chunkSize; SnapSize = snapSize; @@ -180,9 +183,9 @@ namespace Robust.Shared.Map #region TileAccess /// - public TileRef GetTileRef(GridCoordinates worldPos) + public TileRef GetTileRef(EntityCoordinates coords) { - return GetTileRef(WorldToTile(worldPos)); + return GetTileRef(CoordinatesToTile(coords)); } /// @@ -214,9 +217,9 @@ namespace Robust.Shared.Map } /// - public void SetTile(GridCoordinates worldPos, Tile tile) + public void SetTile(EntityCoordinates coords, Tile tile) { - var localTile = WorldToTile(worldPos); + var localTile = CoordinatesToTile(coords); SetTile(new MapIndices(localTile.X, localTile.Y), tile); } @@ -274,9 +277,24 @@ namespace Robust.Shared.Map { var aabb = new Box2(worldArea.Position.X - worldArea.Radius, worldArea.Position.Y - worldArea.Radius, worldArea.Position.X + worldArea.Radius, worldArea.Position.Y + worldArea.Radius); - foreach(var tile in GetTilesIntersecting(aabb, ignoreEmpty)) + foreach (var tile in GetTilesIntersecting(aabb, ignoreEmpty)) { - if (GridTileToLocal(tile.GridIndices).Distance(_mapManager, new GridCoordinates(worldArea.Position,tile.GridIndex)) <= worldArea.Radius) + var local = GridTileToLocal(tile.GridIndices); + var gridId = tile.GridIndex; + + if (!_mapManager.TryGetGrid(gridId, out var grid)) + { + continue; + } + + var to = new EntityCoordinates(grid.GridEntityId, worldArea.Position); + + if (!local.TryDistance(_entityManager, to, out var distance)) + { + continue; + } + + if (distance <= worldArea.Radius) { if (predicate == null || predicate(tile)) { @@ -326,9 +344,9 @@ namespace Robust.Shared.Map #region SnapGridAccess /// - public IEnumerable GetSnapGridCell(GridCoordinates worldPos, SnapGridOffset offset) + public IEnumerable GetSnapGridCell(EntityCoordinates coords, SnapGridOffset offset) { - return GetSnapGridCell(SnapGridCellFor(worldPos, offset), offset); + return GetSnapGridCell(SnapGridCellFor(coords, offset), offset); } /// @@ -339,11 +357,11 @@ namespace Robust.Shared.Map } /// - public MapIndices SnapGridCellFor(GridCoordinates gridPos, SnapGridOffset offset) + public MapIndices SnapGridCellFor(EntityCoordinates coords, SnapGridOffset offset) { - DebugTools.Assert(ParentMapId == _mapManager.GetGrid(gridPos.GridID).ParentMapId); + DebugTools.Assert(ParentMapId == _mapManager.GetGrid(coords.GetGridId(_entityManager)).ParentMapId); - var local = WorldToLocal(gridPos.ToMapPos(_mapManager)); + var local = WorldToLocal(coords.ToMapPos(_entityManager)); return SnapGridCellFor(local, offset); } @@ -376,9 +394,9 @@ namespace Robust.Shared.Map } /// - public void AddToSnapGridCell(GridCoordinates worldPos, SnapGridOffset offset, SnapGridComponent snap) + public void AddToSnapGridCell(EntityCoordinates coords, SnapGridOffset offset, SnapGridComponent snap) { - AddToSnapGridCell(SnapGridCellFor(worldPos, offset), offset, snap); + AddToSnapGridCell(SnapGridCellFor(coords, offset), offset, snap); } /// @@ -389,9 +407,9 @@ namespace Robust.Shared.Map } /// - public void RemoveFromSnapGridCell(GridCoordinates worldPos, SnapGridOffset offset, SnapGridComponent snap) + public void RemoveFromSnapGridCell(EntityCoordinates coords, SnapGridOffset offset, SnapGridComponent snap) { - RemoveFromSnapGridCell(SnapGridCellFor(worldPos, offset), offset, snap); + RemoveFromSnapGridCell(SnapGridCellFor(coords, offset), offset, snap); } private (IMapChunk, MapIndices) ChunkAndOffsetForTile(MapIndices pos) @@ -413,19 +431,17 @@ namespace Robust.Shared.Map } /// - public GridCoordinates MapToGrid(MapCoordinates posWorld) + public EntityCoordinates MapToGrid(MapCoordinates posWorld) { if(posWorld.MapId != ParentMapId) throw new ArgumentException($"Grid {Index} is on map {ParentMapId}, but coords are on map {posWorld.MapId}.", nameof(posWorld)); - return new GridCoordinates(WorldToLocal(posWorld.Position), Index); - } + if (!_mapManager.TryGetGrid(Index, out var grid)) + { + return new EntityCoordinates(EntityUid.Invalid, (posWorld.X, posWorld.Y)); + } - /// - public GridCoordinates LocalToWorld(GridCoordinates posLocal) - { - return new GridCoordinates(posLocal.Position + WorldPosition, - _mapManager.GetDefaultGridId(_mapManager.GetGrid(posLocal.GridID).ParentMapId)); + return new EntityCoordinates(grid.GridEntityId, WorldToLocal(posWorld.Position)); } /// @@ -443,11 +459,11 @@ namespace Robust.Shared.Map } /// - /// Transforms global world coordinates to tile indices relative to grid origin. + /// Transforms entity coordinates to tile indices relative to grid origin. /// - public MapIndices WorldToTile(GridCoordinates gridPos) + public MapIndices CoordinatesToTile(EntityCoordinates coords) { - var local = WorldToLocal(gridPos.ToMapPos(_mapManager)); + var local = WorldToLocal(coords.ToMapPos(_entityManager)); var x = (int)Math.Floor(local.X / TileSize); var y = (int)Math.Floor(local.Y / TileSize); return new MapIndices(x, y); @@ -456,9 +472,9 @@ namespace Robust.Shared.Map /// /// Transforms global world coordinates to chunk indices relative to grid origin. /// - public MapIndices LocalToChunkIndices(GridCoordinates gridPos) + public MapIndices LocalToChunkIndices(EntityCoordinates gridPos) { - var local = WorldToLocal(gridPos.ToMapPos(_mapManager)); + var local = WorldToLocal(gridPos.ToMapPos(_entityManager)); var x = (int)Math.Floor(local.X / (TileSize * ChunkSize)); var y = (int)Math.Floor(local.Y / (TileSize * ChunkSize)); return new MapIndices(x, y); @@ -489,9 +505,9 @@ namespace Robust.Shared.Map } /// - public GridCoordinates GridTileToLocal(MapIndices gridTile) + public EntityCoordinates GridTileToLocal(MapIndices gridTile) { - return new GridCoordinates(gridTile.X * TileSize + (TileSize / 2f), gridTile.Y * TileSize + (TileSize / 2f), this); + return new EntityCoordinates(GridEntityId, (gridTile.X * TileSize + (TileSize / 2f), gridTile.Y * TileSize + (TileSize / 2f))); } public Vector2 GridTileToWorldPos(MapIndices gridTile) diff --git a/Robust.Shared/Map/MapIndices.cs b/Robust.Shared/Map/MapIndices.cs index f1c5d3174..3275e6f1b 100644 --- a/Robust.Shared/Map/MapIndices.cs +++ b/Robust.Shared/Map/MapIndices.cs @@ -101,11 +101,12 @@ namespace Robust.Shared.Map return $"{{{X},{Y}}}"; } - public GridCoordinates ToGridCoordinates(IMapManager mapManager, GridId gridId) + public EntityCoordinates ToEntityCoordinates(IMapManager mapManager, GridId gridId) { - var tile = mapManager.GetGrid(gridId).TileSize; + var grid = mapManager.GetGrid(gridId); + var tile = grid.TileSize; - return new GridCoordinates(X * tile, Y * tile, gridId); + return new EntityCoordinates(grid.GridEntityId, (X * tile, Y * tile)); } /// diff --git a/Robust.Shared/Map/MapManager.cs b/Robust.Shared/Map/MapManager.cs index c02e371c7..7c71909a8 100644 --- a/Robust.Shared/Map/MapManager.cs +++ b/Robust.Shared/Map/MapManager.cs @@ -264,7 +264,7 @@ namespace Robust.Shared.Map } else { - var newEnt = (Entity) _entityManager.CreateEntityUninitialized(null, GridCoordinates.InvalidGrid); + var newEnt = (Entity) _entityManager.CreateEntityUninitialized(null, EntityCoordinates.Invalid); _mapEntities.Add(actualID, newEnt.Uid); var mapComp = newEnt.AddComponent(); @@ -442,7 +442,7 @@ namespace Robust.Shared.Map HighestGridID = actualID; } - var grid = new MapGrid(this, actualID, chunkSize, snapSize, currentMapID); + var grid = new MapGrid(this, _entityManager, actualID, chunkSize, snapSize, currentMapID); _grids.Add(actualID, grid); Logger.DebugS("map", $"Creating new grid {actualID}"); diff --git a/Robust.Shared/Network/Messages/MsgPlacement.cs b/Robust.Shared/Network/Messages/MsgPlacement.cs index a0ad2b36c..ff0833694 100644 --- a/Robust.Shared/Network/Messages/MsgPlacement.cs +++ b/Robust.Shared/Network/Messages/MsgPlacement.cs @@ -23,7 +23,7 @@ namespace Robust.Shared.Network.Messages public bool IsTile { get; set; } public ushort TileType { get; set; } public string EntityTemplateName { get; set; } - public GridCoordinates GridCoordinates { get; set; } + public EntityCoordinates EntityCoordinates { get; set; } public Direction DirRcv { get; set; } public EntityUid EntityUid { get; set; } @@ -43,7 +43,7 @@ namespace Robust.Shared.Network.Messages if (IsTile) TileType = buffer.ReadUInt16(); else EntityTemplateName = buffer.ReadString(); - GridCoordinates = buffer.ReadGridLocalCoordinates(); + EntityCoordinates = buffer.ReadEntityCoordinates(); DirRcv = (Direction)buffer.ReadByte(); break; case PlacementManagerMessage.StartPlacement: @@ -73,7 +73,7 @@ namespace Robust.Shared.Network.Messages if(IsTile) buffer.Write(TileType); else buffer.Write(EntityTemplateName); - buffer.Write(GridCoordinates); + buffer.Write(EntityCoordinates); buffer.Write((byte)DirRcv); break; case PlacementManagerMessage.StartPlacement: diff --git a/Robust.Shared/Network/NetMessageExt.cs b/Robust.Shared/Network/NetMessageExt.cs index 5ade8da34..9627038b7 100644 --- a/Robust.Shared/Network/NetMessageExt.cs +++ b/Robust.Shared/Network/NetMessageExt.cs @@ -8,17 +8,17 @@ namespace Robust.Shared.Network { public static class NetMessageExt { - public static GridCoordinates ReadGridLocalCoordinates(this NetIncomingMessage message) + public static EntityCoordinates ReadEntityCoordinates(this NetIncomingMessage message) { - var gridId = new GridId(message.ReadInt32()); + var entityUid = new EntityUid(message.ReadInt32()); var vector = message.ReadVector2(); - return new GridCoordinates(vector, gridId); + return new EntityCoordinates(entityUid, vector); } - public static void Write(this NetOutgoingMessage message, GridCoordinates coordinates) + public static void Write(this NetOutgoingMessage message, EntityCoordinates coordinates) { - message.Write(coordinates.GridID.Value); + message.Write(coordinates.EntityId); message.Write(coordinates.Position); } diff --git a/Robust.Shared/Physics/PhysicsManager.cs b/Robust.Shared/Physics/PhysicsManager.cs index 5904c1ba0..d4fc57ac8 100644 --- a/Robust.Shared/Physics/PhysicsManager.cs +++ b/Robust.Shared/Physics/PhysicsManager.cs @@ -17,6 +17,7 @@ namespace Robust.Shared.Physics public class PhysicsManager : IPhysicsManager { [Dependency] private readonly IMapManager _mapManager = default!; + [Dependency] private readonly IEntityManager _entityManager = default!; private readonly ConcurrentDictionary _treesPerMap = new ConcurrentDictionary(); @@ -44,13 +45,13 @@ namespace Robust.Shared.Physics return false; } - public bool IsWeightless(GridCoordinates gridPosition) + public bool IsWeightless(EntityCoordinates coordinates) { - var tile = _mapManager.GetGrid(gridPosition.GridID).GetTileRef(gridPosition).Tile; - return !_mapManager.GetGrid(gridPosition.GridID).HasGravity || tile.IsEmpty; + var gridId = coordinates.GetGridId(_entityManager); + var tile = _mapManager.GetGrid(gridId).GetTileRef(coordinates).Tile; + return !_mapManager.GetGrid(gridId).HasGravity || tile.IsEmpty; } - /// /// Calculates the normal vector for two colliding bodies /// diff --git a/Robust.UnitTesting/Client/GameObjects/Components/Transform_Test.cs b/Robust.UnitTesting/Client/GameObjects/Components/Transform_Test.cs index f7efb7ac3..aa2dd5a55 100644 --- a/Robust.UnitTesting/Client/GameObjects/Components/Transform_Test.cs +++ b/Robust.UnitTesting/Client/GameObjects/Components/Transform_Test.cs @@ -66,7 +66,7 @@ namespace Robust.UnitTesting.Client.GameObjects.Components public void ComponentStatePositionTest() { // Arrange - var initialPos = new GridCoordinates(0,0,new GridId(1)); + var initialPos = new EntityCoordinates(GridA.GridEntityId, (0, 0)); var parent = EntityManager.SpawnEntity("dummy", initialPos); var child = EntityManager.SpawnEntity("dummy", initialPos); var parentTrans = parent.Transform; @@ -96,7 +96,7 @@ namespace Robust.UnitTesting.Client.GameObjects.Components public void WorldRotationTest() { // Arrange - var initalPos = new GridCoordinates(0,0,new GridId(1)); + var initalPos = new EntityCoordinates(GridA.GridEntityId, (0, 0)); var node1 = EntityManager.SpawnEntity("dummy", initalPos); var node2 = EntityManager.SpawnEntity("dummy", initalPos); var node3 = EntityManager.SpawnEntity("dummy", initalPos); diff --git a/Robust.UnitTesting/Server/GameObjects/Components/Container_Test.cs b/Robust.UnitTesting/Server/GameObjects/Components/Container_Test.cs index ec1f220a2..a416e64a7 100644 --- a/Robust.UnitTesting/Server/GameObjects/Components/Container_Test.cs +++ b/Robust.UnitTesting/Server/GameObjects/Components/Container_Test.cs @@ -4,6 +4,7 @@ using NUnit.Framework; using Robust.Server.GameObjects.Components.Container; using Robust.Server.Interfaces.GameObjects; using Robust.Shared.Containers; +using Robust.Shared.GameObjects; using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects.Components; using Robust.Shared.Interfaces.Map; @@ -47,7 +48,7 @@ namespace Robust.UnitTesting.Server.GameObjects.Components [Test] public void TestCreation() { - var entity = EntityManager.SpawnEntity("dummy", new GridCoordinates(0,0,new GridId(1))); + var entity = EntityManager.SpawnEntity("dummy", new EntityCoordinates(new EntityUid(1), (0, 0))); var container = ContainerManagerComponent.Create("dummy", entity); @@ -84,8 +85,8 @@ namespace Robust.UnitTesting.Server.GameObjects.Components [Test] public void TestInsertion() { - var owner = EntityManager.SpawnEntity("dummy", new GridCoordinates(0,0,new GridId(1))); - var inserted = EntityManager.SpawnEntity("dummy", new GridCoordinates(0,0,new GridId(1))); + var owner = EntityManager.SpawnEntity("dummy", new EntityCoordinates(new EntityUid(1), (0, 0))); + var inserted = EntityManager.SpawnEntity("dummy", new EntityCoordinates(new EntityUid(1), (0, 0))); var transform = inserted.Transform; var container = ContainerManagerComponent.Create("dummy", owner); @@ -110,10 +111,10 @@ namespace Robust.UnitTesting.Server.GameObjects.Components [Test] public void TestNestedRemoval() { - var owner = EntityManager.SpawnEntity("dummy", new GridCoordinates(0, 0, new GridId(1))); - var inserted = EntityManager.SpawnEntity("dummy", new GridCoordinates(0, 0, new GridId(1))); + var owner = EntityManager.SpawnEntity("dummy", new EntityCoordinates(new EntityUid(1), (0, 0))); + var inserted = EntityManager.SpawnEntity("dummy", new EntityCoordinates(new EntityUid(1), (0, 0))); var transform = inserted.Transform; - var entity = EntityManager.SpawnEntity("dummy", new GridCoordinates(0, 0, new GridId(1))); + var entity = EntityManager.SpawnEntity("dummy", new EntityCoordinates(new EntityUid(1), (0, 0))); var container = ContainerManagerComponent.Create("dummy", owner); Assert.That(container.Insert(inserted), Is.True); @@ -134,10 +135,11 @@ namespace Robust.UnitTesting.Server.GameObjects.Components [Test] public void TestNestedRemovalWithDenial() { - var entityOne = EntityManager.SpawnEntity("dummy", new GridCoordinates(0, 0, new GridId(1))); - var entityTwo = EntityManager.SpawnEntity("dummy", new GridCoordinates(0, 0, new GridId(1))); - var entityThree = EntityManager.SpawnEntity("dummy", new GridCoordinates(0, 0, new GridId(1))); - var entityItem = EntityManager.SpawnEntity("dummy", new GridCoordinates(0, 0, new GridId(1))); + var coordinates = new EntityCoordinates(new EntityUid(1), (0, 0)); + var entityOne = EntityManager.SpawnEntity("dummy", coordinates); + var entityTwo = EntityManager.SpawnEntity("dummy", coordinates); + var entityThree = EntityManager.SpawnEntity("dummy", coordinates); + var entityItem = EntityManager.SpawnEntity("dummy", coordinates); var container = ContainerManagerComponent.Create("dummy", entityOne); var container2 = ContainerManagerComponent.Create("dummy", entityTwo); diff --git a/Robust.UnitTesting/Server/GameObjects/Components/Transform_Test.cs b/Robust.UnitTesting/Server/GameObjects/Components/Transform_Test.cs index 2edcf0778..1d786486b 100644 --- a/Robust.UnitTesting/Server/GameObjects/Components/Transform_Test.cs +++ b/Robust.UnitTesting/Server/GameObjects/Components/Transform_Test.cs @@ -2,6 +2,7 @@ using System.Reflection; using NUnit.Framework; using Robust.Server.Interfaces.GameObjects; +using Robust.Shared.GameObjects; using Robust.Shared.GameObjects.Components.Transform; using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.Map; @@ -43,7 +44,7 @@ namespace Robust.UnitTesting.Server.GameObjects.Components private MapId MapB; private IMapGrid GridB = default!; - private static readonly GridCoordinates InitialPos = new GridCoordinates(0,0,new GridId(1)); + private static readonly EntityCoordinates InitialPos = new EntityCoordinates(new EntityUid(1), (0, 0)); [OneTimeSetUp] public void Setup() @@ -90,8 +91,8 @@ namespace Robust.UnitTesting.Server.GameObjects.Components var childTrans = child.Transform; // that are not on the same map - parentTrans.GridPosition = new GridCoordinates(5, 5, GridA); - childTrans.GridPosition = new GridCoordinates(4, 4, GridB); + parentTrans.Coordinates = new EntityCoordinates(GridA.GridEntityId, (5, 5)); + childTrans.Coordinates = new EntityCoordinates(GridB.GridEntityId, (4, 4)); // if they are parented, the child keeps its world position, but moves to the parents map childTrans.AttachParent(parentTrans); @@ -101,18 +102,18 @@ namespace Robust.UnitTesting.Server.GameObjects.Components { Assert.That(childTrans.MapID, Is.EqualTo(parentTrans.MapID)); Assert.That(childTrans.GridID, Is.EqualTo(parentTrans.GridID)); - Assert.That(childTrans.GridPosition, Is.EqualTo(new GridCoordinates(4, 4, GridA))); + Assert.That(childTrans.Coordinates, Is.EqualTo(new EntityCoordinates(parentTrans.Owner.Uid, (-1, -1)))); Assert.That(childTrans.WorldPosition, Is.EqualTo(new Vector2(4, 4))); }); // move the parent, and the child should move with it childTrans.WorldPosition = new Vector2(6, 6); - parentTrans.WorldPosition += new Vector2(-7, -7); + parentTrans.WorldPosition += new Vector2(-8, -8); - Assert.That(childTrans.WorldPosition, Is.EqualTo(new Vector2(-1, -1))); + Assert.That(childTrans.WorldPosition, Is.EqualTo(new Vector2(-2, -2))); // if we detach parent, the child should be left where it was, still relative to parents grid - var oldLpos = childTrans.GridPosition; + var oldLpos = new Vector2(-2, -2); var oldWpos = childTrans.WorldPosition; childTrans.AttachToGridOrMap(); @@ -121,7 +122,7 @@ namespace Robust.UnitTesting.Server.GameObjects.Components Assert.Multiple(() => { - Assert.That(childTrans.GridPosition.Position, Is.EqualTo(oldLpos.Position)); + Assert.That(childTrans.Coordinates.Position, Is.EqualTo(oldLpos)); Assert.That(childTrans.WorldPosition, Is.EqualTo(oldWpos)); }); } diff --git a/Robust.UnitTesting/Shared/Map/EntityCoordinates_Tests.cs b/Robust.UnitTesting/Shared/Map/EntityCoordinates_Tests.cs index f05b657ab..7716022d2 100644 --- a/Robust.UnitTesting/Shared/Map/EntityCoordinates_Tests.cs +++ b/Robust.UnitTesting/Shared/Map/EntityCoordinates_Tests.cs @@ -157,7 +157,7 @@ namespace Robust.UnitTesting.Shared.Map var mapId = mapManager.CreateMap(); var grid = mapManager.CreateGrid(mapId); var gridEnt = entityManager.GetEntity(grid.GridEntityId); - var newEnt = entityManager.CreateEntityUninitialized("dummy", new GridCoordinates(Vector2.Zero, grid.Index)); + var newEnt = entityManager.CreateEntityUninitialized("dummy", new EntityCoordinates(grid.GridEntityId, Vector2.Zero)); // Grids aren't parented to other grids. Assert.That(gridEnt.Transform.Coordinates.GetGridId(entityManager), Is.EqualTo(GridId.Invalid)); @@ -188,7 +188,7 @@ namespace Robust.UnitTesting.Shared.Map var mapId = mapManager.CreateMap(); var grid = mapManager.CreateGrid(mapId); var gridEnt = entityManager.GetEntity(grid.GridEntityId); - var newEnt = entityManager.CreateEntityUninitialized("dummy", new GridCoordinates(Vector2.Zero, grid.Index)); + var newEnt = entityManager.CreateEntityUninitialized("dummy", new EntityCoordinates(grid.GridEntityId, Vector2.Zero)); Assert.That(gridEnt.Transform.Coordinates.GetMapId(entityManager), Is.EqualTo(mapId)); Assert.That(newEnt.Transform.Coordinates.GetMapId(entityManager), Is.EqualTo(mapId)); @@ -204,7 +204,7 @@ namespace Robust.UnitTesting.Shared.Map var grid = mapManager.CreateGrid(mapId); var mapEnt = mapManager.GetMapEntity(mapId); var gridEnt = entityManager.GetEntity(grid.GridEntityId); - var newEnt = entityManager.CreateEntityUninitialized("dummy", new GridCoordinates(Vector2.Zero, grid.Index)); + var newEnt = entityManager.CreateEntityUninitialized("dummy", new EntityCoordinates(grid.GridEntityId, Vector2.Zero)); Assert.That(mapEnt.Transform.Coordinates.GetParent(entityManager), Is.EqualTo(mapEnt)); Assert.That(gridEnt.Transform.Coordinates.GetParent(entityManager), Is.EqualTo(mapEnt)); @@ -226,7 +226,7 @@ namespace Robust.UnitTesting.Shared.Map var grid = mapManager.CreateGrid(mapId); var mapEnt = mapManager.GetMapEntity(mapId); var gridEnt = entityManager.GetEntity(grid.GridEntityId); - var newEnt = entityManager.CreateEntityUninitialized("dummy", new GridCoordinates(Vector2.Zero, grid.Index)); + var newEnt = entityManager.CreateEntityUninitialized("dummy", new EntityCoordinates(grid.GridEntityId, Vector2.Zero)); Assert.That(mapEnt.Transform.Coordinates.TryGetParent(entityManager, out var mapEntParent), Is.EqualTo(true)); Assert.That(mapEntParent, Is.EqualTo(mapEnt)); @@ -274,7 +274,7 @@ namespace Robust.UnitTesting.Shared.Map var mapId = mapManager.CreateMap(); var grid = mapManager.CreateGrid(mapId); var gridEnt = entityManager.GetEntity(grid.GridEntityId); - var newEnt = entityManager.CreateEntityUninitialized("dummy", new GridCoordinates(entPos, grid.Index)); + var newEnt = entityManager.CreateEntityUninitialized("dummy", new EntityCoordinates(grid.GridEntityId, entPos)); Assert.That(newEnt.Transform.Coordinates.ToMap(entityManager), Is.EqualTo(new MapCoordinates(entPos, mapId))); @@ -282,5 +282,42 @@ namespace Robust.UnitTesting.Shared.Map Assert.That(newEnt.Transform.Coordinates.ToMap(entityManager), Is.EqualTo(new MapCoordinates(entPos + gridPos, mapId))); } + + [Test] + public void WithEntityId() + { + var entityManager = IoCManager.Resolve(); + var mapManager = IoCManager.Resolve(); + + var mapId = mapManager.CreateMap(); + var mapEnt = mapManager.GetMapEntity(mapId); + var grid = mapManager.CreateGrid(mapId); + var gridEnt = entityManager.GetEntity(grid.GridEntityId); + var newEnt = entityManager.CreateEntityUninitialized("dummy", new EntityCoordinates(grid.GridEntityId, Vector2.Zero)); + + Assert.That(newEnt.Transform.Coordinates.WithEntityId(mapEnt).Position, Is.EqualTo(Vector2.Zero)); + + newEnt.Transform.LocalPosition = Vector2.One; + + Assert.That(newEnt.Transform.Coordinates.Position, Is.EqualTo(Vector2.One)); + Assert.That(newEnt.Transform.Coordinates.WithEntityId(mapEnt).Position, Is.EqualTo(Vector2.One)); + + gridEnt.Transform.LocalPosition = Vector2.One; + + Assert.That(newEnt.Transform.Coordinates.Position, Is.EqualTo(Vector2.One)); + Assert.That(newEnt.Transform.Coordinates.WithEntityId(mapEnt).Position, Is.EqualTo(new Vector2(2, 2))); + + var newEntTwo = entityManager.CreateEntityUninitialized("dummy", new EntityCoordinates(newEnt.Uid, Vector2.Zero)); + + Assert.That(newEntTwo.Transform.Coordinates.Position, Is.EqualTo(Vector2.Zero)); + Assert.That(newEntTwo.Transform.Coordinates.WithEntityId(mapEnt).Position, Is.EqualTo(newEnt.Transform.Coordinates.WithEntityId(mapEnt).Position)); + Assert.That(newEntTwo.Transform.Coordinates.WithEntityId(gridEnt).Position, Is.EqualTo(newEnt.Transform.Coordinates.Position)); + + newEntTwo.Transform.LocalPosition = -Vector2.One; + + Assert.That(newEntTwo.Transform.Coordinates.Position, Is.EqualTo(-Vector2.One)); + Assert.That(newEntTwo.Transform.Coordinates.WithEntityId(mapEnt).Position, Is.EqualTo(Vector2.One)); + Assert.That(newEntTwo.Transform.Coordinates.WithEntityId(gridEnt).Position, Is.EqualTo(Vector2.Zero)); + } } }