Client LuaCsForBarotrauma
AfflictionAction.cs
1 using System.Linq;
2 
3 namespace Barotrauma
4 {
9  {
10  [Serialize("", IsPropertySaveable.Yes, description: "Identifier of the affliction.")]
11  public Identifier Affliction { get; set; }
12 
13  [Serialize(0.0f, IsPropertySaveable.Yes, description: "Strength of the affliction.")]
14  public float Strength { get; set; }
15 
16  [Serialize(LimbType.None, IsPropertySaveable.Yes, description: "Type of the limb(s) to apply the affliction on. Only valid if the affliction is limb-specific.")]
17  public LimbType LimbType { get; set; }
18 
19  [Serialize("", IsPropertySaveable.Yes, description: "Tag of the character to apply the affliction on.")]
20  public Identifier TargetTag { get; set; }
21 
22  [Serialize(false, IsPropertySaveable.Yes, description: "Should the strength be multiplied by the maximum vitality of the target?")]
23  public bool MultiplyByMaxVitality { get; set; }
24 
25  public AfflictionAction(ScriptedEvent parentEvent, ContentXElement element) : base(parentEvent, element)
26  {
27  if (Affliction.IsEmpty)
28  {
29  DebugConsole.ThrowError($"Error in {nameof(AfflictionAction)}: affliction not defined (use the attribute \"{nameof(Affliction)}\").",
30  contentPackage: element.ContentPackage);
31  }
32  }
33 
34  private bool isFinished = false;
35 
36  public override bool IsFinished(ref string goTo)
37  {
38  return isFinished;
39  }
40 
41  public override void Reset()
42  {
43  isFinished = false;
44  }
45 
46  public override void Update(float deltaTime)
47  {
48  if (isFinished) { return; }
49  if (AfflictionPrefab.Prefabs.TryGet(Affliction, out var afflictionPrefab))
50  {
51  var targets = ParentEvent.GetTargets(TargetTag);
52  foreach (var target in targets)
53  {
54  if (target != null && target is Character character)
55  {
56  float strength = Strength;
58  {
59  strength *= character.MaxVitality;
60  }
61  if (LimbType != LimbType.None)
62  {
63  var limb = character.AnimController.GetLimb(LimbType);
64  if (strength > 0.0f)
65  {
66  character.CharacterHealth.ApplyAffliction(limb, afflictionPrefab.Instantiate(strength), ignoreUnkillability: true);
67  }
68  else if (strength < 0.0f)
69  {
70  character.CharacterHealth.ReduceAfflictionOnLimb(limb, Affliction, -strength);
71  }
72  }
73  else
74  {
75  if (strength > 0.0f)
76  {
77  character.CharacterHealth.ApplyAffliction(null, afflictionPrefab.Instantiate(strength), ignoreUnkillability: true);
78  }
79  else if (strength < 0.0f)
80  {
81  character.CharacterHealth.ReduceAfflictionOnAllLimbs(Affliction, -strength);
82  }
83  }
84  }
85  }
86  }
87  isFinished = true;
88  }
89 
90  public override string ToDebugString()
91  {
92  return $"{ToolBox.GetDebugSymbol(isFinished)} {nameof(AfflictionAction)} -> (TargetTag: {TargetTag.ColorizeObject()}, " +
93  $"Affliction: {Affliction.ColorizeObject()}, Strength: {Strength.ColorizeObject()}, " +
94  $"LimbType: {LimbType.ColorizeObject()})";
95  }
96  }
97 }
Gives an affliction to a specific character.
override void Update(float deltaTime)
override bool IsFinished(ref string goTo)
Has the action finished.
AfflictionAction(ScriptedEvent parentEvent, ContentXElement element)
override string ToDebugString()
Rich test to display in debugdraw
AfflictionPrefab is a prefab that defines a type of affliction that can be applied to a character....
static readonly PrefabCollection< AfflictionPrefab > Prefabs
ContentPackage? ContentPackage
readonly ScriptedEvent ParentEvent
Definition: EventAction.cs:102
IEnumerable< Entity > GetTargets(Identifier tag)