Toggleable Hardsuit Helmets (#7559)

This commit is contained in:
Leon Friedrich
2022-04-23 15:31:45 +12:00
committed by GitHub
parent 83b47c43c0
commit 1141c19d76
34 changed files with 449 additions and 49 deletions

View File

@@ -89,6 +89,17 @@ public abstract class ActionType : IEquatable<ActionType>, IComparable, ICloneab
/// </summary>
public EntityUid? Provider;
/// <summary>
/// Entity to use for the action icon. Defaults to using <see cref="Provider"/>.
/// </summary>
public EntityUid? EntityIcon
{
get => _entityIcon ?? Provider;
set => _entityIcon = value;
}
private EntityUid? _entityIcon;
/// <summary>
/// Whether the action system should block this action if the user cannot currently interact. Some spells or
/// abilities may want to disable this and implement their own checks.
@@ -255,6 +266,7 @@ public abstract class ActionType : IEquatable<ActionType>, IComparable, ICloneab
Popup = toClone.Popup;
PopupToggleSuffix = toClone.PopupToggleSuffix;
ItemIconStyle = toClone.ItemIconStyle;
_entityIcon = toClone._entityIcon;
}
public bool Equals(ActionType? other)

View File

@@ -43,7 +43,7 @@ public abstract class SharedActionsSystem : EntitySystem
}
#region ComponentStateManagement
protected virtual void Dirty(ActionType action)
public virtual void Dirty(ActionType action)
{
if (action.AttachedEntity == null)
return;

View File

@@ -0,0 +1,20 @@
using Content.Shared.Clothing.EntitySystems;
namespace Content.Shared.Clothing.Components;
/// <summary>
/// This component indicates that this clothing is attached to some other entity with a <see
/// cref="ToggleableClothingComponent"/>. When unequipped, this entity should be returned to the entity that it is
/// attached to, rather than being dumped on the floor or something like that. Intended for use with hardsuits and
/// hardsuit helmets.
/// </summary>
[Friend(typeof(ToggleableClothingSystem))]
[RegisterComponent]
public sealed class AttachedClothingComponent : Component
{
/// <summary>
/// The Id of the piece of clothing that this entity belongs to.
/// </summary>
[DataField("AttachedUid")]
public EntityUid AttachedUid = default!;
}

View File

@@ -0,0 +1,59 @@
using Content.Shared.Actions.ActionTypes;
using Content.Shared.Clothing.EntitySystems;
using Content.Shared.Inventory;
using Robust.Shared.Containers;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Shared.Clothing.Components;
/// <summary>
/// This component gives an item an action that will equip or un-equip some clothing. Intended for use with
/// hardsuits and hardsuit helmets.
/// </summary>
[Friend(typeof(ToggleableClothingSystem))]
[RegisterComponent]
public sealed class ToggleableClothingComponent : Component
{
public const string DefaultClothingContainerId = "toggleable-clothing";
/// <summary>
/// Action used to toggle the clothing on or off.
/// </summary>
[DataField("actionId", customTypeSerializer: typeof(PrototypeIdSerializer<InstantActionPrototype>))]
public string ActionId = "ToggleSuitHelmet";
public InstantAction? ToggleAction = null;
/// <summary>
/// Default clothing entity prototype to spawn into the clothing container.
/// </summary>
[DataField("clothingPrototype", required: true, customTypeSerializer:typeof(PrototypeIdSerializer<EntityPrototype>))]
public readonly string ClothingPrototype = default!;
/// <summary>
/// The inventory slot that the clothing is equipped to.
/// </summary>
[DataField("slot")]
public string Slot = "head";
/// <summary>
/// The inventory slot flags required for this component to function.
/// </summary>
[DataField("requiredSlot")]
public SlotFlags RequiredFlags = SlotFlags.OUTERCLOTHING;
/// <summary>
/// The container that the clothing is stored in when not equipped.
/// </summary>
[DataField("containerId")]
public string ContainerId = DefaultClothingContainerId;
public ContainerSlot? Container;
/// <summary>
/// The Id of the piece of clothing that belongs to this component. Required for map-saving if the clothing is
/// currently not inside of the container.
/// </summary>
[DataField("clothingUid")]
public EntityUid? ClothingUid;
}

View File

@@ -0,0 +1,208 @@
using Content.Shared.Actions;
using Content.Shared.Actions.ActionTypes;
using Content.Shared.Clothing.Components;
using Content.Shared.Interaction;
using Content.Shared.Inventory;
using Content.Shared.Inventory.Events;
using Content.Shared.Popups;
using Robust.Shared.Containers;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
namespace Content.Shared.Clothing.EntitySystems;
public sealed class ToggleableClothingSystem : EntitySystem
{
[Dependency] private readonly SharedContainerSystem _containerSystem = default!;
[Dependency] private readonly SharedActionsSystem _actionsSystem = default!;
[Dependency] private readonly InventorySystem _inventorySystem = default!;
[Dependency] private readonly SharedPopupSystem _popupSystem = default!;
[Dependency] private readonly IPrototypeManager _proto = default!;
private Queue<EntityUid> _toInsert = new();
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ToggleableClothingComponent, ComponentAdd>(OnAdd);
SubscribeLocalEvent<ToggleableClothingComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<ToggleableClothingComponent, ToggleClothingEvent>(OnToggleClothing);
SubscribeLocalEvent<ToggleableClothingComponent, GetItemActionsEvent>(OnGetActions);
SubscribeLocalEvent<ToggleableClothingComponent, ComponentRemove>(OnRemoveToggleable);
SubscribeLocalEvent<ToggleableClothingComponent, GotUnequippedEvent>(OnToggleableUnequip);
SubscribeLocalEvent<AttachedClothingComponent, InteractHandEvent>(OnInteractHand);
SubscribeLocalEvent<AttachedClothingComponent, GotUnequippedEvent>(OnAttachedUnequip);
SubscribeLocalEvent<AttachedClothingComponent, ComponentRemove>(OnRemoveAttached);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
// process delayed insertions. Avoids doing a container insert during a container removal.
while (_toInsert.TryDequeue(out var uid))
{
if (TryComp(uid, out ToggleableClothingComponent? component) && component.ClothingUid != null)
component.Container?.Insert(component.ClothingUid.Value);
}
}
private void OnInteractHand(EntityUid uid, AttachedClothingComponent component, InteractHandEvent args)
{
if (args.Handled)
return;
if (!TryComp(component.AttachedUid, out ToggleableClothingComponent? toggleCom)
|| toggleCom.Container == null)
return;
if (!_inventorySystem.TryUnequip(Transform(uid).ParentUid, toggleCom.Slot, force: true))
return;
toggleCom.Container.Insert(uid, EntityManager);
args.Handled = true;
}
/// <summary>
/// Called when the suit is unequipped, to ensure that the helmet also gets unequipped.
/// </summary>
private void OnToggleableUnequip(EntityUid uid, ToggleableClothingComponent component, GotUnequippedEvent args)
{
if (component.Container != null && component.Container.ContainedEntity != null && component.ClothingUid != null)
_inventorySystem.TryUnequip(args.Equipee, component.Slot, force: true);
}
private void OnRemoveToggleable(EntityUid uid, ToggleableClothingComponent component, ComponentRemove args)
{
// If the parent/owner component of the attached clothing is being removed (entity getting deleted?) we will
// delete the attached entity. We do this regardless of whether or not the attached entity is currently
// "outside" of the container or not. This means that if a hardsuit takes too much damage, the helmet will also
// automatically be deleted.
// remove action.
if (component.ToggleAction?.AttachedEntity != null)
_actionsSystem.RemoveAction(component.ToggleAction.AttachedEntity.Value, component.ToggleAction);
if (component.ClothingUid != null)
QueueDel(component.ClothingUid.Value);
}
private void OnRemoveAttached(EntityUid uid, AttachedClothingComponent component, ComponentRemove args)
{
// if the attached component is being removed (maybe entity is being deleted?) we will just remove the
// toggleable clothing component. This means if you had a hard-suit helmet that took too much damage, you would
// still be left with a suit that was simply missing a helmet. There is currently no way to fix a partially
// broken suit like this.
if (!TryComp(component.AttachedUid, out ToggleableClothingComponent? toggleComp))
return;
if (toggleComp.LifeStage > ComponentLifeStage.Running)
return;
// remove action.
if (toggleComp.ToggleAction?.AttachedEntity != null)
_actionsSystem.RemoveAction(toggleComp.ToggleAction.AttachedEntity.Value, toggleComp.ToggleAction);
RemComp(component.AttachedUid, toggleComp);
}
/// <summary>
/// Called if the helmet was unequipped, to ensure that it gets moved into the suit's container.
/// </summary>
private void OnAttachedUnequip(EntityUid uid, AttachedClothingComponent component, GotUnequippedEvent args)
{
if (component.LifeStage > ComponentLifeStage.Running)
return;
if (!TryComp(component.AttachedUid, out ToggleableClothingComponent? toggleComp))
return;
if (toggleComp.LifeStage > ComponentLifeStage.Running)
return;
// As unequipped gets called in the middle of container removal, we cannot call a container-insert without causing issues.
// So we delay it and process it during a system update:
_toInsert.Enqueue(component.AttachedUid);
}
/// <summary>
/// Equip or unequip the toggleable clothing.
/// </summary>
private void OnToggleClothing(EntityUid uid, ToggleableClothingComponent component, ToggleClothingEvent args)
{
if (args.Handled || component.Container == null || component.ClothingUid == null)
return;
var parent = Transform(uid).ParentUid;
if (component.Container.ContainedEntity == null)
_inventorySystem.TryUnequip(parent, component.Slot);
else if (_inventorySystem.TryGetSlotEntity(parent, component.Slot, out var existing))
{
_popupSystem.PopupEntity(Loc.GetString("toggleable-clothing-remove-first", ("entity", existing)),
args.Performer, Filter.Entities(args.Performer));
}
else
_inventorySystem.TryEquip(parent, component.ClothingUid.Value, component.Slot);
args.Handled = true;
}
private void OnGetActions(EntityUid uid, ToggleableClothingComponent component, GetItemActionsEvent args)
{
if (component.ClothingUid == null || (args.SlotFlags & component.RequiredFlags) != component.RequiredFlags)
return;
if (component.ToggleAction != null)
args.Actions.Add(component.ToggleAction);
}
private void OnAdd(EntityUid uid, ToggleableClothingComponent component, ComponentAdd args)
{
component.Container = _containerSystem.EnsureContainer<ContainerSlot>(uid, component.ContainerId);
}
/// <summary>
/// On map init, either spawn the appropriate entity into the suit slot, or if it already exists, perform some
/// sanity checks. Also updates the action icon to show the toggled-entity.
/// </summary>
private void OnMapInit(EntityUid uid, ToggleableClothingComponent component, MapInitEvent args)
{
if (component.Container!.ContainedEntity is EntityUid ent)
{
DebugTools.Assert(component.ClothingUid == ent, "Unexpected entity present inside of a toggleable clothing container.");
return;
}
if (component.ToggleAction == null
&& _proto.TryIndex(component.ActionId, out InstantActionPrototype? act))
{
component.ToggleAction = new(act);
}
if (component.ClothingUid != null && component.ToggleAction != null)
{
DebugTools.Assert(Exists(component.ClothingUid), "Toggleable clothing is missing expected entity.");
DebugTools.Assert(TryComp(component.ClothingUid, out AttachedClothingComponent? comp), "Toggleable clothing is missing an attached component");
DebugTools.Assert(comp?.AttachedUid == uid, "Toggleable clothing uid mismatch");
}
else
{
var xform = Transform(uid);
component.ClothingUid = Spawn(component.ClothingPrototype, xform.Coordinates);
EnsureComp<AttachedClothingComponent>(component.ClothingUid.Value).AttachedUid = uid;
component.Container.Insert(component.ClothingUid.Value, EntityManager, ownerTransform: xform);
}
if (component.ToggleAction != null)
{
component.ToggleAction.EntityIcon = component.ClothingUid;
_actionsSystem.Dirty(component.ToggleAction);
}
}
}
public sealed class ToggleClothingEvent : InstantActionEvent { }

