ChemicalAmmoSystem.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using Content.Server.Weapons.Ranged.Components;
  2. using Content.Shared.Chemistry.Components;
  3. using Content.Shared.Weapons.Ranged.Events;
  4. using Content.Shared.Chemistry.EntitySystems;
  5. using System.Linq;
  6. namespace Content.Server.Weapons.Ranged.Systems
  7. {
  8. public sealed class ChemicalAmmoSystem : EntitySystem
  9. {
  10. [Dependency] private readonly SharedSolutionContainerSystem _solutionContainerSystem = default!;
  11. public override void Initialize()
  12. {
  13. SubscribeLocalEvent<ChemicalAmmoComponent, AmmoShotEvent>(OnFire);
  14. }
  15. private void OnFire(Entity<ChemicalAmmoComponent> entity, ref AmmoShotEvent args)
  16. {
  17. if (!_solutionContainerSystem.TryGetSolution(entity.Owner, entity.Comp.SolutionName, out var ammoSoln, out var ammoSolution))
  18. return;
  19. var projectiles = args.FiredProjectiles;
  20. var projectileSolutionContainers = new List<(EntityUid, Entity<SolutionComponent>)>();
  21. foreach (var projectile in projectiles)
  22. {
  23. if (_solutionContainerSystem
  24. .TryGetSolution(projectile, entity.Comp.SolutionName, out var projectileSoln, out _))
  25. {
  26. projectileSolutionContainers.Add((projectile, projectileSoln.Value));
  27. }
  28. }
  29. if (!projectileSolutionContainers.Any())
  30. return;
  31. var solutionPerProjectile = ammoSolution.Volume * (1 / projectileSolutionContainers.Count);
  32. foreach (var (_, projectileSolution) in projectileSolutionContainers)
  33. {
  34. var solutionToTransfer = _solutionContainerSystem.SplitSolution(ammoSoln.Value, solutionPerProjectile);
  35. _solutionContainerSystem.TryAddSolution(projectileSolution, solutionToTransfer);
  36. }
  37. _solutionContainerSystem.RemoveAllSolution(ammoSoln.Value);
  38. }
  39. }
  40. }