1
0

SharedPdaSystem.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using Content.Shared.Access.Components;
  2. using Content.Shared.Containers.ItemSlots;
  3. using Robust.Shared.Containers;
  4. namespace Content.Shared.PDA
  5. {
  6. public abstract class SharedPdaSystem : EntitySystem
  7. {
  8. [Dependency] protected readonly ItemSlotsSystem ItemSlotsSystem = default!;
  9. [Dependency] protected readonly SharedAppearanceSystem Appearance = default!;
  10. public override void Initialize()
  11. {
  12. base.Initialize();
  13. SubscribeLocalEvent<PdaComponent, ComponentInit>(OnComponentInit);
  14. SubscribeLocalEvent<PdaComponent, ComponentRemove>(OnComponentRemove);
  15. SubscribeLocalEvent<PdaComponent, EntInsertedIntoContainerMessage>(OnItemInserted);
  16. SubscribeLocalEvent<PdaComponent, EntRemovedFromContainerMessage>(OnItemRemoved);
  17. SubscribeLocalEvent<PdaComponent, GetAdditionalAccessEvent>(OnGetAdditionalAccess);
  18. }
  19. protected virtual void OnComponentInit(EntityUid uid, PdaComponent pda, ComponentInit args)
  20. {
  21. if (pda.IdCard != null)
  22. pda.IdSlot.StartingItem = pda.IdCard;
  23. ItemSlotsSystem.AddItemSlot(uid, PdaComponent.PdaIdSlotId, pda.IdSlot);
  24. ItemSlotsSystem.AddItemSlot(uid, PdaComponent.PdaPenSlotId, pda.PenSlot);
  25. ItemSlotsSystem.AddItemSlot(uid, PdaComponent.PdaPaiSlotId, pda.PaiSlot);
  26. UpdatePdaAppearance(uid, pda);
  27. }
  28. private void OnComponentRemove(EntityUid uid, PdaComponent pda, ComponentRemove args)
  29. {
  30. ItemSlotsSystem.RemoveItemSlot(uid, pda.IdSlot);
  31. ItemSlotsSystem.RemoveItemSlot(uid, pda.PenSlot);
  32. ItemSlotsSystem.RemoveItemSlot(uid, pda.PaiSlot);
  33. }
  34. protected virtual void OnItemInserted(EntityUid uid, PdaComponent pda, EntInsertedIntoContainerMessage args)
  35. {
  36. if (args.Container.ID == PdaComponent.PdaIdSlotId)
  37. pda.ContainedId = args.Entity;
  38. UpdatePdaAppearance(uid, pda);
  39. }
  40. protected virtual void OnItemRemoved(EntityUid uid, PdaComponent pda, EntRemovedFromContainerMessage args)
  41. {
  42. if (args.Container.ID == pda.IdSlot.ID)
  43. pda.ContainedId = null;
  44. UpdatePdaAppearance(uid, pda);
  45. }
  46. private void OnGetAdditionalAccess(EntityUid uid, PdaComponent component, ref GetAdditionalAccessEvent args)
  47. {
  48. if (component.ContainedId is { } id)
  49. args.Entities.Add(id);
  50. }
  51. private void UpdatePdaAppearance(EntityUid uid, PdaComponent pda)
  52. {
  53. Appearance.SetData(uid, PdaVisuals.IdCardInserted, pda.ContainedId != null);
  54. }
  55. }
  56. }