Disposals refactor (#17803)

This commit is contained in:
metalgearsloth
2023-07-06 13:39:34 +10:00
committed by GitHub
parent 0790f31f21
commit 3eb93988e5
21 changed files with 1321 additions and 1261 deletions

View File

@@ -1,26 +0,0 @@
using Content.Shared.Disposal.Components;
using Robust.Client.Animations;
using Robust.Shared.Audio;
namespace Content.Client.Disposal.Components
{
[RegisterComponent]
[ComponentReference(typeof(SharedDisposalUnitComponent))]
public sealed class DisposalUnitComponent : SharedDisposalUnitComponent
{
[DataField("flushSound")]
public readonly SoundSpecifier? FlushSound;
public Animation FlushAnimation = default!;
public DisposalUnitBoundUserInterfaceState? UiState;
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
base.HandleComponentState(curState, nextState);
if (curState is not DisposalUnitComponentState state) return;
RecentlyEjected = state.RecentlyEjected;
}
}
}

View File

@@ -0,0 +1,9 @@
using Content.Shared.Disposal.Components;
namespace Content.Client.Disposal;
[RegisterComponent]
public sealed class DisposalUnitComponent : SharedDisposalUnitComponent
{
}

View File

@@ -1,8 +1,12 @@
using Content.Client.Disposal.Components;
using Content.Client.Disposal.UI;
using Content.Shared.Disposal;
using Content.Shared.Disposal.Components;
using Content.Shared.DragDrop;
using Content.Shared.Emag.Systems;
using Robust.Client.GameObjects;
using Robust.Client.Animations;
using Robust.Client.Graphics;
using Robust.Shared.GameStates;
using Robust.Shared.Physics.Events;
using static Content.Shared.Disposal.Components.SharedDisposalUnitComponent;
namespace Content.Client.Disposal.Systems;
@@ -12,131 +16,65 @@ public sealed class DisposalUnitSystem : SharedDisposalUnitSystem
[Dependency] private readonly AppearanceSystem _appearanceSystem = default!;
[Dependency] private readonly AnimationPlayerSystem _animationSystem = default!;
[Dependency] private readonly SharedAudioSystem _audioSystem = default!;
private const string AnimationKey = "disposal_unit_animation";
private readonly List<EntityUid> _pressuringDisposals = new();
private const string AnimationKey = "disposal_unit_animation";
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<DisposalUnitComponent, ComponentHandleState>(OnHandleState);
SubscribeLocalEvent<DisposalUnitComponent, PreventCollideEvent>(OnPreventCollide);
SubscribeLocalEvent<DisposalUnitComponent, CanDropTargetEvent>(OnCanDragDropOn);
SubscribeLocalEvent<DisposalUnitComponent, GotEmaggedEvent>(OnEmagged);
SubscribeLocalEvent<DisposalUnitComponent, ComponentInit>(OnComponentInit);
SubscribeLocalEvent<DisposalUnitComponent, AppearanceChangeEvent>(OnAppearanceChange);
}
public void UpdateActive(EntityUid disposalEntity, bool active)
private void OnHandleState(EntityUid uid, DisposalUnitComponent component, ref ComponentHandleState args)
{
if (active)
{
if (!_pressuringDisposals.Contains(disposalEntity))
_pressuringDisposals.Add(disposalEntity);
}
else
{
_pressuringDisposals.Remove(disposalEntity);
}
}
public override void FrameUpdate(float frameTime)
{
base.FrameUpdate(frameTime);
for (var i = _pressuringDisposals.Count - 1; i >= 0; i--)
{
var disposal = _pressuringDisposals[i];
if (!UpdateInterface(disposal))
continue;
_pressuringDisposals.RemoveAt(i);
}
}
private bool UpdateInterface(EntityUid disposalUnit)
{
if (!TryComp(disposalUnit, out DisposalUnitComponent? component) || component.Deleted)
return true;
if (component.Deleted)
return true;
if (!TryComp(disposalUnit, out ClientUserInterfaceComponent? userInterface))
return true;
var state = component.UiState;
if (state == null)
return true;
foreach (var inter in userInterface.Interfaces)
{
if (inter is DisposalUnitBoundUserInterface boundInterface)
{
return boundInterface.UpdateWindowState(state) != false;
}
}
return true;
}
private void OnComponentInit(EntityUid uid, DisposalUnitComponent disposalUnit, ComponentInit args)
{
if (!TryComp<SpriteComponent>(uid, out var sprite))
if (args.Current is not DisposalUnitComponentState state)
return;
if (!sprite.LayerMapTryGet(DisposalUnitVisualLayers.Base, out var baseLayerIdx))
return; // Couldn't find the "normal" layer to return to after flush animation
if (!sprite.LayerMapTryGet(DisposalUnitVisualLayers.BaseFlush, out var flushLayerIdx))
return; // Couldn't find the flush animation layer
var originalBaseState = sprite.LayerGetState(baseLayerIdx);
var flushState = sprite.LayerGetState(flushLayerIdx);
// Setup the flush animation to play
disposalUnit.FlushAnimation = new Animation
{
Length = TimeSpan.FromSeconds(disposalUnit.FlushTime),
AnimationTracks = {
new AnimationTrackSpriteFlick {
LayerKey = DisposalUnitVisualLayers.BaseFlush,
KeyFrames = {
// Play the flush animation
new AnimationTrackSpriteFlick.KeyFrame(flushState, 0),
// Return to base state (though, depending on how the unit is
// configured we might get an appearence change event telling
// us to go to charging state)
new AnimationTrackSpriteFlick.KeyFrame(originalBaseState, disposalUnit.FlushTime)
}
},
}
};
if (disposalUnit.FlushSound != null)
{
disposalUnit.FlushAnimation.AnimationTracks.Add(
new AnimationTrackPlaySound
{
KeyFrames = {
new AnimationTrackPlaySound.KeyFrame(_audioSystem.GetSound(disposalUnit.FlushSound), 0)
}
});
}
EnsureComp<AnimationPlayerComponent>(uid);
UpdateState(uid, disposalUnit, sprite);
component.FlushSound = state.FlushSound;
component.State = state.State;
component.NextPressurized = state.NextPressurized;
component.AutomaticEngageTime = state.AutomaticEngageTime;
component.NextFlush = state.NextFlush;
component.Powered = state.Powered;
component.Engaged = state.Engaged;
component.RecentlyEjected.Clear();
component.RecentlyEjected.AddRange(state.RecentlyEjected);
}
private void OnAppearanceChange(EntityUid uid, DisposalUnitComponent unit, ref AppearanceChangeEvent args)
public override void DoInsertDisposalUnit(EntityUid uid, EntityUid toInsert, EntityUid user, SharedDisposalUnitComponent? disposal = null)
{
return;
}
private void OnComponentInit(EntityUid uid, SharedDisposalUnitComponent sharedDisposalUnit, ComponentInit args)
{
if (!TryComp<SpriteComponent>(uid, out var sprite) || !TryComp<AppearanceComponent>(uid, out var appearance))
return;
UpdateState(uid, sharedDisposalUnit, sprite, appearance);
}
private void OnAppearanceChange(EntityUid uid, SharedDisposalUnitComponent unit, ref AppearanceChangeEvent args)
{
if (args.Sprite == null)
{
return;
}
UpdateState(uid, unit, args.Sprite);
UpdateState(uid, unit, args.Sprite, args.Component);
}
// Update visuals and tick animation
private void UpdateState(EntityUid uid, DisposalUnitComponent unit, SpriteComponent sprite)
/// <summary>
/// Update visuals and tick animation
/// </summary>
private void UpdateState(EntityUid uid, SharedDisposalUnitComponent unit, SpriteComponent sprite, AppearanceComponent appearance)
{
if (!_appearanceSystem.TryGetData<VisualState>(uid, Visuals.VisualState, out var state))
if (!_appearanceSystem.TryGetData<VisualState>(uid, Visuals.VisualState, out var state, appearance))
{
return;
}
@@ -144,24 +82,69 @@ public sealed class DisposalUnitSystem : SharedDisposalUnitSystem
sprite.LayerSetVisible(DisposalUnitVisualLayers.Unanchored, state == VisualState.UnAnchored);
sprite.LayerSetVisible(DisposalUnitVisualLayers.Base, state == VisualState.Anchored);
sprite.LayerSetVisible(DisposalUnitVisualLayers.BaseCharging, state == VisualState.Charging);
sprite.LayerSetVisible(DisposalUnitVisualLayers.BaseFlush, state == VisualState.Flushing);
sprite.LayerSetVisible(DisposalUnitVisualLayers.BaseFlush, state is VisualState.Flushing or VisualState.Charging);
// This is a transient state so not too worried about replaying in range.
if (state == VisualState.Flushing)
{
if (!_animationSystem.HasRunningAnimation(uid, AnimationKey))
{
_animationSystem.Play(uid, unit.FlushAnimation, AnimationKey);
var flushState = new RSI.StateId("disposal-flush");
// Setup the flush animation to play
var anim = new Animation
{
Length = unit.FlushDelay,
AnimationTracks =
{
new AnimationTrackSpriteFlick
{
LayerKey = DisposalUnitVisualLayers.BaseFlush,
KeyFrames =
{
// Play the flush animation
new AnimationTrackSpriteFlick.KeyFrame(flushState, 0),
// Return to base state (though, depending on how the unit is
// configured we might get an appearance change event telling
// us to go to charging state)
new AnimationTrackSpriteFlick.KeyFrame("disposal-charging", (float) unit.FlushDelay.TotalSeconds)
}
},
}
};
if (unit.FlushSound != null)
{
anim.AnimationTracks.Add(
new AnimationTrackPlaySound
{
KeyFrames =
{
new AnimationTrackPlaySound.KeyFrame(_audioSystem.GetSound(unit.FlushSound), 0)
}
});
}
_animationSystem.Play(uid, anim, AnimationKey);
}
}
else if (state == VisualState.Charging)
{
sprite.LayerSetState(DisposalUnitVisualLayers.BaseFlush, new RSI.StateId("disposal-charging"));
}
else
{
_animationSystem.Stop(uid, AnimationKey);
}
if (!_appearanceSystem.TryGetData<HandleState>(uid, Visuals.Handle, out var handleState))
if (!_appearanceSystem.TryGetData<HandleState>(uid, Visuals.Handle, out var handleState, appearance))
{
handleState = HandleState.Normal;
}
sprite.LayerSetVisible(DisposalUnitVisualLayers.OverlayEngaged, handleState != HandleState.Normal);
if (!_appearanceSystem.TryGetData<LightStates>(uid, Visuals.Light, out var lightState))
if (!_appearanceSystem.TryGetData<LightStates>(uid, Visuals.Light, out var lightState, appearance))
{
lightState = LightStates.Off;
}

View File

@@ -1,6 +1,6 @@
using Content.Client.Disposal.Components;
using Content.Client.Disposal.Systems;
using Content.Shared.Disposal;
using Content.Shared.Disposal.Components;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Robust.Client.UserInterface.Controls;
@@ -16,7 +16,9 @@ namespace Content.Client.Disposal.UI
{
[Dependency] private readonly IEntityManager _entityManager = default!;
// What are you doing here
public MailingUnitWindow? MailingUnitWindow;
public DisposalUnitWindow? DisposalUnitWindow;
public DisposalUnitBoundUserInterface(ClientUserInterfaceComponent owner, Enum uiKey) : base(owner, uiKey)
@@ -76,41 +78,27 @@ namespace Content.Client.Disposal.UI
return;
}
var entityId = Owner.Owner;
if (!_entityManager.TryGetComponent(entityId, out DisposalUnitComponent? component))
return;
switch (state)
{
case MailingUnitBoundUserInterfaceState mailingUnitState:
MailingUnitWindow?.UpdateState(mailingUnitState);
component.UiState = mailingUnitState.DisposalState;
break;
case DisposalUnitBoundUserInterfaceState disposalUnitState:
DisposalUnitWindow?.UpdateState(disposalUnitState);
component.UiState = disposalUnitState;
break;
}
_entityManager.System<DisposalUnitSystem>().UpdateActive(entityId, true);
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (!disposing) return;
if (!disposing)
return;
MailingUnitWindow?.Dispose();
DisposalUnitWindow?.Dispose();
}
public bool? UpdateWindowState(DisposalUnitBoundUserInterfaceState state)
{
return UiKey is DisposalUnitUiKey
? DisposalUnitWindow?.UpdateState(state)
: MailingUnitWindow?.UpdatePressure(state.FullPressureTime);
}
}
}

View File

@@ -2,6 +2,7 @@ using Content.Shared.Disposal.Components;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Timing;
using static Content.Shared.Disposal.Components.SharedDisposalUnitComponent;
namespace Content.Client.Disposal.UI
@@ -12,6 +13,8 @@ namespace Content.Client.Disposal.UI
[GenerateTypedNameReferences]
public sealed partial class DisposalUnitWindow : DefaultWindow
{
public TimeSpan FullPressure;
public DisposalUnitWindow()
{
IoCManager.InjectDependencies(this);
@@ -22,14 +25,19 @@ namespace Content.Client.Disposal.UI
/// Update the interface state for the disposals window.
/// </summary>
/// <returns>true if we should stop updating every frame.</returns>
public bool UpdateState(DisposalUnitBoundUserInterfaceState state)
public void UpdateState(DisposalUnitBoundUserInterfaceState state)
{
Title = state.UnitName;
UnitState.Text = state.UnitState;
Power.Pressed = state.Powered;
Engage.Pressed = state.Engaged;
FullPressure = state.FullPressureTime;
}
return !state.Powered || PressureBar.UpdatePressure(state.FullPressureTime);
protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
PressureBar.UpdatePressure(FullPressure);
}
}
}