Fix disposal units in general (#1552)

* Makes disposal units able to be entered when unpowered

* Make the unit's light turn off when unpowered

* Remove event handlers on component removal

* Make the disposal unit's lever engage when queueing an auto engage

* Autoengaging the lever on insert was a mistake

* Make the engage button active when engaged

* Make the engage button toggleable

* Fix nullable error
This commit is contained in:
DrSmugleaf
2020-07-31 14:50:46 +02:00
committed by GitHub
parent eb09281a18
commit 7a983062a9
3 changed files with 73 additions and 25 deletions

View File

@@ -65,7 +65,11 @@ namespace Content.Client.GameObjects.Components.Disposal
Children = Children =
{ {
new Label {Text = Loc.GetString("Handle:")}, new Label {Text = Loc.GetString("Handle:")},
(Engage = new Button {Text = Loc.GetString("Engage")}) (Engage = new Button
{
Text = Loc.GetString("Engage"),
ToggleMode = true
})
} }
}, },
new Control {CustomMinimumSize = (0, 10)}, new Control {CustomMinimumSize = (0, 10)},
@@ -135,6 +139,7 @@ namespace Content.Client.GameObjects.Components.Disposal
_unitState.Text = state.UnitState; _unitState.Text = state.UnitState;
UpdatePressureBar(state.Pressure); UpdatePressureBar(state.Pressure);
Power.Pressed = state.Powered; Power.Pressed = state.Powered;
Engage.Pressed = state.Engaged;
} }
} }
} }

View File

