Skip to content

Commit 25d9e31

Browse files
Add uniform random scale to ParticleVector2Parameter (#1130)
* Add uniform random scale to ParticleVector2Parameter * Bump version to 5.5.0
1 parent 7050570 commit 25d9e31

5 files changed

Lines changed: 35 additions & 5 deletions

File tree

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<!-- Version Configuration -->
1010
<PropertyGroup>
11-
<Version>5.4.1</Version>
11+
<Version>5.5.0</Version>
1212
<MonoGameVersion>3.8.4.1</MonoGameVersion>
1313
<KNIVersion>4.0.9001</KNIVersion>
1414
</PropertyGroup>

source/MonoGame.Extended/Particles/Data/ParticleVector2Parameter.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,25 @@ public struct ParticleVector2Parameter : IEquatable<ParticleVector2Parameter>
3737
/// </summary>
3838
public Vector2 RandomMax;
3939

40+
/// <summary>
41+
/// When <see langword="true"/> and <see cref="Kind"/> is <see cref="ParticleValueKind.Random"/>, a single random
42+
/// value is sampled and applied to both X and Y, preserving the aspect ratio of each particle.
43+
/// </summary>
44+
/// <remarks>
45+
/// The X components of <see cref="RandomMin"/> and <see cref="RandomMax"/> define the uniform range.
46+
/// When <see langword="false"/>, X and Y are sampled independently.
47+
/// </remarks>
48+
public bool Uniform;
49+
4050
/// <summary>
4151
/// Gets the current value of this parameter based on its <see cref="Kind"/>
4252
/// </summary>
4353
/// <remarks>
4454
/// If <see cref="Kind"/> is <see cref="ParticleValueKind.Constant"/>, returns <see cref="Constant"/>.
45-
/// If <see cref="Kind"/> is <see cref="ParticleValueKind.Random"/>, returns a random value between
46-
/// <see cref="RandomMin"/> and <see cref="RandomMax"/>.
55+
/// If <see cref="Kind"/> is <see cref="ParticleValueKind.Random"/> and <see cref="Uniform"/> is
56+
/// <see langword="false"/>, returns a random value with X and Y sampled independently from their respective ranges.
57+
/// If <see cref="Kind"/> is <see cref="ParticleValueKind.Random"/> and <see cref="Uniform"/> is
58+
/// <see langword="true"/>, returns a random value with a single sample applied to both X and Y using the X range.
4759
/// </remarks>
4860
public Vector2 Value
4961
{
@@ -55,6 +67,12 @@ public Vector2 Value
5567
return Constant;
5668
}
5769

70+
if (Uniform)
71+
{
72+
float s = FastRandom.Shared.NextSingle(RandomMin.X, RandomMax.X);
73+
return new Vector2(s, s);
74+
}
75+
5876
Vector2 v;
5977
v.X = FastRandom.Shared.NextSingle(RandomMin.X, RandomMax.X);
6078
v.Y = FastRandom.Shared.NextSingle(RandomMin.Y, RandomMax.Y);

source/MonoGame.Extended/Particles/ParticleEffectReader.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,8 @@ private ParticleVector2Parameter ReadParticleVector2Parameter(XmlReader reader)
363363
{
364364
Vector2 min = reader.GetAttributeVector2(nameof(ParticleVector2Parameter.RandomMin));
365365
Vector2 max = reader.GetAttributeVector2(nameof(ParticleVector2Parameter.RandomMax));
366-
return new ParticleVector2Parameter(min, max);
366+
bool uniform = reader.GetAttributeBool(nameof(ParticleVector2Parameter.Uniform), false);
367+
return new ParticleVector2Parameter(min, max) { Uniform = uniform };
367368
}
368369

369370
return new ParticleVector2Parameter(Vector2.Zero);

source/MonoGame.Extended/Particles/ParticleEffectSerializer.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ private static ParticleVector2Parameter ReadParticleVector2Parameter(XmlReader r
302302
{
303303
Vector2 min = reader.GetAttributeVector2(nameof(ParticleVector2Parameter.RandomMin), default);
304304
Vector2 max = reader.GetAttributeVector2(nameof(ParticleVector2Parameter.RandomMax), default);
305-
return new ParticleVector2Parameter(min, max);
305+
bool uniform = reader.GetAttributeBool(nameof(ParticleVector2Parameter.Uniform), false);
306+
return new ParticleVector2Parameter(min, max) { Uniform = uniform };
306307
}
307308

308309
return new ParticleVector2Parameter(Vector2.Zero);
@@ -847,6 +848,11 @@ private static void WriteParticleVector2Parameter(XmlWriter writer, string name,
847848
{
848849
writer.WriteAttributeVector2(nameof(ParticleVector2Parameter.RandomMin), parameter.RandomMin);
849850
writer.WriteAttributeVector2(nameof(ParticleVector2Parameter.RandomMax), parameter.RandomMax);
851+
852+
if (parameter.Uniform)
853+
{
854+
writer.WriteAttributeString(nameof(ParticleVector2Parameter.Uniform), "true");
855+
}
850856
}
851857

852858
writer.WriteEndElement();

source/MonoGame.Extended/Particles/ParticleEffectWriter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ private void WriteParticleVector2Parameter(string name, ParticleVector2Parameter
206206
{
207207
_writer.WriteAttributeVector2(nameof(ParticleVector2Parameter.RandomMin), parameter.RandomMin);
208208
_writer.WriteAttributeVector2(nameof(ParticleVector2Parameter.RandomMax), parameter.RandomMax);
209+
210+
if (parameter.Uniform)
211+
{
212+
_writer.WriteAttributeString(nameof(ParticleVector2Parameter.Uniform), "true");
213+
}
209214
}
210215

211216
_writer.WriteEndElement();

0 commit comments

Comments
 (0)