WaitOperator.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. namespace Content.Server.NPC.HTN.PrimitiveTasks.Operators;
  2. /// <summary>
  3. /// Waits the specified amount of time. Removes the key when finished.
  4. /// </summary>
  5. public sealed partial class WaitOperator : HTNOperator
  6. {
  7. [Dependency] private readonly IEntityManager _entManager = default!;
  8. /// <summary>
  9. /// Blackboard key for the time we'll wait for.
  10. /// </summary>
  11. [DataField("key", required: true)] public string Key = string.Empty;
  12. public override HTNOperatorStatus Update(NPCBlackboard blackboard, float frameTime)
  13. {
  14. if (!blackboard.TryGetValue<float>(Key, out var timer, _entManager))
  15. {
  16. return HTNOperatorStatus.Finished;
  17. }
  18. timer -= frameTime;
  19. blackboard.SetValue(Key, timer);
  20. return timer <= 0f ? HTNOperatorStatus.Finished : HTNOperatorStatus.Continuing;
  21. }
  22. public override void TaskShutdown(NPCBlackboard blackboard, HTNOperatorStatus status)
  23. {
  24. base.TaskShutdown(blackboard, status);
  25. // The replacement plan may want this value so only dump it if we're successful.
  26. if (status != HTNOperatorStatus.BetterPlan)
  27. {
  28. blackboard.Remove<float>(Key);
  29. }
  30. }
  31. }