1
0

SharedBorgSystem.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using Content.Shared.Containers.ItemSlots;
  2. using Content.Shared.IdentityManagement;
  3. using Content.Shared.Item.ItemToggle;
  4. using Content.Shared.Movement.Components;
  5. using Content.Shared.Movement.Systems;
  6. using Content.Shared.Popups;
  7. using Content.Shared.PowerCell.Components;
  8. using Content.Shared.Silicons.Borgs.Components;
  9. using Content.Shared.UserInterface;
  10. using Content.Shared.Wires;
  11. using Robust.Shared.Containers;
  12. namespace Content.Shared.Silicons.Borgs;
  13. /// <summary>
  14. /// This handles logic, interactions, and UI related to <see cref="BorgChassisComponent"/> and other related components.
  15. /// </summary>
  16. public abstract partial class SharedBorgSystem : EntitySystem
  17. {
  18. [Dependency] protected readonly SharedContainerSystem Container = default!;
  19. [Dependency] protected readonly ItemSlotsSystem ItemSlots = default!;
  20. [Dependency] protected readonly ItemToggleSystem Toggle = default!;
  21. [Dependency] protected readonly SharedPopupSystem Popup = default!;
  22. /// <inheritdoc/>
  23. public override void Initialize()
  24. {
  25. base.Initialize();
  26. SubscribeLocalEvent<BorgChassisComponent, ComponentStartup>(OnStartup);
  27. SubscribeLocalEvent<BorgChassisComponent, ItemSlotInsertAttemptEvent>(OnItemSlotInsertAttempt);
  28. SubscribeLocalEvent<BorgChassisComponent, ItemSlotEjectAttemptEvent>(OnItemSlotEjectAttempt);
  29. SubscribeLocalEvent<BorgChassisComponent, EntInsertedIntoContainerMessage>(OnInserted);
  30. SubscribeLocalEvent<BorgChassisComponent, EntRemovedFromContainerMessage>(OnRemoved);
  31. SubscribeLocalEvent<BorgChassisComponent, RefreshMovementSpeedModifiersEvent>(OnRefreshMovementSpeedModifiers);
  32. SubscribeLocalEvent<BorgChassisComponent, ActivatableUIOpenAttemptEvent>(OnUIOpenAttempt);
  33. SubscribeLocalEvent<TryGetIdentityShortInfoEvent>(OnTryGetIdentityShortInfo);
  34. InitializeRelay();
  35. }
  36. private void OnTryGetIdentityShortInfo(TryGetIdentityShortInfoEvent args)
  37. {
  38. if (args.Handled)
  39. {
  40. return;
  41. }
  42. if (!HasComp<BorgChassisComponent>(args.ForActor))
  43. {
  44. return;
  45. }
  46. args.Title = Name(args.ForActor).Trim();
  47. args.Handled = true;
  48. }
  49. private void OnItemSlotInsertAttempt(EntityUid uid, BorgChassisComponent component, ref ItemSlotInsertAttemptEvent args)
  50. {
  51. if (args.Cancelled)
  52. return;
  53. if (!TryComp<PowerCellSlotComponent>(uid, out var cellSlotComp) ||
  54. !TryComp<WiresPanelComponent>(uid, out var panel))
  55. return;
  56. if (!ItemSlots.TryGetSlot(uid, cellSlotComp.CellSlotId, out var cellSlot) || cellSlot != args.Slot)
  57. return;
  58. if (!panel.Open || args.User == uid)
  59. args.Cancelled = true;
  60. }
  61. private void OnItemSlotEjectAttempt(EntityUid uid, BorgChassisComponent component, ref ItemSlotEjectAttemptEvent args)
  62. {
  63. if (args.Cancelled)
  64. return;
  65. if (!TryComp<PowerCellSlotComponent>(uid, out var cellSlotComp) ||
  66. !TryComp<WiresPanelComponent>(uid, out var panel))
  67. return;
  68. if (!ItemSlots.TryGetSlot(uid, cellSlotComp.CellSlotId, out var cellSlot) || cellSlot != args.Slot)
  69. return;
  70. if (!panel.Open || args.User == uid)
  71. args.Cancelled = true;
  72. }
  73. private void OnStartup(EntityUid uid, BorgChassisComponent component, ComponentStartup args)
  74. {
  75. if (!TryComp<ContainerManagerComponent>(uid, out var containerManager))
  76. return;
  77. component.BrainContainer = Container.EnsureContainer<ContainerSlot>(uid, component.BrainContainerId, containerManager);
  78. component.ModuleContainer = Container.EnsureContainer<Container>(uid, component.ModuleContainerId, containerManager);
  79. }
  80. private void OnUIOpenAttempt(EntityUid uid, BorgChassisComponent component, ActivatableUIOpenAttemptEvent args)
  81. {
  82. // borgs can't view their own ui
  83. if (args.User == uid)
  84. args.Cancel();
  85. }
  86. protected virtual void OnInserted(EntityUid uid, BorgChassisComponent component, EntInsertedIntoContainerMessage args)
  87. {
  88. }
  89. protected virtual void OnRemoved(EntityUid uid, BorgChassisComponent component, EntRemovedFromContainerMessage args)
  90. {
  91. }
  92. private void OnRefreshMovementSpeedModifiers(EntityUid uid, BorgChassisComponent component, RefreshMovementSpeedModifiersEvent args)
  93. {
  94. if (Toggle.IsActivated(uid))
  95. return;
  96. if (!TryComp<MovementSpeedModifierComponent>(uid, out var movement))
  97. return;
  98. var sprintDif = movement.BaseWalkSpeed / movement.BaseSprintSpeed;
  99. args.ModifySpeed(1f, sprintDif);
  100. }
  101. /// <summary>
  102. /// Sets <see cref="BorgModuleComponent.DefaultModule"/>.
  103. /// </summary>
  104. public void SetBorgModuleDefault(Entity<BorgModuleComponent> ent, bool newDefault)
  105. {
  106. ent.Comp.DefaultModule = newDefault;
  107. Dirty(ent);
  108. }
  109. }