Skip to content

Instantly share code, notes, and snippets.

@syntaxi
Created July 25, 2018 14:58
Show Gist options
  • Save syntaxi/2a18b1670fe26b59cbdae7c686356cde to your computer and use it in GitHub Desktop.
Save syntaxi/2a18b1670fe26b59cbdae7c686356cde to your computer and use it in GitHub Desktop.
Example of having a child particle component

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.

ChildParticleComponent

My Version

public class ChildParticleComponent implements Component {
    public EntityRef particleEntity = EntityRef.NULL;
}

Helper Method

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;
}

Adding

My Version

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);
    }
}

Removing

My Version

public EntityRef removeParticleEffect(EntityRef target) {
    ChildrenParticleComponent particleComponent = getParticleComponent(target);
    EntityRef child = particleComponent.particleEntity;
    target.removeComponent(ChildrenParticleComponent.class);
    return child;
}

Getting

Don't have a version as I don't need it

public EntityRef removeParticleEffect(EntityRef target) {
    ChildrenParticleComponent particleComponent = getParticleComponent(target);
    return particleComponent.particleEntity;
}

Notes

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment