SharedGunSystem.Solution.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using Content.Shared.Chemistry.Components;
  2. using Content.Shared.Weapons.Ranged.Components;
  3. using Content.Shared.Weapons.Ranged.Events;
  4. using Robust.Shared.Map;
  5. namespace Content.Shared.Weapons.Ranged.Systems;
  6. public partial class SharedGunSystem
  7. {
  8. protected virtual void InitializeSolution()
  9. {
  10. SubscribeLocalEvent<SolutionAmmoProviderComponent, TakeAmmoEvent>(OnSolutionTakeAmmo);
  11. SubscribeLocalEvent<SolutionAmmoProviderComponent, GetAmmoCountEvent>(OnSolutionAmmoCount);
  12. }
  13. private void OnSolutionTakeAmmo(EntityUid uid, SolutionAmmoProviderComponent component, TakeAmmoEvent args)
  14. {
  15. var shots = Math.Min(args.Shots, component.Shots);
  16. // Don't dirty if it's an empty fire.
  17. if (shots == 0)
  18. return;
  19. for (var i = 0; i < shots; i++)
  20. {
  21. args.Ammo.Add(GetSolutionShot(uid, component, args.Coordinates));
  22. component.Shots--;
  23. }
  24. UpdateSolutionShots(uid, component);
  25. UpdateSolutionAppearance(uid, component);
  26. }
  27. private void OnSolutionAmmoCount(EntityUid uid, SolutionAmmoProviderComponent component, ref GetAmmoCountEvent args)
  28. {
  29. args.Count = component.Shots;
  30. args.Capacity = component.MaxShots;
  31. }
  32. protected virtual void UpdateSolutionShots(EntityUid uid, SolutionAmmoProviderComponent component, Solution? solution = null)
  33. {
  34. }
  35. protected virtual (EntityUid Entity, IShootable) GetSolutionShot(EntityUid uid, SolutionAmmoProviderComponent component, EntityCoordinates position)
  36. {
  37. var ent = Spawn(component.Prototype, position);
  38. return (ent, EnsureShootable(ent));
  39. }
  40. protected void UpdateSolutionAppearance(EntityUid uid, SolutionAmmoProviderComponent component)
  41. {
  42. if (!TryComp<AppearanceComponent>(uid, out var appearance))
  43. return;
  44. Appearance.SetData(uid, AmmoVisuals.HasAmmo, component.Shots != 0, appearance);
  45. Appearance.SetData(uid, AmmoVisuals.AmmoCount, component.Shots, appearance);
  46. Appearance.SetData(uid, AmmoVisuals.AmmoMax, component.MaxShots, appearance);
  47. }
  48. }