NPCUseActionOnTargetSystem.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using Content.Server.NPC.Components;
  2. using Content.Server.NPC.HTN;
  3. using Content.Shared.Actions;
  4. using Robust.Shared.Timing;
  5. namespace Content.Server.NPC.Systems;
  6. public sealed class NPCUseActionOnTargetSystem : EntitySystem
  7. {
  8. [Dependency] private readonly IGameTiming _timing = default!;
  9. [Dependency] private readonly SharedActionsSystem _actions = default!;
  10. /// <inheritdoc/>
  11. public override void Initialize()
  12. {
  13. base.Initialize();
  14. SubscribeLocalEvent<NPCUseActionOnTargetComponent, MapInitEvent>(OnMapInit);
  15. }
  16. private void OnMapInit(Entity<NPCUseActionOnTargetComponent> ent, ref MapInitEvent args)
  17. {
  18. ent.Comp.ActionEnt = _actions.AddAction(ent, ent.Comp.ActionId);
  19. }
  20. public bool TryUseTentacleAttack(Entity<NPCUseActionOnTargetComponent?> user, EntityUid target)
  21. {
  22. if (!Resolve(user, ref user.Comp, false))
  23. return false;
  24. if (!TryComp<EntityWorldTargetActionComponent>(user.Comp.ActionEnt, out var action))
  25. return false;
  26. if (!_actions.ValidAction(action))
  27. return false;
  28. if (action.Event != null)
  29. {
  30. action.Event.Coords = Transform(target).Coordinates;
  31. }
  32. _actions.PerformAction(user,
  33. null,
  34. user.Comp.ActionEnt.Value,
  35. action,
  36. action.BaseEvent,
  37. _timing.CurTime,
  38. false);
  39. return true;
  40. }
  41. public override void Update(float frameTime)
  42. {
  43. base.Update(frameTime);
  44. // Tries to use the attack on the current target.
  45. var query = EntityQueryEnumerator<NPCUseActionOnTargetComponent, HTNComponent>();
  46. while (query.MoveNext(out var uid, out var comp, out var htn))
  47. {
  48. if (!htn.Blackboard.TryGetValue<EntityUid>(comp.TargetKey, out var target, EntityManager))
  49. continue;
  50. TryUseTentacleAttack((uid, comp), target);
  51. }
  52. }
  53. }