Skip to content

2. Creating a block that emits a particle

pamtdoh edited this page Dec 14, 2017 · 4 revisions

Our goal is to make a block that emits a particle every time it is activated.

Step 1

Let's add a block first, follow this tutorial if you don't know how.

We now should have a block definition located in blocks/particleBlock.block, for the purpose of this tutorial our block name is particleBlock. We will also need to make the block as an entity, so we could interact with it.

{
  "displayName": "Particle Block",
  "categories": ["misc"],
  "hardness": 1,
  "entity": {
    "prefab": "particleBlock"
  }
}

Step 2

Define our particleBlock prefab in assets/prefabs/particleBlock.prefab, add a component called EmitParticleOnActivate, we will add this later.

{
  "EmitParticleOnActivate": {}
}

Step 3

Add our smoke texture to assets/textures/fx_smoke.png and add our particle effects prefab in assets/prefabs/particleEffects/smoke.prefab, here we will define how the particle behaves. We will get into that in the next tutorial, for now just copy from below.

{
  "location": {},
  "particleDataSprite": {
    "texture": "TutorialParticleSystem:fx_smoke"
  },
  "energyRangeGenerator": {
    "minEnergy": 1,
    "maxEnergy": 2
  },
  "scaleRangeGenerator": {
    "minScale": [1, 1, 1],
    "maxScale": [1.5, 1.5, 1.5]
  },
  "velocityRangeGenerator": {
    "minVelocity": [0, 1, 0],
    "maxVelocity": [0, 2, 0]
  },
  "velocityAffector": {},
  "particleEmitter": {
    "lifeTime": 10,
    "spawnRateMax": 10,
    "spawnRateMin": 10,
    "particleSpawnsLeft": 100,
    "maxParticles": 75,
    "particleCollision": false,
    "destroyEntityWhenDead": true
  },
  "network": {}
}

Step 4

Now we will add our EmitParticleOnActivate component and system.

src/main/java/org/terasology/TutorialParticleSystem/EmitParticleOnActivateComponent.java

package org.terasology.TutorialParticleSystem;

import org.terasology.entitySystem.Component;

public class EmitParticleOnActivateComponent implements Component {

}

src/main/java/org/terasology/TutorialParticleSystem/EmitParticleOnActivateSystem.java

package org.terasology.TutorialParticleSystem;

import org.terasology.entitySystem.entity.EntityBuilder;
import org.terasology.entitySystem.entity.EntityManager;
import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.entitySystem.event.ReceiveEvent;
import org.terasology.entitySystem.systems.BaseComponentSystem;
import org.terasology.entitySystem.systems.RegisterSystem;
import org.terasology.logic.common.ActivateEvent;
import org.terasology.logic.location.LocationComponent;
import org.terasology.math.geom.Vector3f;
import org.terasology.registry.In;
import org.terasology.world.block.BlockComponent;

@RegisterSystem
public class EmitParticleOnActivateSystem extends BaseComponentSystem{

    @In
    private EntityManager entityManager;

    @ReceiveEvent(components = {EmitParticleOnActivateComponent.class, BlockComponent.class})
    public void onActivate(ActivateEvent event, EntityRef entity) {
        Vector3f loc = entity.getComponent(LocationComponent.class).getWorldPosition();
        Vector3f loc_emitter = loc;
        loc_emitter.setY(loc.getY()+1); // Set the location one block above the activated block
        EntityBuilder builder = entityManager.newBuilder("TutorialParticleSystem:smoke");
        builder.getComponent(LocationComponent.class).setWorldPosition(loc_emitter);
        builder.build(); // Spawn the particle emitter
    }
}

Step 5

Test it! Use the command give particleBlock, place the block and then press 'e' to activate it.