Client LuaCsForBarotrauma
StatusEffectAction.cs
1 using System.Collections.Generic;
2 
3 namespace Barotrauma
4 {
9  {
10  private readonly List<StatusEffect> effects = new List<StatusEffect>();
11 
12  private readonly int actionIndex;
13 
14  [Serialize("", IsPropertySaveable.Yes, description: "Tag of the entity or entities the status effect should target.")]
15  public Identifier TargetTag { get; set; }
16 
17  public StatusEffectAction(ScriptedEvent parentEvent, ContentXElement element) : base(parentEvent, element)
18  {
19  actionIndex = 0;
20  foreach (var subElement in parentEvent.Prefab.ConfigElement.Descendants())
21  {
22  if (subElement == element) { break; }
23  actionIndex++;
24  }
25 
26  foreach (var subElement in element.Elements())
27  {
28  switch (subElement.Name.ToString().ToLowerInvariant())
29  {
30  case "statuseffect":
31  effects.Add(StatusEffect.Load(subElement, $"{nameof(StatusEffectAction)} ({parentEvent.Prefab.Identifier})"));
32  break;
33  }
34  }
35  }
36 
37  private bool isFinished = false;
38 
39  public override bool IsFinished(ref string goTo)
40  {
41  return isFinished;
42  }
43  public override void Reset()
44  {
45  isFinished = false;
46  }
47 
48  public override void Update(float deltaTime)
49  {
50  if (isFinished) { return; }
51  var eventTargets = ParentEvent.GetTargets(TargetTag);
52  foreach (StatusEffect effect in effects)
53  {
54  foreach (var target in eventTargets)
55  {
56  if (effect.HasTargetType(StatusEffect.TargetType.NearbyCharacters))
57  {
58  List<ISerializableEntity> nearbyTargets = new List<ISerializableEntity>();
59  effect.AddNearbyTargets(target.WorldPosition, nearbyTargets);
60  foreach (var nearbyTarget in nearbyTargets)
61  {
62  ApplyOnTarget(nearbyTarget as Entity, effect);
63  }
64  continue;
65  }
66  ApplyOnTarget(target, effect);
67  }
68  }
69 #if SERVER
70  ServerWrite(eventTargets);
71 #endif
72  isFinished = true;
73 
74  void ApplyOnTarget(Entity target, StatusEffect effect)
75  {
76  if (target is Item targetItem)
77  {
78  effect.Apply(effect.type, deltaTime, target, targetItem.AllPropertyObjects);
79  }
80  else
81  {
82  effect.Apply(effect.type, deltaTime, target, target as ISerializableEntity);
83  }
84  }
85  }
86 
87  public override string ToDebugString()
88  {
89  return $"{ToolBox.GetDebugSymbol(isFinished)} {nameof(StatusEffectAction)} -> (TargetTag: {TargetTag.ColorizeObject()}";
90  }
91  }
92 }
readonly ScriptedEvent ParentEvent
Definition: EventAction.cs:106
IEnumerable< Entity > GetTargets(Identifier tag)
Executes all the StatusEffects defined as child elements of the action.
override string ToDebugString()
Rich test to display in debugdraw
override void Update(float deltaTime)
StatusEffectAction(ScriptedEvent parentEvent, ContentXElement element)
override bool IsFinished(ref string goTo)
Has the action finished.
StatusEffects can be used to execute various kinds of effects: modifying the state of some entity in ...
static StatusEffect Load(ContentXElement element, string parentDebugName)
void AddNearbyTargets(Vector2 worldPosition, List< ISerializableEntity > targets)
virtual void Apply(ActionType type, float deltaTime, Entity entity, ISerializableEntity target, Vector2? worldPosition=null)