1
0

GunSystem.Solution.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using Content.Server.Chemistry.Components;
  2. using Content.Shared.Chemistry.Components;
  3. using Content.Shared.Chemistry.EntitySystems;
  4. using Content.Shared.FixedPoint;
  5. using Content.Shared.Vapor;
  6. using Content.Shared.Weapons.Ranged;
  7. using Content.Shared.Weapons.Ranged.Components;
  8. using Robust.Shared.Map;
  9. namespace Content.Server.Weapons.Ranged.Systems;
  10. public sealed partial class GunSystem
  11. {
  12. [Dependency] private readonly SharedSolutionContainerSystem _solutionContainer = default!;
  13. protected override void InitializeSolution()
  14. {
  15. base.InitializeSolution();
  16. SubscribeLocalEvent<SolutionAmmoProviderComponent, MapInitEvent>(OnSolutionMapInit);
  17. SubscribeLocalEvent<SolutionAmmoProviderComponent, SolutionContainerChangedEvent>(OnSolutionChanged);
  18. }
  19. private void OnSolutionMapInit(Entity<SolutionAmmoProviderComponent> entity, ref MapInitEvent args)
  20. {
  21. UpdateSolutionShots(entity.Owner, entity.Comp);
  22. }
  23. private void OnSolutionChanged(Entity<SolutionAmmoProviderComponent> entity, ref SolutionContainerChangedEvent args)
  24. {
  25. if (args.Solution.Name == entity.Comp.SolutionId)
  26. UpdateSolutionShots(entity.Owner, entity.Comp, args.Solution);
  27. }
  28. protected override void UpdateSolutionShots(EntityUid uid, SolutionAmmoProviderComponent component, Solution? solution = null)
  29. {
  30. var shots = 0;
  31. var maxShots = 0;
  32. if (solution == null && !_solutionContainer.TryGetSolution(uid, component.SolutionId, out _, out solution))
  33. {
  34. component.Shots = shots;
  35. DirtyField(uid, component, nameof(SolutionAmmoProviderComponent.Shots));
  36. component.MaxShots = maxShots;
  37. DirtyField(uid, component, nameof(SolutionAmmoProviderComponent.MaxShots));
  38. return;
  39. }
  40. shots = (int) (solution.Volume / component.FireCost);
  41. maxShots = (int) (solution.MaxVolume / component.FireCost);
  42. component.Shots = shots;
  43. DirtyField(uid, component, nameof(SolutionAmmoProviderComponent.Shots));
  44. component.MaxShots = maxShots;
  45. DirtyField(uid, component, nameof(SolutionAmmoProviderComponent.MaxShots));
  46. UpdateSolutionAppearance(uid, component);
  47. }
  48. protected override (EntityUid Entity, IShootable) GetSolutionShot(EntityUid uid, SolutionAmmoProviderComponent component, EntityCoordinates position)
  49. {
  50. var (ent, shootable) = base.GetSolutionShot(uid, component, position);
  51. if (!_solutionContainer.TryGetSolution(uid, component.SolutionId, out var solution, out _))
  52. return (ent, shootable);
  53. var newSolution = _solutionContainer.SplitSolution(solution.Value, component.FireCost);
  54. if (newSolution.Volume <= FixedPoint2.Zero)
  55. return (ent, shootable);
  56. if (TryComp<AppearanceComponent>(ent, out var appearance))
  57. {
  58. Appearance.SetData(ent, VaporVisuals.Color, newSolution.GetColor(ProtoManager).WithAlpha(1f), appearance);
  59. Appearance.SetData(ent, VaporVisuals.State, true, appearance);
  60. }
  61. // Add the solution to the vapor and actually send the thing
  62. if (_solutionContainer.TryGetSolution(ent, VaporComponent.SolutionName, out var vaporSolution, out _))
  63. {
  64. _solutionContainer.TryAddSolution(vaporSolution.Value, newSolution);
  65. }
  66. return (ent, shootable);
  67. }
  68. }