So I have a component called ChildParticleComponent
. My component has a few extra things as I've got differet requirements but I'll describe the most simple form possible.
public class ChildParticleComponent implements Component {
public EntityRef particleEntity = EntityRef.NULL;
}
I use this to avoid duplicate code and to handle the case where there is no ChildParticleComponent yet gracefully.
private ChildrenParticleComponent getParticleComponent(EntityRef target) {
ChildrenParticleComponent particleComponent;
if (target.hasComponent(ChildrenParticleComponent.class)) {
particleComponent = target.getComponent(ChildrenParticleComponent.class);
} else {
particleComponent = new ChildrenParticleComponent();
}
return particleComponent;
}
public void addParticleEffect(EntityRef target, EntityRef particleEntity) {
if (target.exists()) {
/* Get the particle entity */
ChildrenParticleComponent particleComponent = getParticleComponent(target);
particleComponent.particleEntity = particleEntity;
/* Here we bind the child's Location Component to the parents */
LocationComponent targetLoc = target.getComponent(LocationComponent.class);
LocationComponent childLoc = particleEntity.getComponent(LocationComponent.class);
childLoc.setWorldPosition(targetLoc.getWorldPosition());
Location.attachChild(target, particleEntity);
particleEntity.setOwner(target);
/* Possibly not needed, but better safe than sorry */
target.saveComponent(particleComponent);
}
}
public EntityRef removeParticleEffect(EntityRef target) {
ChildrenParticleComponent particleComponent = getParticleComponent(target);
EntityRef child = particleComponent.particleEntity;
target.removeComponent(ChildrenParticleComponent.class);
return child;
}
Don't have a version as I don't need it
public EntityRef removeParticleEffect(EntityRef target) {
ChildrenParticleComponent particleComponent = getParticleComponent(target);
return particleComponent.particleEntity;
}
The actual method I have actually uses a Map<String, EntityRef>
in order to allow for multiple particle effects on the one entity. I also pass in the prefab as I'm creating a new entity each time, not reusing the same one.
I've linked to my version of the methods in the sections. Usage of my versions can be found in this system