From e3ccfeba9f48a5d80e59fe2f752cf833012a93c5 Mon Sep 17 00:00:00 2001 From: Fingercomp Date: Sun, 3 Aug 2025 15:43:27 +0300 Subject: [PATCH] Fix a race when spawning ocelot log particles --- .../desktop/node/OcelotLogParticleNode.scala | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/main/scala/ocelot/desktop/node/OcelotLogParticleNode.scala b/src/main/scala/ocelot/desktop/node/OcelotLogParticleNode.scala index b45939f..3d2554a 100644 --- a/src/main/scala/ocelot/desktop/node/OcelotLogParticleNode.scala +++ b/src/main/scala/ocelot/desktop/node/OcelotLogParticleNode.scala @@ -13,12 +13,27 @@ import ocelot.desktop.ui.particle.Particle import scala.util.Random trait OcelotLogParticleNode extends Node { + private var queuedParticles: Int = 0 + eventHandlers += { case BrainEvent(OcelotInterface.LogEvent.CardToUser(_, _)) => - val system = UiHandler.root.workspaceView.particleSystem - if (system.count[LogParticle](Some(this)) < MaxLogParticles) { - system.add(new LogParticle) - } + synchronized(queuedParticles += 1) + } + + override def update(): Unit = { + super.update() + spawnParticles() + } + + private def spawnParticles(): Unit = synchronized { + val system = UiHandler.root.workspaceView.particleSystem + val toSpawn = queuedParticles min (MaxLogParticles - system.count[LogParticle](Some(this))) max 0 + + for (_ <- 0 until toSpawn) { + system.add(new LogParticle) + } + + queuedParticles = 0 } private class LogParticle extends Particle(time = -LogParticleGrow, speed = LogParticleMoveSpeed, origin = Some(this)) {