Difference between revisions of "Custom Configurable Emitters"
(Created page with "You may have found that the particle effects made possible by Configurable Emitters just don't provide enough customisation to provide the effect that you want. For example, w...") |
|||
Line 41: | Line 41: | ||
ParticleSystem = ParticleIO.loadConfiguredSystem("emitters/custom.xml", myCustomEmitter); | ParticleSystem = ParticleIO.loadConfiguredSystem("emitters/custom.xml", myCustomEmitter); | ||
</pre> | </pre> | ||
+ | |||
+ | [[Category:Particles]] |
Latest revision as of 16:50, 8 August 2013
You may have found that the particle effects made possible by Configurable Emitters just don't provide enough customisation to provide the effect that you want. For example, what if you want to implement collision detection in your emitter? You could do this by simply creating a subclass of a ConfigurableEmitter to implement these more advanced properties. But what if you also what to be able to load particle system settings from an XML file using the ParticleIO class? The ParticleIO class doesn't know about your custom subclass so it's unable to return an instance of it when it loads settings from an XML file. This is where the ConfigurableEmitterFactory comes in. This article will explain what it is and how to use it to create more advanced custom particle effects.
The ConfigurableEmitterFactory[edit]
The ConfigurableEmitterFactory is an interface which describes a method called createEmitter() which returns a Configurable Emitter, or any subclass of a ConfigurableEmitter. When you create a subclass of a ConfigurableEmitter you must also create a ConfigurableEmitterFactory and then pass this on to the ParticleIO class so it is able to return a ConfigurableEmitter of any subclass that you specify.
In fact this is less complicated than it sounds. The best way to do this is to subclass the ConfigurableEmitter and implement the ConfigurableEmitterFactory interface in the same class. In this class there will be the createEmitter() method along with any overloaded methods of the ConfigurableEmitter class to provide custom functionality. Here is an example of an emitter that accepts a Boundary object in its constructor which it uses to then provide collision detection.
import org.newdawn.slick.particles.ConfigurableEmitter; import org.newdawn.slick.particles.ConfigurableEmitterFactory; import org.newdawn.slick.particles.Particle; public class DynamicEmitter extends ConfigurableEmitter implements ConfigurableEmitterFactory{ private Boundary boundary; //The method required by the ConfigurableEmitterFactory interface public ConfigurableEmitter createEmitter(String name) { return this; } public DynamicEmitter(Boundary boundary) { super("dynamicemitter"); this.boundry = boundary; } public void updateParticle(Particle particle, int delta) { super.updateParticle(particle, delta); //Check for collisions if (boundry.checkCollision(particle.getX(), particle.getY())) { particle.setVelocity(0, 0); } } }
To use this class you simply pass an instance of it to the ParticleIO class when loading a particle system from an XML file. For example:
DynamicEmitter myCustomEmitter = new DynamicEmitter(boundry); ParticleSystem = ParticleIO.loadConfiguredSystem("emitters/custom.xml", myCustomEmitter);