2023-07-08 14:08:32 +10:00
|
|
|
using System.Numerics;
|
2023-01-19 19:48:29 -04:00
|
|
|
using Robust.Shared.Map;
|
2023-01-17 18:01:53 -04:00
|
|
|
|
|
|
|
|
namespace Content.Shared.Gravity;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles offsetting a sprite when there is no gravity
|
|
|
|
|
/// </summary>
|
|
|
|
|
public abstract class SharedFloatingVisualizerSystem : EntitySystem
|
|
|
|
|
{
|
|
|
|
|
[Dependency] private readonly SharedGravitySystem GravitySystem = default!;
|
|
|
|
|
|
|
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
|
|
|
|
SubscribeLocalEvent<FloatingVisualsComponent, ComponentStartup>(OnComponentStartup);
|
|
|
|
|
SubscribeLocalEvent<GravityChangedEvent>(OnGravityChanged);
|
|
|
|
|
SubscribeLocalEvent<FloatingVisualsComponent, EntParentChangedMessage>(OnEntParentChanged);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Offsets a sprite with a linear interpolation animation
|
|
|
|
|
/// </summary>
|
|
|
|
|
public virtual void FloatAnimation(EntityUid uid, Vector2 offset, string animationKey, float animationTime, bool stop = false) { }
|
|
|
|
|
|
|
|
|
|
protected bool CanFloat(EntityUid uid, FloatingVisualsComponent component, TransformComponent? transform = null)
|
|
|
|
|
{
|
2023-01-21 12:51:12 +13:00
|
|
|
if (!Resolve(uid, ref transform))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (transform.MapID == MapId.Nullspace)
|
|
|
|
|
return false;
|
|
|
|
|
|
2023-01-17 18:01:53 -04:00
|
|
|
component.CanFloat = GravitySystem.IsWeightless(uid, xform: transform);
|
2024-03-19 23:27:02 -04:00
|
|
|
Dirty(uid, component);
|
2023-01-17 18:01:53 -04:00
|
|
|
return component.CanFloat;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnComponentStartup(EntityUid uid, FloatingVisualsComponent component, ComponentStartup args)
|
|
|
|
|
{
|
|
|
|
|
if (CanFloat(uid, component))
|
|
|
|
|
FloatAnimation(uid, component.Offset, component.AnimationKey, component.AnimationTime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnGravityChanged(ref GravityChangedEvent args)
|
|
|
|
|
{
|
2023-10-19 12:34:31 -07:00
|
|
|
var query = EntityQueryEnumerator<FloatingVisualsComponent, TransformComponent>();
|
|
|
|
|
while (query.MoveNext(out var uid, out var floating, out var transform))
|
2023-01-17 18:01:53 -04:00
|
|
|
{
|
2023-01-19 19:48:29 -04:00
|
|
|
if (transform.MapID == MapId.Nullspace)
|
|
|
|
|
continue;
|
|
|
|
|
|
2023-01-17 18:01:53 -04:00
|
|
|
if (transform.GridUid != args.ChangedGridIndex)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
floating.CanFloat = !args.HasGravity;
|
2023-10-19 12:34:31 -07:00
|
|
|
Dirty(uid, floating);
|
2023-01-17 18:01:53 -04:00
|
|
|
|
|
|
|
|
if (!args.HasGravity)
|
|
|
|
|
FloatAnimation(uid, floating.Offset, floating.AnimationKey, floating.AnimationTime);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnEntParentChanged(EntityUid uid, FloatingVisualsComponent component, ref EntParentChangedMessage args)
|
|
|
|
|
{
|
|
|
|
|
var transform = args.Transform;
|
|
|
|
|
if (CanFloat(uid, component, transform))
|
|
|
|
|
FloatAnimation(uid, component.Offset, component.AnimationKey, component.AnimationTime);
|
|
|
|
|
}
|
|
|
|
|
}
|