Replace Scale with SpeedMultiplier

This commit is contained in:
Andrei Zavatski 2022-11-16 15:12:57 +03:00
parent 109aa37dd8
commit cc4f05f3d3

View File

@ -57,14 +57,6 @@ namespace osu.Game.Graphics.Backgrounds
/// </summary> /// </summary>
protected virtual float SpawnRatio => 1; protected virtual float SpawnRatio => 1;
private readonly BindableFloat triangleScale = new BindableFloat(1f);
public float TriangleScale
{
get => triangleScale.Value;
set => triangleScale.Value = value;
}
/// <summary> /// <summary>
/// The relative velocity of the triangles. Default is 1. /// The relative velocity of the triangles. Default is 1.
/// </summary> /// </summary>
@ -102,7 +94,7 @@ namespace osu.Game.Graphics.Backgrounds
colourTop.BindValueChanged(_ => updateTexture()); colourTop.BindValueChanged(_ => updateTexture());
colourBottom.BindValueChanged(_ => updateTexture(), true); colourBottom.BindValueChanged(_ => updateTexture(), true);
triangleScale.BindValueChanged(_ => Reset(), true); addTriangles(true);
} }
private void updateTexture() private void updateTexture()
@ -138,20 +130,17 @@ namespace osu.Game.Graphics.Backgrounds
float elapsedSeconds = (float)Time.Elapsed / 1000; float elapsedSeconds = (float)Time.Elapsed / 1000;
// Since position is relative, the velocity needs to scale inversely with DrawHeight. // Since position is relative, the velocity needs to scale inversely with DrawHeight.
// Since we will later multiply by the scale of individual triangles we normalize by float movedDistance = -elapsedSeconds * Velocity * base_velocity / DrawHeight;
// dividing by triangleScale.
float movedDistance = -elapsedSeconds * Velocity * base_velocity / (DrawHeight * TriangleScale);
for (int i = 0; i < parts.Count; i++) for (int i = 0; i < parts.Count; i++)
{ {
TriangleParticle newParticle = parts[i]; TriangleParticle newParticle = parts[i];
// Scale moved distance by the size of the triangle. Smaller triangles should move more slowly. newParticle.Position.Y += Math.Max(0.5f, parts[i].SpeedMultiplier) * movedDistance;
newParticle.Position.Y += Math.Max(0.5f, parts[i].Scale) * movedDistance;
parts[i] = newParticle; parts[i] = newParticle;
float bottomPos = parts[i].Position.Y + triangle_size * parts[i].Scale * equilateral_triangle_ratio / DrawHeight; float bottomPos = parts[i].Position.Y + triangle_size * equilateral_triangle_ratio / DrawHeight;
if (bottomPos < 0) if (bottomPos < 0)
parts.RemoveAt(i); parts.RemoveAt(i);
} }
@ -177,7 +166,7 @@ namespace osu.Game.Graphics.Backgrounds
// Limited by the maximum size of QuadVertexBuffer for safety. // Limited by the maximum size of QuadVertexBuffer for safety.
const int max_triangles = ushort.MaxValue / (IRenderer.VERTICES_PER_QUAD + 2); const int max_triangles = ushort.MaxValue / (IRenderer.VERTICES_PER_QUAD + 2);
AimCount = (int)Math.Min(max_triangles, DrawWidth * DrawHeight * 0.002f / (TriangleScale * TriangleScale) * SpawnRatio); AimCount = (int)Math.Min(max_triangles, DrawWidth * DrawHeight * 0.001f * SpawnRatio);
int currentCount = parts.Count; int currentCount = parts.Count;
@ -194,7 +183,7 @@ namespace osu.Game.Graphics.Backgrounds
if (randomY) if (randomY)
{ {
// since triangles are drawn from the top - allow them to be positioned a bit above the screen // since triangles are drawn from the top - allow them to be positioned a bit above the screen
float maxOffset = triangle_size * particle.Scale * equilateral_triangle_ratio / DrawHeight; float maxOffset = triangle_size * equilateral_triangle_ratio / DrawHeight;
y = Interpolation.ValueAt(nextRandom(), -maxOffset, 1f, 0f, 1f); y = Interpolation.ValueAt(nextRandom(), -maxOffset, 1f, 0f, 1f);
} }
@ -204,7 +193,7 @@ namespace osu.Game.Graphics.Backgrounds
} }
/// <summary> /// <summary>
/// Creates a triangle particle with a random scale. /// Creates a triangle particle with a random speed multiplier.
/// </summary> /// </summary>
/// <returns>The triangle particle.</returns> /// <returns>The triangle particle.</returns>
protected virtual TriangleParticle CreateTriangle() protected virtual TriangleParticle CreateTriangle()
@ -215,9 +204,9 @@ namespace osu.Game.Graphics.Backgrounds
float u1 = 1 - nextRandom(); //uniform(0,1] random floats float u1 = 1 - nextRandom(); //uniform(0,1] random floats
float u2 = 1 - nextRandom(); float u2 = 1 - nextRandom();
float randStdNormal = (float)(Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2)); // random normal(0,1) float randStdNormal = (float)(Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2)); // random normal(0,1)
float scale = Math.Max(TriangleScale * (mean + std_dev * randStdNormal), 0.1f); // random normal(mean,stdDev^2) float speedMultiplier = Math.Max(mean + std_dev * randStdNormal, 0.1f); // random normal(mean,stdDev^2)
return new TriangleParticle { Scale = scale }; return new TriangleParticle { SpeedMultiplier = speedMultiplier };
} }
private float nextRandom() => (float)(stableRandom?.NextDouble() ?? RNG.NextSingle()); private float nextRandom() => (float)(stableRandom?.NextDouble() ?? RNG.NextSingle());
@ -267,7 +256,7 @@ namespace osu.Game.Graphics.Backgrounds
foreach (TriangleParticle particle in parts) foreach (TriangleParticle particle in parts)
{ {
var offset = triangle_size * new Vector2(particle.Scale * 0.5f, particle.Scale * equilateral_triangle_ratio); var offset = triangle_size * new Vector2(0.5f, equilateral_triangle_ratio);
Vector2 topLeft = particle.Position * size + new Vector2(-offset.X, 0f); Vector2 topLeft = particle.Position * size + new Vector2(-offset.X, 0f);
Vector2 topRight = particle.Position * size + new Vector2(offset.X, 0); Vector2 topRight = particle.Position * size + new Vector2(offset.X, 0);
@ -310,9 +299,9 @@ namespace osu.Game.Graphics.Backgrounds
public Vector2 Position; public Vector2 Position;
/// <summary> /// <summary>
/// The scale of the triangle. /// The speed multiplier of the triangle.
/// </summary> /// </summary>
public float Scale; public float SpeedMultiplier;
} }
} }
} }