NodeContainerSystem.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using System.Diagnostics.CodeAnalysis;
  2. using Content.Server.NodeContainer.NodeGroups;
  3. using Content.Server.NodeContainer.Nodes;
  4. using Content.Shared.Examine;
  5. using JetBrains.Annotations;
  6. namespace Content.Server.NodeContainer.EntitySystems
  7. {
  8. /// <summary>
  9. /// Manages <see cref="NodeContainerComponent"/> events.
  10. /// </summary>
  11. /// <seealso cref="NodeGroupSystem"/>
  12. [UsedImplicitly]
  13. public sealed class NodeContainerSystem : EntitySystem
  14. {
  15. [Dependency] private readonly NodeGroupSystem _nodeGroupSystem = default!;
  16. private EntityQuery<NodeContainerComponent> _query;
  17. public override void Initialize()
  18. {
  19. base.Initialize();
  20. SubscribeLocalEvent<NodeContainerComponent, ComponentInit>(OnInitEvent);
  21. SubscribeLocalEvent<NodeContainerComponent, ComponentStartup>(OnStartupEvent);
  22. SubscribeLocalEvent<NodeContainerComponent, ComponentShutdown>(OnShutdownEvent);
  23. SubscribeLocalEvent<NodeContainerComponent, AnchorStateChangedEvent>(OnAnchorStateChanged);
  24. SubscribeLocalEvent<NodeContainerComponent, ReAnchorEvent>(OnReAnchor);
  25. SubscribeLocalEvent<NodeContainerComponent, MoveEvent>(OnMoveEvent);
  26. SubscribeLocalEvent<NodeContainerComponent, ExaminedEvent>(OnExamine);
  27. _query = GetEntityQuery<NodeContainerComponent>();
  28. }
  29. public bool TryGetNode<T>(NodeContainerComponent component, string? identifier, [NotNullWhen(true)] out T? node) where T : Node
  30. {
  31. if (identifier == null)
  32. {
  33. node = null;
  34. return false;
  35. }
  36. if (component.Nodes.TryGetValue(identifier, out var n) && n is T t)
  37. {
  38. node = t;
  39. return true;
  40. }
  41. node = null;
  42. return false;
  43. }
  44. public bool TryGetNode<T>(Entity<NodeContainerComponent?> ent, string identifier, [NotNullWhen(true)] out T? node) where T : Node
  45. {
  46. if (_query.Resolve(ent, ref ent.Comp, false)
  47. && ent.Comp.Nodes.TryGetValue(identifier, out var n)
  48. && n is T t)
  49. {
  50. node = t;
  51. return true;
  52. }
  53. node = null;
  54. return false;
  55. }
  56. public bool TryGetNodes<T1, T2>(
  57. Entity<NodeContainerComponent?> ent,
  58. string id1,
  59. string id2,
  60. [NotNullWhen(true)] out T1? node1,
  61. [NotNullWhen(true)] out T2? node2)
  62. where T1 : Node
  63. where T2 : Node
  64. {
  65. if (_query.Resolve(ent, ref ent.Comp, false)
  66. && ent.Comp.Nodes.TryGetValue(id1, out var n1)
  67. && n1 is T1 t1
  68. && ent.Comp.Nodes.TryGetValue(id2, out var n2)
  69. && n2 is T2 t2)
  70. {
  71. node1 = t1;
  72. node2 = t2;
  73. return true;
  74. }
  75. node1 = null;
  76. node2 = null;
  77. return false;
  78. }
  79. public bool TryGetNodes<T1, T2, T3>(
  80. Entity<NodeContainerComponent?> ent,
  81. string id1,
  82. string id2,
  83. string id3,
  84. [NotNullWhen(true)] out T1? node1,
  85. [NotNullWhen(true)] out T2? node2,
  86. [NotNullWhen(true)] out T3? node3)
  87. where T1 : Node
  88. where T2 : Node
  89. where T3 : Node
  90. {
  91. if (_query.Resolve(ent, ref ent.Comp, false)
  92. && ent.Comp.Nodes.TryGetValue(id1, out var n1)
  93. && n1 is T1 t1
  94. && ent.Comp.Nodes.TryGetValue(id2, out var n2)
  95. && n2 is T2 t2
  96. && ent.Comp.Nodes.TryGetValue(id3, out var n3)
  97. && n3 is T3 t3)
  98. {
  99. node1 = t1;
  100. node2 = t2;
  101. node3 = t3;
  102. return true;
  103. }
  104. node1 = null;
  105. node2 = null;
  106. node3 = null;
  107. return false;
  108. }
  109. private void OnInitEvent(EntityUid uid, NodeContainerComponent component, ComponentInit args)
  110. {
  111. foreach (var (key, node) in component.Nodes)
  112. {
  113. node.Name = key;
  114. node.Initialize(uid, EntityManager);
  115. }
  116. }
  117. private void OnStartupEvent(EntityUid uid, NodeContainerComponent component, ComponentStartup args)
  118. {
  119. foreach (var node in component.Nodes.Values)
  120. {
  121. _nodeGroupSystem.QueueReflood(node);
  122. }
  123. }
  124. private void OnShutdownEvent(EntityUid uid, NodeContainerComponent component, ComponentShutdown args)
  125. {
  126. foreach (var node in component.Nodes.Values)
  127. {
  128. _nodeGroupSystem.QueueNodeRemove(node);
  129. node.Deleting = true;
  130. }
  131. }
  132. private void OnAnchorStateChanged(
  133. EntityUid uid,
  134. NodeContainerComponent component,
  135. ref AnchorStateChangedEvent args)
  136. {
  137. foreach (var node in component.Nodes.Values)
  138. {
  139. if (!node.NeedAnchored)
  140. continue;
  141. node.OnAnchorStateChanged(EntityManager, args.Anchored);
  142. if (args.Anchored)
  143. _nodeGroupSystem.QueueReflood(node);
  144. else
  145. _nodeGroupSystem.QueueNodeRemove(node);
  146. }
  147. }
  148. private void OnReAnchor(EntityUid uid, NodeContainerComponent component, ref ReAnchorEvent args)
  149. {
  150. foreach (var node in component.Nodes.Values)
  151. {
  152. _nodeGroupSystem.QueueNodeRemove(node);
  153. _nodeGroupSystem.QueueReflood(node);
  154. }
  155. }
  156. private void OnMoveEvent(EntityUid uid, NodeContainerComponent container, ref MoveEvent ev)
  157. {
  158. if (ev.NewRotation == ev.OldRotation)
  159. {
  160. return;
  161. }
  162. var xform = ev.Component;
  163. foreach (var node in container.Nodes.Values)
  164. {
  165. if (node is not IRotatableNode rotatableNode)
  166. continue;
  167. // Don't bother updating nodes that can't even be connected to anything atm.
  168. if (!node.Connectable(EntityManager, xform))
  169. continue;
  170. if (rotatableNode.RotateNode(in ev))
  171. _nodeGroupSystem.QueueReflood(node);
  172. }
  173. }
  174. private void OnExamine(EntityUid uid, NodeContainerComponent component, ExaminedEvent args)
  175. {
  176. if (!component.Examinable || !args.IsInDetailsRange)
  177. return;
  178. foreach (var node in component.Nodes.Values)
  179. {
  180. if (node == null) continue;
  181. switch (node.NodeGroupID)
  182. {
  183. case NodeGroupID.HVPower:
  184. args.PushMarkup(
  185. Loc.GetString("node-container-component-on-examine-details-hvpower"));
  186. break;
  187. case NodeGroupID.MVPower:
  188. args.PushMarkup(
  189. Loc.GetString("node-container-component-on-examine-details-mvpower"));
  190. break;
  191. case NodeGroupID.Apc:
  192. args.PushMarkup(
  193. Loc.GetString("node-container-component-on-examine-details-apc"));
  194. break;
  195. }
  196. }
  197. }
  198. }
  199. }