1 using Microsoft.Xna.Framework;
3 using System.Collections.Generic;
4 using System.Collections.Immutable;
21 public string Name => $
"Decorative Sprite";
36 private float blinkTimer = 0.0f;
39 public Vector2
Offset {
get;
private set; }
50 private float rotationSpeedRadians;
51 private float absRotationSpeedRadians;
58 return MathHelper.ToDegrees(rotationSpeedRadians);
62 rotationSpeedRadians = MathHelper.ToRadians(value);
63 absRotationSpeedRadians = Math.Abs(rotationSpeedRadians);
67 private float rotationRadians;
73 return MathHelper.ToDegrees(rotationRadians);
77 rotationRadians = MathHelper.ToRadians(value);
81 private Vector2 randomRotationRadians;
87 return new Vector2(MathHelper.ToDegrees(randomRotationRadians.X), MathHelper.ToDegrees(randomRotationRadians.Y));
91 randomRotationRadians =
new Vector2(MathHelper.ToRadians(value.X), MathHelper.ToRadians(value.Y));
100 private set { scale = MathHelper.Clamp(value, 0.0f, 10.0f); }
120 public Color Color {
get;
set; }
125 internal List<PropertyConditional> IsActiveConditionals {
get;
private set; } =
new List<PropertyConditional>();
129 internal List<PropertyConditional> AnimationConditionals {
get;
private set; } =
new List<PropertyConditional>();
133 Sprite =
new Sprite(element, path, file, lazyLoad: lazyLoad);
136 foreach (var subElement
in element.Elements())
139 List<PropertyConditional> conditionalList =
null;
140 switch (subElement.Name.ToString().ToLowerInvariant())
143 case "isactiveconditional":
144 conditionalList = IsActiveConditionals;
146 case "animationconditional":
147 conditionalList = AnimationConditionals;
156 public Vector2
GetOffset(ref
float offsetState, Vector2 randomOffsetMultiplier,
float rotation = 0.0f)
171 offset =
new Vector2(
172 offset.X * (PerlinNoise.GetPerlin(t, t) - 0.5f),
173 offset.Y * (PerlinNoise.GetPerlin(t + 0.5f, t + 0.5f) - 0.5f));
177 offset +=
new Vector2(
180 if (Math.Abs(rotation) > 0.01f)
182 Matrix transform = Matrix.CreateRotationZ(rotation);
183 offset = Vector2.Transform(offset, transform);
188 public float GetRotation(ref
float rotationState,
float randomRotationFactor)
193 rotationState %= MathHelper.TwoPi / absRotationSpeedRadians;
195 rotationRadians * (float)Math.Sin(rotationState * rotationSpeedRadians)
196 + MathHelper.Lerp(randomRotationRadians.X, randomRotationRadians.Y, randomRotationFactor);
198 rotationState %= 1.0f / absRotationSpeedRadians;
200 rotationRadians * (PerlinNoise.GetPerlin(rotationState * absRotationSpeedRadians, rotationState * absRotationSpeedRadians) - 0.5f)
201 + MathHelper.Lerp(randomRotationRadians.X, randomRotationRadians.Y, randomRotationFactor);
205 rotationState * rotationSpeedRadians
206 + MathHelper.Lerp(randomRotationRadians.X, randomRotationRadians.Y, randomRotationFactor);
219 public static void UpdateSpriteStates(ImmutableDictionary<
int, ImmutableArray<DecorativeSprite>> spriteGroups, Dictionary<DecorativeSprite, State> animStates,
220 int entityID,
float deltaTime, Func<PropertyConditional, bool> checkConditional)
222 foreach (
int spriteGroup
in spriteGroups.Keys)
224 for (
int i = 0; i < spriteGroups[spriteGroup].Length; i++)
226 var decorativeSprite = spriteGroups[spriteGroup][i];
227 if (decorativeSprite ==
null) {
continue; }
230 int activeSpriteIndex = entityID % spriteGroups[spriteGroup].Length;
231 if (i != activeSpriteIndex)
233 animStates[decorativeSprite].IsActive =
false;
239 var spriteState = animStates[decorativeSprite];
240 spriteState.IsActive =
true;
243 if (!checkConditional(conditional))
245 spriteState.IsActive =
false;
249 if (!spriteState.IsActive) {
continue; }
250 if (decorativeSprite.BlinkFrequency > 0.0f)
252 decorativeSprite.blinkTimer += deltaTime * decorativeSprite.BlinkFrequency;
253 decorativeSprite.blinkTimer %= 1.0f;
254 if (decorativeSprite.blinkTimer > 0.5f)
256 spriteState.IsActive =
false;
264 if (!checkConditional(conditional)) { animate =
false;
break; }
266 if (!animate) {
continue; }
267 spriteState.OffsetState += deltaTime;
268 spriteState.RotationState += deltaTime;
float RandomRotationFactor
Vector2 RandomOffsetMultiplier
AnimationType RotationAnim
Vector2 GetOffset(ref float offsetState, Vector2 randomOffsetMultiplier, float rotation=0.0f)
DecorativeSprite(ContentXElement element, string path="", string file="", bool lazyLoad=false)
Dictionary< Identifier, SerializableProperty > SerializableProperties
static void UpdateSpriteStates(ImmutableDictionary< int, ImmutableArray< DecorativeSprite >> spriteGroups, Dictionary< DecorativeSprite, State > animStates, int entityID, float deltaTime, Func< PropertyConditional, bool > checkConditional)
float GetRotation(ref float rotationState, float randomRotationFactor)
int RandomGroupID
If > 0, only one sprite of the same group is used (chosen randomly)
float GetScale(float randomScaleModifier)
Conditionals are used by some in-game mechanics to require one or more conditions to be met for those...
static IEnumerable< PropertyConditional > FromXElement(ContentXElement element, Predicate< XAttribute >? predicate=null)
static Dictionary< Identifier, SerializableProperty > DeserializeProperties(object obj, XElement element=null)