Re-organize all projects (#4166)
This commit is contained in:
94
Content.Server/Cabinet/ItemCabinetComponent.cs
Normal file
94
Content.Server/Cabinet/ItemCabinetComponent.cs
Normal file
@@ -0,0 +1,94 @@
|
||||
using Content.Shared.ActionBlocker;
|
||||
using Content.Shared.Verbs;
|
||||
using Content.Shared.Whitelist;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Cabinet
|
||||
{
|
||||
/// <summary>
|
||||
/// Used for entities that can hold one item that fits the whitelist, which can be extracted by interacting with
|
||||
/// the entity, and can have an item fitting the whitelist placed back inside
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public class ItemCabinetComponent : Component
|
||||
{
|
||||
public override string Name => "ItemCabinet";
|
||||
|
||||
/// <summary>
|
||||
/// Sound to be played when the cabinet door is opened.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("doorSound")]
|
||||
public string? DoorSound { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The prototype that should be spawned inside the cabinet when it is map initialized.
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
[DataField("spawnPrototype")]
|
||||
public string? SpawnPrototype { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A whitelist defining which entities are allowed into the cabinet.
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
[DataField("whitelist")]
|
||||
public EntityWhitelist? Whitelist = null;
|
||||
|
||||
[ViewVariables]
|
||||
public ContainerSlot ItemContainer = default!;
|
||||
|
||||
/// <summary>
|
||||
/// Whether the cabinet is currently open or not.
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
[DataField("opened")]
|
||||
public bool Opened { get; set; } = false;
|
||||
|
||||
[Verb]
|
||||
public sealed class EjectItemFromCabinetVerb : Verb<ItemCabinetComponent>
|
||||
{
|
||||
protected override void GetData(IEntity user, ItemCabinetComponent component, VerbData data)
|
||||
{
|
||||
if (component.ItemContainer.ContainedEntity == null || !component.Opened || !ActionBlockerSystem.CanInteract(user))
|
||||
data.Visibility = VerbVisibility.Invisible;
|
||||
else
|
||||
{
|
||||
data.Text = Loc.GetString("comp-item-cabinet-eject-verb-text");
|
||||
data.IconTexture = "/Textures/Interface/VerbIcons/eject.svg.192dpi.png";
|
||||
data.Visibility = VerbVisibility.Visible;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Activate(IEntity user, ItemCabinetComponent component)
|
||||
{
|
||||
component.Owner.EntityManager.EventBus.RaiseLocalEvent(component.Owner.Uid, new TryEjectItemCabinetEvent(user), false);
|
||||
}
|
||||
}
|
||||
|
||||
[Verb]
|
||||
public sealed class ToggleItemCabinetVerb : Verb<ItemCabinetComponent>
|
||||
{
|
||||
protected override void GetData(IEntity user, ItemCabinetComponent component, VerbData data)
|
||||
{
|
||||
if (!ActionBlockerSystem.CanInteract(user))
|
||||
data.Visibility = VerbVisibility.Invisible;
|
||||
else
|
||||
{
|
||||
data.Text = Loc.GetString(component.Opened ? "comp-item-cabinet-close-verb-text" : "comp-item-cabinet-open-verb-text");
|
||||
data.IconTexture = component.Opened ? "/Textures/Interface/VerbIcons/close.svg.192dpi.png" : "/Textures/Interface/VerbIcons/open.svg.192dpi.png";
|
||||
data.Visibility = VerbVisibility.Visible;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Activate(IEntity user, ItemCabinetComponent component)
|
||||
{
|
||||
component.Owner.EntityManager.EventBus.RaiseLocalEvent(component.Owner.Uid, new ToggleItemCabinetEvent(), false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
188
Content.Server/Cabinet/ItemCabinetSystem.cs
Normal file
188
Content.Server/Cabinet/ItemCabinetSystem.cs
Normal file
@@ -0,0 +1,188 @@
|
||||
using Content.Server.Hands.Components;
|
||||
using Content.Server.Items;
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.Cabinet;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Notification;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Player;
|
||||
|
||||
namespace Content.Server.Cabinet
|
||||
{
|
||||
public class ItemCabinetSystem : EntitySystem
|
||||
{
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<ItemCabinetComponent, MapInitEvent>(OnMapInitialize);
|
||||
|
||||
SubscribeLocalEvent<ItemCabinetComponent, InteractUsingEvent>(OnInteractUsing);
|
||||
SubscribeLocalEvent<ItemCabinetComponent, InteractHandEvent>(OnInteractHand);
|
||||
SubscribeLocalEvent<ItemCabinetComponent, ActivateInWorldEvent>(OnActivateInWorld);
|
||||
|
||||
SubscribeLocalEvent<ItemCabinetComponent, TryEjectItemCabinetEvent>(OnTryEjectItemCabinet);
|
||||
SubscribeLocalEvent<ItemCabinetComponent, TryInsertItemCabinetEvent>(OnTryInsertItemCabinet);
|
||||
SubscribeLocalEvent<ItemCabinetComponent, ToggleItemCabinetEvent>(OnToggleItemCabinet);
|
||||
}
|
||||
|
||||
private void OnMapInitialize(EntityUid uid, ItemCabinetComponent comp, MapInitEvent args)
|
||||
{
|
||||
var owner = EntityManager.GetEntity(uid);
|
||||
comp.ItemContainer =
|
||||
owner.EnsureContainer<ContainerSlot>("item_cabinet", out _);
|
||||
|
||||
if(comp.SpawnPrototype != null)
|
||||
comp.ItemContainer.Insert(EntityManager.SpawnEntity(comp.SpawnPrototype, owner.Transform.Coordinates));
|
||||
|
||||
UpdateVisuals(comp);
|
||||
}
|
||||
|
||||
private void OnInteractUsing(EntityUid uid, ItemCabinetComponent comp, InteractUsingEvent args)
|
||||
{
|
||||
args.Handled = true;
|
||||
if (!comp.Opened)
|
||||
{
|
||||
RaiseLocalEvent(uid, new ToggleItemCabinetEvent(), false);
|
||||
}
|
||||
else
|
||||
{
|
||||
RaiseLocalEvent(uid, new TryInsertItemCabinetEvent(args.User, args.Used), false);
|
||||
}
|
||||
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
private void OnInteractHand(EntityUid uid, ItemCabinetComponent comp, InteractHandEvent args)
|
||||
{
|
||||
args.Handled = true;
|
||||
if (comp.Opened)
|
||||
{
|
||||
if (comp.ItemContainer.ContainedEntity == null)
|
||||
{
|
||||
RaiseLocalEvent(uid, new ToggleItemCabinetEvent(), false);
|
||||
return;
|
||||
}
|
||||
RaiseLocalEvent(uid, new TryEjectItemCabinetEvent(args.User), false);
|
||||
}
|
||||
else
|
||||
{
|
||||
RaiseLocalEvent(uid, new ToggleItemCabinetEvent(), false);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnActivateInWorld(EntityUid uid, ItemCabinetComponent comp, ActivateInWorldEvent args)
|
||||
{
|
||||
args.Handled = true;
|
||||
RaiseLocalEvent(uid, new ToggleItemCabinetEvent(), false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Toggles the ItemCabinet's state.
|
||||
/// </summary>
|
||||
private void OnToggleItemCabinet(EntityUid uid, ItemCabinetComponent comp, ToggleItemCabinetEvent args)
|
||||
{
|
||||
comp.Opened = !comp.Opened;
|
||||
ClickLatchSound(comp);
|
||||
UpdateVisuals(comp);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to insert an entity into the ItemCabinet's slot from the user's hands.
|
||||
/// </summary>
|
||||
private static void OnTryInsertItemCabinet(EntityUid uid, ItemCabinetComponent comp, TryInsertItemCabinetEvent args)
|
||||
{
|
||||
if (comp.ItemContainer.ContainedEntity != null || args.Cancelled || (comp.Whitelist != null && !comp.Whitelist.IsValid(args.Item)))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!args.User.TryGetComponent<HandsComponent>(out var hands) || !hands.Drop(args.Item, comp.ItemContainer))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
UpdateVisuals(comp);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to eject the ItemCabinet's item, either into the user's hands or onto the floor.
|
||||
/// </summary>
|
||||
private static void OnTryEjectItemCabinet(EntityUid uid, ItemCabinetComponent comp, TryEjectItemCabinetEvent args)
|
||||
{
|
||||
if (comp.ItemContainer.ContainedEntity == null || args.Cancelled)
|
||||
return;
|
||||
if (args.User.TryGetComponent(out HandsComponent? hands))
|
||||
{
|
||||
|
||||
if (comp.ItemContainer.ContainedEntity.TryGetComponent<ItemComponent>(out var item))
|
||||
{
|
||||
comp.Owner.PopupMessage(args.User,
|
||||
Loc.GetString("comp-item-cabinet-successfully-taken",
|
||||
("item", comp.ItemContainer.ContainedEntity),
|
||||
("cabinet", comp.Owner)));
|
||||
hands.PutInHandOrDrop(item);
|
||||
}
|
||||
}
|
||||
else if (comp.ItemContainer.Remove(comp.ItemContainer.ContainedEntity))
|
||||
{
|
||||
comp.ItemContainer.ContainedEntity.Transform.Coordinates = args.User.Transform.Coordinates;
|
||||
}
|
||||
UpdateVisuals(comp);
|
||||
}
|
||||
|
||||
private static void UpdateVisuals(ItemCabinetComponent comp)
|
||||
{
|
||||
if (comp.Owner.TryGetComponent(out SharedAppearanceComponent? appearance))
|
||||
{
|
||||
appearance.SetData(ItemCabinetVisuals.IsOpen, comp.Opened);
|
||||
appearance.SetData(ItemCabinetVisuals.ContainsItem, comp.ItemContainer.ContainedEntity != null);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ClickLatchSound(ItemCabinetComponent comp)
|
||||
{
|
||||
if (comp.DoorSound == null) return;
|
||||
SoundSystem.Play(Filter.Pvs(comp.Owner), comp.DoorSound, comp.Owner, AudioHelpers.WithVariation(0.15f));
|
||||
}
|
||||
}
|
||||
|
||||
public class ToggleItemCabinetEvent : EntityEventArgs
|
||||
{
|
||||
}
|
||||
|
||||
public class TryEjectItemCabinetEvent : CancellableEntityEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// The user who tried to eject the item.
|
||||
/// </summary>
|
||||
public IEntity User;
|
||||
|
||||
public TryEjectItemCabinetEvent(IEntity user)
|
||||
{
|
||||
User = user;
|
||||
}
|
||||
}
|
||||
|
||||
public class TryInsertItemCabinetEvent : CancellableEntityEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// The user who tried to eject the item.
|
||||
/// </summary>
|
||||
public IEntity User;
|
||||
|
||||
/// <summary>
|
||||
/// The item to be inserted.
|
||||
/// </summary>
|
||||
public IEntity Item;
|
||||
|
||||
public TryInsertItemCabinetEvent(IEntity user, IEntity item)
|
||||
{
|
||||
User = user;
|
||||
Item = item;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user