Fix foldables (#19717)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Content.Client.Storage.Components;
|
||||
using Content.Shared.Destructible;
|
||||
using Content.Shared.Foldable;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Lock;
|
||||
using Content.Shared.Movement.Events;
|
||||
@@ -23,6 +24,7 @@ public sealed class EntityStorageSystem : SharedEntityStorageSystem
|
||||
SubscribeLocalEvent<EntityStorageComponent, DestructionEventArgs>(OnDestruction);
|
||||
SubscribeLocalEvent<EntityStorageComponent, GetVerbsEvent<InteractionVerb>>(AddToggleOpenVerb);
|
||||
SubscribeLocalEvent<EntityStorageComponent, ContainerRelayMovementEntityEvent>(OnRelayMovement);
|
||||
SubscribeLocalEvent<EntityStorageComponent, FoldAttemptEvent>(OnFoldAttempt);
|
||||
|
||||
SubscribeLocalEvent<EntityStorageComponent, ComponentGetState>(OnGetState);
|
||||
SubscribeLocalEvent<EntityStorageComponent, ComponentHandleState>(OnHandleState);
|
||||
|
||||
@@ -5,6 +5,7 @@ using Content.Server.Construction.Components;
|
||||
using Content.Server.Storage.Components;
|
||||
using Content.Server.Tools.Systems;
|
||||
using Content.Shared.Destructible;
|
||||
using Content.Shared.Foldable;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Lock;
|
||||
using Content.Shared.Movement.Events;
|
||||
@@ -35,6 +36,7 @@ public sealed class EntityStorageSystem : SharedEntityStorageSystem
|
||||
SubscribeLocalEvent<EntityStorageComponent, DestructionEventArgs>(OnDestruction);
|
||||
SubscribeLocalEvent<EntityStorageComponent, GetVerbsEvent<InteractionVerb>>(AddToggleOpenVerb);
|
||||
SubscribeLocalEvent<EntityStorageComponent, ContainerRelayMovementEntityEvent>(OnRelayMovement);
|
||||
SubscribeLocalEvent<EntityStorageComponent, FoldAttemptEvent>(OnFoldAttempt);
|
||||
|
||||
SubscribeLocalEvent<EntityStorageComponent, ComponentGetState>(OnGetState);
|
||||
SubscribeLocalEvent<EntityStorageComponent, ComponentHandleState>(OnHandleState);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using Content.Shared.Buckle.Components;
|
||||
using Content.Shared.Destructible;
|
||||
using Content.Shared.DragDrop;
|
||||
using Content.Shared.Foldable;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Storage;
|
||||
using Content.Shared.Verbs;
|
||||
@@ -31,6 +32,7 @@ public abstract partial class SharedBuckleSystem
|
||||
|
||||
SubscribeLocalEvent<StrapComponent, DragDropTargetEvent>(OnStrapDragDropTarget);
|
||||
SubscribeLocalEvent<StrapComponent, CanDropTargetEvent>(OnCanDropTarget);
|
||||
SubscribeLocalEvent<StrapComponent, FoldAttemptEvent>(OnAttemptFold);
|
||||
|
||||
SubscribeLocalEvent<StrapComponent, MoveEvent>(OnStrapMoveEvent);
|
||||
}
|
||||
@@ -115,7 +117,7 @@ public abstract partial class SharedBuckleSystem
|
||||
if (args.Handled)
|
||||
return;
|
||||
|
||||
ToggleBuckle(args.User, args.User, uid);
|
||||
args.Handled = ToggleBuckle(args.User, args.User, uid);
|
||||
}
|
||||
|
||||
private void AddStrapVerbs(EntityUid uid, StrapComponent component, GetVerbsEvent<InteractionVerb> args)
|
||||
@@ -199,6 +201,14 @@ public abstract partial class SharedBuckleSystem
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
private void OnAttemptFold(EntityUid uid, StrapComponent component, ref FoldAttemptEvent args)
|
||||
{
|
||||
if (args.Cancelled)
|
||||
return;
|
||||
|
||||
args.Cancelled = component.BuckledEntities.Count != 0;
|
||||
}
|
||||
|
||||
private void OnStrapDragDropTarget(EntityUid uid, StrapComponent component, ref DragDropTargetEvent args)
|
||||
{
|
||||
if (!StrapCanDragDropOn(uid, args.User, uid, args.Dragged, component))
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
using System.Linq;
|
||||
using Content.Shared.Buckle;
|
||||
using Content.Shared.Buckle.Components;
|
||||
using Content.Shared.Storage.Components;
|
||||
using Content.Shared.Verbs;
|
||||
using Robust.Shared.Containers;
|
||||
@@ -91,69 +89,61 @@ public sealed class FoldableSystem : EntitySystem
|
||||
args.Cancel();
|
||||
}
|
||||
|
||||
public bool TryToggleFold(EntityUid uid, FoldableComponent comp)
|
||||
public bool TryToggleFold(EntityUid uid, FoldableComponent comp)
|
||||
{
|
||||
return TrySetFolded(uid, comp, !comp.IsFolded);
|
||||
}
|
||||
|
||||
public bool CanToggleFold(EntityUid uid, FoldableComponent? fold = null)
|
||||
{
|
||||
if (!Resolve(uid, ref fold))
|
||||
return false;
|
||||
|
||||
// Can't un-fold in any container (locker, hands, inventory, whatever).
|
||||
if (_container.IsEntityInContainer(uid))
|
||||
return false;
|
||||
|
||||
var ev = new FoldAttemptEvent();
|
||||
RaiseLocalEvent(uid, ref ev);
|
||||
return !ev.Cancelled;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Try to fold/unfold
|
||||
/// </summary>
|
||||
public bool TrySetFolded(EntityUid uid, FoldableComponent comp, bool state)
|
||||
{
|
||||
if (state == comp.IsFolded)
|
||||
return false;
|
||||
|
||||
if (!CanToggleFold(uid, comp))
|
||||
return false;
|
||||
|
||||
SetFolded(uid, comp, state);
|
||||
return true;
|
||||
}
|
||||
|
||||
#region Verb
|
||||
|
||||
private void AddFoldVerb(EntityUid uid, FoldableComponent component, GetVerbsEvent<AlternativeVerb> args)
|
||||
{
|
||||
if (!args.CanAccess || !args.CanInteract || args.Hands == null || !CanToggleFold(uid, component))
|
||||
return;
|
||||
|
||||
AlternativeVerb verb = new()
|
||||
{
|
||||
return TrySetFolded(uid, comp, !comp.IsFolded);
|
||||
}
|
||||
Act = () => TryToggleFold(uid, component),
|
||||
Text = component.IsFolded ? Loc.GetString("unfold-verb") : Loc.GetString("fold-verb"),
|
||||
Icon = new SpriteSpecifier.Texture(new ("/Textures/Interface/VerbIcons/fold.svg.192dpi.png")),
|
||||
|
||||
public bool CanToggleFold(EntityUid uid, FoldableComponent? fold = null)
|
||||
{
|
||||
if (!Resolve(uid, ref fold))
|
||||
return false;
|
||||
// If the object is unfolded and they click it, they want to fold it, if it's folded, they want to pick it up
|
||||
Priority = component.IsFolded ? 0 : 2,
|
||||
};
|
||||
|
||||
// Can't un-fold in any container (locker, hands, inventory, whatever).
|
||||
if (_container.IsEntityInContainer(uid))
|
||||
return false;
|
||||
args.Verbs.Add(verb);
|
||||
}
|
||||
|
||||
// If an entity is buckled to the object we can't pick it up or fold it
|
||||
if (TryComp(uid, out StrapComponent? strap) && strap.BuckledEntities.Any())
|
||||
return false;
|
||||
|
||||
if (TryComp(uid, out SharedEntityStorageComponent? storage))
|
||||
{
|
||||
if (storage.Open || storage.Contents.ContainedEntities.Any())
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Try to fold/unfold
|
||||
/// </summary>
|
||||
public bool TrySetFolded(EntityUid uid, FoldableComponent comp, bool state)
|
||||
{
|
||||
if (state == comp.IsFolded)
|
||||
return false;
|
||||
|
||||
if (!CanToggleFold(uid, comp))
|
||||
return false;
|
||||
|
||||
SetFolded(uid, comp, state);
|
||||
return true;
|
||||
}
|
||||
|
||||
#region Verb
|
||||
|
||||
private void AddFoldVerb(EntityUid uid, FoldableComponent component, GetVerbsEvent<AlternativeVerb> args)
|
||||
{
|
||||
if (!args.CanAccess || !args.CanInteract || args.Hands == null || !CanToggleFold(uid, component))
|
||||
return;
|
||||
|
||||
AlternativeVerb verb = new()
|
||||
{
|
||||
Act = () => TryToggleFold(uid, component),
|
||||
Text = component.IsFolded ? Loc.GetString("unfold-verb") : Loc.GetString("fold-verb"),
|
||||
Icon = new SpriteSpecifier.Texture(new ("/Textures/Interface/VerbIcons/fold.svg.192dpi.png")),
|
||||
|
||||
// If the object is unfolded and they click it, they want to fold it, if it's folded, they want to pick it up
|
||||
Priority = component.IsFolded ? 0 : 2,
|
||||
};
|
||||
|
||||
args.Verbs.Add(verb);
|
||||
}
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public enum FoldedVisuals : byte
|
||||
@@ -161,3 +151,10 @@ public sealed class FoldableSystem : EntitySystem
|
||||
State
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event raised on an entity to determine if it can be folded.
|
||||
/// </summary>
|
||||
/// <param name="Cancelled"></param>
|
||||
[ByRefEvent]
|
||||
public record struct FoldAttemptEvent(bool Cancelled = false);
|
||||
|
||||
@@ -21,7 +21,7 @@ public abstract class SharedItemSystem : EntitySystem
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<ItemComponent, GetVerbsEvent<InteractionVerb>>(AddPickupVerb);
|
||||
SubscribeLocalEvent<ItemComponent, InteractHandEvent>(OnHandInteract);
|
||||
SubscribeLocalEvent<ItemComponent, InteractHandEvent>(OnHandInteract, before: new []{typeof(SharedItemSystem)});
|
||||
SubscribeLocalEvent<ItemComponent, StackCountChangedEvent>(OnStackCountChanged);
|
||||
|
||||
SubscribeLocalEvent<ItemComponent, ComponentGetState>(OnGetState);
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Linq;
|
||||
using System.Numerics;
|
||||
using Content.Shared.Body.Components;
|
||||
using Content.Shared.Destructible;
|
||||
using Content.Shared.Foldable;
|
||||
using Content.Shared.Hands.Components;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Item;
|
||||
@@ -130,6 +131,13 @@ public abstract class SharedEntityStorageSystem : EntitySystem
|
||||
}
|
||||
}
|
||||
|
||||
protected void OnFoldAttempt(EntityUid uid, SharedEntityStorageComponent component, ref FoldAttemptEvent args)
|
||||
{
|
||||
if (args.Cancelled)
|
||||
return;
|
||||
args.Cancelled = component.Open || component.Contents.ContainedEntities.Count != 0;
|
||||
}
|
||||
|
||||
protected void AddToggleOpenVerb(EntityUid uid, SharedEntityStorageComponent component, GetVerbsEvent<InteractionVerb> args)
|
||||
{
|
||||
if (!args.CanAccess || !args.CanInteract)
|
||||
|
||||
Reference in New Issue
Block a user