Files
OldThink/Content.Client/Orbit/OrbitVisualsSystem.cs

152 lines
5.5 KiB
C#
Raw Permalink Normal View History

using System.Numerics;
2022-02-19 12:16:44 -07:00
using Content.Shared.Follower.Components;
using Robust.Client.Animations;
using Robust.Client.GameObjects;
using Robust.Shared.Animations;
using Robust.Shared.Random;
namespace Content.Client.Orbit;
2022-02-19 17:42:11 -07:00
public sealed class OrbitVisualsSystem : EntitySystem
2022-02-19 12:16:44 -07:00
{
[Dependency] private readonly IRobustRandom _robustRandom = default!;
[Dependency] private readonly AnimationPlayerSystem _animations = default!;
2022-02-19 12:16:44 -07:00
private readonly string _orbitAnimationKey = "orbiting";
private readonly string _orbitStopKey = "orbiting_stop";
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<OrbitVisualsComponent, ComponentInit>(OnComponentInit);
2022-02-19 17:42:11 -07:00
SubscribeLocalEvent<OrbitVisualsComponent, ComponentRemove>(OnComponentRemove);
2022-02-19 12:16:44 -07:00
SubscribeLocalEvent<OrbitVisualsComponent, AnimationCompletedEvent>(OnAnimationCompleted);
}
private void OnComponentInit(EntityUid uid, OrbitVisualsComponent component, ComponentInit args)
{
component.OrbitDistance =
_robustRandom.NextFloat(0.75f * component.OrbitDistance, 1.25f * component.OrbitDistance);
component.OrbitLength = _robustRandom.NextFloat(0.5f * component.OrbitLength, 1.5f * component.OrbitLength);
2022-06-22 19:36:30 -07:00
if (TryComp<SpriteComponent>(uid, out var sprite))
{
sprite.EnableDirectionOverride = true;
sprite.DirectionOverride = Direction.South;
}
var animationPlayer = EnsureComp<AnimationPlayerComponent>(uid);
if (_animations.HasRunningAnimation(uid, animationPlayer, _orbitAnimationKey))
2022-02-19 17:42:11 -07:00
return;
2022-02-19 12:16:44 -07:00
if (_animations.HasRunningAnimation(uid, animationPlayer, _orbitStopKey))
2022-02-19 12:16:44 -07:00
{
_animations.Stop(uid, animationPlayer, _orbitStopKey);
2022-02-19 12:16:44 -07:00
}
2022-02-19 17:42:11 -07:00
_animations.Play(uid, animationPlayer, GetOrbitAnimation(component), _orbitAnimationKey);
2022-02-19 12:16:44 -07:00
}
2022-02-19 17:42:11 -07:00
private void OnComponentRemove(EntityUid uid, OrbitVisualsComponent component, ComponentRemove args)
2022-02-19 12:16:44 -07:00
{
2022-06-22 19:36:30 -07:00
if (!TryComp<SpriteComponent>(uid, out var sprite))
2022-02-19 12:16:44 -07:00
return;
2022-06-22 19:36:30 -07:00
sprite.EnableDirectionOverride = false;
var animationPlayer = EnsureComp<AnimationPlayerComponent>(uid);
if (_animations.HasRunningAnimation(uid, animationPlayer, _orbitAnimationKey))
2022-02-19 17:42:11 -07:00
{
_animations.Stop(uid, animationPlayer, _orbitAnimationKey);
2022-02-19 17:42:11 -07:00
}
2022-02-19 12:16:44 -07:00
if (!_animations.HasRunningAnimation(uid, animationPlayer, _orbitStopKey))
2022-02-19 12:16:44 -07:00
{
_animations.Play(uid, animationPlayer, GetStopAnimation(component, sprite), _orbitStopKey);
2022-02-19 17:42:11 -07:00
}
}
2022-02-19 12:16:44 -07:00
2022-02-19 17:42:11 -07:00
public override void FrameUpdate(float frameTime)
{
base.FrameUpdate(frameTime);
2022-02-19 12:16:44 -07:00
2023-01-15 13:38:53 +11:00
foreach (var (orbit, sprite) in EntityManager.EntityQuery<OrbitVisualsComponent, SpriteComponent>())
2022-02-19 12:16:44 -07:00
{
2022-02-19 17:42:11 -07:00
var angle = new Angle(Math.PI * 2 * orbit.Orbit);
var vec = angle.RotateVec(new Vector2(orbit.OrbitDistance, 0));
sprite.Rotation = angle;
sprite.Offset = vec;
2022-02-19 12:16:44 -07:00
}
}
private void OnAnimationCompleted(EntityUid uid, OrbitVisualsComponent component, AnimationCompletedEvent args)
{
if (args.Key == _orbitAnimationKey && TryComp(uid, out AnimationPlayerComponent? animationPlayer))
2022-02-19 12:16:44 -07:00
{
_animations.Play(uid, animationPlayer, GetOrbitAnimation(component), _orbitAnimationKey);
2022-02-19 12:16:44 -07:00
}
}
private Animation GetOrbitAnimation(OrbitVisualsComponent component)
{
var length = component.OrbitLength;
return new Animation()
{
Length = TimeSpan.FromSeconds(length),
AnimationTracks =
{
new AnimationTrackComponentProperty()
{
ComponentType = typeof(OrbitVisualsComponent),
Property = nameof(OrbitVisualsComponent.Orbit),
KeyFrames =
{
new AnimationTrackProperty.KeyFrame(0.0f, 0f),
new AnimationTrackProperty.KeyFrame(1.0f, length),
},
InterpolationMode = AnimationInterpolationMode.Linear
}
}
};
}
2023-01-15 13:38:53 +11:00
private Animation GetStopAnimation(OrbitVisualsComponent component, SpriteComponent sprite)
2022-02-19 12:16:44 -07:00
{
var length = component.OrbitStopLength;
return new Animation()
{
Length = TimeSpan.FromSeconds(length),
AnimationTracks =
{
new AnimationTrackComponentProperty()
{
2023-01-15 13:38:53 +11:00
ComponentType = typeof(SpriteComponent),
Property = nameof(SpriteComponent.Offset),
2022-02-19 12:16:44 -07:00
KeyFrames =
{
new AnimationTrackProperty.KeyFrame(sprite.Offset, 0f),
new AnimationTrackProperty.KeyFrame(Vector2.Zero, length),
},
InterpolationMode = AnimationInterpolationMode.Linear
},
new AnimationTrackComponentProperty()
{
2023-01-15 13:38:53 +11:00
ComponentType = typeof(SpriteComponent),
Property = nameof(SpriteComponent.Rotation),
2022-02-19 12:16:44 -07:00
KeyFrames =
{
new AnimationTrackProperty.KeyFrame(sprite.Rotation.Reduced(), 0f),
new AnimationTrackProperty.KeyFrame(Angle.Zero, length),
},
InterpolationMode = AnimationInterpolationMode.Linear
}
}
};
}
}