Clothing/item ECS & cleanup (#9706)

This commit is contained in:
Kara
2022-07-27 03:53:47 -07:00
committed by GitHub
parent 0f0420eca9
commit 258ec0cac1
164 changed files with 938 additions and 918 deletions

View File

@@ -0,0 +1,76 @@
using Content.Shared.Hands.Components;
using Robust.Shared.Serialization;
namespace Content.Shared.Item;
/// <summary>
/// Handles items which can be picked up to hands and placed in pockets, as well as storage containers
/// like backpacks.
/// </summary>
[RegisterComponent]
public sealed class ItemComponent : Component
{
[Access(typeof(SharedItemSystem), Other = AccessPermissions.ReadExecute)]
[ViewVariables(VVAccess.ReadWrite)]
[DataField("size")]
public int Size = 5;
[DataField("inhandVisuals")]
public Dictionary<HandLocation, List<SharedSpriteComponent.PrototypeLayerData>> InhandVisuals = new();
[Access(typeof(SharedItemSystem))]
[ViewVariables(VVAccess.ReadWrite)]
[DataField("heldPrefix")]
public string? HeldPrefix;
/// <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>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("sprite")]
public readonly string? RsiPath;
}
[Serializable, NetSerializable]
public sealed class ItemComponentState : ComponentState
{
public int Size { get; }
public string? HeldPrefix { get; }
public ItemComponentState(int size, string? heldPrefix)
{
Size = size;
HeldPrefix = heldPrefix;
}
}
/// <summary>
/// Raised when an item's visual state is changed. The event is directed at the entity that contains this item, so
/// that it can properly update its hands or inventory sprites and GUI.
/// </summary>
[Serializable, NetSerializable]
public sealed class VisualsChangedEvent : EntityEventArgs
{
public readonly EntityUid Item;
public readonly string ContainerId;
public VisualsChangedEvent(EntityUid item, string containerId)
{
Item = item;
ContainerId = containerId;
}
}
/// <summary>
/// Reference sizes for common containers and items.
/// </summary>
public enum ReferenceSizes
{
Wallet = 4,
Pocket = 12,
Box = 24,
Belt = 30,
Toolbox = 60,
Backpack = 100,
NoStoring = 9999
}

View File

@@ -1,147 +0,0 @@
using Content.Shared.Hands.Components;
using Content.Shared.Inventory;
using Content.Shared.Sound;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
using static Robust.Shared.GameObjects.SharedSpriteComponent;
namespace Content.Shared.Item
{
/// <summary>
/// Players can pick up, drop, and put items in bags, and they can be seen in player's hands.
/// </summary>
[NetworkedComponent()]
public abstract class SharedItemComponent : Component
{
[Dependency] private readonly IEntityManager _entMan = default!;
/// <summary>
/// How much big this item is.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public int Size
{
get => _size;
set
{
_size = value;
Dirty();
}
}
[DataField("size")]
private int _size;
[DataField("inhandVisuals")]
public Dictionary<HandLocation, List<PrototypeLayerData>> InhandVisuals = new();
[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>
/// <remarks>
/// Only used if <see cref="InhandVisuals"/> or <see cref="ClothingVisuals"/> are unspecified.
/// </remarks>
[ViewVariables(VVAccess.ReadWrite)]
public string? EquippedPrefix
{
get => _equippedPrefix;
set
{
_equippedPrefix = value;
EntitySystem.Get<SharedItemSystem>().VisualsChanged(Owner, this);
Dirty();
}
}
[DataField("HeldPrefix")]
private string? _equippedPrefix;
[ViewVariables]
[DataField("Slots")]
public SlotFlags SlotFlags = SlotFlags.PREVENTEQUIP; //Different from None, NONE allows equips if no slot flags are required
[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>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("sprite")]
public readonly string? RsiPath;
public void RemovedFromSlot()
{
if (_entMan.TryGetComponent(Owner, out SharedSpriteComponent? component))
component.Visible = true;
}
public virtual void EquippedToSlot()
{
if (_entMan.TryGetComponent(Owner, out SharedSpriteComponent? component))
component.Visible = false;
}
}
[Serializable, NetSerializable]
public sealed class ItemComponentState : ComponentState
{
public int Size { get; }
public string? EquippedPrefix { get; }
public ItemComponentState(int size, string? equippedPrefix)
{
Size = size;
EquippedPrefix = equippedPrefix;
}
}
/// <summary>
/// Raised when an item's visual state is changed. The event is directed at the entity that contains this item, so
/// that it can properly update its hands or inventory sprites and GUI.
/// </summary>
[Serializable, NetSerializable]
public sealed class VisualsChangedEvent : EntityEventArgs
{
public readonly EntityUid Item;
public readonly string ContainerId;
public VisualsChangedEvent(EntityUid item, string containerId)
{
Item = item;
ContainerId = containerId;
}
}
/// <summary>
/// Reference sizes for common containers and items.
/// </summary>
public enum ReferenceSizes
{
Wallet = 4,
Pocket = 12,
Box = 24,
Belt = 30,
Toolbox = 60,
Backpack = 100,
NoStoring = 9999
}
}

View File

@@ -1,95 +1,124 @@
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Interaction;
using Content.Shared.Inventory.Events;
using Content.Shared.Verbs;
using Robust.Shared.Containers;
using Robust.Shared.GameStates;
namespace Content.Shared.Item
namespace Content.Shared.Item;
public abstract class SharedItemSystem : EntitySystem
{
public abstract class SharedItemSystem : EntitySystem
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
[Dependency] private readonly SharedContainerSystem _container = default!;
public override void Initialize()
{
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
base.Initialize();
SubscribeLocalEvent<ItemComponent, GetVerbsEvent<InteractionVerb>>(AddPickupVerb);
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SharedItemComponent, GetVerbsEvent<InteractionVerb>>(AddPickupVerb);
SubscribeLocalEvent<SharedSpriteComponent, GotEquippedEvent>(OnEquipped);
SubscribeLocalEvent<SharedSpriteComponent, GotUnequippedEvent>(OnUnequipped);
SubscribeLocalEvent<ItemComponent, InteractHandEvent>(OnHandInteract);
SubscribeLocalEvent<SharedSpriteComponent, GotEquippedEvent>(OnEquipped);
SubscribeLocalEvent<SharedSpriteComponent, GotUnequippedEvent>(OnUnequipped);
SubscribeLocalEvent<SharedItemComponent, InteractHandEvent>(OnHandInteract);
SubscribeLocalEvent<ItemComponent, ComponentGetState>(OnGetState);
SubscribeLocalEvent<ItemComponent, ComponentHandleState>(OnHandleState);
}
SubscribeLocalEvent<SharedItemComponent, ComponentGetState>(OnGetState);
SubscribeLocalEvent<SharedItemComponent, ComponentHandleState>(OnHandleState);
}
#region Public API
private void OnHandInteract(EntityUid uid, SharedItemComponent component, InteractHandEvent args)
{
if (args.Handled || !component.CanPickup)
return;
public void SetSize(EntityUid uid, int size, ItemComponent? component = null)
{
if (!Resolve(uid, ref component))
return;
args.Handled = _handsSystem.TryPickup(args.User, uid, animateUser: false);
}
component.Size = size;
Dirty(component);
}
private void OnHandleState(EntityUid uid, SharedItemComponent component, ref ComponentHandleState args)
{
if (args.Current is not ItemComponentState state)
return;
public void SetHeldPrefix(EntityUid uid, string? heldPrefix, ItemComponent? component = null)
{
if (!Resolve(uid, ref component))
return;
component.Size = state.Size;
component.EquippedPrefix = state.EquippedPrefix;
}
component.HeldPrefix = heldPrefix;
Dirty(component);
VisualsChanged(uid);
}
private void OnGetState(EntityUid uid, SharedItemComponent component, ref ComponentGetState args)
{
args.State = new ItemComponentState(component.Size, component.EquippedPrefix);
}
#endregion
// Although netsync is being set to false for items client can still update these
// Realistically:
// Container should already hide these
// Client is the only thing that matters.
private void OnHandInteract(EntityUid uid, ItemComponent component, InteractHandEvent args)
{
if (args.Handled)
return;
private void OnUnequipped(EntityUid uid, SharedSpriteComponent component, GotUnequippedEvent args)
{
component.Visible = true;
}
args.Handled = _handsSystem.TryPickup(args.User, uid, animateUser: false);
}
private void OnEquipped(EntityUid uid, SharedSpriteComponent component, GotEquippedEvent args)
{
component.Visible = false;
}
private void OnHandleState(EntityUid uid, ItemComponent component, ref ComponentHandleState args)
{
if (args.Current is not ItemComponentState state)
return;
private void AddPickupVerb(EntityUid uid, SharedItemComponent component, GetVerbsEvent<InteractionVerb> args)
{
if (args.Hands == null ||
args.Using != null ||
!args.CanAccess ||
!args.CanInteract ||
!component.CanPickup ||
!_handsSystem.CanPickupAnyHand(args.User, args.Target, handsComp: args.Hands, item: component))
return;
component.Size = state.Size;
component.HeldPrefix = state.HeldPrefix;
}
InteractionVerb verb = new();
verb.Act = () => _handsSystem.TryPickupAnyHand(args.User, args.Target, checkActionBlocker: false, handsComp: args.Hands, item: component);
verb.IconTexture = "/Textures/Interface/VerbIcons/pickup.svg.192dpi.png";
private void OnGetState(EntityUid uid, ItemComponent component, ref ComponentGetState args)
{
args.State = new ItemComponentState(component.Size, component.HeldPrefix);
}
// if the item already in a container (that is not the same as the user's), then change the text.
// this occurs when the item is in their inventory or in an open backpack
args.User.TryGetContainer(out var userContainer);
if (args.Target.TryGetContainer(out var container) && container != userContainer)
verb.Text = Loc.GetString("pick-up-verb-get-data-text-inventory");
else
verb.Text = Loc.GetString("pick-up-verb-get-data-text");
// Although netsync is being set to false for items client can still update these
// Realistically:
// Container should already hide these
// Client is the only thing that matters.
args.Verbs.Add(verb);
}
private void OnUnequipped(EntityUid uid, SharedSpriteComponent component, GotUnequippedEvent args)
{
component.Visible = true;
}
/// <summary>
/// Notifies any entity that is holding or wearing this item that they may need to update their sprite.
/// </summary>
public virtual void VisualsChanged(EntityUid owner, SharedItemComponent? item = null)
{ }
private void OnEquipped(EntityUid uid, SharedSpriteComponent component, GotEquippedEvent args)
{
component.Visible = false;
}
private void AddPickupVerb(EntityUid uid, ItemComponent component, GetVerbsEvent<InteractionVerb> args)
{
if (args.Hands == null ||
args.Using != null ||
!args.CanAccess ||
!args.CanInteract) //||
//!_handsSystem.CanPickupAnyHand(args.User, args.Target, handsComp: args.Hands, item: component))
return;
InteractionVerb verb = new();
// TODO ITEM
//verb.Act = () => _handsSystem.TryPickupAnyHand(args.User, args.Target, checkActionBlocker: false,
// handsComp: args.Hands, item: component);
verb.IconTexture = "/Textures/Interface/VerbIcons/pickup.svg.192dpi.png";
// if the item already in a container (that is not the same as the user's), then change the text.
// this occurs when the item is in their inventory or in an open backpack
_container.TryGetContainingContainer(args.User, out var userContainer);
if (_container.TryGetContainingContainer(args.Target, out var container) && container != userContainer)
verb.Text = Loc.GetString("pick-up-verb-get-data-text-inventory");
else
verb.Text = Loc.GetString("pick-up-verb-get-data-text");
args.Verbs.Add(verb);
}
/// <summary>
/// Notifies any entity that is holding or wearing this item that they may need to update their sprite.
/// </summary>
/// <remarks>
/// This is used for updating both inhand sprites and clothing sprites, but it's here just cause it needs to
/// be in one place.
/// </remarks>
public virtual void VisualsChanged(EntityUid owner)
{
}
}