BorgSystem.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using Content.Shared.Mobs;
  2. using Content.Shared.Silicons.Borgs;
  3. using Content.Shared.Silicons.Borgs.Components;
  4. using Robust.Client.GameObjects;
  5. using Robust.Shared.Containers;
  6. namespace Content.Client.Silicons.Borgs;
  7. /// <inheritdoc/>
  8. public sealed class BorgSystem : SharedBorgSystem
  9. {
  10. [Dependency] private readonly AppearanceSystem _appearance = default!;
  11. public override void Initialize()
  12. {
  13. base.Initialize();
  14. SubscribeLocalEvent<BorgChassisComponent, AppearanceChangeEvent>(OnBorgAppearanceChanged);
  15. SubscribeLocalEvent<MMIComponent, AppearanceChangeEvent>(OnMMIAppearanceChanged);
  16. }
  17. private void OnBorgAppearanceChanged(EntityUid uid, BorgChassisComponent component, ref AppearanceChangeEvent args)
  18. {
  19. if (args.Sprite == null)
  20. return;
  21. UpdateBorgAppearance(uid, component, args.Component, args.Sprite);
  22. }
  23. protected override void OnInserted(EntityUid uid, BorgChassisComponent component, EntInsertedIntoContainerMessage args)
  24. {
  25. if (!component.Initialized)
  26. return;
  27. base.OnInserted(uid, component, args);
  28. UpdateBorgAppearance(uid, component);
  29. }
  30. protected override void OnRemoved(EntityUid uid, BorgChassisComponent component, EntRemovedFromContainerMessage args)
  31. {
  32. if (!component.Initialized)
  33. return;
  34. base.OnRemoved(uid, component, args);
  35. UpdateBorgAppearance(uid, component);
  36. }
  37. private void UpdateBorgAppearance(EntityUid uid,
  38. BorgChassisComponent? component = null,
  39. AppearanceComponent? appearance = null,
  40. SpriteComponent? sprite = null)
  41. {
  42. if (!Resolve(uid, ref component, ref appearance, ref sprite))
  43. return;
  44. if (_appearance.TryGetData<MobState>(uid, MobStateVisuals.State, out var state, appearance))
  45. {
  46. if (state != MobState.Alive)
  47. {
  48. sprite.LayerSetVisible(BorgVisualLayers.Light, false);
  49. return;
  50. }
  51. }
  52. if (!_appearance.TryGetData<bool>(uid, BorgVisuals.HasPlayer, out var hasPlayer, appearance))
  53. hasPlayer = false;
  54. sprite.LayerSetVisible(BorgVisualLayers.Light, component.BrainEntity != null || hasPlayer);
  55. sprite.LayerSetState(BorgVisualLayers.Light, hasPlayer ? component.HasMindState : component.NoMindState);
  56. }
  57. private void OnMMIAppearanceChanged(EntityUid uid, MMIComponent component, ref AppearanceChangeEvent args)
  58. {
  59. if (args.Sprite == null)
  60. return;
  61. var sprite = args.Sprite;
  62. if (!_appearance.TryGetData(uid, MMIVisuals.BrainPresent, out bool brain))
  63. brain = false;
  64. if (!_appearance.TryGetData(uid, MMIVisuals.HasMind, out bool hasMind))
  65. hasMind = false;
  66. sprite.LayerSetVisible(MMIVisualLayers.Brain, brain);
  67. if (!brain)
  68. {
  69. sprite.LayerSetState(MMIVisualLayers.Base, component.NoBrainState);
  70. }
  71. else
  72. {
  73. var state = hasMind
  74. ? component.HasMindState
  75. : component.NoMindState;
  76. sprite.LayerSetState(MMIVisualLayers.Base, state);
  77. }
  78. }
  79. /// <summary>
  80. /// Sets the sprite states used for the borg "is there a mind or not" indication.
  81. /// </summary>
  82. /// <param name="borg">The entity and component to modify.</param>
  83. /// <param name="hasMindState">The state to use if the borg has a mind.</param>
  84. /// <param name="noMindState">The state to use if the borg has no mind.</param>
  85. /// <seealso cref="BorgChassisComponent.HasMindState"/>
  86. /// <seealso cref="BorgChassisComponent.NoMindState"/>
  87. public void SetMindStates(Entity<BorgChassisComponent> borg, string hasMindState, string noMindState)
  88. {
  89. borg.Comp.HasMindState = hasMindState;
  90. borg.Comp.NoMindState = noMindState;
  91. }
  92. }