Search in sources :

Example 1 with ParticleDataSpriteComponent

use of org.terasology.particles.components.ParticleDataSpriteComponent in project Terasology by MovingBlocks.

the class BlockDamageAuthoritySystem method createBlockParticleEffect.

/**
 * Creates a new entity for the block damage particle effect.
 *
 * If the terrain texture of the damaged block is available, the particles will have the block texture. Otherwise,
 * the default sprite (smoke) is used.
 *
 * @param family the {@link BlockFamily} of the damaged block
 * @param location the location of the damaged block
 */
private void createBlockParticleEffect(BlockFamily family, Vector3f location) {
    EntityBuilder builder = entityManager.newBuilder("core:defaultBlockParticles");
    builder.getComponent(LocationComponent.class).setWorldPosition(location);
    Optional<Texture> terrainTexture = Assets.getTexture("engine:terrain");
    if (terrainTexture.isPresent() && terrainTexture.get().isLoaded()) {
        final BlockAppearance blockAppearance = family.getArchetypeBlock().getPrimaryAppearance();
        final float relativeTileSize = worldAtlas.getRelativeTileSize();
        final float particleScale = 0.25f;
        final float spriteSize = relativeTileSize * particleScale;
        ParticleDataSpriteComponent spriteComponent = builder.getComponent(ParticleDataSpriteComponent.class);
        spriteComponent.texture = terrainTexture.get();
        spriteComponent.textureSize.set(spriteSize, spriteSize);
        final List<Vector2f> offsets = computeOffsets(blockAppearance, particleScale);
        TextureOffsetGeneratorComponent textureOffsetGeneratorComponent = builder.getComponent(TextureOffsetGeneratorComponent.class);
        textureOffsetGeneratorComponent.validOffsets.addAll(offsets);
    }
    builder.build();
}
Also used : ParticleDataSpriteComponent(org.terasology.particles.components.ParticleDataSpriteComponent) TextureOffsetGeneratorComponent(org.terasology.particles.components.generators.TextureOffsetGeneratorComponent) BlockAppearance(org.terasology.world.block.BlockAppearance) Vector2f(org.terasology.math.geom.Vector2f) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) LocationComponent(org.terasology.logic.location.LocationComponent) Texture(org.terasology.rendering.assets.texture.Texture)

Aggregations

EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)1 LocationComponent (org.terasology.logic.location.LocationComponent)1 Vector2f (org.terasology.math.geom.Vector2f)1 ParticleDataSpriteComponent (org.terasology.particles.components.ParticleDataSpriteComponent)1 TextureOffsetGeneratorComponent (org.terasology.particles.components.generators.TextureOffsetGeneratorComponent)1 Texture (org.terasology.rendering.assets.texture.Texture)1 BlockAppearance (org.terasology.world.block.BlockAppearance)1