Client LuaCsForBarotrauma
CustomDeformation.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Xml.Linq;
5 using Microsoft.Xna.Framework;
6 
8 {
10  {
11  [Editable(MinValueFloat = 0.0f, MaxValueFloat = 10.0f),
12  Serialize(0.0f, IsPropertySaveable.Yes, description: "How fast the deformation \"oscillates\" back and forth. " +
13  "For example, if the sprite is stretched up, setting this value above zero would make it do a wave-like movement up and down.")]
14  public override float Frequency { get; set; } = 1;
15 
16  [Serialize(1.0f, IsPropertySaveable.Yes, description: "The \"strength\" of the deformation."), Editable(MinValueFloat = 0.0f, MaxValueFloat = 10.0f)]
17  public float Amplitude { get; set; }
18 
19  public CustomDeformationParams(XElement element) : base(element)
20  {
21  }
22  }
23 
25  {
26  private List<Vector2[]> deformRows = new List<Vector2[]>();
27 
29 
30  public override float Phase
31  {
32  get { return phase; }
33  set
34  {
35  phase = value;
36  //phase %= MathHelper.TwoPi;
37  }
38  }
39  private float phase;
40 
41  public CustomDeformation(XElement element) : base(element, new CustomDeformationParams(element))
42  {
43  phase = Rand.Range(0.0f, MathHelper.TwoPi);
44 
45  if (element == null)
46  {
47  deformRows.Add(new Vector2[] { Vector2.Zero, Vector2.Zero });
48  deformRows.Add(new Vector2[] { Vector2.Zero, Vector2.Zero });
49  }
50  else
51  {
52  for (int i = 0; ; i++)
53  {
54  string row = element.GetAttributeString("row" + i, "");
55  if (string.IsNullOrWhiteSpace(row)) break;
56 
57  string[] splitRow = row.Split(' ');
58  Vector2[] rowVectors = new Vector2[splitRow.Length];
59  for (int j = 0; j < splitRow.Length; j++)
60  {
61  rowVectors[j] = XMLExtensions.ParseVector2(splitRow[j]);
62  }
63  deformRows.Add(rowVectors);
64  }
65  }
66 
67  if (deformRows.Count() == 0 || deformRows.First() == null || deformRows.First().Length == 0)
68  {
69  return;
70  }
71 
72  var configDeformation = new Vector2[deformRows.First().Length, deformRows.Count];
73  for (int x = 0; x < configDeformation.GetLength(0); x++)
74  {
75  for (int y = 0; y < configDeformation.GetLength(1); y++)
76  {
77  configDeformation[x, y] = deformRows[y][x];
78  }
79  }
80 
81  //construct an array for the desired resolution,
82  //interpolating values if the resolution configured in the xml is smaller
83  //deformation = new Vector2[Resolution.X, Resolution.Y];
84  float divX = 1.0f / Resolution.X, divY = 1.0f / Resolution.Y;
85  for (int x = 0; x < Resolution.X; x++)
86  {
87  float normalizedX = x / (float)(Resolution.X - 1);
88  for (int y = 0; y < Resolution.Y; y++)
89  {
90  float normalizedY = y / (float)(Resolution.Y - 1);
91 
92  Point indexTopLeft = new Point(
93  Math.Min((int)Math.Floor(normalizedX * (configDeformation.GetLength(0) - 1)), configDeformation.GetLength(0) - 1),
94  Math.Min((int)Math.Floor(normalizedY * (configDeformation.GetLength(1) - 1)), configDeformation.GetLength(1) - 1));
95  Point indexBottomRight = new Point(
96  Math.Min(indexTopLeft.X + 1, configDeformation.GetLength(0) - 1),
97  Math.Min(indexTopLeft.Y + 1, configDeformation.GetLength(1) - 1));
98 
99  Vector2 deformTopLeft = configDeformation[indexTopLeft.X, indexTopLeft.Y];
100  Vector2 deformTopRight = configDeformation[indexBottomRight.X, indexTopLeft.Y];
101  Vector2 deformBottomLeft = configDeformation[indexTopLeft.X, indexBottomRight.Y];
102  Vector2 deformBottomRight = configDeformation[indexBottomRight.X, indexBottomRight.Y];
103 
104  Deformation[x, y] = Vector2.Lerp(
105  Vector2.Lerp(deformTopLeft, deformTopRight, (normalizedX % divX) / divX),
106  Vector2.Lerp(deformBottomLeft, deformBottomRight, (normalizedX % divX) / divX),
107  (normalizedY % divY) / divY);
108  }
109  }
110  }
111 
112  protected override void GetDeformation(out Vector2[,] deformation, out float multiplier, bool inverse)
113  {
114  deformation = Deformation;
115  multiplier = CustomDeformationParams.Frequency <= 0.0f ?
117  (float)Math.Sin(inverse ? -phase : phase) * CustomDeformationParams.Amplitude;
118  multiplier *= Params.Strength;
119  }
120 
121  public override void Update(float deltaTime)
122  {
123  if (!Params.UseMovementSine)
124  {
125  phase += deltaTime * CustomDeformationParams.Frequency;
126  phase %= MathHelper.TwoPi;
127  }
128  }
129 
130  public override void Save(XElement element)
131  {
132  base.Save(element);
133  for (int i = 0; i < deformRows.Count; i++)
134  {
135  element.Add(new XAttribute("row" + i, string.Join(" ", deformRows[i].Select(r => XMLExtensions.Vector2ToString(r)))));
136  }
137  }
138  }
139 }
override void GetDeformation(out Vector2[,] deformation, out float multiplier, bool inverse)