Client LuaCsForBarotrauma
CharacterAbilityApplyForce.cs
1 using Microsoft.Xna.Framework;
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 
6 namespace Barotrauma.Abilities
7 {
9  {
10  private readonly float force;
11  private readonly float maxVelocity;
12 
13  private readonly string afflictionIdentifier;
14 
15  private readonly HashSet<LimbType> limbTypes = new HashSet<LimbType>();
16 
17  public override bool AppliesEffectOnIntervalUpdate => true;
18  public CharacterAbilityApplyForce(CharacterAbilityGroup characterAbilityGroup, ContentXElement abilityElement) : base(characterAbilityGroup, abilityElement)
19  {
20  force = abilityElement.GetAttributeFloat("force", 0f);
21  maxVelocity = abilityElement.GetAttributeFloat("maxvelocity", 10f);
22  afflictionIdentifier = abilityElement.GetAttributeString("afflictionidentifier", "");
23 
24  string[] limbTypesStr = abilityElement.GetAttributeStringArray("limbtypes", new string[0]);
25  foreach (string limbTypeStr in limbTypesStr)
26  {
27  if (Enum.TryParse(limbTypeStr, out LimbType limbType))
28  {
29  limbTypes.Add(limbType);
30  }
31  else
32  {
33  DebugConsole.ThrowError($"Error in talent \"{characterAbilityGroup.CharacterTalent.DebugIdentifier}\" - \"{limbTypeStr}\" is not a valid limb type.",
34  contentPackage: abilityElement.ContentPackage);
35  }
36  }
37  }
38 
39  protected override void ApplyEffect()
40  {
41  float strength = 1.0f;
42  if (!string.IsNullOrEmpty(afflictionIdentifier))
43  {
44  Affliction affliction = Character.CharacterHealth.GetAffliction(afflictionIdentifier);
45  if (affliction == null) { return; }
46  strength = affliction.Strength / affliction.Prefab.MaxStrength;
47  }
48 
49  foreach (Limb limb in Character.AnimController.Limbs)
50  {
51  if (limb.IsSevered || limb.Removed) { continue; }
52  if (limbTypes.Any())
53  {
54  if (!limbTypes.Contains(limb.type)) { continue; }
55  }
56  if (Character.AnimController.TargetMovement.LengthSquared() < 0.001f) { continue; }
57  limb.body.ApplyForce(Vector2.Normalize(limb.Mass * Character.AnimController.TargetMovement) * force * strength, maxVelocity);
58  }
59  }
60  }
61 }
CharacterAbilityApplyForce(CharacterAbilityGroup characterAbilityGroup, ContentXElement abilityElement)
virtual float Strength
Definition: Affliction.cs:31
readonly AfflictionPrefab Prefab
Definition: Affliction.cs:12
readonly float MaxStrength
The maximum strength this affliction can have.
Affliction GetAffliction(string identifier, bool allowLimbAfflictions=true)
string? GetAttributeString(string key, string? def)
float GetAttributeFloat(string key, float def)
string?[] GetAttributeStringArray(string key, string[]? def, bool convertToLowerInvariant=false)
void ApplyForce(Vector2 force, float maxVelocity=NetConfig.MaxPhysicsBodyVelocity)