View File

@@ -90,7 +90,7 @@ public abstract partial class SharedHandsSystem : EntitySystem
if (handContainer == null || handContainer.ContainedEntity != null)
return false;
if (!Resolve(entity, ref item, false))
if (!Resolve(entity, ref item, false) || !item.CanPickup)
return false;
if (TryComp(entity, out PhysicsComponent? physics) && physics.BodyType == BodyType.Static)

View File

@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using Content.Shared.Clothing.Components;
using Content.Shared.Hands.Components;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Interaction;
@@ -11,6 +12,7 @@ using Content.Shared.Strip.Components;
using Robust.Shared.Audio;
using Robust.Shared.Containers;
using Robust.Shared.Map;
using Robust.Shared.Network;
using Robust.Shared.Player;
using Robust.Shared.Timing;
@@ -24,6 +26,7 @@ public abstract partial class InventorySystem
[Dependency] private readonly SharedContainerSystem _containerSystem = default!;
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly INetManager _netMan = default!;
private void InitializeEquip()
{
@@ -48,6 +51,10 @@ public abstract partial class InventorySystem
if (TryGetSlotEntity(args.User, slotDef.Name, out var slotEntity, inv))
{
// Item in slot has to be quick equipable as well
if (TryComp(slotEntity, out SharedItemComponent? item) && !item.QuickEquip)
continue;
if (!TryUnequip(args.User, slotDef.Name, true, inventory: inv))
continue;
@@ -175,11 +182,18 @@ public abstract partial class InventorySystem
if(!silent && item.EquipSound != null && _gameTiming.IsFirstTimePredicted)
{
var filter = Filter.Pvs(target);
Filter filter;
// don't play double audio for predicted interactions
if (predicted)
filter.RemoveWhereAttachedEntity(entity => entity == actor);
if (_netMan.IsClient)
filter = Filter.Local();
else
{
filter = Filter.Pvs(target);
// don't play double audio for predicted interactions
if (predicted)
filter.RemoveWhereAttachedEntity(entity => entity == actor);
}
SoundSystem.Play(filter, item.EquipSound.GetSound(), target, AudioParams.Default.WithVolume(-2f));
}
@@ -193,6 +207,11 @@ public abstract partial class InventorySystem
public bool CanAccess(EntityUid actor, EntityUid target, EntityUid itemUid)
{
// if the item is something like a hardsuit helmet, it may be contained within the hardsuit.
// in that case, we check accesibility for the owner-entity instead.
if (TryComp(itemUid, out AttachedClothingComponent? attachedComp))
itemUid = attachedComp.AttachedUid;
// Can the actor reach the target?
if (actor != target && !(_interactionSystem.InRangeUnobstructed(actor, target) && _containerSystem.IsInSameOrParentContainer(actor, target)))
return false;
@@ -273,18 +292,18 @@ public abstract partial class InventorySystem
return true;
}
public bool TryUnequip(EntityUid uid, string slot, bool silent = false, bool force = false,
InventoryComponent? inventory = null) => TryUnequip(uid, uid, slot, silent, force, inventory);
public bool TryUnequip(EntityUid uid, string slot, bool silent = false, bool force = false, bool predicted = false,
InventoryComponent? inventory = null, SharedItemComponent? item = null) => TryUnequip(uid, uid, slot, silent, force, predicted, inventory, item);
public bool TryUnequip(EntityUid actor, EntityUid target, string slot, bool silent = false,
bool force = false, InventoryComponent? inventory = null) =>
TryUnequip(actor, target, slot, out _, silent, force, inventory);
bool force = false, bool predicted = false, InventoryComponent? inventory = null, SharedItemComponent? item = null) =>
TryUnequip(actor, target, slot, out _, silent, force, predicted, inventory, item);
public bool TryUnequip(EntityUid uid, string slot, [NotNullWhen(true)] out EntityUid? removedItem, bool silent = false, bool force = false,
InventoryComponent? inventory = null) => TryUnequip(uid, uid, slot, out removedItem, silent, force, inventory);
public bool TryUnequip(EntityUid uid, string slot, [NotNullWhen(true)] out EntityUid? removedItem, bool silent = false, bool force = false, bool predicted = false,
InventoryComponent? inventory = null, SharedItemComponent? item = null) => TryUnequip(uid, uid, slot, out removedItem, silent, force, predicted, inventory, item);
public bool TryUnequip(EntityUid actor, EntityUid target, string slot, [NotNullWhen(true)] out EntityUid? removedItem, bool silent = false,
bool force = false, InventoryComponent? inventory = null)
bool force = false, bool predicted = false, InventoryComponent? inventory = null, SharedItemComponent? item = null)
{
removedItem = null;
if (!Resolve(target, ref inventory, false))
@@ -321,7 +340,7 @@ public abstract partial class InventorySystem
if (slotDef != slotDefinition && slotDef.DependsOn == slotDefinition.Name)
{
//this recursive call might be risky
TryUnequip(actor, target, slotDef.Name, true, true, inventory);
TryUnequip(actor, target, slotDef.Name, true, true, predicted, inventory);
}
}
@@ -340,6 +359,24 @@ public abstract partial class InventorySystem
Transform(removedItem.Value).Coordinates = Transform(target).Coordinates;
if (!silent && Resolve(removedItem.Value, ref item) && item.UnequipSound != null && _gameTiming.IsFirstTimePredicted)
{
Filter filter;
if (_netMan.IsClient)
filter = Filter.Local();
else
{
filter = Filter.Pvs(target);
// don't play double audio for predicted interactions
if (predicted)
filter.RemoveWhereAttachedEntity(entity => entity == actor);
}
SoundSystem.Play(filter, item.UnequipSound.GetSound(), target, AudioParams.Default.WithVolume(-2f));
}
inventory.Dirty();
_movementSpeed.RefreshMovementSpeedModifiers(target);

