Fix a race when spawning ocelot log particles

This commit is contained in:
Fingercomp 2025-08-03 15:43:27 +03:00
parent 69dcca71dc
commit e3ccfeba9f
No known key found for this signature in database
GPG Key ID: BBC71CEE45D86E37

View File

@ -13,12 +13,27 @@ import ocelot.desktop.ui.particle.Particle
import scala.util.Random import scala.util.Random
trait OcelotLogParticleNode extends Node { trait OcelotLogParticleNode extends Node {
private var queuedParticles: Int = 0
eventHandlers += { eventHandlers += {
case BrainEvent(OcelotInterface.LogEvent.CardToUser(_, _)) => case BrainEvent(OcelotInterface.LogEvent.CardToUser(_, _)) =>
synchronized(queuedParticles += 1)
}
override def update(): Unit = {
super.update()
spawnParticles()
}
private def spawnParticles(): Unit = synchronized {
val system = UiHandler.root.workspaceView.particleSystem val system = UiHandler.root.workspaceView.particleSystem
if (system.count[LogParticle](Some(this)) < MaxLogParticles) { val toSpawn = queuedParticles min (MaxLogParticles - system.count[LogParticle](Some(this))) max 0
for (_ <- 0 until toSpawn) {
system.add(new LogParticle) system.add(new LogParticle)
} }
queuedParticles = 0
} }
private class LogParticle extends Particle(time = -LogParticleGrow, speed = LogParticleMoveSpeed, origin = Some(this)) { private class LogParticle extends Particle(time = -LogParticleGrow, speed = LogParticleMoveSpeed, origin = Some(this)) {