LibGDX ParticleEffectLoader ✨⬇️
LibGDX ParticleEffectLoader 2D
2 min readMar 5, 2024
In this article, I provide an example of the code of my part effects manager class:
class ParticleEffectManager(var assetManager: AssetManager) {
private val params = ParticleEffectLoader.ParticleEffectParameter()
private val particleLoader = ParticleEffectLoader(InternalFileHandleResolver())
var loadableParticleEffectList = mutableListOf<ParticleEffectData>()
fun load() {
assetManager.setLoader(ParticleEffect::class.java, ".p", particleLoader)
loadableParticleEffectList.onEach { assetManager.load(it.path, ParticleEffect::class.java, params.apply {
atlasFile = "particle/particle.atlas"
}) }
}
fun init() {
loadableParticleEffectList.onEach { it.effect = assetManager[it.path, ParticleEffect::class.java] }
loadableParticleEffectList.clear()
}
enum class EnumParticleEffect(val data: ParticleEffectData) {
ASTEROID(ParticleEffectData("particle/asteroid.p")),
}
data class ParticleEffectData(val path: String) {
lateinit var effect: ParticleEffect
}
}
In order for the download to work, you need to upload your particle effect and the texture atlas for this effect to the assets folder:
How to use it?
In the class where you download resources, do this:
val particleEffectManager = ParticleEffectManager(assetManager)
override fun show() {
with(particleEffectManager) {
// load ALL effects
loadableParticleEffectList = ParticleEffectManager.EnumParticleEffect.values().map { it.data }.toMutableList()
load()
// load ONE effects
loadableParticleEffectList = mutableListOf(
ParticleEffectManager.EnumParticleEffect.ASTEROID.data,
)
load()
}
assetManager.finishLoading()
particleEffectManager.init()
}
To get the effect, use:
val ASTEROID = ParticleEffectManager.EnumParticleEffect.ASTEROID.data.effect
To reproduce the effects I will use an ParticleEffectActor:
val particleEffect = ParticleEffectActor(ASTEROID, false)
If there will be several actors with 1 effect, then notice the effect in the ParticleEffect object:
val particleEffect_1 = ParticleEffectActor(ParticleEffect(ASTEROID), false)
val particleEffect_2 = ParticleEffectActor(ParticleEffect(ASTEROID), false)