ocelot-desktop/src/main/scala/ocelot/desktop/node/OcelotLogParticleNode.scala
2025-08-03 01:35:50 +02:00

58 lines
2.0 KiB
Scala

package ocelot.desktop.node
import ocelot.desktop.ColorScheme
import ocelot.desktop.entity.traits.OcelotInterface
import ocelot.desktop.geometry.FloatUtils.ExtendedFloat
import ocelot.desktop.geometry.Vector2D
import ocelot.desktop.graphics.Graphics
import ocelot.desktop.node.OcelotLogParticleNode._
import ocelot.desktop.ui.UiHandler
import ocelot.desktop.ui.event.BrainEvent
import ocelot.desktop.ui.particle.Particle
import scala.util.Random
trait OcelotLogParticleNode extends Node {
eventHandlers += {
case BrainEvent(OcelotInterface.LogEvent.CardToUser(_, _)) =>
val system = UiHandler.root.workspaceView.particleSystem
if (system.count[LogParticle](Some(this)) < MaxLogParticles) {
system.add(new LogParticle)
}
}
private class LogParticle extends Particle(-LogParticleGrow, origin = Some(this)) {
private val angle: Float = Random.between(0f, 2 * math.Pi.toFloat * LogParticleMaxAngle)
override def update(dt: Float): Unit = {
time += LogParticleMoveSpeed * dt
}
override def draw(g: Graphics): Unit = {
val size = (1 + time / LogParticleGrow).clamp() * LogParticleSize
val offset = time.clamp() * LogParticleMoveDistance
val alpha = 1 - time.clamp()
val r1 = (bounds.w max bounds.h) / math.sqrt(2) + offset + LogParticlePadding
val r2 = r1 + size
for (i <- 0 until LogParticleCount) {
val a = angle + (2 * math.Pi).toFloat * i / LogParticleCount
val v = Vector2D.unit(a)
val p1 = v * r1 + bounds.center
val p2 = v * r2 + bounds.center
g.line(p1, p2, 1f, ColorScheme("LogParticle").mapA(_ => alpha))
}
}
}
}
object OcelotLogParticleNode {
private val MaxLogParticles: Int = 15
private val LogParticleMaxAngle: Float = 0.25f
private val LogParticleCount: Int = 12
private val LogParticleGrow: Float = 0.25f
private val LogParticlePadding: Float = 2f
private val LogParticleSize: Float = 10f
private val LogParticleMoveSpeed: Float = 1f
private val LogParticleMoveDistance: Float = 20f
}