Fix smoothing error (#6352)

This commit is contained in:
Leon Friedrich
2022-01-29 07:25:49 +13:00
committed by GitHub
parent b75f005bb4
commit 79243d1379
3 changed files with 68 additions and 85 deletions

View File

@@ -36,7 +36,7 @@ namespace Content.Client.IconSmoothing
internal ISpriteComponent? Sprite { get; private set; } internal ISpriteComponent? Sprite { get; private set; }
private (GridId, Vector2i) _lastPosition; public (GridId, Vector2i)? LastPosition;
/// <summary> /// <summary>
/// We will smooth with other objects with the same key. /// We will smooth with other objects with the same key.
@@ -77,7 +77,7 @@ namespace Content.Client.IconSmoothing
// ensures lastposition initial value is populated on spawn. Just calling // ensures lastposition initial value is populated on spawn. Just calling
// the hook here would cause a dirty event to fire needlessly // the hook here would cause a dirty event to fire needlessly
UpdateLastPosition(); UpdateLastPosition();
_entMan.EventBus.RaiseEvent(EventSource.Local, new IconSmoothDirtyEvent(Owner, null, Mode)); EntitySystem.Get<IconSmoothSystem>().UpdateSmoothing(Owner, this);
} }
if (Sprite != null && Mode == IconSmoothingMode.Corners) if (Sprite != null && Mode == IconSmoothingMode.Corners)
@@ -96,29 +96,39 @@ namespace Content.Client.IconSmoothing
private void UpdateLastPosition() private void UpdateLastPosition()
{ {
if (_mapManager.TryGetGrid(_entMan.GetComponent<TransformComponent>(Owner).GridID, out var grid)) var transform = _entMan.GetComponent<TransformComponent>(Owner);
if (_mapManager.TryGetGrid(transform.GridID, out var grid))
{ {
_lastPosition = (_entMan.GetComponent<TransformComponent>(Owner).GridID, grid.TileIndicesFor(_entMan.GetComponent<TransformComponent>(Owner).Coordinates)); LastPosition = (transform.GridID, grid.TileIndicesFor(transform.Coordinates));
} }
else else
{ {
// When this is called during component startup, the transform can end up being with an invalid grid ID. // When this is called during component startup, the transform can end up being with an invalid grid ID.
// In that case, use this. // In that case, use this.
_lastPosition = (GridId.Invalid, new Vector2i(0, 0)); LastPosition = (GridId.Invalid, new Vector2i(0, 0));
} }
} }
internal virtual void CalculateNewSprite() internal virtual void CalculateNewSprite()
{ {
if (!_mapManager.TryGetGrid(_entMan.GetComponent<TransformComponent>(Owner).GridID, out var grid)) var transform = _entMan.GetComponent<TransformComponent>(Owner);
if (!transform.Anchored)
{ {
Logger.Error($"Failed to calculate IconSmoothComponent sprite in {Owner} because grid {_entMan.GetComponent<TransformComponent>(Owner).GridID} was missing."); CalculateNewSprite(null);
return;
}
if (!_mapManager.TryGetGrid(transform.GridID, out var grid))
{
Logger.Error($"Failed to calculate IconSmoothComponent sprite in {Owner} because grid {transform.GridID} was missing.");
return; return;
} }
CalculateNewSprite(grid); CalculateNewSprite(grid);
} }
internal virtual void CalculateNewSprite(IMapGrid grid) internal virtual void CalculateNewSprite(IMapGrid? grid)
{ {
switch (Mode) switch (Mode)
{ {
@@ -135,15 +145,21 @@ namespace Content.Client.IconSmoothing
} }
} }
private void CalculateNewSpriteCardinal(IMapGrid grid) private void CalculateNewSpriteCardinal(IMapGrid? grid)
{ {
if (!_entMan.GetComponent<TransformComponent>(Owner).Anchored || Sprite == null) if (Sprite == null)
{ {
return; return;
} }
var dirs = CardinalConnectDirs.None; var dirs = CardinalConnectDirs.None;
if (grid == null)
{
Sprite.LayerSetState(0, $"{StateBase}{(int) dirs}");
return;
}
var position = _entMan.GetComponent<TransformComponent>(Owner).Coordinates; var position = _entMan.GetComponent<TransformComponent>(Owner).Coordinates;
if (MatchingEntity(grid.GetInDir(position, Direction.North))) if (MatchingEntity(grid.GetInDir(position, Direction.North)))
dirs |= CardinalConnectDirs.North; dirs |= CardinalConnectDirs.North;
@@ -157,7 +173,7 @@ namespace Content.Client.IconSmoothing
Sprite.LayerSetState(0, $"{StateBase}{(int) dirs}"); Sprite.LayerSetState(0, $"{StateBase}{(int) dirs}");
} }
private void CalculateNewSpriteCorners(IMapGrid grid) private void CalculateNewSpriteCorners(IMapGrid? grid)
{ {
if (Sprite == null) if (Sprite == null)
{ {
@@ -172,9 +188,9 @@ namespace Content.Client.IconSmoothing
Sprite.LayerSetState(CornerLayers.NW, $"{StateBase}{(int) cornerNW}"); Sprite.LayerSetState(CornerLayers.NW, $"{StateBase}{(int) cornerNW}");
} }
protected (CornerFill ne, CornerFill nw, CornerFill sw, CornerFill se) CalculateCornerFill(IMapGrid grid) protected (CornerFill ne, CornerFill nw, CornerFill sw, CornerFill se) CalculateCornerFill(IMapGrid? grid)
{ {
if (!_entMan.GetComponent<TransformComponent>(Owner).Anchored) if (grid == null)
{ {
return (CornerFill.None, CornerFill.None, CornerFill.None, CornerFill.None); return (CornerFill.None, CornerFill.None, CornerFill.None, CornerFill.None);
} }
@@ -259,19 +275,7 @@ namespace Content.Client.IconSmoothing
{ {
base.Shutdown(); base.Shutdown();
if (_entMan.GetComponent<TransformComponent>(Owner).Anchored) EntitySystem.Get<IconSmoothSystem>().UpdateSmoothing(Owner, this);
{
_entMan.EventBus.RaiseEvent(EventSource.Local, new IconSmoothDirtyEvent(Owner, _lastPosition, Mode));
}
}
public void AnchorStateChanged()
{
if (_entMan.GetComponent<TransformComponent>(Owner).Anchored)
{
_entMan.EventBus.RaiseEvent(EventSource.Local, new IconSmoothDirtyEvent(Owner, _lastPosition, Mode));
UpdateLastPosition();
}
} }
[System.Diagnostics.Contracts.Pure] [System.Diagnostics.Contracts.Pure]

View File

@@ -24,7 +24,6 @@ namespace Content.Client.IconSmoothing
{ {
base.Initialize(); base.Initialize();
SubscribeLocalEvent<IconSmoothDirtyEvent>(HandleDirtyEvent);
SubscribeLocalEvent<IconSmoothComponent, AnchorStateChangedEvent>(HandleAnchorChanged); SubscribeLocalEvent<IconSmoothComponent, AnchorStateChangedEvent>(HandleAnchorChanged);
} }
@@ -46,54 +45,51 @@ namespace Content.Client.IconSmoothing
} }
} }
private void HandleDirtyEvent(IconSmoothDirtyEvent ev) public void UpdateSmoothing(EntityUid uid, IconSmoothComponent? comp = null)
{ {
// Yes, we updates ALL smoothing entities surrounding us even if they would never smooth with us. if (!Resolve(uid, ref comp))
// This is simpler to implement. If you want to optimize it be my guest. return;
var senderEnt = ev.Sender;
if (EntityManager.EntityExists(senderEnt) &&
_mapManager.TryGetGrid(EntityManager.GetComponent<TransformComponent>(senderEnt).GridID, out var grid1) &&
EntityManager.TryGetComponent(senderEnt, out IconSmoothComponent? iconSmooth)
&& iconSmooth.Running)
{
var coords = EntityManager.GetComponent<TransformComponent>(senderEnt).Coordinates;
_dirtyEntities.Enqueue(senderEnt); _dirtyEntities.Enqueue(uid);
AddValidEntities(grid1.GetInDir(coords, Direction.North));
AddValidEntities(grid1.GetInDir(coords, Direction.South)); var transform = Transform(uid);
AddValidEntities(grid1.GetInDir(coords, Direction.East)); Vector2i pos;
AddValidEntities(grid1.GetInDir(coords, Direction.West));
if (ev.Mode == IconSmoothingMode.Corners) if (transform.Anchored && _mapManager.TryGetGrid(transform.GridID, out var grid))
{ {
AddValidEntities(grid1.GetInDir(coords, Direction.NorthEast)); pos = grid.CoordinatesToTile(transform.Coordinates);
AddValidEntities(grid1.GetInDir(coords, Direction.SouthEast)); }
AddValidEntities(grid1.GetInDir(coords, Direction.SouthWest)); else
AddValidEntities(grid1.GetInDir(coords, Direction.NorthWest)); {
} // Entity is no longer valid, update around the last position it was at.
if (comp.LastPosition is not (GridId gridId, Vector2i oldPos))
return;
if (!_mapManager.TryGetGrid(gridId, out grid))
return;
pos = oldPos;
} }
// Entity is no longer valid, update around the last position it was at. // Yes, we updates ALL smoothing entities surrounding us even if they would never smooth with us.
if (ev.LastPosition.HasValue && _mapManager.TryGetGrid(ev.LastPosition.Value.grid, out var grid)) // This is simpler to implement. If you want to optimize it be my guest.
{
var pos = ev.LastPosition.Value.pos;
AddValidEntities(grid.GetAnchoredEntities(pos + new Vector2i(1, 0))); AddValidEntities(grid.GetAnchoredEntities(pos + new Vector2i(1, 0)));
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)));
AddValidEntities(grid.GetAnchoredEntities(pos + new Vector2i(0, -1))); AddValidEntities(grid.GetAnchoredEntities(pos + new Vector2i(0, -1)));
if (ev.Mode == IconSmoothingMode.Corners) if (comp.Mode == IconSmoothingMode.Corners)
{ {
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)));
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 static void HandleAnchorChanged(EntityUid uid, IconSmoothComponent component, ref AnchorStateChangedEvent args) private void HandleAnchorChanged(EntityUid uid, IconSmoothComponent component, ref AnchorStateChangedEvent args)
{ {
component.AnchorStateChanged(); UpdateSmoothing(uid, component);
} }
private void AddValidEntities(IEnumerable<EntityUid> candidates) private void AddValidEntities(IEnumerable<EntityUid> candidates)
@@ -124,21 +120,4 @@ namespace Content.Client.IconSmoothing
smoothing.UpdateGeneration = _generation; smoothing.UpdateGeneration = _generation;
} }
} }
/// <summary>
/// Event raised by a <see cref="IconSmoothComponent"/> when it needs to be recalculated.
/// </summary>
public sealed class IconSmoothDirtyEvent : EntityEventArgs
{
public IconSmoothDirtyEvent(EntityUid sender, (GridId grid, Vector2i pos)? lastPosition, IconSmoothingMode mode)
{
LastPosition = lastPosition;
Mode = mode;
Sender = sender;
}
public (GridId grid, Vector2i pos)? LastPosition { get; }
public IconSmoothingMode Mode { get; }
public EntityUid Sender { get; }
}
} }

View File

@@ -9,7 +9,7 @@ namespace Content.Client.Wall.Components
{ {
[RegisterComponent] [RegisterComponent]
[ComponentReference(typeof(IconSmoothComponent))] [ComponentReference(typeof(IconSmoothComponent))]
public class ReinforcedWallComponent : IconSmoothComponent public class ReinforcedWallComponent : IconSmoothComponent // whyyyyyyyyy
{ {
public override string Name => "ReinforcedWall"; public override string Name => "ReinforcedWall";
@@ -36,7 +36,7 @@ namespace Content.Client.Wall.Components
} }
} }
internal override void CalculateNewSprite(IMapGrid grid) internal override void CalculateNewSprite(IMapGrid? grid)
{ {
base.CalculateNewSprite(grid); base.CalculateNewSprite(grid);