CatStation/Content.Client/Sprite/SpriteFadeSystem.cs
Xeri Marai fcc7f9e7f8
Sprite Fade Overlay Restricted to overlapping clickables (#37907)
* Sprite Fade Overlay Restricted to overlapping clickables

* Added an IgnoreClickableRestrict for larger objects, and put it on Anomaly Generator as an example

* Adjusting fade changes based on review

* double for avoid

* {

* fsnjhjasfhfsajkasfhj

---------

Co-authored-by: jessicamaybe <jessica@jessicamaybe.com>
Co-authored-by: Princess Cheeseballs <66055347+Princess-Cheeseballs@users.noreply.github.com>
2026-06-29 05:54:09 +00:00

184 lines
6.7 KiB
C#

using System.Linq;
using Content.Client.Gameplay;
using Content.Shared.Sprite;
using Robust.Client.GameObjects;
using Robust.Client.Input;
using Robust.Client.Player;
using Robust.Client.State;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface;
using Robust.Shared.Map;
using Robust.Shared.Physics.Systems;
using Robust.Shared.Physics;
namespace Content.Client.Sprite;
public sealed partial class SpriteFadeSystem : EntitySystem
{
/*
* If the player entity is obstructed under the specified components then it will drop the alpha for that entity
* so the player is still visible.
*/
[Dependency] private IPlayerManager _playerManager = default!;
[Dependency] private IStateManager _stateManager = default!;
[Dependency] private FixtureSystem _fixtures = default!;
[Dependency] private SharedTransformSystem _transform = default!;
[Dependency] private IUserInterfaceManager _uiManager = default!;
[Dependency] private IInputManager _inputManager = default!;
[Dependency] private SharedPhysicsSystem _physics = default!;
[Dependency] private SpriteSystem _sprite = default!;
[Dependency] private EntityQuery<SpriteComponent> _spriteQuery = default!;
[Dependency] private EntityQuery<SpriteFadeComponent> _fadeQuery = default!;
[Dependency] private EntityQuery<FadingSpriteComponent> _fadingQuery = default!;
[Dependency] private EntityQuery<FixturesComponent> _fixturesQuery = default!;
private List<(MapCoordinates Point, bool ExcludeBoundingBox)> _points = new();
private readonly HashSet<FadingSpriteComponent> _comps = new();
private const float TargetAlpha = 0.4f;
private const float ChangeRate = 1f;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<FadingSpriteComponent, ComponentShutdown>(OnFadingShutdown);
}
private void OnFadingShutdown(EntityUid uid, FadingSpriteComponent component, ComponentShutdown args)
{
if (MetaData(uid).EntityLifeStage >= EntityLifeStage.Terminating || !TryComp<SpriteComponent>(uid, out var sprite))
return;
_sprite.SetColor((uid, sprite), sprite.Color.WithAlpha(component.OriginalAlpha));
}
/// <summary>
/// Adds sprites to the fade set, and brings their alpha downwards
/// </summary>
private void FadeIn(float change)
{
var player = _playerManager.LocalEntity;
// ExcludeBoundingBox is set if we don't want to fade this sprite within the collision bounding boxes for the given POI
_points.Clear();
if (_uiManager.CurrentlyHovered is IViewportControl vp
&& _inputManager.MouseScreenPosition.IsValid)
{
_points.Add((vp.PixelToMap(_inputManager.MouseScreenPosition.Position), true));
}
if (TryComp(player, out TransformComponent? playerXform))
{
_points.Add((_transform.GetMapCoordinates(_playerManager.LocalEntity!.Value, xform: playerXform), false));
}
if (_stateManager.CurrentState is GameplayState state && _spriteQuery.TryGetComponent(player, out var playerSprite))
{
foreach (var (mapPos, excludeBB) in _points)
{
var clickable = state.GetClickableEntities(mapPos, excludeFaded: false).ToList();
// Also want to handle large entities even if they may not be clickable.
// We need to know if we're at the end of the list or not.
for (var i = 0; i < clickable.Count; i++)
{
var ent = clickable[i];
if (ent == player ||
!_fadeQuery.TryGetComponent(ent, out var fadeComp) ||
!_spriteQuery.TryGetComponent(ent, out var sprite) ||
sprite.DrawDepth < playerSprite.DrawDepth)
{
continue;
}
// If it intersects a fixture ignore it.
if (excludeBB && _fixturesQuery.TryComp(ent, out var body))
{
var transform = _physics.GetPhysicsTransform(ent);
var collided = false;
foreach (var fixture in body.Fixtures.Values)
{
if (!fixture.Hard)
continue;
if (_fixtures.TestPoint(fixture.Shape, transform, mapPos.Position))
{
collided = true;
break;
}
}
// Check next entity
if (collided)
{
continue;
}
// If this sprite doesn't always fade, and it's at the bottom of the stack, then don't fade!
if (!fadeComp.AlwaysFade && i + 1 == clickable.Count)
break;
}
if (!_fadingQuery.TryComp(ent, out var fading))
{
fading = AddComp<FadingSpriteComponent>(ent);
fading.OriginalAlpha = sprite.Color.A;
}
_comps.Add(fading);
var newColor = Math.Max(sprite.Color.A - change, TargetAlpha);
if (!sprite.Color.A.Equals(newColor))
{
_sprite.SetColor((ent, sprite), sprite.Color.WithAlpha(newColor));
}
}
}
}
}
/// <summary>
/// Bring sprites back up to their original alpha if they aren't in the fade set, and removes their fade component when done
/// </summary>
private void FadeOut(float change)
{
var query = AllEntityQuery<FadingSpriteComponent>();
while (query.MoveNext(out var uid, out var comp))
{
if (_comps.Contains(comp))
continue;
if (!_spriteQuery.TryGetComponent(uid, out var sprite))
continue;
var newColor = Math.Min(sprite.Color.A + change, comp.OriginalAlpha);
if (!newColor.Equals(sprite.Color.A))
{
_sprite.SetColor((uid, sprite), sprite.Color.WithAlpha(newColor));
}
else
{
RemCompDeferred<FadingSpriteComponent>(uid);
}
}
}
public override void FrameUpdate(float frameTime)
{
base.FrameUpdate(frameTime);
var change = ChangeRate * frameTime;
FadeIn(change);
FadeOut(change);
_comps.Clear();
}
}