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;
14  public bool EndConversation;
15 
16  private int currentSubAction = 0;
17 
19  {
20  get
21  {
22  if (currentSubAction >= 0 && Actions.Count > currentSubAction)
23  {
24  return Actions[currentSubAction];
25  }
26  return null;
27  }
28  }
29 
30  public SubactionGroup(ScriptedEvent scriptedEvent, ContentXElement elem)
31  {
32  Text = elem.GetAttribute("text")?.Value ?? "";
33  Actions = new List<EventAction>();
34  EndConversation = elem.GetAttributeBool("endconversation", false);
35  foreach (var e in elem.Elements())
36  {
37  if (e.Name.ToString().Equals("statuseffect", StringComparison.OrdinalIgnoreCase))
38  {
39  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.",
40  contentPackage: elem.ContentPackage);
41  continue;
42  }
43  var action = Instantiate(scriptedEvent, e);
44  if (action != null) { Actions.Add(action); }
45  }
46  }
47 
48  public bool IsFinished(ref string goTo)
49  {
50  if (currentSubAction < Actions.Count)
51  {
52  string innerGoTo = null;
53  if (Actions[currentSubAction].IsFinished(ref innerGoTo))
54  {
55  if (string.IsNullOrEmpty(innerGoTo))
56  {
57  currentSubAction++;
58  }
59  else
60  {
61  goTo = innerGoTo;
62  return true;
63  }
64  }
65  }
66  if (currentSubAction >= Actions.Count)
67  {
68  return true;
69  }
70  return false;
71  }
72 
73  public bool SetGoToTarget(string goTo)
74  {
75  currentSubAction = 0;
76  for (int i = 0; i < Actions.Count; i++)
77  {
78  if (Actions[i].SetGoToTarget(goTo))
79  {
80  currentSubAction = i;
81  return true;
82  }
83  }
84  return false;
85  }
86 
87  public void Reset()
88  {
89  Actions.ForEach(a => a.Reset());
90  currentSubAction = 0;
91  }
92 
93  public void Update(float deltaTime)
94  {
95  if (currentSubAction < Actions.Count)
96  {
97  Actions[currentSubAction].Update(deltaTime);
98  }
99  }
100  }
101 
102  public readonly ScriptedEvent ParentEvent;
103 
104  public EventAction(ScriptedEvent parentEvent, ContentXElement element)
105  {
106  ParentEvent = parentEvent;
108  }
109 
115  public abstract bool IsFinished(ref string goToLabel);
116 
117  public virtual bool SetGoToTarget(string goTo)
118  {
119  return false;
120  }
121 
122  public abstract void Reset();
123 
124  public virtual bool CanBeFinished()
125  {
126  return true;
127  }
128 
129  public virtual IEnumerable<EventAction> GetSubActions()
130  {
131  return Enumerable.Empty<EventAction>();
132  }
133 
134  public virtual void Update(float deltaTime) { }
135 
136  public static EventAction Instantiate(ScriptedEvent scriptedEvent, ContentXElement element)
137  {
138  Type actionType;
139  try
140  {
141  Identifier typeName = element.Name.ToString().ToIdentifier();
142  if (typeName == "TutorialSegmentAction")
143  {
144  typeName = nameof(EventObjectiveAction).ToIdentifier();
145  }
146  else if (typeName == "TutorialHighlightAction")
147  {
148  typeName = nameof(HighlightAction).ToIdentifier();
149  }
150  actionType = Type.GetType("Barotrauma." + typeName, throwOnError: true, ignoreCase: true);
151  if (actionType == null) { throw new NullReferenceException(); }
152  }
153  catch
154  {
155  DebugConsole.ThrowError($"Could not find an {nameof(EventAction)} class of the type \"{element.Name}\".",
156  contentPackage: element.ContentPackage);
157  return null;
158  }
159 
160  ConstructorInfo constructor = actionType.GetConstructor(new[] { typeof(ScriptedEvent), typeof(ContentXElement) });
161  try
162  {
163  if (constructor == null)
164  {
165  throw new Exception($"Error in scripted event \"{scriptedEvent.Prefab.Identifier}\" - could not find a constructor for the EventAction \"{actionType}\".");
166  }
167  return constructor.Invoke(new object[] { scriptedEvent, element }) as EventAction;
168  }
169  catch (Exception ex)
170  {
171  DebugConsole.ThrowError(ex.InnerException != null ? ex.InnerException.ToString() : ex.ToString(),
172  contentPackage: element.ContentPackage);
173  return null;
174  }
175  }
176 
177  protected void ApplyTagsToHulls(Entity entity, Identifier hullTag, Identifier linkedHullTag)
178  {
179  var currentHull = entity switch
180  {
181  Item item => item.CurrentHull,
182  Character character => character.CurrentHull,
183  _ => null,
184  };
185  if (currentHull == null) { return; }
186 
187  if (!hullTag.IsEmpty)
188  {
189  ParentEvent.AddTarget(hullTag, currentHull);
190  }
191  if (!linkedHullTag.IsEmpty)
192  {
193  ParentEvent.AddTarget(linkedHullTag, currentHull);
194  foreach (var linkedHull in currentHull.GetLinkedEntities<Hull>())
195  {
196  ParentEvent.AddTarget(linkedHullTag, linkedHull);
197  }
198  }
199  }
200 
201  protected string GetEventDebugName()
202  {
203  return ParentEvent?.Prefab?.Identifier is { IsEmpty: false } identifier ? $"the event \"{identifier}\"" : "an unknown event";
204  }
205 
206 
219  public virtual string ToDebugString()
220  {
221  return $"[?] {GetType().Name}";
222  }
223  }
224 }
ContentPackage? ContentPackage
bool GetAttributeBool(string key, bool def)
XAttribute? GetAttribute(string name)
SubactionGroup(ScriptedEvent scriptedEvent, ContentXElement elem)
Definition: EventAction.cs:30
bool IsFinished(ref string goTo)
Definition: EventAction.cs:48
virtual bool CanBeFinished()
Definition: EventAction.cs:124
virtual IEnumerable< EventAction > GetSubActions()
Definition: EventAction.cs:129
readonly ScriptedEvent ParentEvent
Definition: EventAction.cs:102
abstract void Reset()
EventAction(ScriptedEvent parentEvent, ContentXElement element)
Definition: EventAction.cs:104
virtual bool SetGoToTarget(string goTo)
Definition: EventAction.cs:117
static EventAction Instantiate(ScriptedEvent scriptedEvent, ContentXElement element)
Definition: EventAction.cs:136
abstract bool IsFinished(ref string goToLabel)
Has the action finished.
void ApplyTagsToHulls(Entity entity, Identifier hullTag, Identifier linkedHullTag)
Definition: EventAction.cs:177
virtual void Update(float deltaTime)
Definition: EventAction.cs:134
virtual string ToDebugString()
Rich test to display in debugdraw
Definition: EventAction.cs:219
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)