Client LuaCsForBarotrauma
NPCWaitAction.cs
1 using System.Collections.Generic;
2 using System.Linq;
3 
4 namespace Barotrauma
5 {
10  {
11  [Serialize("", IsPropertySaveable.Yes, description: "Tag of the NPC(s) that should wait.")]
12  public Identifier NPCTag { get; set; }
13 
14  [Serialize(true, IsPropertySaveable.Yes, description: "Should the NPC start or stop waiting?")]
15  public bool Wait { get; set; }
16 
17  private bool isFinished = false;
18 
19 
20  public NPCWaitAction(ScriptedEvent parentEvent, ContentXElement element) : base(parentEvent, element) { }
21 
22  private IEnumerable<Character> affectedNpcs;
23 
24  public override void Update(float deltaTime)
25  {
26  if (isFinished) { return; }
27 
28  affectedNpcs = ParentEvent.GetTargets(NPCTag).Where(c => c is Character).Select(c => c as Character);
29 
30  foreach (var npc in affectedNpcs)
31  {
32  if (npc.Removed) { continue; }
33  if (npc.AIController is not HumanAIController humanAiController) { continue; }
34 
35  if (Wait)
36  {
37  var gotoObjective = new AIObjectiveGoTo(
38  AIObjectiveGoTo.GetTargetHull(npc) as ISpatialEntity ?? npc, npc, humanAiController.ObjectiveManager, repeat: true)
39  {
40  FaceTargetOnCompleted = false,
41  OverridePriority = 100.0f,
42  SourceEventAction = this,
43  IsWaitOrder = true,
44  CloseEnough = 100
45  };
46  humanAiController.ObjectiveManager.AddObjective(gotoObjective);
47  humanAiController.ObjectiveManager.WaitTimer = 0.0f;
48  }
49  else
50  {
51  AbandonGoToObjectives(humanAiController);
52  }
53  }
54  isFinished = true;
55  }
56 
57  public override bool IsFinished(ref string goTo)
58  {
59  return isFinished;
60  }
61 
62  public override void Reset()
63  {
64  if (affectedNpcs != null)
65  {
66  foreach (var npc in affectedNpcs)
67  {
68  if (npc.Removed || npc.AIController is not HumanAIController aiController) { continue; }
69  AbandonGoToObjectives(aiController);
70  }
71  affectedNpcs = null;
72  }
73  isFinished = false;
74  }
75 
76  private void AbandonGoToObjectives(HumanAIController aiController)
77  {
78  foreach (var objective in aiController.ObjectiveManager.Objectives)
79  {
80  if (objective is AIObjectiveGoTo gotoObjective && gotoObjective.SourceEventAction?.ParentEvent == ParentEvent)
81  {
82  gotoObjective.Abandon = true;
83  }
84  }
85  }
86 
87  public override string ToDebugString()
88  {
89  return $"{ToolBox.GetDebugSymbol(isFinished)} {nameof(NPCWaitAction)} -> (NPCTag: {NPCTag.ColorizeObject()}, Wait: {Wait.ColorizeObject()})";
90  }
91  }
92 }
EventAction SourceEventAction
Which event action (if any) created this objective
List< AIObjective > Objectives
Excluding the current order.
readonly ScriptedEvent ParentEvent
Definition: EventAction.cs:106
Makes an NPC stop and wait.
override bool IsFinished(ref string goTo)
Has the action finished.
override void Reset()
NPCWaitAction(ScriptedEvent parentEvent, ContentXElement element)
override void Update(float deltaTime)
override string ToDebugString()
Rich test to display in debugdraw
IEnumerable< Entity > GetTargets(Identifier tag)