Make IconSmoothComponent more failure-tolerant of the grid not being available (Fixes power stuff disappearing) (#4063)

This (hopefully) fixes power stuff disappearing by making IconSmoothComponent more failure-tolerant of grids not being available.
It might still leave issues in, though.
This commit is contained in:
20kdc
2021-05-23 19:18:36 +01:00
committed by GitHub
parent ff57833c84
commit 7598684e88
2 changed files with 37 additions and 16 deletions

View File

@@ -6,6 +6,7 @@ using Robust.Client.GameObjects;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.IoC; using Robust.Shared.IoC;
using Robust.Shared.Map; using Robust.Shared.Map;
using Robust.Shared.Log;
using Robust.Shared.Maths; using Robust.Shared.Maths;
using Robust.Shared.Serialization.Manager.Attributes; using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility; using Robust.Shared.Utility;
@@ -76,9 +77,7 @@ namespace Content.Client.GameObjects.Components.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
var grid = _mapManager.GetGrid(Owner.Transform.GridID); UpdateLastPosition();
_lastPosition = (Owner.Transform.GridID, grid.TileIndicesFor(Owner.Transform.Coordinates));
Owner.EntityManager.EventBus.RaiseEvent(EventSource.Local, new IconSmoothDirtyEvent(Owner, null, Mode)); Owner.EntityManager.EventBus.RaiseEvent(EventSource.Local, new IconSmoothDirtyEvent(Owner, null, Mode));
} }
@@ -96,15 +95,39 @@ namespace Content.Client.GameObjects.Components.IconSmoothing
} }
} }
private void UpdateLastPosition()
{
if (_mapManager.TryGetGrid(Owner.Transform.GridID, out var grid))
{
_lastPosition = (Owner.Transform.GridID, grid.TileIndicesFor(Owner.Transform.Coordinates));
}
else
{
// When this is called during component startup, the transform can end up being with an invalid grid ID.
// In that case, use this.
_lastPosition = (GridId.Invalid, new Vector2i(0, 0));
}
}
internal virtual void CalculateNewSprite() internal virtual void CalculateNewSprite()
{
if (!_mapManager.TryGetGrid(Owner.Transform.GridID, out var grid))
{
Logger.Error($"Failed to calculate IconSmoothComponent sprite in {Owner} because grid {Owner.Transform.GridID} was missing.");
return;
}
CalculateNewSprite(grid);
}
internal virtual void CalculateNewSprite(IMapGrid grid)
{ {
switch (Mode) switch (Mode)
{ {
case IconSmoothingMode.Corners: case IconSmoothingMode.Corners:
CalculateNewSpriteCorners(); CalculateNewSpriteCorners(grid);
break; break;
case IconSmoothingMode.CardinalFlags: case IconSmoothingMode.CardinalFlags:
CalculateNewSpriteCardinal(); CalculateNewSpriteCardinal(grid);
break; break;
case IconSmoothingMode.NoSprite: case IconSmoothingMode.NoSprite:
break; break;
@@ -113,7 +136,7 @@ namespace Content.Client.GameObjects.Components.IconSmoothing
} }
} }
private void CalculateNewSpriteCardinal() private void CalculateNewSpriteCardinal(IMapGrid grid)
{ {
if (!Owner.Transform.Anchored || Sprite == null) if (!Owner.Transform.Anchored || Sprite == null)
{ {
@@ -122,7 +145,6 @@ namespace Content.Client.GameObjects.Components.IconSmoothing
var dirs = CardinalConnectDirs.None; var dirs = CardinalConnectDirs.None;
var grid = _mapManager.GetGrid(Owner.Transform.GridID);
var position = Owner.Transform.Coordinates; var position = Owner.Transform.Coordinates;
if (MatchingEntity(grid.GetInDir(position, Direction.North))) if (MatchingEntity(grid.GetInDir(position, Direction.North)))
dirs |= CardinalConnectDirs.North; dirs |= CardinalConnectDirs.North;
@@ -136,14 +158,14 @@ namespace Content.Client.GameObjects.Components.IconSmoothing
Sprite.LayerSetState(0, $"{StateBase}{(int) dirs}"); Sprite.LayerSetState(0, $"{StateBase}{(int) dirs}");
} }
private void CalculateNewSpriteCorners() private void CalculateNewSpriteCorners(IMapGrid grid)
{ {
if (Sprite == null) if (Sprite == null)
{ {
return; return;
} }
var (cornerNE, cornerNW, cornerSW, cornerSE) = CalculateCornerFill(); var (cornerNE, cornerNW, cornerSW, cornerSE) = CalculateCornerFill(grid);
Sprite.LayerSetState(CornerLayers.NE, $"{StateBase}{(int) cornerNE}"); Sprite.LayerSetState(CornerLayers.NE, $"{StateBase}{(int) cornerNE}");
Sprite.LayerSetState(CornerLayers.SE, $"{StateBase}{(int) cornerSE}"); Sprite.LayerSetState(CornerLayers.SE, $"{StateBase}{(int) cornerSE}");
@@ -151,14 +173,13 @@ namespace Content.Client.GameObjects.Components.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() protected (CornerFill ne, CornerFill nw, CornerFill sw, CornerFill se) CalculateCornerFill(IMapGrid grid)
{ {
if (!Owner.Transform.Anchored) if (!Owner.Transform.Anchored)
{ {
return (CornerFill.None, CornerFill.None, CornerFill.None, CornerFill.None); return (CornerFill.None, CornerFill.None, CornerFill.None, CornerFill.None);
} }
var grid = _mapManager.GetGrid(Owner.Transform.GridID);
var position = Owner.Transform.Coordinates; var position = Owner.Transform.Coordinates;
var n = MatchingEntity(grid.GetInDir(position, Direction.North)); var n = MatchingEntity(grid.GetInDir(position, Direction.North));
var ne = MatchingEntity(grid.GetInDir(position, Direction.NorthEast)); var ne = MatchingEntity(grid.GetInDir(position, Direction.NorthEast));
@@ -249,8 +270,7 @@ namespace Content.Client.GameObjects.Components.IconSmoothing
if (Owner.Transform.Anchored) if (Owner.Transform.Anchored)
{ {
Owner.EntityManager.EventBus.RaiseEvent(EventSource.Local, new IconSmoothDirtyEvent(Owner, _lastPosition, Mode)); Owner.EntityManager.EventBus.RaiseEvent(EventSource.Local, new IconSmoothDirtyEvent(Owner, _lastPosition, Mode));
var grid = _mapManager.GetGrid(Owner.Transform.GridID); UpdateLastPosition();
_lastPosition = (Owner.Transform.GridID, grid.TileIndicesFor(Owner.Transform.Coordinates));
} }
} }

View File

@@ -2,6 +2,7 @@ using Content.Client.GameObjects.Components.IconSmoothing;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes; using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables; using Robust.Shared.ViewVariables;
using Robust.Shared.Map;
using static Robust.Client.GameObjects.SpriteComponent; using static Robust.Client.GameObjects.SpriteComponent;
namespace Content.Client.GameObjects.Components namespace Content.Client.GameObjects.Components
@@ -35,11 +36,11 @@ namespace Content.Client.GameObjects.Components
} }
} }
internal override void CalculateNewSprite() internal override void CalculateNewSprite(IMapGrid grid)
{ {
base.CalculateNewSprite(); base.CalculateNewSprite(grid);
var (cornerNE, cornerNW, cornerSW, cornerSE) = CalculateCornerFill(); var (cornerNE, cornerNW, cornerSW, cornerSE) = CalculateCornerFill(grid);
if (Sprite != null) if (Sprite != null)
{ {