1
0

BuildMech.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using Content.Server.Mech.Systems;
  2. using Content.Server.Power.Components;
  3. using Content.Shared.Construction;
  4. using Content.Shared.Mech.Components;
  5. using JetBrains.Annotations;
  6. using Robust.Server.Containers;
  7. using Robust.Shared.Containers;
  8. using Robust.Shared.Prototypes;
  9. using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
  10. namespace Content.Server.Construction.Completions;
  11. /// <summary>
  12. /// Creates the mech entity while transferring all relevant parts inside of it,
  13. /// for right now, the cell that was used in construction.
  14. /// </summary>
  15. [UsedImplicitly, DataDefinition]
  16. public sealed partial class BuildMech : IGraphAction
  17. {
  18. [DataField("mechPrototype", required: true, customTypeSerializer: typeof(PrototypeIdSerializer<EntityPrototype>))]
  19. public string MechPrototype = string.Empty;
  20. [DataField("container")]
  21. public string Container = "battery-container";
  22. // TODO use or generalize ConstructionSystem.ChangeEntity();
  23. public void PerformAction(EntityUid uid, EntityUid? userUid, IEntityManager entityManager)
  24. {
  25. if (!entityManager.TryGetComponent(uid, out ContainerManagerComponent? containerManager))
  26. {
  27. Logger.Warning($"Mech construct entity {uid} did not have a container manager! Aborting build mech action.");
  28. return;
  29. }
  30. var containerSystem = entityManager.EntitySysManager.GetEntitySystem<ContainerSystem>();
  31. var mechSys = entityManager.System<MechSystem>();
  32. if (!containerSystem.TryGetContainer(uid, Container, out var container, containerManager))
  33. {
  34. Logger.Warning($"Mech construct entity {uid} did not have the specified '{Container}' container! Aborting build mech action.");
  35. return;
  36. }
  37. if (container.ContainedEntities.Count != 1)
  38. {
  39. Logger.Warning($"Mech construct entity {uid} did not have exactly one item in the specified '{Container}' container! Aborting build mech action.");
  40. }
  41. var cell = container.ContainedEntities[0];
  42. if (!entityManager.TryGetComponent<BatteryComponent>(cell, out var batteryComponent))
  43. {
  44. Logger.Warning($"Mech construct entity {uid} had an invalid entity in container \"{Container}\"! Aborting build mech action.");
  45. return;
  46. }
  47. containerSystem.Remove(cell, container);
  48. var transform = entityManager.GetComponent<TransformComponent>(uid);
  49. var mech = entityManager.SpawnEntity(MechPrototype, transform.Coordinates);
  50. if (entityManager.TryGetComponent<MechComponent>(mech, out var mechComp) && mechComp.BatterySlot.ContainedEntity == null)
  51. {
  52. mechSys.InsertBattery(mech, cell, mechComp, batteryComponent);
  53. containerSystem.Insert(cell, mechComp.BatterySlot);
  54. }
  55. var entChangeEv = new ConstructionChangeEntityEvent(mech, uid);
  56. entityManager.EventBus.RaiseLocalEvent(uid, entChangeEv);
  57. entityManager.EventBus.RaiseLocalEvent(mech, entChangeEv, broadcast: true);
  58. entityManager.QueueDeleteEntity(uid);
  59. }
  60. }