SharedScatteringGrenadeSystem.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using Content.Shared.Explosion.Components;
  2. using Content.Shared.Interaction;
  3. using Content.Shared.Whitelist;
  4. using Robust.Shared.Containers;
  5. namespace Content.Shared.Explosion.EntitySystems;
  6. public abstract class SharedScatteringGrenadeSystem : EntitySystem
  7. {
  8. [Dependency] private readonly EntityWhitelistSystem _whitelistSystem = default!;
  9. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  10. [Dependency] private readonly SharedContainerSystem _container = default!;
  11. public override void Initialize()
  12. {
  13. base.Initialize();
  14. SubscribeLocalEvent<ScatteringGrenadeComponent, ComponentInit>(OnScatteringInit);
  15. SubscribeLocalEvent<ScatteringGrenadeComponent, ComponentStartup>(OnScatteringStartup);
  16. SubscribeLocalEvent<ScatteringGrenadeComponent, InteractUsingEvent>(OnScatteringInteractUsing);
  17. }
  18. private void OnScatteringInit(Entity<ScatteringGrenadeComponent> entity, ref ComponentInit args)
  19. {
  20. entity.Comp.Container = _container.EnsureContainer<Container>(entity.Owner, "cluster-payload");
  21. }
  22. /// <summary>
  23. /// Setting the unspawned count based on capacity, so we know how many new entities to spawn
  24. /// Update appearance based on initial fill amount
  25. /// </summary>
  26. private void OnScatteringStartup(Entity<ScatteringGrenadeComponent> entity, ref ComponentStartup args)
  27. {
  28. if (entity.Comp.FillPrototype == null)
  29. return;
  30. entity.Comp.UnspawnedCount = Math.Max(0, entity.Comp.Capacity - entity.Comp.Container.ContainedEntities.Count);
  31. UpdateAppearance(entity);
  32. Dirty(entity, entity.Comp);
  33. }
  34. /// <summary>
  35. /// There are some scattergrenades you can fill up with more grenades (like clusterbangs)
  36. /// This covers how you insert more into it
  37. /// </summary>
  38. private void OnScatteringInteractUsing(Entity<ScatteringGrenadeComponent> entity, ref InteractUsingEvent args)
  39. {
  40. if (entity.Comp.Whitelist == null)
  41. return;
  42. // Make sure there's room for another grenade to be added
  43. if (entity.Comp.Count >= entity.Comp.Capacity)
  44. return;
  45. if (args.Handled || !_whitelistSystem.IsValid(entity.Comp.Whitelist, args.Used))
  46. return;
  47. _container.Insert(args.Used, entity.Comp.Container);
  48. UpdateAppearance(entity);
  49. args.Handled = true;
  50. }
  51. /// <summary>
  52. /// Update appearance based off of total count of contents
  53. /// </summary>
  54. private void UpdateAppearance(Entity<ScatteringGrenadeComponent> entity)
  55. {
  56. if (!TryComp<AppearanceComponent>(entity, out var appearanceComponent))
  57. return;
  58. _appearance.SetData(entity, ClusterGrenadeVisuals.GrenadesCounter, entity.Comp.Count, appearanceComponent);
  59. }
  60. }