MechAssemblySystem.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using Content.Server.Mech.Components;
  2. using Content.Shared.Interaction;
  3. using Content.Shared.Tag;
  4. using Content.Shared.Tools.Components;
  5. using Content.Shared.Tools.Systems;
  6. using Robust.Server.Containers;
  7. using Robust.Shared.Containers;
  8. namespace Content.Server.Mech.Systems;
  9. /// <summary>
  10. /// Handles <see cref="MechAssemblyComponent"/> and the insertion
  11. /// and removal of parts from the assembly.
  12. /// </summary>
  13. public sealed class MechAssemblySystem : EntitySystem
  14. {
  15. [Dependency] private readonly ContainerSystem _container = default!;
  16. [Dependency] private readonly TagSystem _tag = default!;
  17. [Dependency] private readonly SharedToolSystem _toolSystem = default!;
  18. /// <inheritdoc/>
  19. public override void Initialize()
  20. {
  21. SubscribeLocalEvent<MechAssemblyComponent, ComponentInit>(OnInit);
  22. SubscribeLocalEvent<MechAssemblyComponent, InteractUsingEvent>(OnInteractUsing);
  23. }
  24. private void OnInit(EntityUid uid, MechAssemblyComponent component, ComponentInit args)
  25. {
  26. component.PartsContainer = _container.EnsureContainer<Container>(uid, "mech-assembly-container");
  27. }
  28. private void OnInteractUsing(EntityUid uid, MechAssemblyComponent component, InteractUsingEvent args)
  29. {
  30. if (_toolSystem.HasQuality(args.Used, component.QualityNeeded))
  31. {
  32. foreach (var tag in component.RequiredParts.Keys)
  33. {
  34. component.RequiredParts[tag] = false;
  35. }
  36. _container.EmptyContainer(component.PartsContainer);
  37. return;
  38. }
  39. if (!TryComp<TagComponent>(args.Used, out var tagComp))
  40. return;
  41. foreach (var (tag, val) in component.RequiredParts)
  42. {
  43. if (!val && _tag.HasTag(tagComp, tag))
  44. {
  45. component.RequiredParts[tag] = true;
  46. _container.Insert(args.Used, component.PartsContainer);
  47. break;
  48. }
  49. }
  50. //check to see if we have all the parts
  51. foreach (var val in component.RequiredParts.Values)
  52. {
  53. if (!val)
  54. return;
  55. }
  56. Spawn(component.FinishedPrototype, Transform(uid).Coordinates);
  57. EntityManager.DeleteEntity(uid);
  58. }
  59. }