Client LuaCsForBarotrauma
Propulsion.cs
1 using System.Xml.Linq;
2 using Microsoft.Xna.Framework;
4 #if CLIENT
5 using Microsoft.Xna.Framework.Graphics;
7 #endif
8 
10 {
12  {
13  public enum UseEnvironment
14  {
15  Air, Water, Both, None
16  };
17 
18  private float useState;
19 
20  [Serialize(UseEnvironment.Both, IsPropertySaveable.No, description: "Can the item be used in air, underwater or both.")]
21  public UseEnvironment UsableIn { get; set; }
22 
23  [Serialize(0.0f, IsPropertySaveable.No, description: "The force to apply to the user's body."), Editable(MinValueFloat = -1000.0f, MaxValueFloat = 1000.0f)]
24  public float Force { get; set; }
25 
26  [Serialize(true, IsPropertySaveable.No, description: "If the item is held in RightHand or LeftHand, apply extra force there")]
27  public bool ApplyToHands { get; set; }
28 #if CLIENT
29  private string particles;
30  [Serialize("", IsPropertySaveable.No, description: "The name of the particle prefab the item emits when used.")]
31  public string Particles
32  {
33  get { return particles; }
34  set { particles = value; }
35  }
36 #endif
37 
39  : base(item,element)
40  {
41  }
42 
43  public override bool Use(float deltaTime, Character character = null)
44  {
45  if (character == null || character.Removed) { return false; }
46  if (!character.IsKeyDown(InputType.Aim) || character.Stun > 0.0f) { return false; }
47  if (UsableIn == UseEnvironment.None) { return false; }
48 
49  IsActive = true;
50  useState = 0.1f;
51 
52  if (character.AnimController.InWater)
53  {
54  if (UsableIn == UseEnvironment.Air) { return true; }
55  }
56  else
57  {
58  if (UsableIn == UseEnvironment.Water) { return true; }
59  }
60 
61  Vector2 dir = character.CursorPosition - character.Position;
62  if (!MathUtils.IsValid(dir)) { return true; }
63  float length = 200;
64  dir = dir.ClampLength(length) / length;
65  Vector2 propulsion = dir * Force * character.PropulsionSpeedMultiplier * (1.0f + character.GetStatValue(StatTypes.PropulsionSpeed));
66  if (character.AnimController.InWater && Force > 0.0f) { character.AnimController.TargetMovement = dir; }
67 
68  foreach (Limb limb in character.AnimController.Limbs)
69  {
70  if (limb.WearingItems.Find(w => w.WearableComponent.Item == item) == null) { continue; }
71  limb.body.ApplyForce(propulsion);
72  }
73 
74  character.AnimController.Collider.ApplyForce(propulsion);
75 
76  if (ApplyToHands)
77  {
78  if (character.Inventory.IsInLimbSlot(item, InvSlotType.RightHand))
79  {
80  character.AnimController.GetLimb(LimbType.RightHand)?.body.ApplyForce(propulsion);
81  }
82  if (character.Inventory.IsInLimbSlot(item, InvSlotType.LeftHand))
83  {
84  character.AnimController.GetLimb(LimbType.LeftHand)?.body.ApplyForce(propulsion);
85  }
86  }
87 
88 #if CLIENT
89  if (!string.IsNullOrWhiteSpace(particles))
90  {
92  item.body.Rotation + ((item.body.Dir > 0.0f) ? 0.0f : MathHelper.Pi), 0.0f, item.CurrentHull);
93  }
94 #endif
95 
96  return true;
97  }
98 
99  public override void Update(float deltaTime, Camera cam)
100  {
101  useState -= deltaTime;
102 
103  if (useState <= 0.0f)
104  {
105  IsActive = false;
106  }
107 
108  if (item.AiTarget != null && IsActive)
109  {
111  }
112  }
113  }
114 }
AITarget AiTarget
Definition: Entity.cs:55
static ParticleManager ParticleManager
Definition: GameMain.cs:101
The base class for components holding the different functionalities of the item
override void Update(float deltaTime, Camera cam)
Definition: Propulsion.cs:99
override bool Use(float deltaTime, Character character=null)
Definition: Propulsion.cs:43
Propulsion(Item item, ContentXElement element)
Definition: Propulsion.cs:38
readonly List< WearableSprite > WearingItems
Particle CreateParticle(string prefabName, Vector2 position, float angle, float speed, Hull hullGuess=null, float collisionIgnoreTimer=0f, Tuple< Vector2, Vector2 > tracerPoints=null)
void ApplyForce(Vector2 force, float maxVelocity=NetConfig.MaxPhysicsBodyVelocity)
StatTypes
StatTypes are used to alter several traits of a character. They are mostly used by talents.
Definition: Enums.cs:195