From ccbb30cdda8370ad626b30a56d1d9c7f169d03ec Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 18 Jul 2023 19:52:45 +0900 Subject: [PATCH] Fix `ParticleSpewer` not correctly accounting for lower frame rates (and spawning less particles) --- osu.Game/Graphics/ParticleSpewer.cs | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/osu.Game/Graphics/ParticleSpewer.cs b/osu.Game/Graphics/ParticleSpewer.cs index 8519cf0c59..02053e1be7 100644 --- a/osu.Game/Graphics/ParticleSpewer.cs +++ b/osu.Game/Graphics/ParticleSpewer.cs @@ -20,9 +20,9 @@ namespace osu.Game.Graphics { private readonly FallingParticle[] particles; private int currentIndex; - private double lastParticleAdded; + private double? lastParticleAdded; - private readonly double cooldown; + private readonly double timeBetweenSpawns; private readonly double maxDuration; /// @@ -44,7 +44,7 @@ namespace osu.Game.Graphics particles = new FallingParticle[perSecond * (int)Math.Ceiling(maxDuration / 1000)]; - cooldown = 1000f / perSecond; + timeBetweenSpawns = 1000f / perSecond; this.maxDuration = maxDuration; } @@ -52,18 +52,27 @@ namespace osu.Game.Graphics { base.Update(); - if (Active.Value && CanSpawnParticles && Math.Abs(Time.Current - lastParticleAdded) > cooldown) + Invalidate(Invalidation.DrawNode); + + if (!Active.Value || !CanSpawnParticles) { + lastParticleAdded = null; + return; + } + + while (lastParticleAdded == null || Math.Abs(Time.Current - lastParticleAdded.Value) > timeBetweenSpawns) + { + lastParticleAdded ??= Time.Current; + var newParticle = CreateParticle(); - newParticle.StartTime = (float)Time.Current; + newParticle.StartTime = (float)lastParticleAdded; particles[currentIndex] = newParticle; currentIndex = (currentIndex + 1) % particles.Length; - lastParticleAdded = Time.Current; - } - Invalidate(Invalidation.DrawNode); + lastParticleAdded += timeBetweenSpawns; + } } ///