Rejigging Item slots (#4933)

* itemslot overhaul

* remove "shared" prefix

* handle component shutdown

* comments, cleanup, tests

* comments and minor tweak

* rename ItemSlotManagerState

* fix swapping

* fix merge

* Add ItemSlot verb text override

* fix merge  (IEntity -> entityUid)

* Fix merge (LabelSystem)

* Fix merge (nuke disk)

* fix test
This commit is contained in:
Leon Friedrich
2021-11-20 18:26:01 +13:00
committed by GitHub
parent 19c5fed53a
commit 91b185e3c2
28 changed files with 805 additions and 554 deletions

View File

@@ -4,54 +4,63 @@ using Content.Shared.Containers.ItemSlots;
using Content.Shared.Interaction;
using Content.Shared.Verbs;
using Robust.Shared.Audio;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Player;
using System;
namespace Content.Server.Cabinet
{
public class ItemCabinetSystem : EntitySystem
{
[Dependency] private readonly SharedItemSlotsSystem _itemSlotsSystem = default!;
[Dependency] private readonly ItemSlotsSystem _itemSlotsSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ItemCabinetComponent, InteractUsingEvent>(OnInteractUsing);
SubscribeLocalEvent<ItemCabinetComponent, InteractHandEvent>(OnInteractHand);
SubscribeLocalEvent<ItemCabinetComponent, ComponentInit>(OnComponentInit);
SubscribeLocalEvent<ItemCabinetComponent, ComponentRemove>(OnComponentRemove);
SubscribeLocalEvent<ItemCabinetComponent, ComponentStartup>(OnComponentStartup);
SubscribeLocalEvent<ItemCabinetComponent, ActivateInWorldEvent>(OnActivateInWorld);
SubscribeLocalEvent<ItemCabinetComponent, ComponentStartup>(InitializeAppearance);
SubscribeLocalEvent<ItemCabinetComponent, ItemSlotChangedEvent>(OnItemSlotChanged);
SubscribeLocalEvent<ItemCabinetComponent, GetActivationVerbsEvent>(AddToggleOpenVerb);
SubscribeLocalEvent<ItemCabinetComponent, EntInsertedIntoContainerMessage>(OnContainerModified);
SubscribeLocalEvent<ItemCabinetComponent, EntRemovedFromContainerMessage>(OnContainerModified);
}
private void InitializeAppearance(EntityUid uid, ItemCabinetComponent component, ComponentStartup args)
private void OnComponentInit(EntityUid uid, ItemCabinetComponent cabinet, ComponentInit args)
{
UpdateAppearance(uid, component);
_itemSlotsSystem.AddItemSlot(uid, cabinet.Name, cabinet.CabinetSlot);
}
private void OnComponentRemove(EntityUid uid, ItemCabinetComponent cabinet, ComponentRemove args)
{
_itemSlotsSystem.RemoveItemSlot(uid, cabinet.CabinetSlot);
}
private void OnComponentStartup(EntityUid uid, ItemCabinetComponent cabinet, ComponentStartup args)
{
UpdateAppearance(uid, cabinet);
_itemSlotsSystem.SetLock(uid, cabinet.CabinetSlot.ID, !cabinet.Opened);
}
private void UpdateAppearance(EntityUid uid,
ItemCabinetComponent? cabinet = null,
SharedItemSlotsComponent? itemSlots = null,
SharedAppearanceComponent? appearance = null)
{
if (!Resolve(uid, ref cabinet, ref itemSlots, ref appearance, false))
if (!Resolve(uid, ref cabinet, ref appearance, false))
return;
appearance.SetData(ItemCabinetVisuals.IsOpen, cabinet.Opened);
if (!itemSlots.Slots.TryGetValue(cabinet.CabinetSlot, out var slot))
return;
appearance.SetData(ItemCabinetVisuals.ContainsItem, slot.HasEntity);
appearance.SetData(ItemCabinetVisuals.ContainsItem, cabinet.CabinetSlot.HasItem);
}
private void OnItemSlotChanged(EntityUid uid, ItemCabinetComponent cabinet, ItemSlotChangedEvent args)
private void OnContainerModified(EntityUid uid, ItemCabinetComponent cabinet, ContainerModifiedMessage args)
{
UpdateAppearance(uid, cabinet, args.SlotsComponent);
if (args.Container.ID == cabinet.CabinetSlot.ID)
UpdateAppearance(uid, cabinet);
}
private void AddToggleOpenVerb(EntityUid uid, ItemCabinetComponent cabinet, GetActivationVerbsEvent args)
@@ -75,44 +84,6 @@ namespace Content.Server.Cabinet
args.Verbs.Add(toggleVerb);
}
/// <summary>
/// Try insert an item if the cabinet is opened. Otherwise, just try open it.
/// </summary>
private void OnInteractUsing(EntityUid uid, ItemCabinetComponent comp, InteractUsingEvent args)
{
if (args.Handled)
return;
if (!comp.Opened)
ToggleItemCabinet(uid, comp);
else
_itemSlotsSystem.TryInsertContent(uid, args.Used, args.User);
args.Handled = true;
}
/// <summary>
/// If the cabinet is opened and has an entity, try and take it. Otherwise toggle the cabinet open/closed;
/// </summary>
private void OnInteractHand(EntityUid uid, ItemCabinetComponent comp, InteractHandEvent args)
{
if (args.Handled)
return;
if (!EntityManager.TryGetComponent(uid, out SharedItemSlotsComponent itemSlots))
return;
if (!itemSlots.Slots.TryGetValue(comp.CabinetSlot, out var slot))
return;
if (comp.Opened && slot.HasEntity)
_itemSlotsSystem.TryEjectContent(uid, comp.CabinetSlot, args.User);
else
ToggleItemCabinet(uid, comp);
args.Handled = true;
}
private void OnActivateInWorld(EntityUid uid, ItemCabinetComponent comp, ActivateInWorldEvent args)
{
if (args.Handled)
@@ -132,6 +103,7 @@ namespace Content.Server.Cabinet
cabinet.Opened = !cabinet.Opened;
SoundSystem.Play(Filter.Pvs(uid), cabinet.DoorSound.GetSound(), uid, AudioHelpers.WithVariation(0.15f));
_itemSlotsSystem.SetLock(uid, cabinet.CabinetSlot.ID, !cabinet.Opened);
UpdateAppearance(uid, cabinet);
}