ECS secret stash and toilet (#5685)

Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
Alex Evgrashin
2021-12-21 13:07:17 +03:00
committed by GitHub
parent c242a612ad
commit 900a8118c5
7 changed files with 481 additions and 298 deletions

View File

@@ -1,120 +1,44 @@
using Content.Server.Hands.Components;
using Content.Server.Items;
using Content.Shared.Acts;
using Content.Server.Storage.EntitySystems;
using Content.Server.Toilet;
using Content.Shared.Containers.ItemSlots;
using Content.Shared.Item;
using Content.Shared.Popups;
using Robust.Shared.Analyzers;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Storage.Components
{
/// <summary>
/// Logic for secret single slot stash, like plant pot or toilet cistern
/// Logic for a secret slot stash, like plant pot or toilet cistern.
/// Unlike <see cref="ItemSlotsComponent"/> it doesn't have interaction logic or verbs.
/// Other classes like <see cref="ToiletComponent"/> should implement it.
/// </summary>
[RegisterComponent]
public class SecretStashComponent : Component, IDestroyAct
[Friend(typeof(SecretStashSystem))]
public class SecretStashComponent : Component
{
[Dependency] private readonly IEntityManager _entMan = default!;
public override string Name => "SecretStash";
/// <summary>
/// Max item size that can be fitted into secret stash.
/// </summary>
[ViewVariables] [DataField("maxItemSize")]
private int _maxItemSize = (int) ReferenceSizes.Pocket;
public int MaxItemSize = (int) ReferenceSizes.Pocket;
/// <summary>
/// IC secret stash name. For example "the toilet cistern".
/// If empty string, will replace it with entity name in init.
/// </summary>
[ViewVariables] [DataField("secretPartName")]
private readonly string? _secretPartNameOverride = null;
[ViewVariables] private ContainerSlot _itemContainer = default!;
public string SecretPartName => _secretPartNameOverride ?? Loc.GetString("comp-secret-stash-secret-part-name", ("name", _entMan.GetComponent<MetaDataComponent>(Owner).EntityName));
protected override void Initialize()
{
base.Initialize();
_itemContainer = ContainerHelpers.EnsureContainer<ContainerSlot>(Owner, "stash", out _);
}
public string SecretPartName = "";
/// <summary>
/// Tries to hide item inside secret stash from hands of user
/// Container used to keep secret stash item.
/// </summary>
/// <param name="user"></param>
/// <param name="itemToHide"></param>
/// <returns>True if item was hidden inside stash</returns>
public bool TryHideItem(EntityUid user, EntityUid itemToHide)
{
if (_itemContainer.ContainedEntity != null)
{
Owner.PopupMessage(user, Loc.GetString("comp-secret-stash-action-hide-container-not-empty"));
return false;
}
[ViewVariables]
public ContainerSlot ItemContainer = default!;
if (!_entMan.TryGetComponent(itemToHide, out ItemComponent? item))
return false;
if (item.Size > _maxItemSize)
{
Owner.PopupMessage(user,
Loc.GetString("comp-secret-stash-action-hide-item-too-big",("item", itemToHide),("stash", SecretPartName)));
return false;
}
if (!_entMan.TryGetComponent(user, out HandsComponent? hands))
return false;
if (!hands.Drop(itemToHide, _itemContainer))
return false;
Owner.PopupMessage(user, Loc.GetString("comp-secret-stash-action-hide-success", ("item", itemToHide), ("this", SecretPartName)));
return true;
}
/// <summary>
/// Try get item and place it in users hand
/// If user can't take it by hands, will drop item from container
/// </summary>
/// <param name="user"></param>
/// <returns>True if user recieved item</returns>
public bool TryGetItem(EntityUid user)
{
if (_itemContainer.ContainedEntity is not {Valid: true} contained)
return false;
Owner.PopupMessage(user, Loc.GetString("comp-secret-stash-action-get-item-found-something", ("stash", SecretPartName)));
if (_entMan.TryGetComponent(user, out HandsComponent? hands))
{
if (!_entMan.TryGetComponent(contained, out ItemComponent? item))
return false;
hands.PutInHandOrDrop(item);
}
else if (_itemContainer.Remove(contained))
{
_entMan.GetComponent<TransformComponent>(contained).Coordinates = _entMan.GetComponent<TransformComponent>(Owner).Coordinates;
}
return true;
}
/// <summary>
/// Is there something inside secret stash item container?
/// </summary>
/// <returns></returns>
public bool HasItemInside()
{
return _itemContainer.ContainedEntity != null;
}
public void OnDestroy(DestructionEventArgs eventArgs)
{
// drop item inside
if (_itemContainer.ContainedEntity is {Valid: true} contained)
{
_entMan.GetComponent<TransformComponent>(contained).Coordinates = _entMan.GetComponent<TransformComponent>(Owner).Coordinates;
}
}
}
}

View File

@@ -0,0 +1,136 @@
using Content.Server.Items;
using Content.Server.Popups;
using Content.Server.Storage.Components;
using Content.Shared.Acts;
using Content.Shared.Hands.Components;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Player;
namespace Content.Server.Storage.EntitySystems
{
public class SecretStashSystem : EntitySystem
{
[Dependency] private readonly PopupSystem _popupSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SecretStashComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<SecretStashComponent, DestructionEventArgs>(OnDestroyed);
}
private void OnInit(EntityUid uid, SecretStashComponent component, ComponentInit args)
{
// set default secret part name
if (component.SecretPartName == "")
{
var meta = EntityManager.GetComponent<MetaDataComponent>(uid);
var entityName = Loc.GetString("comp-secret-stash-secret-part-name", ("name", meta.EntityName));
component.SecretPartName = entityName;
}
component.ItemContainer = ContainerHelpers.EnsureContainer<ContainerSlot>(uid, "stash", out _);
}
private void OnDestroyed(EntityUid uid, SecretStashComponent component, DestructionEventArgs args)
{
component.ItemContainer.EmptyContainer();
}
/// <summary>
/// Is there something inside secret stash item container?
/// </summary>
public bool HasItemInside(EntityUid uid, SecretStashComponent? component = null)
{
if (!Resolve(uid, ref component))
return false;
return component.ItemContainer.ContainedEntity != null;
}
/// <summary>
/// Tries to hide item inside secret stash from hands of user.
/// </summary>
/// <returns>True if item was hidden inside stash</returns>
public bool TryHideItem(EntityUid uid, EntityUid userUid, EntityUid itemToHideUid,
SecretStashComponent? component = null, ItemComponent? item = null,
MetaDataComponent? itemMeta = null, SharedHandsComponent? hands = null)
{
if (!Resolve(uid, ref component))
return false;
if (!Resolve(itemToHideUid, ref item, ref itemMeta))
return false;
if (!Resolve(userUid, ref hands))
return false;
// check if secret stash is already occupied
var container = component.ItemContainer;
if (container.ContainedEntity != null)
{
var msg = Loc.GetString("comp-secret-stash-action-hide-container-not-empty");
_popupSystem.PopupEntity(msg, uid, Filter.Entities(userUid));
return false;
}
// check if item is too big to fit into secret stash
var itemName = itemMeta.EntityName;
if (item.Size > component.MaxItemSize)
{
var msg = Loc.GetString("comp-secret-stash-action-hide-item-too-big",
("item", itemName), ("stash", component.SecretPartName));
_popupSystem.PopupEntity(msg, uid, Filter.Entities(userUid));
return false;
}
// try to move item from hands to stash container
if (!hands.Drop(itemToHideUid, container))
{
return false;
}
// all done, show success message
var successMsg = Loc.GetString("comp-secret-stash-action-hide-success",
("item", itemName), ("this", component.SecretPartName));
_popupSystem.PopupEntity(successMsg, uid, Filter.Entities(userUid));
return true;
}
/// <summary>
/// Try get item and place it in users hand.
/// If user can't take it by hands, will drop item from container.
/// </summary>
/// <returns>True if user received item</returns>
public bool TryGetItem(EntityUid uid, EntityUid userUid, SecretStashComponent? component = null,
SharedHandsComponent? hands = null)
{
if (!Resolve(uid, ref component))
return false;
if (!Resolve(userUid, ref hands))
return false;
// check if secret stash has something inside
var container = component.ItemContainer;
if (container.ContainedEntity == null)
{
return false;
}
// get item inside container
var itemUid = container.ContainedEntity;
if (!EntityManager.TryGetComponent(itemUid, out ItemComponent? item))
{
return false;
}
hands.PutInHandOrDrop(item);
// show success message
var successMsg = Loc.GetString("comp-secret-stash-action-get-item-found-something",
("stash", component.SecretPartName));
_popupSystem.PopupEntity(successMsg, uid, Filter.Entities(userUid));
return true;
}
}
}