@@ -38,6 +38,7 @@ namespace Content.Server.GameObjects.Components.Disposal
{ {
#pragma warning disable 649 #pragma warning disable 649
[Dependency] private readonly IServerNotifyManager _notifyManager = default!; [Dependency] private readonly IServerNotifyManager _notifyManager = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
#pragma warning restore 649 #pragma warning restore 649
public override string Name => "DisposalUnit"; public override string Name => "DisposalUnit";
@@ -116,7 +117,7 @@ namespace Content.Server.GameObjects.Components.Disposal
public bool CanInsert(IEntity entity) public bool CanInsert(IEntity entity)
{ {
if (!Powered || !Anchored) if (!Anchored)
{ {
return false; return false;
} }
@@ -191,11 +192,20 @@ namespace Content.Server.GameObjects.Components.Disposal
return _pressure >= 1 && Powered && Anchored; return _pressure >= 1 && Powered && Anchored;
} }
private void ToggleEngage()
{
Engaged ^= true;
if (Engaged)
{
TryFlush();
}
}
public bool TryFlush() public bool TryFlush()
{ {
if (!CanFlush()) if (!CanFlush())
{ {
Engaged = true;
return false; return false;
} }
@@ -252,7 +262,8 @@ namespace Content.Server.GameObjects.Components.Disposal
private DisposalUnitBoundUserInterfaceState GetInterfaceState() private DisposalUnitBoundUserInterfaceState GetInterfaceState()
{ {
return new DisposalUnitBoundUserInterfaceState(Owner.Name, Loc.GetString($"{State}"), _pressure, Powered); var state = Loc.GetString($"{State}");
return new DisposalUnitBoundUserInterfaceState(Owner.Name, state, _pressure, Powered, Engaged);
} }
private void UpdateInterface() private void UpdateInterface()
@@ -300,7 +311,7 @@ namespace Content.Server.GameObjects.Components.Disposal
TryEjectContents(); TryEjectContents();
break; break;
case UiButton.Engage: case UiButton.Engage:
TryFlush(); ToggleEngage();
break; break;
case UiButton.Power: case UiButton.Power:
TogglePower(); TogglePower();
@@ -324,9 +335,6 @@ namespace Content.Server.GameObjects.Components.Disposal
return; return;
} }
appearance.SetData(Visuals.Handle, Engaged
? HandleState.Engaged
: HandleState.Normal);
if (!Anchored) if (!Anchored)
{ {
@@ -335,26 +343,37 @@ namespace Content.Server.GameObjects.Components.Disposal
appearance.SetData(Visuals.Light, LightState.Off); appearance.SetData(Visuals.Light, LightState.Off);
return; return;
} }
else
{
appearance.SetData(Visuals.VisualState, VisualState.Anchored);
}
appearance.SetData(Visuals.Handle, Engaged
? HandleState.Engaged
: HandleState.Normal);
if (!Powered)
{
appearance.SetData(Visuals.Light, LightState.Off);
return;
}
if (flush) if (flush)
{ {
appearance.SetData(Visuals.VisualState, VisualState.Flushing); appearance.SetData(Visuals.VisualState, VisualState.Flushing);
appearance.SetData(Visuals.Light, LightState.Off); appearance.SetData(Visuals.Light, LightState.Off);
return;
} }
else
if (ContainedEntities.Count > 0)
{ {
appearance.SetData(Visuals.VisualState, VisualState.Anchored); appearance.SetData(Visuals.Light, LightState.Full);
return;
if (ContainedEntities.Count > 0)
{
appearance.SetData(Visuals.Light, LightState.Full);
return;
}
appearance.SetData(Visuals.Light, _pressure < 1
? LightState.Charging
: LightState.Ready);
} }
appearance.SetData(Visuals.Light, _pressure < 1
? LightState.Charging
: LightState.Ready);
} }
public void Update(float frameTime) public void Update(float frameTime)
@@ -383,6 +402,11 @@ namespace Content.Server.GameObjects.Components.Disposal
UpdateInterface(); UpdateInterface();
} }
private void PowerStateChanged(object? sender, PowerStateEventArgs args)
{
UpdateVisualState();
}
public override void ExposeData(ObjectSerializer serializer) public override void ExposeData(ObjectSerializer serializer)
{ {
base.ExposeData(serializer); base.ExposeData(serializer);
@@ -423,14 +447,30 @@ namespace Content.Server.GameObjects.Components.Disposal
base.Startup(); base.Startup();
Owner.EnsureComponent<AnchorableComponent>(); Owner.EnsureComponent<AnchorableComponent>();
var collidable = Owner.EnsureComponent<CollidableComponent>();
var collidable = Owner.EnsureComponent<CollidableComponent>();
collidable.AnchoredChanged += UpdateVisualState; collidable.AnchoredChanged += UpdateVisualState;
if (Owner.TryGetComponent(out PowerReceiverComponent receiver))
{
receiver.OnPowerStateChanged += PowerStateChanged;
}
UpdateVisualState(); UpdateVisualState();
} }
public override void OnRemove() public override void OnRemove()
{ {
if (Owner.TryGetComponent(out ICollidableComponent collidable))
{
collidable.AnchoredChanged -= UpdateVisualState;
}
if (Owner.TryGetComponent(out PowerReceiverComponent receiver))
{
receiver.OnPowerStateChanged -= PowerStateChanged;
}
foreach (var entity in _container.ContainedEntities.ToArray()) foreach (var entity in _container.ContainedEntities.ToArray())
{ {
_container.ForceRemove(entity); _container.ForceRemove(entity);
@@ -453,15 +493,14 @@ namespace Content.Server.GameObjects.Components.Disposal
switch (message) switch (message)
{ {
case RelayMovementEntityMessage msg: case RelayMovementEntityMessage msg:
var timing = IoCManager.Resolve<IGameTiming>();
if (Engaged || if (Engaged ||
!msg.Entity.HasComponent<HandsComponent>() || !msg.Entity.HasComponent<HandsComponent>() ||
timing.CurTime < _lastExitAttempt + ExitAttemptDelay) _gameTiming.CurTime < _lastExitAttempt + ExitAttemptDelay)
{ {
break; break;
} }
_lastExitAttempt = timing.CurTime; _lastExitAttempt = _gameTiming.CurTime;
Remove(msg.Entity); Remove(msg.Entity);
break; break;
} }
@@ -556,6 +595,7 @@ namespace Content.Server.GameObjects.Components.Disposal
protected override void Activate(IEntity user, DisposalUnitComponent component) protected override void Activate(IEntity user, DisposalUnitComponent component)
{ {
component.Engaged = true;
component.TryFlush(); component.TryFlush();
} }
} }

View File

@@ -63,13 +63,16 @@ namespace Content.Shared.GameObjects.Components.Disposal
public readonly string UnitState; public readonly string UnitState;
public readonly float Pressure; public readonly float Pressure;
public readonly bool Powered; public readonly bool Powered;
public readonly bool Engaged;
public DisposalUnitBoundUserInterfaceState(string unitName, string unitState, float pressure, bool powered) public DisposalUnitBoundUserInterfaceState(string unitName, string unitState, float pressure, bool powered,
bool engaged)
{ {
UnitName = unitName; UnitName = unitName;
UnitState = unitState; UnitState = unitState;
Pressure = pressure; Pressure = pressure;
Powered = powered; Powered = powered;
Engaged = engaged;
} }
} }