Client LuaCsForBarotrauma
EventAction.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Reflection;
5 
6 namespace Barotrauma
7 {
8  abstract class EventAction
9  {
10  public class SubactionGroup
11  {
12  public string Text;
13  public List<EventAction> Actions;
18  public bool EndConversation;
19 
20  private int currentSubAction = 0;
21 
23  {
24  get
25  {
26  if (currentSubAction >= 0 && Actions.Count > currentSubAction)
27  {
28  return Actions[currentSubAction];
29  }
30  return null;
31  }
32  }
33 
34  public SubactionGroup(ScriptedEvent scriptedEvent, ContentXElement elem)
35  {
36  Text = elem.GetAttribute("text")?.Value ?? "";
37  Actions = new List<EventAction>();
38  EndConversation = elem.GetAttributeBool("endconversation", false);
39  foreach (var e in elem.Elements())
40  {
41  if (e.Name.ToString().Equals("statuseffect", StringComparison.OrdinalIgnoreCase))
42  {
43  DebugConsole.ThrowError($"Error in event prefab \"{scriptedEvent.Prefab.Identifier}\". Status effect configured as a sub action (text: \"{Text}\"). Please configure status effects as child elements of a StatusEffectAction.",
44  contentPackage: elem.ContentPackage);
45  continue;
46  }
47  var action = Instantiate(scriptedEvent, e);
48  if (action != null) { Actions.Add(action); }
49  }
50  }
51 
52  public bool IsFinished(ref string goTo)
53  {
54  if (currentSubAction < Actions.Count)
55  {
56  string innerGoTo = null;
57  if (Actions[currentSubAction].IsFinished(ref innerGoTo))
58  {
59  if (string.IsNullOrEmpty(innerGoTo))
60  {
61  currentSubAction++;
62  }
63  else
64  {
65  goTo = innerGoTo;
66  return true;
67  }
68  }
69  }
70  if (currentSubAction >= Actions.Count)
71  {
72  return true;
73  }
74  return false;
75  }
76 
77  public bool SetGoToTarget(string goTo)
78  {
79  currentSubAction = 0;
80  for (int i = 0; i < Actions.Count; i++)
81  {
82  if (Actions[i].SetGoToTarget(goTo))
83  {
84  currentSubAction = i;
85  return true;
86  }
87  }
88  return false;
89  }
90 
91  public void Reset()
92  {
93  Actions.ForEach(a => a.Reset());
94  currentSubAction = 0;
95  }
96 
97  public void Update(float deltaTime)
98  {
99  if (currentSubAction < Actions.Count)
100  {
101  Actions[currentSubAction].Update(deltaTime);
102  }
103  }
104  }
105 
106  public readonly ScriptedEvent ParentEvent;
107 
108  public EventAction(ScriptedEvent parentEvent, ContentXElement element)
109  {
110  ParentEvent = parentEvent;
112  }
113 
119  public abstract bool IsFinished(ref string goToLabel);
120 
121  public virtual bool SetGoToTarget(string goTo)
122  {
123  return false;
124  }
125 
126  public abstract void Reset();
127 
128  public virtual bool CanBeFinished()
129  {
130  return true;
131  }
132 
133  public virtual IEnumerable<EventAction> GetSubActions()
134  {
135  return Enumerable.Empty<EventAction>();
136  }
137 
138  public virtual void Update(float deltaTime) { }
139 
140  public static EventAction Instantiate(ScriptedEvent scriptedEvent, ContentXElement element)
141  {
142  Type actionType;
143  try
144  {
145  Identifier typeName = element.Name.ToString().ToIdentifier();
146  if (typeName == "TutorialSegmentAction")
147  {
148  typeName = nameof(EventObjectiveAction).ToIdentifier();
149  }
150  else if (typeName == "TutorialHighlightAction")
151  {
152  typeName = nameof(HighlightAction).ToIdentifier();
153  }
154  actionType = Type.GetType("Barotrauma." + typeName, throwOnError: true, ignoreCase: true);
155  if (actionType == null) { throw new NullReferenceException(); }
156  }
157  catch
158  {
159  DebugConsole.ThrowError($"Could not find an {nameof(EventAction)} class of the type \"{element.Name}\".",
160  contentPackage: element.ContentPackage);
161  return null;
162  }
163 
164  ConstructorInfo constructor = actionType.GetConstructor(new[] { typeof(ScriptedEvent), typeof(ContentXElement) });
165  try
166  {
167  if (constructor == null)
168  {
169  throw new Exception($"Error in scripted event \"{scriptedEvent.Prefab.Identifier}\" - could not find a constructor for the EventAction \"{actionType}\".");
170  }
171  return constructor.Invoke(new object[] { scriptedEvent, element }) as EventAction;
172  }
173  catch (Exception ex)
174  {
175  DebugConsole.ThrowError(ex.InnerException != null ? ex.InnerException.ToString() : ex.ToString(),
176  contentPackage: element.ContentPackage);
177  return null;
178  }
179  }
180 
181  protected void ApplyTagsToHulls(Entity entity, Identifier hullTag, Identifier linkedHullTag)
182  {
183  var currentHull = entity switch
184  {
185  Item item => item.CurrentHull,
186  Character character => character.CurrentHull,
187  _ => null,
188  };
189  if (currentHull == null) { return; }
190 
191  if (!hullTag.IsEmpty)
192  {
193  ParentEvent.AddTarget(hullTag, currentHull);
194  }
195  if (!linkedHullTag.IsEmpty)
196  {
197  ParentEvent.AddTarget(linkedHullTag, currentHull);
198  foreach (var linkedHull in currentHull.GetLinkedEntities<Hull>())
199  {
200  ParentEvent.AddTarget(linkedHullTag, linkedHull);
201  }
202  }
203  }
204 
205  protected string GetEventDebugName()
206  {
207  return ParentEvent?.Prefab?.Identifier is { IsEmpty: false } identifier ? $"the event \"{identifier}\"" : "an unknown event";
208  }
209 
210 
223  public virtual string ToDebugString()
224  {
225  return $"[?] {GetType().Name}";
226  }
227  }
228 }
ContentPackage? ContentPackage
bool GetAttributeBool(string key, bool def)
XAttribute? GetAttribute(string name)
SubactionGroup(ScriptedEvent scriptedEvent, ContentXElement elem)
Definition: EventAction.cs:34
bool EndConversation
Should this option end the conversation (closing the conversation prompt?). By default,...
Definition: EventAction.cs:18
bool IsFinished(ref string goTo)
Definition: EventAction.cs:52
virtual bool CanBeFinished()
Definition: EventAction.cs:128
virtual IEnumerable< EventAction > GetSubActions()
Definition: EventAction.cs:133
readonly ScriptedEvent ParentEvent
Definition: EventAction.cs:106
abstract void Reset()
EventAction(ScriptedEvent parentEvent, ContentXElement element)
Definition: EventAction.cs:108
virtual bool SetGoToTarget(string goTo)
Definition: EventAction.cs:121
static EventAction Instantiate(ScriptedEvent scriptedEvent, ContentXElement element)
Definition: EventAction.cs:140
abstract bool IsFinished(ref string goToLabel)
Has the action finished.
void ApplyTagsToHulls(Entity entity, Identifier hullTag, Identifier linkedHullTag)
Definition: EventAction.cs:181
virtual void Update(float deltaTime)
Definition: EventAction.cs:138
virtual string ToDebugString()
Rich test to display in debugdraw
Definition: EventAction.cs:223
EventPrefab Prefab
Definition: Event.cs:16
Displays an objective in the top-right corner of the screen, or modifies an existing objective in som...
readonly Identifier Identifier
Definition: Prefab.cs:34
void AddTarget(Identifier tag, Entity target)
static Dictionary< Identifier, SerializableProperty > DeserializeProperties(object obj, XElement element=null)