1 using Microsoft.Xna.Framework;
3 using System.Collections.Generic;
38 public string Name => $
"Deformation ({Type})";
74 private Point _resolution;
78 get {
return _resolution; }
81 if (_resolution == value) {
return; }
89 if (element !=
null &&
string.IsNullOrEmpty(
Type))
91 Type = element.GetAttributeString(
"typename",
string.Empty);
105 public virtual float Phase {
get;
set; }
111 private static readonly
string[] deformationTypes =
new string[] {
"Inflate",
"Custom",
"Noise",
"BendJoint",
"ReactToTriggerers" };
114 get {
return deformationTypes; }
129 return Load(
null, deformationType, parentDebugName);
133 return Load(element,
null, parentDebugName);
136 private static SpriteDeformation Load(XElement element,
string deformationType,
string parentDebugName)
138 string typeName = deformationType;
142 typeName = element.GetAttributeString(
"typename",
null) ?? element.GetAttributeString(
"type",
"");
146 switch (typeName.ToLowerInvariant())
149 newDeformation =
new Inflate(element);
152 newDeformation =
new CustomDeformation(element);
155 newDeformation =
new NoiseDeformation(element);
159 newDeformation =
new JointBendDeformation(element);
161 case "reacttotriggerers":
162 return new PositionalDeformation(element);
164 if (Enum.TryParse(typeName, out PositionalDeformation.ReactionType reactionType))
166 newDeformation =
new PositionalDeformation(element)
173 DebugConsole.ThrowError(
"Could not load sprite deformation animation in " + parentDebugName +
" - \"" + typeName +
"\" is not a valid deformation type.");
178 if (newDeformation !=
null)
180 newDeformation.Params.Type = typeName;
182 return newDeformation;
187 this.
Params = deformationParams;
198 protected abstract void GetDeformation(out Vector2[,] deformation, out
float multiplier,
bool inverse);
200 public abstract void Update(
float deltaTime);
202 private static readonly List<int> yValues =
new List<int>();
203 public static Vector2[,]
GetDeformation(IEnumerable<SpriteDeformation> animations, Vector2 scale,
bool inverseY =
false)
214 Point resolution = animations.First().Resolution;
215 if (animations.Any(a => a.Resolution != resolution))
217 DebugConsole.ThrowError(
"All animations must have the same resolution! Using the lowest resolution.");
218 resolution = animations.OrderBy(anim => anim.Resolution.X + anim.Resolution.Y).First().Resolution;
219 animations.ForEach(a => a.Resolution = resolution);
222 Vector2[,] deformation =
new Vector2[resolution.X, resolution.Y];
226 for (
int y = 0; y < resolution.Y; y++)
234 animation.
GetDeformation(out Vector2[,] animDeformation, out
float multiplier, inverseY);
235 for (
int x = 0; x < resolution.X; x++)
237 for (
int y = 0; y < resolution.Y; y++)
242 deformation[x, yValues[y]] = animDeformation[x, y] * scale * multiplier;
245 deformation[x, yValues[y]] += animDeformation[x, y] * scale * multiplier;
248 deformation[x, yValues[y]] *= animDeformation[x, y] * multiplier;
257 public virtual void Save(XElement element)
static Dictionary< Identifier, SerializableProperty > DeserializeProperties(object obj, XElement element=null)
static void SerializeProperties(ISerializableEntity obj, XElement element, bool saveIfDefault=false, bool ignoreEditable=false)