View File

@@ -37,6 +37,20 @@ namespace Content.Shared.Item
[DataField("clothingVisuals")]
public Dictionary<string, List<PrototypeLayerData>> ClothingVisuals = new();
/// <summary>
/// Whether or not this item can be picked up.
/// </summary>
/// <remarks>
/// This should almost always be true for items. But in some special cases, an item can be equipped but not
/// picked up. E.g., hardsuit helmets are attached to the suit, so we want to disable things like the pickup
/// verb.
/// </remarks>
[DataField("canPickup")]
public bool CanPickup = true;
[DataField("quickEquip")]
public bool QuickEquip = true;
/// <summary>
/// Part of the state of the sprite shown on the player when this item is in their hands or inventory.
/// </summary>
@@ -61,9 +75,12 @@ namespace Content.Shared.Item
[DataField("Slots")]
public SlotFlags SlotFlags = SlotFlags.PREVENTEQUIP; //Different from None, NONE allows equips if no slot flags are required
[DataField("EquipSound")]
[DataField("equipSound")]
public SoundSpecifier? EquipSound { get; set; } = default!;
[DataField("unequipSound")]
public SoundSpecifier? UnequipSound = default!;
/// <summary>
/// Rsi of the sprite shown on the player when this item is in their hands. Used to generate a default entry for <see cref="InhandVisuals"/>
/// </summary>

View File

@@ -1,4 +1,3 @@
using Content.Shared.Hands.Components;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Interaction;
using Content.Shared.Inventory.Events;
@@ -27,7 +26,7 @@ namespace Content.Shared.Item
private void OnHandInteract(EntityUid uid, SharedItemComponent component, InteractHandEvent args)
{
if (args.Handled)
if (args.Handled || !component.CanPickup)
return;
args.Handled = _handsSystem.TryPickup(args.User, uid, animateUser: false);
@@ -68,6 +67,7 @@ namespace Content.Shared.Item
args.Using != null ||
!args.CanAccess ||
!args.CanInteract ||
!component.CanPickup ||
!_handsSystem.CanPickupAnyHand(args.User, args.Target, handsComp: args.Hands, item: component))
return;