Implement ECS alternative for IDragDropOn and fully ECS disposal units (#6380)

Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
Julian Giebel
2022-01-30 17:53:22 +01:00
committed by GitHub
parent 1bece1c1ba
commit 305cff2659
10 changed files with 147 additions and 128 deletions

View File

@@ -9,10 +9,13 @@ using Content.Server.Disposal.Unit.Components;
using Content.Server.DoAfter;
using Content.Server.Hands.Components;
using Content.Server.Power.Components;
using Content.Server.UserInterface;
using Content.Shared.ActionBlocker;
using Content.Shared.Acts;
using Content.Shared.Atmos;
using Content.Shared.Disposal;
using Content.Shared.Disposal.Components;
using Content.Shared.DragDrop;
using Content.Shared.Interaction;
using Content.Shared.Item;
using Content.Shared.Movement;
@@ -20,6 +23,7 @@ using Content.Shared.Popups;
using Content.Shared.Throwing;
using Content.Shared.Verbs;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
@@ -27,6 +31,7 @@ using Robust.Shared.Localization;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Player;
using Robust.Shared.Random;
namespace Content.Server.Disposal.Unit.EntitySystems
@@ -60,6 +65,8 @@ namespace Content.Server.Disposal.Unit.EntitySystems
SubscribeLocalEvent<DisposalUnitComponent, ActivateInWorldEvent>(HandleActivate);
SubscribeLocalEvent<DisposalUnitComponent, InteractHandEvent>(HandleInteractHand);
SubscribeLocalEvent<DisposalUnitComponent, InteractUsingEvent>(HandleInteractUsing);
SubscribeLocalEvent<DisposalUnitComponent, DragDropEvent>(HandleDragDropOn);
SubscribeLocalEvent<DisposalUnitComponent, DestructionEventArgs>(HandleDestruction);
// Verbs
SubscribeLocalEvent<DisposalUnitComponent, GetAlternativeVerbsEvent>(AddFlushEjectVerbs);
@@ -67,11 +74,14 @@ namespace Content.Server.Disposal.Unit.EntitySystems
// Units
SubscribeLocalEvent<DoInsertDisposalUnitEvent>(DoInsertDisposalUnit);
//UI
SubscribeLocalEvent<DisposalUnitComponent, SharedDisposalUnitComponent.UiButtonPressedMessage>(OnUiButtonPressed);
}
private void AddFlushEjectVerbs(EntityUid uid, DisposalUnitComponent component, GetAlternativeVerbsEvent args)
{
if (!args.CanAccess || !args.CanInteract || component.ContainedEntities.Count == 0)
if (!args.CanAccess || !args.CanInteract || component.Container.ContainedEntities.Count == 0)
return;
// Verbs to flush the unit
@@ -96,7 +106,7 @@ namespace Content.Server.Disposal.Unit.EntitySystems
// unwilling to accept that this is where they belong and don't want to accidentally climb inside.
if (!args.CanAccess ||
!args.CanInteract ||
component.ContainedEntities.Contains(args.User) ||
component.Container.ContainedEntities.Contains(args.User) ||
!_actionBlockerSystem.CanMove(args.User))
return;
@@ -142,6 +152,35 @@ namespace Content.Server.Disposal.Unit.EntitySystems
}
#region UI Handlers
private void OnUiButtonPressed(EntityUid uid, DisposalUnitComponent component, SharedDisposalUnitComponent.UiButtonPressedMessage args)
{
if (args.Session.AttachedEntity is not {Valid: true} player)
{
return;
}
if (!_actionBlockerSystem.CanInteract(player) || !_actionBlockerSystem.CanUse(player))
{
return;
}
switch (args.Button)
{
case SharedDisposalUnitComponent.UiButton.Eject:
TryEjectContents(component);
break;
case SharedDisposalUnitComponent.UiButton.Engage:
ToggleEngage(component);
break;
case SharedDisposalUnitComponent.UiButton.Power:
TogglePower(component);
SoundSystem.Play(Filter.Pvs(component.Owner), "/Audio/Machines/machine_switch.ogg", component.Owner, AudioParams.Default.WithVolume(-2f));
break;
default:
throw new ArgumentOutOfRangeException();
}
}
public void ToggleEngage(DisposalUnitComponent component)
{
component.Engaged ^= true;
@@ -180,7 +219,7 @@ namespace Content.Server.Disposal.Unit.EntitySystems
if (IsValidInteraction(args))
{
component.UserInterface?.Open(actor.PlayerSession);
component.Owner.GetUIOrNull(SharedDisposalUnitComponent.DisposalUnitUiKey.Key)?.Open(actor.PlayerSession);
}
}
@@ -191,7 +230,7 @@ namespace Content.Server.Disposal.Unit.EntitySystems
// Duplicated code here, not sure how else to get actor inside to make UserInterface happy.
if (!IsValidInteraction(args)) return;
component.UserInterface?.Open(actor.PlayerSession);
component.Owner.GetUIOrNull(SharedDisposalUnitComponent.DisposalUnitUiKey.Key)?.Open(actor.PlayerSession);
args.Handled = true;
}
@@ -230,11 +269,6 @@ namespace Content.Server.Disposal.Unit.EntitySystems
{
component.Container = component.Owner.EnsureContainer<Container>(component.Name);
if (component.UserInterface != null)
{
component.UserInterface.OnReceiveMessage += component.OnUiReceiveMessage;
}
UpdateInterface(component, component.Powered);
if (!EntityManager.HasComponent<AnchorableComponent>(component.Owner))
@@ -250,7 +284,7 @@ namespace Content.Server.Disposal.Unit.EntitySystems
component.Container.ForceRemove(entity);
}
component.UserInterface?.CloseAll();
component.Owner.GetUIOrNull(SharedDisposalUnitComponent.DisposalUnitUiKey.Key)?.CloseAll();
component.AutomaticEngageToken?.Cancel();
component.AutomaticEngageToken = null;
@@ -264,6 +298,8 @@ namespace Content.Server.Disposal.Unit.EntitySystems
if (!component.Running)
return;
component.Powered = args.Powered;
// TODO: Need to check the other stuff.
if (!args.Powered)
{
@@ -318,6 +354,16 @@ namespace Content.Server.Disposal.Unit.EntitySystems
if (!args.Anchored)
TryEjectContents(component);
}
private void HandleDestruction(EntityUid uid, DisposalUnitComponent component, DestructionEventArgs args)
{
TryEjectContents(component);
}
private void HandleDragDropOn(EntityUid uid, DisposalUnitComponent component, DragDropEvent args)
{
args.Handled = TryInsert(component.Owner, args.Dragged, args.User);
}
#endregion
/// <summary>
@@ -328,6 +374,7 @@ namespace Content.Server.Disposal.Unit.EntitySystems
var oldPressure = component.Pressure;
component.Pressure = MathF.Min(1.0f, component.Pressure + PressurePerSecond * frameTime);
component.State = component.Pressure >= 1 ? SharedDisposalUnitComponent.PressureState.Ready : SharedDisposalUnitComponent.PressureState.Pressurizing;
var state = component.State;
@@ -399,13 +446,13 @@ namespace Content.Server.Disposal.Unit.EntitySystems
return true;
}
public void TryInsert(EntityUid unitId, EntityUid toInsertId, EntityUid userId, DisposalUnitComponent? unit = null)
public bool TryInsert(EntityUid unitId, EntityUid toInsertId, EntityUid userId, DisposalUnitComponent? unit = null)
{
if (!Resolve(unitId, ref unit))
return;
return false;
if (!CanInsert(unit, toInsertId))
return;
return false;
var delay = userId == toInsertId ? unit.EntryDelay : unit.DraggedEntryDelay;
var ev = new DoInsertDisposalUnitEvent(userId, toInsertId, unitId);
@@ -413,7 +460,7 @@ namespace Content.Server.Disposal.Unit.EntitySystems
if (delay <= 0)
{
DoInsertDisposalUnit(ev);
return;
return true;
}
// Can't check if our target AND disposals moves currently so we'll just check target.
@@ -429,6 +476,7 @@ namespace Content.Server.Disposal.Unit.EntitySystems
};
_doAfterSystem.DoAfter(doAfterArgs);
return true;
}
@@ -465,6 +513,7 @@ namespace Content.Server.Disposal.Unit.EntitySystems
component.AutomaticEngageToken = null;
component.Pressure = 0;
component.State = component.Pressure >= 1 ? SharedDisposalUnitComponent.PressureState.Ready : SharedDisposalUnitComponent.PressureState.Pressurizing;
component.Engaged = false;
@@ -479,7 +528,7 @@ namespace Content.Server.Disposal.Unit.EntitySystems
{
var stateString = Loc.GetString($"{component.State}");
var state = new SharedDisposalUnitComponent.DisposalUnitBoundUserInterfaceState(EntityManager.GetComponent<MetaDataComponent>(component.Owner).EntityName, stateString, EstimatedFullPressure(component), powered, component.Engaged);
component.UserInterface?.SetState(state);
component.Owner.GetUIOrNull(SharedDisposalUnitComponent.DisposalUnitUiKey.Key)?.SetState(state);
}
private TimeSpan EstimatedFullPressure(DisposalUnitComponent component)
@@ -531,7 +580,7 @@ namespace Content.Server.Disposal.Unit.EntitySystems
return;
}
if (component.ContainedEntities.Count > 0)
if (component.Container.ContainedEntities.Count > 0)
{
appearance.SetData(SharedDisposalUnitComponent.Visuals.Light, SharedDisposalUnitComponent.LightState.Full);
return;
@@ -546,7 +595,7 @@ namespace Content.Server.Disposal.Unit.EntitySystems
{
component.Container.Remove(entity);
if (component.ContainedEntities.Count == 0)
if (component.Container.ContainedEntities.Count == 0)
{
component.AutomaticEngageToken?.Cancel();
component.AutomaticEngageToken = null;
@@ -608,14 +657,14 @@ namespace Content.Server.Disposal.Unit.EntitySystems
/// </summary>
public void TryQueueEngage(DisposalUnitComponent component)
{
if (component.Deleted || !component.Powered && component.ContainedEntities.Count == 0)
if (component.Deleted || !component.Powered && component.Container.ContainedEntities.Count == 0)
{
return;
}
component.AutomaticEngageToken = new CancellationTokenSource();
component.Owner.SpawnTimer(component._automaticEngageTime, () =>
component.Owner.SpawnTimer(component.AutomaticEngageTime, () =>
{
if (!TryFlush(component))
{
@@ -630,7 +679,7 @@ namespace Content.Server.Disposal.Unit.EntitySystems
if (EntityManager.TryGetComponent(entity, out ActorComponent? actor))
{
component.UserInterface?.Close(actor.PlayerSession);
component.Owner.GetUIOrNull(SharedDisposalUnitComponent.DisposalUnitUiKey.Key)?.Close(actor.PlayerSession);
}
UpdateVisualState(component);