Client LuaCsForBarotrauma
CharacterAbilityGiveExperience.cs
1 namespace Barotrauma.Abilities;
2 
3 internal sealed class CharacterAbilityGiveExperience : CharacterAbility
4 {
5  public override bool AppliesEffectOnIntervalUpdate => true;
6 
7  private readonly int amount;
8  private readonly int level;
9 
10  public CharacterAbilityGiveExperience(CharacterAbilityGroup characterAbilityGroup, ContentXElement abilityElement) : base(characterAbilityGroup, abilityElement)
11  {
12  amount = abilityElement.GetAttributeInt("amount", 0);
13  level = abilityElement.GetAttributeInt("level", 0);
14 
15  if (amount == 0 && level == 0)
16  {
17  DebugConsole.ThrowError($"Error in talent {CharacterTalent.DebugIdentifier} - no exp amount or level defined in {nameof(CharacterAbilityGiveExperience)}.",
18  contentPackage: abilityElement.ContentPackage);
19  }
20  if (amount > 0 && level > 0)
21  {
22  DebugConsole.ThrowError($"Error in talent {CharacterTalent.DebugIdentifier} - {nameof(CharacterAbilityGiveExperience)} defines both an exp amount and a level.",
23  contentPackage: abilityElement.ContentPackage);
24  }
25  }
26 
27  private void ApplyEffectSpecific(Character targetCharacter)
28  {
29  if (level > 0)
30  {
31  targetCharacter.Info?.GiveExperience(targetCharacter.Info.GetExperienceRequiredForLevel(level) + amount);
32  }
33  else if (amount != 0)
34  {
35  targetCharacter.Info?.GiveExperience(amount);
36  }
37  }
38 
39  protected override void ApplyEffect(AbilityObject abilityObject)
40  {
41  if (abilityObject is AbilityCharacterKill { Killer: { } killer })
42  {
43  ApplyEffectSpecific(killer);
44  }
45  else if ((abilityObject as IAbilityCharacter)?.Character is { } targetCharacter)
46  {
47  ApplyEffectSpecific(targetCharacter);
48  }
49  else
50  {
51  ApplyEffectSpecific(Character);
52  }
53  }
54 
55  protected override void ApplyEffect()
56  {
57  ApplyEffectSpecific(Character);
58  }
59 }