ConstructionSystem.Computer.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using Content.Server.Construction.Components;
  2. using Content.Server.Power.Components;
  3. using Content.Shared.Computer;
  4. using Content.Shared.Power;
  5. using Robust.Shared.Containers;
  6. namespace Content.Server.Construction;
  7. public sealed partial class ConstructionSystem
  8. {
  9. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  10. private void InitializeComputer()
  11. {
  12. SubscribeLocalEvent<ComputerComponent, ComponentInit>(OnCompInit);
  13. SubscribeLocalEvent<ComputerComponent, MapInitEvent>(OnCompMapInit);
  14. SubscribeLocalEvent<ComputerComponent, PowerChangedEvent>(OnCompPowerChange);
  15. }
  16. private void OnCompInit(EntityUid uid, ComputerComponent component, ComponentInit args)
  17. {
  18. // Let's ensure the container manager and container are here.
  19. _container.EnsureContainer<Container>(uid, "board");
  20. if (TryComp<ApcPowerReceiverComponent>(uid, out var powerReceiver))
  21. {
  22. _appearance.SetData(uid, ComputerVisuals.Powered, powerReceiver.Powered);
  23. }
  24. }
  25. private void OnCompMapInit(Entity<ComputerComponent> component, ref MapInitEvent args)
  26. {
  27. CreateComputerBoard(component);
  28. }
  29. private void OnCompPowerChange(EntityUid uid, ComputerComponent component, ref PowerChangedEvent args)
  30. {
  31. _appearance.SetData(uid, ComputerVisuals.Powered, args.Powered);
  32. }
  33. /// <summary>
  34. /// Creates the corresponding computer board on the computer.
  35. /// This exists so when you deconstruct computers that were serialized with the map,
  36. /// you can retrieve the computer board.
  37. /// </summary>
  38. private void CreateComputerBoard(Entity<ComputerComponent> ent)
  39. {
  40. var component = ent.Comp;
  41. // Ensure that the construction component is aware of the board container.
  42. if (TryComp<ConstructionComponent>(ent, out var construction))
  43. AddContainer(ent, "board", construction);
  44. // We don't do anything if this is null or empty.
  45. if (string.IsNullOrEmpty(component.BoardPrototype))
  46. return;
  47. var container = _container.EnsureContainer<Container>(ent, "board");
  48. // We already contain a board. Note: We don't check if it's the right one!
  49. if (container.ContainedEntities.Count != 0)
  50. return;
  51. var board = EntityManager.SpawnEntity(component.BoardPrototype, Transform(ent).Coordinates);
  52. if (!_container.Insert(board, container))
  53. Log.Warning($"Couldn't insert board {board} to computer {ent}!");
  54. }
  55. }