Improve sprite fading behaviour (#35863)

* Click through faded sprites

* Count the mouse position for which sprites to fade
This commit is contained in:
pathetic meowmeow
2025-04-03 02:58:05 -04:00
committed by GitHub
parent ce53629f1d
commit e9b89a1c6b
4 changed files with 105 additions and 34 deletions

View File

@@ -1,8 +1,14 @@
using System.Numerics;
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;
@@ -17,6 +23,9 @@ public sealed class SpriteFadeSystem : EntitySystem
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IStateManager _stateManager = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
[Dependency] private readonly IUserInterfaceManager _uiManager = default!;
[Dependency] private readonly IInputManager _inputManager = default!;
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
private readonly HashSet<FadingSpriteComponent> _comps = new();
@@ -24,6 +33,11 @@ public sealed class SpriteFadeSystem : EntitySystem
private EntityQuery<SpriteFadeComponent> _fadeQuery;
private EntityQuery<FadingSpriteComponent> _fadingQuery;
/// <summary>
/// Radius of the mouse point for the intersection test
/// </summary>
private static Vector2 MouseRadius = new Vector2(10f * float.Epsilon, 10f * float.Epsilon);
private const float TargetAlpha = 0.4f;
private const float ChangeRate = 1f;
@@ -46,46 +60,82 @@ public sealed class SpriteFadeSystem : EntitySystem
sprite.Color = sprite.Color.WithAlpha(component.OriginalAlpha);
}
public override void FrameUpdate(float frameTime)
/// <summary>
/// Adds sprites to the fade set, and brings their alpha downwards
/// </summary>
private void FadeIn(float change)
{
base.FrameUpdate(frameTime);
var player = _playerManager.LocalEntity;
var change = ChangeRate * frameTime;
// ExcludeBoundingBox is set if we don't want to fade this sprite within the collision bounding boxes for the given POI
var pointsOfInterest = new List<(MapCoordinates Point, bool ExcludeBoundingBox)>();
if (TryComp(player, out TransformComponent? playerXform) &&
_stateManager.CurrentState is GameplayState state &&
_spriteQuery.TryGetComponent(player, out var playerSprite))
if (_uiManager.CurrentlyHovered is IViewportControl vp
&& _inputManager.MouseScreenPosition.IsValid)
{
var mapPos = _transform.GetMapCoordinates(_playerManager.LocalEntity!.Value, xform: playerXform);
pointsOfInterest.Add((vp.PixelToMap(_inputManager.MouseScreenPosition.Position), true));
}
// Also want to handle large entities even if they may not be clickable.
foreach (var ent in state.GetClickableEntities(mapPos))
if (TryComp(player, out TransformComponent? playerXform))
{
pointsOfInterest.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 pointsOfInterest)
{
if (ent == player ||
!_fadeQuery.HasComponent(ent) ||
!_spriteQuery.TryGetComponent(ent, out var sprite) ||
sprite.DrawDepth < playerSprite.DrawDepth)
// Also want to handle large entities even if they may not be clickable.
foreach (var ent in state.GetClickableEntities(mapPos, excludeFaded: false))
{
continue;
}
if (ent == player ||
!_fadeQuery.HasComponent(ent) ||
!_spriteQuery.TryGetComponent(ent, out var sprite) ||
sprite.DrawDepth < playerSprite.DrawDepth)
{
continue;
}
if (!_fadingQuery.TryComp(ent, out var fading))
{
fading = AddComp<FadingSpriteComponent>(ent);
fading.OriginalAlpha = sprite.Color.A;
}
if (excludeBB)
{
var test = new Box2Rotated(mapPos.Position - MouseRadius, mapPos.Position + MouseRadius);
var collided = false;
foreach (var fixture in _physics.GetCollidingEntities(mapPos.MapId, test))
{
if (fixture.Owner == ent)
{
collided = true;
break;
}
}
if (collided)
{
break;
}
}
_comps.Add(fading);
var newColor = Math.Max(sprite.Color.A - change, TargetAlpha);
if (!_fadingQuery.TryComp(ent, out var fading))
{
fading = AddComp<FadingSpriteComponent>(ent);
fading.OriginalAlpha = sprite.Color.A;
}
if (!sprite.Color.A.Equals(newColor))
{
sprite.Color = sprite.Color.WithAlpha(newColor);
_comps.Add(fading);
var newColor = Math.Max(sprite.Color.A - change, TargetAlpha);
if (!sprite.Color.A.Equals(newColor))
{
sprite.Color = 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))
{
@@ -106,6 +156,16 @@ public sealed class SpriteFadeSystem : EntitySystem
RemCompDeferred<FadingSpriteComponent>(uid);
}
}
}
public override void FrameUpdate(float frameTime)
{
base.FrameUpdate(frameTime);
var change = ChangeRate * frameTime;
FadeIn(change);
FadeOut(change);
_comps.Clear();
}