PneumaticCannonSystem.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using Content.Server.Atmos.Components;
  2. using Content.Server.Atmos.EntitySystems;
  3. using Content.Server.Storage.EntitySystems;
  4. using Content.Server.Stunnable;
  5. using Content.Server.Weapons.Ranged.Systems;
  6. using Content.Shared.Containers.ItemSlots;
  7. using Content.Shared.Interaction;
  8. using Content.Shared.PneumaticCannon;
  9. using Content.Shared.StatusEffect;
  10. using Content.Shared.Tools.Systems;
  11. using Content.Shared.Weapons.Ranged.Components;
  12. using Content.Shared.Weapons.Ranged.Events;
  13. using Content.Shared.Weapons.Ranged.Systems;
  14. using Robust.Shared.Containers;
  15. namespace Content.Server.PneumaticCannon;
  16. public sealed class PneumaticCannonSystem : SharedPneumaticCannonSystem
  17. {
  18. [Dependency] private readonly AtmosphereSystem _atmos = default!;
  19. [Dependency] private readonly GasTankSystem _gasTank = default!;
  20. [Dependency] private readonly GunSystem _gun = default!;
  21. [Dependency] private readonly StunSystem _stun = default!;
  22. [Dependency] private readonly ItemSlotsSystem _slots = default!;
  23. [Dependency] private readonly SharedToolSystem _toolSystem = default!;
  24. public override void Initialize()
  25. {
  26. base.Initialize();
  27. SubscribeLocalEvent<PneumaticCannonComponent, InteractUsingEvent>(OnInteractUsing, before: new []{ typeof(StorageSystem) });
  28. SubscribeLocalEvent<PneumaticCannonComponent, GunShotEvent>(OnShoot);
  29. SubscribeLocalEvent<PneumaticCannonComponent, ContainerIsInsertingAttemptEvent>(OnContainerInserting);
  30. SubscribeLocalEvent<PneumaticCannonComponent, GunRefreshModifiersEvent>(OnGunRefreshModifiers);
  31. }
  32. private void OnInteractUsing(EntityUid uid, PneumaticCannonComponent component, InteractUsingEvent args)
  33. {
  34. if (args.Handled)
  35. return;
  36. if (!_toolSystem.HasQuality(args.Used, component.ToolModifyPower))
  37. return;
  38. var val = (int) component.Power;
  39. val = (val + 1) % (int) PneumaticCannonPower.Len;
  40. component.Power = (PneumaticCannonPower) val;
  41. Popup.PopupEntity(Loc.GetString("pneumatic-cannon-component-change-power",
  42. ("power", component.Power.ToString())), uid, args.User);
  43. component.ProjectileSpeed = GetProjectileSpeedFromPower(component);
  44. if (TryComp<GunComponent>(uid, out var gun))
  45. _gun.RefreshModifiers((uid, gun));
  46. args.Handled = true;
  47. }
  48. private void OnContainerInserting(EntityUid uid, PneumaticCannonComponent component, ContainerIsInsertingAttemptEvent args)
  49. {
  50. if (args.Container.ID != PneumaticCannonComponent.TankSlotId)
  51. return;
  52. if (!TryComp<GasTankComponent>(args.EntityUid, out var gas))
  53. return;
  54. // only accept tanks if it uses gas
  55. if (gas.Air.TotalMoles >= component.GasUsage && component.GasUsage > 0f)
  56. return;
  57. args.Cancel();
  58. }
  59. private void OnShoot(Entity<PneumaticCannonComponent> cannon, ref GunShotEvent args)
  60. {
  61. var (uid, component) = cannon;
  62. // require a gas tank if it uses gas
  63. var gas = GetGas(cannon);
  64. if (gas == null && component.GasUsage > 0f)
  65. return;
  66. if (TryComp<StatusEffectsComponent>(args.User, out var status)
  67. && component.Power == PneumaticCannonPower.High)
  68. {
  69. _stun.TryParalyze(args.User, TimeSpan.FromSeconds(component.HighPowerStunTime), true, status);
  70. Popup.PopupEntity(Loc.GetString("pneumatic-cannon-component-power-stun",
  71. ("cannon", uid)), cannon, args.User);
  72. }
  73. // ignore gas stuff if the cannon doesn't use any
  74. if (gas == null)
  75. return;
  76. // this should always be possible, as we'll eject the gas tank when it no longer is
  77. var environment = _atmos.GetContainingMixture(cannon.Owner, false, true);
  78. var removed = _gasTank.RemoveAir(gas.Value, component.GasUsage);
  79. if (environment != null && removed != null)
  80. {
  81. _atmos.Merge(environment, removed);
  82. }
  83. if (gas.Value.Comp.Air.TotalMoles >= component.GasUsage)
  84. return;
  85. // eject gas tank
  86. _slots.TryEject(uid, PneumaticCannonComponent.TankSlotId, args.User, out _);
  87. }
  88. private void OnGunRefreshModifiers(Entity<PneumaticCannonComponent> ent, ref GunRefreshModifiersEvent args)
  89. {
  90. if (ent.Comp.ProjectileSpeed is { } speed)
  91. args.ProjectileSpeed = speed;
  92. }
  93. /// <summary>
  94. /// Returns whether the pneumatic cannon has enough gas to shoot an item, as well as the tank itself.
  95. /// </summary>
  96. private Entity<GasTankComponent>? GetGas(EntityUid uid)
  97. {
  98. if (!Container.TryGetContainer(uid, PneumaticCannonComponent.TankSlotId, out var container) ||
  99. container is not ContainerSlot slot || slot.ContainedEntity is not {} contained)
  100. return null;
  101. return TryComp<GasTankComponent>(contained, out var gasTank) ? (contained, gasTank) : null;
  102. }
  103. private float GetProjectileSpeedFromPower(PneumaticCannonComponent component)
  104. {
  105. return component.Power switch
  106. {
  107. PneumaticCannonPower.High => component.BaseProjectileSpeed * 4f,
  108. PneumaticCannonPower.Medium => component.BaseProjectileSpeed,
  109. PneumaticCannonPower.Low or _ => component.BaseProjectileSpeed * 0.5f,
  110. };
  111. }
  112. }