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;
}