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
|
|
|
|
|
{
|
2025-08-19 11:35:09 -07:00
|
|
|
[Dependency] private readonly SharedGravitySystem _gravity = default!;
|
2023-01-17 18:01:53 -04:00
|
|
|
|
|
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
|
|
|
|
SubscribeLocalEvent<FloatingVisualsComponent, ComponentStartup>(OnComponentStartup);
|
2025-08-19 11:35:09 -07:00
|
|
|
SubscribeLocalEvent<FloatingVisualsComponent, WeightlessnessChangedEvent>(OnWeightlessnessChanged);
|
2023-01-17 18:01:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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) { }
|
|
|
|
|
|
2025-08-19 11:35:09 -07:00
|
|
|
protected bool CanFloat(Entity<FloatingVisualsComponent> entity)
|
2023-01-17 18:01:53 -04:00
|
|
|
{
|
2025-08-19 11:35:09 -07:00
|
|
|
entity.Comp.CanFloat = _gravity.IsWeightless(entity.Owner);
|
|
|
|
|
Dirty(entity);
|
|
|
|
|
return entity.Comp.CanFloat;
|
2023-01-17 18:01:53 -04:00
|
|
|
}
|
|
|
|
|
|
2025-08-19 11:35:09 -07:00
|
|
|
private void OnComponentStartup(Entity<FloatingVisualsComponent> entity, ref ComponentStartup args)
|
2023-01-17 18:01:53 -04:00
|
|
|
{
|
2025-08-19 11:35:09 -07:00
|
|
|
if (CanFloat(entity))
|
|
|
|
|
FloatAnimation(entity, entity.Comp.Offset, entity.Comp.AnimationKey, entity.Comp.AnimationTime);
|
2023-01-17 18:01:53 -04:00
|
|
|
}
|
|
|
|
|
|
2025-08-19 11:35:09 -07:00
|
|
|
private void OnWeightlessnessChanged(Entity<FloatingVisualsComponent> entity, ref WeightlessnessChangedEvent args)
|
2023-01-17 18:01:53 -04:00
|
|
|
{
|
2025-08-19 11:35:09 -07:00
|
|
|
if (entity.Comp.CanFloat == args.Weightless)
|
|
|
|
|
return;
|
2023-01-17 18:01:53 -04:00
|
|
|
|
2025-08-19 11:35:09 -07:00
|
|
|
entity.Comp.CanFloat = CanFloat(entity);
|
|
|
|
|
Dirty(entity);
|
2023-01-17 18:01:53 -04:00
|
|
|
|
2025-08-19 11:35:09 -07:00
|
|
|
if (args.Weightless)
|
|
|
|
|
FloatAnimation(entity, entity.Comp.Offset, entity.Comp.AnimationKey, entity.Comp.AnimationTime);
|
2023-01-17 18:01:53 -04:00
|
|
|
}
|
|
|
|
|
}
|