SnapGridComponent Removal (#3884)

* Removed SnapGridOffset, there is only center now.

* SnapGridComponent methods are now static.

* Removed SnapGridComponent.OnPositionChanged.

* Refactored static functions off SnapGridComponent to MapGrid.
Refactored away usages of SnapGridComponent.Position.

* Added Transform.Anchored for checking if an entity is a tile entity.
More refactoring for static MapGrid functions.

* Static snapgrid methods on MapGrid are no longer static.

* Add setter to ITransformComponent.Anchored.
Removed direct references to SnapGridComponent from content.

* Grid functions now deal with EntityUids instead of SnapGridComponents.
Began renaming public API functions from SnapGrid to Anchor.

* Remove the SnapGridComponent 'Offset' field from all yaml files. This was removed in code previously, so the yaml linter was upset.

* Update engine submodule to v0.4.46.
This commit is contained in:
Acruid
2021-04-28 10:49:37 -07:00
committed by GitHub
parent 578b767791
commit 00e01d51fd
74 changed files with 306 additions and 309 deletions

View File

@@ -1,12 +1,14 @@
using System;
using System;
using System.Collections.Generic;
using Content.Client.GameObjects.EntitySystems;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
using static Robust.Client.GameObjects.SpriteComponent;
namespace Content.Client.GameObjects.Components.IconSmoothing
@@ -25,6 +27,8 @@ namespace Content.Client.GameObjects.Components.IconSmoothing
[RegisterComponent]
public class IconSmoothComponent : Component
{
[Dependency] private readonly IMapManager _mapManager = default!;
[DataField("mode")]
private IconSmoothingMode _mode = IconSmoothingMode.Corners;
@@ -32,8 +36,6 @@ namespace Content.Client.GameObjects.Components.IconSmoothing
internal ISpriteComponent? Sprite { get; private set; }
internal SnapGridComponent? SnapGrid { get; private set; }
private (GridId, Vector2i) _lastPosition;
/// <summary>
@@ -62,7 +64,6 @@ namespace Content.Client.GameObjects.Components.IconSmoothing
{
base.Initialize();
SnapGrid = Owner.GetComponent<SnapGridComponent>();
Sprite = Owner.GetComponent<ISpriteComponent>();
}
@@ -71,15 +72,14 @@ namespace Content.Client.GameObjects.Components.IconSmoothing
{
base.Startup();
if (SnapGrid != null)
if (Owner.Transform.Anchored)
{
SnapGrid.OnPositionChanged += SnapGridOnPositionChanged;
// ensures lastposition initial value is populated on spawn. Just calling
// the hook here would cause a dirty event to fire needlessly
_lastPosition = (Owner.Transform.GridID, SnapGrid.Position);
var grid = _mapManager.GetGrid(Owner.Transform.GridID);
_lastPosition = (Owner.Transform.GridID, grid.TileIndicesFor(Owner.Transform.Coordinates));
Owner.EntityManager.EventBus.RaiseEvent(EventSource.Local, new IconSmoothDirtyEvent(Owner,null, SnapGrid.Offset, Mode));
Owner.EntityManager.EventBus.RaiseEvent(EventSource.Local, new IconSmoothDirtyEvent(Owner, null, Mode));
}
if (Sprite != null && Mode == IconSmoothingMode.Corners)
@@ -115,20 +115,22 @@ namespace Content.Client.GameObjects.Components.IconSmoothing
private void CalculateNewSpriteCardinal()
{
if (SnapGrid == null || Sprite == null)
if (!Owner.Transform.Anchored || Sprite == null)
{
return;
}
var dirs = CardinalConnectDirs.None;
if (MatchingEntity(SnapGrid.GetInDir(Direction.North)))
var grid = _mapManager.GetGrid(Owner.Transform.GridID);
var position = Owner.Transform.Coordinates;
if (MatchingEntity(grid.GetInDir(position, Direction.North)))
dirs |= CardinalConnectDirs.North;
if (MatchingEntity(SnapGrid.GetInDir(Direction.South)))
if (MatchingEntity(grid.GetInDir(position, Direction.South)))
dirs |= CardinalConnectDirs.South;
if (MatchingEntity(SnapGrid.GetInDir(Direction.East)))
if (MatchingEntity(grid.GetInDir(position, Direction.East)))
dirs |= CardinalConnectDirs.East;
if (MatchingEntity(SnapGrid.GetInDir(Direction.West)))
if (MatchingEntity(grid.GetInDir(position, Direction.West)))
dirs |= CardinalConnectDirs.West;
Sprite.LayerSetState(0, $"{StateBase}{(int) dirs}");
@@ -151,19 +153,21 @@ namespace Content.Client.GameObjects.Components.IconSmoothing
protected (CornerFill ne, CornerFill nw, CornerFill sw, CornerFill se) CalculateCornerFill()
{
if (SnapGrid == null)
if (!Owner.Transform.Anchored)
{
return (CornerFill.None, CornerFill.None, CornerFill.None, CornerFill.None);
}
var n = MatchingEntity(SnapGrid.GetInDir(Direction.North));
var ne = MatchingEntity(SnapGrid.GetInDir(Direction.NorthEast));
var e = MatchingEntity(SnapGrid.GetInDir(Direction.East));
var se = MatchingEntity(SnapGrid.GetInDir(Direction.SouthEast));
var s = MatchingEntity(SnapGrid.GetInDir(Direction.South));
var sw = MatchingEntity(SnapGrid.GetInDir(Direction.SouthWest));
var w = MatchingEntity(SnapGrid.GetInDir(Direction.West));
var nw = MatchingEntity(SnapGrid.GetInDir(Direction.NorthWest));
var grid = _mapManager.GetGrid(Owner.Transform.GridID);
var position = Owner.Transform.Coordinates;
var n = MatchingEntity(grid.GetInDir(position, Direction.North));
var ne = MatchingEntity(grid.GetInDir(position, Direction.NorthEast));
var e = MatchingEntity(grid.GetInDir(position, Direction.East));
var se = MatchingEntity(grid.GetInDir(position, Direction.SouthEast));
var s = MatchingEntity(grid.GetInDir(position, Direction.South));
var sw = MatchingEntity(grid.GetInDir(position, Direction.SouthWest));
var w = MatchingEntity(grid.GetInDir(position, Direction.West));
var nw = MatchingEntity(grid.GetInDir(position, Direction.NorthWest));
// ReSharper disable InconsistentNaming
var cornerNE = CornerFill.None;
@@ -234,28 +238,28 @@ namespace Content.Client.GameObjects.Components.IconSmoothing
{
base.Shutdown();
if (SnapGrid != null)
if (Owner.Transform.Anchored)
{
SnapGrid.OnPositionChanged -= SnapGridOnPositionChanged;
Owner.EntityManager.EventBus.RaiseEvent(EventSource.Local, new IconSmoothDirtyEvent(Owner, _lastPosition, SnapGrid.Offset, Mode));
Owner.EntityManager.EventBus.RaiseEvent(EventSource.Local, new IconSmoothDirtyEvent(Owner, _lastPosition, Mode));
}
}
private void SnapGridOnPositionChanged()
public void SnapGridOnPositionChanged()
{
if (SnapGrid != null)
if (Owner.Transform.Anchored)
{
Owner.EntityManager.EventBus.RaiseEvent(EventSource.Local, new IconSmoothDirtyEvent(Owner, _lastPosition, SnapGrid.Offset, Mode));
_lastPosition = (Owner.Transform.GridID, SnapGrid.Position);
Owner.EntityManager.EventBus.RaiseEvent(EventSource.Local, new IconSmoothDirtyEvent(Owner, _lastPosition, Mode));
var grid = _mapManager.GetGrid(Owner.Transform.GridID);
_lastPosition = (Owner.Transform.GridID, grid.TileIndicesFor(Owner.Transform.Coordinates));
}
}
[System.Diagnostics.Contracts.Pure]
protected bool MatchingEntity(IEnumerable<IEntity> candidates)
protected bool MatchingEntity(IEnumerable<EntityUid> candidates)
{
foreach (var entity in candidates)
{
if (!entity.TryGetComponent(out IconSmoothComponent? other))
if (!Owner.EntityManager.ComponentManager.TryGetComponent(entity, out IconSmoothComponent? other))
{
continue;
}

View File

@@ -1,10 +1,13 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using Content.Client.GameObjects.Components.IconSmoothing;
using Robust.Client.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
using static Robust.Client.GameObjects.SpriteComponent;
@@ -23,6 +26,8 @@ namespace Content.Client.GameObjects.Components
{
public override string Name => "LowWall";
[Dependency] private readonly IMapManager _mapManager = default!;
public CornerFill LastCornerNE { get; private set; }
public CornerFill LastCornerSE { get; private set; }
public CornerFill LastCornerSW { get; private set; }
@@ -65,19 +70,22 @@ namespace Content.Client.GameObjects.Components
{
base.CalculateNewSprite();
if (Sprite == null || SnapGrid == null || _overlaySprite == null)
if (Sprite == null || !Owner.Transform.Anchored || _overlaySprite == null)
{
return;
}
var (n, nl) = MatchingWall(SnapGrid.GetInDir(Direction.North));
var (ne, nel) = MatchingWall(SnapGrid.GetInDir(Direction.NorthEast));
var (e, el) = MatchingWall(SnapGrid.GetInDir(Direction.East));
var (se, sel) = MatchingWall(SnapGrid.GetInDir(Direction.SouthEast));
var (s, sl) = MatchingWall(SnapGrid.GetInDir(Direction.South));
var (sw, swl) = MatchingWall(SnapGrid.GetInDir(Direction.SouthWest));
var (w, wl) = MatchingWall(SnapGrid.GetInDir(Direction.West));
var (nw, nwl) = MatchingWall(SnapGrid.GetInDir(Direction.NorthWest));
var grid = _mapManager.GetGrid(Owner.Transform.GridID);
var coords = Owner.Transform.Coordinates;
var (n, nl) = MatchingWall(grid.GetInDir(coords, Direction.North));
var (ne, nel) = MatchingWall(grid.GetInDir(coords, Direction.NorthEast));
var (e, el) = MatchingWall(grid.GetInDir(coords, Direction.East));
var (se, sel) = MatchingWall(grid.GetInDir(coords, Direction.SouthEast));
var (s, sl) = MatchingWall(grid.GetInDir(coords, Direction.South));
var (sw, swl) = MatchingWall(grid.GetInDir(coords, Direction.SouthWest));
var (w, wl) = MatchingWall(grid.GetInDir(coords, Direction.West));
var (nw, nwl) = MatchingWall(grid.GetInDir(coords, Direction.NorthWest));
// ReSharper disable InconsistentNaming
var cornerNE = CornerFill.None;
@@ -194,9 +202,9 @@ namespace Content.Client.GameObjects.Components
LastCornerSW = cornerSW;
LastCornerNW = cornerNW;
foreach (var entity in SnapGrid.GetLocal())
foreach (var entity in grid.GetLocal(coords))
{
if (entity.TryGetComponent(out WindowComponent? window))
if (Owner.EntityManager.ComponentManager.TryGetComponent(entity, out WindowComponent? window))
{
window.UpdateSprite();
}
@@ -204,11 +212,11 @@ namespace Content.Client.GameObjects.Components
}
[Pure]
private (bool connected, bool lowWall) MatchingWall(IEnumerable<IEntity> candidates)
private (bool connected, bool lowWall) MatchingWall(IEnumerable<EntityUid> candidates)
{
foreach (var entity in candidates)
{
if (!entity.TryGetComponent(out IconSmoothComponent? other))
if (!Owner.EntityManager.ComponentManager.TryGetComponent(entity, out IconSmoothComponent? other))
{
continue;
}

View File

@@ -1,8 +1,11 @@
using System.Diagnostics.CodeAnalysis;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Content.Client.GameObjects.EntitySystems;
using Content.Shared.GameObjects.Components;
using Robust.Client.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Serialization.Manager.Attributes;
using static Content.Client.GameObjects.Components.IconSmoothing.IconSmoothComponent;
@@ -12,18 +15,18 @@ namespace Content.Client.GameObjects.Components
[ComponentReference(typeof(SharedWindowComponent))]
public sealed class WindowComponent : SharedWindowComponent
{
[Dependency] private readonly IMapManager _mapManager = default!;
[DataField("base")]
private string? _stateBase;
private ISpriteComponent? _sprite;
private SnapGridComponent? _snapGrid;
public override void Initialize()
{
base.Initialize();
_sprite = Owner.GetComponent<ISpriteComponent>();
_snapGrid = Owner.GetComponent<SnapGridComponent>();
}
/// <inheritdoc />
@@ -31,11 +34,6 @@ namespace Content.Client.GameObjects.Components
{
base.Startup();
if (_snapGrid != null)
{
_snapGrid.OnPositionChanged += SnapGridOnPositionChanged;
}
Owner.EntityManager.EventBus.RaiseEvent(EventSource.Local, new WindowSmoothDirtyEvent(Owner));
if (_sprite != null)
@@ -67,18 +65,7 @@ namespace Content.Client.GameObjects.Components
}
}
/// <inheritdoc />
protected override void Shutdown()
{
if (_snapGrid != null)
{
_snapGrid.OnPositionChanged -= SnapGridOnPositionChanged;
}
base.Shutdown();
}
private void SnapGridOnPositionChanged()
public void SnapGridOnPositionChanged()
{
Owner.EntityManager.EventBus.RaiseEvent(EventSource.Local, new WindowSmoothDirtyEvent(Owner));
}
@@ -102,14 +89,14 @@ namespace Content.Client.GameObjects.Components
private LowWallComponent? FindLowWall()
{
if (_snapGrid == null)
{
if (!Owner.Transform.Anchored)
return null;
}
foreach (var entity in _snapGrid.GetLocal())
var grid = _mapManager.GetGrid(Owner.Transform.GridID);
var coords = Owner.Transform.Coordinates;
foreach (var entity in grid.GetLocal(coords))
{
if (entity.TryGetComponent(out LowWallComponent? lowWall))
if (Owner.EntityManager.ComponentManager.TryGetComponent(entity, out LowWallComponent? lowWall))
{
return lowWall;
}

View File

@@ -1,9 +1,11 @@
using System;
using System;
using Content.Shared.GameObjects.Components;
using Content.Shared.Utility;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
namespace Content.Client.GameObjects.Components
{
@@ -15,10 +17,10 @@ namespace Content.Client.GameObjects.Components
base.OnChangeData(component);
var sprite = component.Owner.GetComponent<ISpriteComponent>();
if (!component.Owner.TryGetComponent(out SnapGridComponent? snapGrid))
if (!component.Owner.Transform.Anchored)
return;
var lowWall = FindLowWall(snapGrid);
var lowWall = FindLowWall(IoCManager.Resolve<IMapManager>(), component.Owner.Transform);
if (lowWall == null)
return;
@@ -48,11 +50,13 @@ namespace Content.Client.GameObjects.Components
}
}
private static LowWallComponent? FindLowWall(SnapGridComponent snapGrid)
private static LowWallComponent? FindLowWall(IMapManager mapManager, ITransformComponent transform)
{
foreach (var entity in snapGrid.GetLocal())
var grid = mapManager.GetGrid(transform.GridID);
var coords = transform.Coordinates;
foreach (var entity in grid.GetLocal(coords))
{
if (entity.TryGetComponent(out LowWallComponent? lowWall))
if (transform.Owner.EntityManager.ComponentManager.TryGetComponent(entity, out LowWallComponent? lowWall))
{
return lowWall;
}

View File

@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using Content.Client.GameObjects.Components.IconSmoothing;
using JetBrains.Annotations;
@@ -6,6 +6,7 @@ using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Utility;
namespace Content.Client.GameObjects.EntitySystems
{
@@ -17,7 +18,7 @@ namespace Content.Client.GameObjects.EntitySystems
{
[Dependency] private readonly IMapManager _mapManager = default!;
private readonly Queue<IEntity> _dirtyEntities = new();
private readonly Queue<EntityUid> _dirtyEntities = new();
private int _generation;
@@ -27,13 +28,18 @@ namespace Content.Client.GameObjects.EntitySystems
base.Initialize();
SubscribeLocalEvent<IconSmoothDirtyEvent>(HandleDirtyEvent);
SubscribeLocalEvent<IconSmoothComponent, SnapGridPositionChangedEvent>(HandleSnapGridMove);
}
public override void Shutdown()
{
base.Shutdown();
UnsubscribeLocalEvent<IconSmoothDirtyEvent>();
UnsubscribeLocalEvent<IconSmoothComponent, SnapGridPositionChangedEvent>(HandleSnapGridMove);
}
public override void FrameUpdate(float frameTime)
@@ -63,20 +69,20 @@ namespace Content.Client.GameObjects.EntitySystems
senderEnt.TryGetComponent(out IconSmoothComponent? iconSmooth)
&& iconSmooth.Running)
{
var snapGrid = senderEnt.GetComponent<SnapGridComponent>();
var grid1 = _mapManager.GetGrid(senderEnt.Transform.GridID);
var coords = senderEnt.Transform.Coordinates;
_dirtyEntities.Enqueue(senderEnt);
AddValidEntities(snapGrid.GetInDir(Direction.North));
AddValidEntities(snapGrid.GetInDir(Direction.South));
AddValidEntities(snapGrid.GetInDir(Direction.East));
AddValidEntities(snapGrid.GetInDir(Direction.West));
_dirtyEntities.Enqueue(senderEnt.Uid);
AddValidEntities(grid1.GetInDir(coords, Direction.North));
AddValidEntities(grid1.GetInDir(coords, Direction.South));
AddValidEntities(grid1.GetInDir(coords, Direction.East));
AddValidEntities(grid1.GetInDir(coords, Direction.West));
if (ev.Mode == IconSmoothingMode.Corners)
{
AddValidEntities(snapGrid.GetInDir(Direction.NorthEast));
AddValidEntities(snapGrid.GetInDir(Direction.SouthEast));
AddValidEntities(snapGrid.GetInDir(Direction.SouthWest));
AddValidEntities(snapGrid.GetInDir(Direction.NorthWest));
AddValidEntities(grid1.GetInDir(coords, Direction.NorthEast));
AddValidEntities(grid1.GetInDir(coords, Direction.SouthEast));
AddValidEntities(grid1.GetInDir(coords, Direction.SouthWest));
AddValidEntities(grid1.GetInDir(coords, Direction.NorthWest));
}
}
@@ -85,43 +91,43 @@ namespace Content.Client.GameObjects.EntitySystems
{
var pos = ev.LastPosition.Value.pos;
AddValidEntities(grid.GetSnapGridCell(pos + new Vector2i(1, 0), ev.Offset));
AddValidEntities(grid.GetSnapGridCell(pos + new Vector2i(-1, 0), ev.Offset));
AddValidEntities(grid.GetSnapGridCell(pos + new Vector2i(0, 1), ev.Offset));
AddValidEntities(grid.GetSnapGridCell(pos + new Vector2i(0, -1), ev.Offset));
AddValidEntities(grid.GetAnchoredEntities(pos + new Vector2i(1, 0)));
AddValidEntities(grid.GetAnchoredEntities(pos + new Vector2i(-1, 0)));
AddValidEntities(grid.GetAnchoredEntities(pos + new Vector2i(0, 1)));
AddValidEntities(grid.GetAnchoredEntities(pos + new Vector2i(0, -1)));
if (ev.Mode == IconSmoothingMode.Corners)
{
AddValidEntities(grid.GetSnapGridCell(pos + new Vector2i(1, 1), ev.Offset));
AddValidEntities(grid.GetSnapGridCell(pos + new Vector2i(-1, -1), ev.Offset));
AddValidEntities(grid.GetSnapGridCell(pos + new Vector2i(-1, 1), ev.Offset));
AddValidEntities(grid.GetSnapGridCell(pos + new Vector2i(1, -1), ev.Offset));
AddValidEntities(grid.GetAnchoredEntities(pos + new Vector2i(1, 1)));
AddValidEntities(grid.GetAnchoredEntities(pos + new Vector2i(-1, -1)));
AddValidEntities(grid.GetAnchoredEntities(pos + new Vector2i(-1, 1)));
AddValidEntities(grid.GetAnchoredEntities(pos + new Vector2i(1, -1)));
}
}
}
private void AddValidEntities(IEnumerable<IEntity> candidates)
private static void HandleSnapGridMove(EntityUid uid, IconSmoothComponent component, SnapGridPositionChangedEvent args)
{
component.SnapGridOnPositionChanged();
}
private void AddValidEntities(IEnumerable<EntityUid> candidates)
{
foreach (var entity in candidates)
{
if (entity.HasComponent<IconSmoothComponent>())
if (ComponentManager.HasComponent<IconSmoothComponent>(entity))
{
_dirtyEntities.Enqueue(entity);
}
}
}
private void AddValidEntities(IEnumerable<IComponent> candidates)
{
AddValidEntities(candidates.Select(c => c.Owner));
}
private void CalculateNewSprite(IEntity entity)
private void CalculateNewSprite(EntityUid euid)
{
// The generation check prevents updating an entity multiple times per tick.
// As it stands now, it's totally possible for something to get queued twice.
// Generation on the component is set after an update so we can cull updates that happened this generation.
if (!entity.IsValid()
|| !entity.TryGetComponent(out IconSmoothComponent? smoothing)
if (!EntityManager.EntityExists(euid)
|| !ComponentManager.TryGetComponent(euid, out IconSmoothComponent? smoothing)
|| smoothing.UpdateGeneration == _generation)
{
return;
@@ -138,16 +144,14 @@ namespace Content.Client.GameObjects.EntitySystems
/// </summary>
public sealed class IconSmoothDirtyEvent : EntityEventArgs
{
public IconSmoothDirtyEvent(IEntity sender, (GridId grid, Vector2i pos)? lastPosition, SnapGridOffset offset, IconSmoothingMode mode)
public IconSmoothDirtyEvent(IEntity sender, (GridId grid, Vector2i pos)? lastPosition, IconSmoothingMode mode)
{
LastPosition = lastPosition;
Offset = offset;
Mode = mode;
Sender = sender;
}
public (GridId grid, Vector2i pos)? LastPosition { get; }
public SnapGridOffset Offset { get; }
public IconSmoothingMode Mode { get; }
public IEntity Sender { get; }
}

View File

@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using Content.Client.GameObjects.Components;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
@@ -15,6 +15,7 @@ namespace Content.Client.GameObjects.EntitySystems
base.Initialize();
SubscribeLocalEvent<WindowSmoothDirtyEvent>(HandleDirtyEvent);
SubscribeLocalEvent<WindowComponent, SnapGridPositionChangedEvent>(HandleSnapGridMove);
}
public override void Shutdown()
@@ -22,6 +23,7 @@ namespace Content.Client.GameObjects.EntitySystems
base.Shutdown();
UnsubscribeLocalEvent<WindowSmoothDirtyEvent>();
UnsubscribeLocalEvent<WindowComponent, SnapGridPositionChangedEvent>(HandleSnapGridMove);
}
private void HandleDirtyEvent(WindowSmoothDirtyEvent ev)
@@ -32,6 +34,11 @@ namespace Content.Client.GameObjects.EntitySystems
}
}
private static void HandleSnapGridMove(EntityUid uid, WindowComponent component, SnapGridPositionChangedEvent args)
{
component.SnapGridOnPositionChanged();
}
public override void FrameUpdate(float frameTime)
{
base.FrameUpdate(frameTime);