2021-04-28 10:49:37 -07:00
|
|
|
using System.Collections.Generic;
|
2019-03-28 11:14:03 +01:00
|
|
|
using JetBrains.Annotations;
|
2019-04-15 21:11:38 -06:00
|
|
|
using Robust.Shared.GameObjects;
|
|
|
|
|
using Robust.Shared.IoC;
|
|
|
|
|
using Robust.Shared.Map;
|
|
|
|
|
using Robust.Shared.Maths;
|
2019-03-28 11:14:03 +01:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Client.IconSmoothing
|
2019-03-28 11:14:03 +01:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Entity system implementing the logic for <see cref="IconSmoothComponent"/>
|
|
|
|
|
/// </summary>
|
|
|
|
|
[UsedImplicitly]
|
|
|
|
|
internal sealed class IconSmoothSystem : EntitySystem
|
|
|
|
|
{
|
2020-08-24 14:10:28 +02:00
|
|
|
[Dependency] private readonly IMapManager _mapManager = default!;
|
2019-03-28 11:14:03 +01:00
|
|
|
|
2021-04-28 10:49:37 -07:00
|
|
|
private readonly Queue<EntityUid> _dirtyEntities = new();
|
2019-03-28 11:14:03 +01:00
|
|
|
|
|
|
|
|
private int _generation;
|
|
|
|
|
|
2020-02-18 19:43:54 -08:00
|
|
|
/// <inheritdoc />
|
2019-03-28 11:14:03 +01:00
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
2021-04-09 16:08:12 +02:00
|
|
|
base.Initialize();
|
|
|
|
|
|
2020-02-19 17:08:59 -08:00
|
|
|
SubscribeLocalEvent<IconSmoothDirtyEvent>(HandleDirtyEvent);
|
2021-06-19 19:41:26 -07:00
|
|
|
SubscribeLocalEvent<IconSmoothComponent, AnchorStateChangedEvent>(HandleAnchorChanged);
|
2021-04-09 16:08:12 +02:00
|
|
|
}
|
|
|
|
|
|
2019-03-28 11:14:03 +01:00
|
|
|
public override void FrameUpdate(float frameTime)
|
|
|
|
|
{
|
|
|
|
|
base.FrameUpdate(frameTime);
|
|
|
|
|
|
|
|
|
|
if (_dirtyEntities.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_generation += 1;
|
|
|
|
|
|
|
|
|
|
// Performance: This could be spread over multiple updates, or made parallel.
|
|
|
|
|
while (_dirtyEntities.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
CalculateNewSprite(_dirtyEntities.Dequeue());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-19 14:39:00 -08:00
|
|
|
private void HandleDirtyEvent(IconSmoothDirtyEvent ev)
|
2019-03-28 11:14:03 +01:00
|
|
|
{
|
|
|
|
|
// Yes, we updates ALL smoothing entities surrounding us even if they would never smooth with us.
|
|
|
|
|
// This is simpler to implement. If you want to optimize it be my guest.
|
2020-02-19 14:39:00 -08:00
|
|
|
var senderEnt = ev.Sender;
|
2021-12-05 18:09:01 +01:00
|
|
|
if (EntityManager.EntityExists(senderEnt) &&
|
|
|
|
|
_mapManager.TryGetGrid(EntityManager.GetComponent<TransformComponent>(senderEnt).GridID, out var grid1) &&
|
|
|
|
|
EntityManager.TryGetComponent(senderEnt, out IconSmoothComponent? iconSmooth)
|
2019-10-14 17:09:45 +02:00
|
|
|
&& iconSmooth.Running)
|
2019-03-28 11:14:03 +01:00
|
|
|
{
|
2021-12-05 18:09:01 +01:00
|
|
|
var coords = EntityManager.GetComponent<TransformComponent>(senderEnt).Coordinates;
|
2021-04-28 10:49:37 -07:00
|
|
|
|
2021-12-03 15:53:09 +01:00
|
|
|
_dirtyEntities.Enqueue(senderEnt);
|
2021-04-28 10:49:37 -07:00
|
|
|
AddValidEntities(grid1.GetInDir(coords, Direction.North));
|
|
|
|
|
AddValidEntities(grid1.GetInDir(coords, Direction.South));
|
|
|
|
|
AddValidEntities(grid1.GetInDir(coords, Direction.East));
|
|
|
|
|
AddValidEntities(grid1.GetInDir(coords, Direction.West));
|
2019-03-28 11:14:03 +01:00
|
|
|
if (ev.Mode == IconSmoothingMode.Corners)
|
|
|
|
|
{
|
2021-04-28 10:49:37 -07:00
|
|
|
AddValidEntities(grid1.GetInDir(coords, Direction.NorthEast));
|
|
|
|
|
AddValidEntities(grid1.GetInDir(coords, Direction.SouthEast));
|
|
|
|
|
AddValidEntities(grid1.GetInDir(coords, Direction.SouthWest));
|
|
|
|
|
AddValidEntities(grid1.GetInDir(coords, Direction.NorthWest));
|
2019-03-28 11:14:03 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-10-14 17:09:45 +02:00
|
|
|
|
2020-06-22 21:02:50 -04:00
|
|
|
// Entity is no longer valid, update around the last position it was at.
|
|
|
|
|
if (ev.LastPosition.HasValue && _mapManager.TryGetGrid(ev.LastPosition.Value.grid, out var grid))
|
2019-03-28 11:14:03 +01:00
|
|
|
{
|
|
|
|
|
var pos = ev.LastPosition.Value.pos;
|
|
|
|
|
|
2021-04-28 10:49:37 -07:00
|
|
|
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)));
|
2019-03-28 11:14:03 +01:00
|
|
|
if (ev.Mode == IconSmoothingMode.Corners)
|
|
|
|
|
{
|
2021-04-28 10:49:37 -07:00
|
|
|
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)));
|
2019-03-28 11:14:03 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-21 11:49:31 +02:00
|
|
|
private static void HandleAnchorChanged(EntityUid uid, IconSmoothComponent component, ref AnchorStateChangedEvent args)
|
2021-04-28 10:49:37 -07:00
|
|
|
{
|
2021-06-19 19:41:26 -07:00
|
|
|
component.AnchorStateChanged();
|
2021-04-28 10:49:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AddValidEntities(IEnumerable<EntityUid> candidates)
|
2019-03-28 11:14:03 +01:00
|
|
|
{
|
|
|
|
|
foreach (var entity in candidates)
|
|
|
|
|
{
|
2021-09-28 13:35:29 +02:00
|
|
|
if (EntityManager.HasComponent<IconSmoothComponent>(entity))
|
2019-03-28 11:14:03 +01:00
|
|
|
{
|
|
|
|
|
_dirtyEntities.Enqueue(entity);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 10:49:37 -07:00
|
|
|
private void CalculateNewSprite(EntityUid euid)
|
2019-03-28 11:14:03 +01:00
|
|
|
{
|
|
|
|
|
// 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.
|
2021-04-28 10:49:37 -07:00
|
|
|
if (!EntityManager.EntityExists(euid)
|
2021-09-28 13:35:29 +02:00
|
|
|
|| !EntityManager.TryGetComponent(euid, out IconSmoothComponent? smoothing)
|
2019-03-28 11:14:03 +01:00
|
|
|
|| smoothing.UpdateGeneration == _generation)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-26 13:53:06 +02:00
|
|
|
smoothing.CalculateNewSprite();
|
2019-03-28 11:14:03 +01:00
|
|
|
|
|
|
|
|
smoothing.UpdateGeneration = _generation;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Event raised by a <see cref="IconSmoothComponent"/> when it needs to be recalculated.
|
|
|
|
|
/// </summary>
|
2021-03-09 11:22:48 -08:00
|
|
|
public sealed class IconSmoothDirtyEvent : EntityEventArgs
|
2019-03-28 11:14:03 +01:00
|
|
|
{
|
2021-12-05 18:09:01 +01:00
|
|
|
public IconSmoothDirtyEvent(EntityUid sender, (GridId grid, Vector2i pos)? lastPosition, IconSmoothingMode mode)
|
2019-03-28 11:14:03 +01:00
|
|
|
{
|
|
|
|
|
LastPosition = lastPosition;
|
|
|
|
|
Mode = mode;
|
2020-02-19 14:39:00 -08:00
|
|
|
Sender = sender;
|
2019-03-28 11:14:03 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-11 15:21:21 +02:00
|
|
|
public (GridId grid, Vector2i pos)? LastPosition { get; }
|
2019-03-28 11:14:03 +01:00
|
|
|
public IconSmoothingMode Mode { get; }
|
2021-12-05 18:09:01 +01:00
|
|
|
public EntityUid Sender { get; }
|
2019-03-28 11:14:03 +01:00
|
|
|
}
|
|
|
|
|
}
|