LibGDX: ParticleEffectActor ✨
With the possibility of ROTATING the effect.
1 min readMay 27, 2024
Full code for ParticleEffectActor:
class AParticleEffectActor(
private val particleEffect: ParticleEffect,
private val resetOnStart : Boolean
): Actor(), Disposable {
private val originalAngles: List<Static.AngleRange> = particleEffect.emitters.map { emitter ->
Static.AngleRange(
emitter.angle.lowMin,
emitter.angle.lowMax,
emitter.angle.highMin,
emitter.angle.highMax
)
}
override fun act(delta: Float) {
super.act(delta)
particleEffect.setPosition(x, y)
particleEffect.update(delta)
}
override fun draw(batch: Batch, parentAlpha: Float) {
particleEffect.draw(batch)
}
fun start() {
if (resetOnStart) {
particleEffect.reset(false)
}
particleEffect.start()
}
override fun scaleChanged() {
super.scaleChanged()
particleEffect.scaleEffect(scaleX, scaleY, scaleY)
}
override fun positionChanged() {
super.positionChanged()
particleEffect.setPosition(x, y)
}
override fun rotationChanged() {
super.rotationChanged()
particleEffect.emitters.forEachIndexed { index, emitter ->
originalAngles[index].apply {
emitter.angle.setLow(lowMin + rotation, lowMax + rotation)
emitter.angle.setHigh(highMin + rotation, highMax + rotation)
}
}
}
override fun setPosition(x: Float, y: Float) {
super.setPosition(x, y)
particleEffect.setPosition(x, y)
}
override fun setRotation(degrees: Float) {
super.setRotation(degrees)
rotationChanged()
}
override fun dispose() {
particleEffect.dispose()
}
fun allowCompletion() {
particleEffect.allowCompletion()
}
object Static {
data class AngleRange(
val lowMin: Float,
val lowMax: Float,
val highMin: Float,
val highMax: Float
)
}
}