Files
tbd-station-14/Content.Shared/Containers/ItemSlot/SharedItemSlotsSystem.cs
Alex Evgrashin e5df8dbee3 Moving PDA to ECS (#4538)
* Moved pen slot to separate component

* Moved it all to more generic item slot class

* Add sounds

* Item slots now supports many slots

* Some clean-up

* Refactored slots a bit

* Moving ID card out

* Moving pda to system

* Moving PDA owner to ECS

* Moved PDA flashlight to separate component

* Toggle lights work through events

* Fixing UI

* Moving uplink to separate component

* Continue moving uplink to separate component

* More cleaning

* Removing pda shared

* Nuked shared pda component

* Fixed flashlight

* Pen slot now showed in UI

* Light toggle now shows correctly in UI

* Small refactoring of item slots

* Added contained entity

* Fixed tests

* Finished with PDA

* Moving PDA uplink to separate window

* Adding-removing uplink should show new button

* Working on a better debug

* Debug command to add uplink

* Uplink send state to UI

* Almost working UI

* Uplink correcty updates when you buy-sell items

* Ups

* Moved localization to separate file

* Minor fixes

* Removed item slots methods events

* Removed PDA owner name

* Removed one uplink event

* Deleted all uplink events

* Removed flashlight events

* Update Content.Shared/Traitor/Uplink/UplinkVisuals.cs

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>

* Update Content.Server/Containers/ItemSlot/ItemSlotsSystem.cs

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>

* Update Content.Server/Containers/ItemSlot/ItemSlotsSystem.cs

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>

* Update Content.Server/GameTicking/Presets/PresetTraitorDeathMatch.cs

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>

* Item slots system review

* Flashlight review

* PDA to XAML

* Move UplinkMenu to seperate class, fix WeightedColors methods

* Move UI to XAML

* Moved events to entity id

* Address review

* Removed uplink extensions

* Minor fix

* Moved item slots to shared

* My bad Robust...

* Fixed pda sound

* Fixed pda tests

* Fixed pda test again

Co-authored-by: Alexander Evgrashin <evgrashin.adl@gmail.com>
Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Co-authored-by: Visne <vincefvanwijk@gmail.com>
2021-10-03 15:05:52 +11:00

188 lines
6.7 KiB
C#

using Content.Shared.Hands.Components;
using Content.Shared.Interaction;
using Content.Shared.Item;
using Content.Shared.Popups;
using Robust.Shared.Audio;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.Localization;
using Robust.Shared.Player;
namespace Content.Shared.Containers.ItemSlots
{
public class SharedItemSlotsSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SharedItemSlotsComponent, ComponentInit>(OnComponentInit);
SubscribeLocalEvent<SharedItemSlotsComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<SharedItemSlotsComponent, InteractUsingEvent>(OnInteractUsing);
}
private void OnComponentInit(EntityUid uid, SharedItemSlotsComponent itemSlots, ComponentInit args)
{
// create container for each slot
foreach (var pair in itemSlots.Slots)
{
var slotName = pair.Key;
var slot = pair.Value;
slot.ContainerSlot = ContainerHelpers.EnsureContainer<ContainerSlot>(itemSlots.Owner, slotName);
}
}
private void OnMapInit(EntityUid uid, SharedItemSlotsComponent itemSlots, MapInitEvent args)
{
foreach (var pair in itemSlots.Slots)
{
var slot = pair.Value;
var slotName = pair.Key;
// Check if someone already put item inside container
if (slot.ContainerSlot.ContainedEntity != null)
continue;
// Try to spawn item inside each slot
if (!string.IsNullOrEmpty(slot.StartingItem))
{
var item = EntityManager.SpawnEntity(slot.StartingItem, itemSlots.Owner.Transform.Coordinates);
slot.ContainerSlot.Insert(item);
RaiseLocalEvent(uid, new ItemSlotChanged(itemSlots, slotName, slot));
}
}
}
private void OnInteractUsing(EntityUid uid, SharedItemSlotsComponent itemSlots, InteractUsingEvent args)
{
if (args.Handled)
return;
args.Handled = TryInsertContent(itemSlots, args.Used, args.User);
}
/// <summary>
/// Tries to insert item in any fitting item slot from users hand
/// </summary>
/// <returns>False if failed to insert item</returns>
public bool TryInsertContent(SharedItemSlotsComponent itemSlots, IEntity item, IEntity user)
{
foreach (var pair in itemSlots.Slots)
{
var slotName = pair.Key;
var slot = pair.Value;
// check if item allowed in whitelist
if (slot.Whitelist != null && !slot.Whitelist.IsValid(item))
continue;
// check if slot is empty
if (slot.ContainerSlot.Contains(item))
continue;
if (!user.TryGetComponent(out SharedHandsComponent? hands))
{
itemSlots.Owner.PopupMessage(user, Loc.GetString("item-slots-try-insert-no-hands"));
return true;
}
// get item inside container
IEntity? swap = null;
if (slot.ContainerSlot.ContainedEntity != null)
swap = slot.ContainerSlot.ContainedEntity;
// return if user can't drop active item in hand
if (!hands.TryDropEntityToFloor(item))
return true;
// swap item in hand and item in slot
if (swap != null)
hands.TryPutInAnyHand(swap);
// insert item
slot.ContainerSlot.Insert(item);
RaiseLocalEvent(itemSlots.Owner.Uid, new ItemSlotChanged(itemSlots, slotName, slot));
// play sound
if (slot.InsertSound != null)
SoundSystem.Play(Filter.Pvs(itemSlots.Owner), slot.InsertSound.GetSound(), itemSlots.Owner);
return true;
}
return false;
}
/// <summary>
/// Tries to insert item in known slot. Doesn't interact with user
/// </summary>
/// <returns>False if failed to insert item</returns>
public bool TryInsertContent(SharedItemSlotsComponent itemSlots, IEntity item, string slotName)
{
if (!itemSlots.Slots.TryGetValue(slotName, out var slot))
return false;
if (slot.ContainerSlot.ContainedEntity != null)
return false;
// check if item allowed in whitelist
if (slot.Whitelist != null && !slot.Whitelist.IsValid(item))
return false;
slot.ContainerSlot.Insert(item);
RaiseLocalEvent(itemSlots.Owner.Uid, new ItemSlotChanged(itemSlots, slotName, slot));
return true;
}
/// <summary>
/// Check if slot has some content in it (without ejecting item)
/// </summary>
/// <returns>Null if doesn't have any content</returns>
public IEntity? PeekItemInSlot(SharedItemSlotsComponent itemSlots, string slotName)
{
if (!itemSlots.Slots.TryGetValue(slotName, out var slot))
return null;
var item = slot.ContainerSlot.ContainedEntity;
return item;
}
/// <summary>
/// Try to eject item from slot to users hands
/// </summary>
public bool TryEjectContent(SharedItemSlotsComponent itemSlots, string slotName, IEntity? user)
{
if (!itemSlots.Slots.TryGetValue(slotName, out var slot))
return false;
if (slot.ContainerSlot.ContainedEntity == null)
return false;
var item = slot.ContainerSlot.ContainedEntity;
if (!slot.ContainerSlot.Remove(item))
return false;
// try eject item to users hand
if (user != null)
{
if (user.TryGetComponent(out SharedHandsComponent? hands))
{
hands.TryPutInAnyHand(item);
}
else
{
itemSlots.Owner.PopupMessage(user, Loc.GetString("item-slots-try-insert-no-hands"));
}
}
if (slot.EjectSound != null)
SoundSystem.Play(Filter.Pvs(itemSlots.Owner), slot.EjectSound.GetSound(), itemSlots.Owner);
RaiseLocalEvent(itemSlots.Owner.Uid, new ItemSlotChanged(itemSlots, slotName, slot));
return true;
}
}
}