ActionGunSystem.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using Content.Shared.Actions;
  2. using Content.Shared.Weapons.Ranged.Components;
  3. namespace Content.Shared.Weapons.Ranged.Systems;
  4. public sealed class ActionGunSystem : EntitySystem
  5. {
  6. [Dependency] private readonly SharedActionsSystem _actions = default!;
  7. [Dependency] private readonly SharedGunSystem _gun = default!;
  8. public override void Initialize()
  9. {
  10. base.Initialize();
  11. SubscribeLocalEvent<ActionGunComponent, MapInitEvent>(OnMapInit);
  12. SubscribeLocalEvent<ActionGunComponent, ComponentShutdown>(OnShutdown);
  13. SubscribeLocalEvent<ActionGunComponent, ActionGunShootEvent>(OnShoot);
  14. }
  15. private void OnMapInit(Entity<ActionGunComponent> ent, ref MapInitEvent args)
  16. {
  17. if (string.IsNullOrEmpty(ent.Comp.Action))
  18. return;
  19. _actions.AddAction(ent, ref ent.Comp.ActionEntity, ent.Comp.Action);
  20. ent.Comp.Gun = Spawn(ent.Comp.GunProto);
  21. }
  22. private void OnShutdown(Entity<ActionGunComponent> ent, ref ComponentShutdown args)
  23. {
  24. if (ent.Comp.Gun is {} gun)
  25. QueueDel(gun);
  26. }
  27. private void OnShoot(Entity<ActionGunComponent> ent, ref ActionGunShootEvent args)
  28. {
  29. if (TryComp<GunComponent>(ent.Comp.Gun, out var gun))
  30. _gun.AttemptShoot(ent, ent.Comp.Gun.Value, gun, args.Target);
  31. }
  32. }