Client LuaCsForBarotrauma
PositionalDeformation.cs
1 using Microsoft.Xna.Framework;
2 using System;
3 using System.Xml.Linq;
4 
6 {
8  {
13  [Serialize(0.0f, IsPropertySaveable.Yes, description: "0 = no falloff, the entire sprite is stretched, 1 = stretching the center of the sprite has no effect at the edges."), Editable(MinValueFloat = 0.0f, MaxValueFloat = 1.0f)]
14  public float Falloff { get; set; }
15 
19  [Serialize(1.0f, IsPropertySaveable.Yes, description: "Maximum stretch per vertex (1 = the size of the sprite)"), Editable(MinValueFloat = 0.0f, MaxValueFloat = 10.0f)]
20  public float MaxDeformation { get; set; }
21 
22 
26  [Serialize(10.0f, IsPropertySaveable.Yes, description: "How fast the sprite reacts to being stretched"), Editable(MinValueFloat = 0.0f, MaxValueFloat = 10.0f)]
27  public float ReactionSpeed { get; set; }
28 
32  [Serialize(0.05f, IsPropertySaveable.Yes, description: "How fast the sprite returns back to normal after stretching ends"), Editable(MinValueFloat = 0.0f, MaxValueFloat = 10.0f)]
33  public float RecoverSpeed { get; set; }
34 
35  public PositionalDeformationParams(XElement element) : base(element)
36  {
37  }
38  }
43  {
44  public enum ReactionType
45  {
46  ReactToTriggerers
47  }
48 
50 
51  private PositionalDeformationParams positionalDeformationParams => Params as PositionalDeformationParams;
52 
53  public PositionalDeformation(XElement element) : base(element, new PositionalDeformationParams(element))
54  {
55  }
56 
57  public override void Update(float deltaTime)
58  {
59  if (positionalDeformationParams.RecoverSpeed <= 0.0f) { return; }
60 
61  for (int x = 0; x < Resolution.X; x++)
62  {
63  for (int y = 0; y < Resolution.Y; y++)
64  {
65  if (Deformation[x,y].LengthSquared() < 0.000001f)
66  {
67  Deformation[x, y] = Vector2.Zero;
68  continue;
69  }
70 
71  Vector2 reduction = Deformation[x, y];
72  Deformation[x, y] -= reduction.ClampLength(positionalDeformationParams.RecoverSpeed) * deltaTime;
73  }
74  }
75  }
76 
77  public void Deform(Vector2 worldPosition, Vector2 amount, float deltaTime, Matrix transformMatrix)
78  {
79  Vector2 pos = Vector2.Transform(worldPosition, transformMatrix);
80  Point deformIndex = new Point((int)(pos.X * (Resolution.X - 1)), (int)(pos.Y * (Resolution.Y - 1)));
81 
82  if (deformIndex.X < 0 || deformIndex.Y < 0) { return; }
83  if (deformIndex.X >= Resolution.X || deformIndex.Y >= Resolution.Y) { return; }
84 
85  amount = amount.ClampLength(positionalDeformationParams.MaxDeformation);
86 
87  float invFalloff = 1.0f - positionalDeformationParams.Falloff;
88 
89  for (int x = 0; x < Resolution.X; x++)
90  {
91  float normalizedDiffX = Math.Abs(x - deformIndex.X) / (Resolution.X * 0.5f);
92  for (int y = 0; y < Resolution.Y; y++)
93  {
94  float normalizedDiffY = Math.Abs(y - deformIndex.Y) / (Resolution.Y * 0.5f);
95  Vector2 targetDeformation = amount * MathHelper.Clamp(1.0f - new Vector2(normalizedDiffX, normalizedDiffY).Length() * positionalDeformationParams.Falloff, 0.0f, 1.0f);
96 
97  Vector2 diff = targetDeformation - Deformation[x, y];
98  Deformation[x, y] += diff.ClampLength(positionalDeformationParams.ReactionSpeed) * deltaTime;
99  }
100  }
101  }
102 
103  protected override void GetDeformation(out Vector2[,] deformation, out float multiplier, bool inverse)
104  {
105  deformation = Deformation;
106  multiplier = 1.0f;
107  }
108  }
109 }
Stretch a position in the deformable sprite to some direction
override void GetDeformation(out Vector2[,] deformation, out float multiplier, bool inverse)
void Deform(Vector2 worldPosition, Vector2 amount, float deltaTime, Matrix transformMatrix)
float ReactionSpeed
How fast the sprite reacts to being stretched
float RecoverSpeed
How fast the sprite returns back to normal after stretching ends
float MaxDeformation
Maximum stretch per vertex (1 = the size of the sprite)
float Falloff
0 = no falloff, the entire sprite is stretched 1 = stretching the center of the sprite has no effect ...