Search in sources :

Example 1 with BlockSelectionRenderer

use of org.terasology.rendering.world.selection.BlockSelectionRenderer in project Terasology by MovingBlocks.

the class BlockDamageRenderer method renderOverlay.

@Override
public void renderOverlay() {
    if (blockSelectionRenderer == null) {
        Texture texture = Assets.getTextureRegion("core:blockdamageeffects#1").get().getTexture();
        blockSelectionRenderer = new BlockSelectionRenderer(texture);
    }
    // group the entities into what texture they will use so that there is less recreating meshes (changing a texture region on the BlockSelectionRenderer
    // will recreate the mesh to use the different UV coordinates).  Also this allows
    Multimap<Integer, Vector3i> groupedEntitiesByEffect = ArrayListMultimap.create();
    for (EntityRef entity : entityManager.getEntitiesWith(HealthComponent.class, BlockComponent.class)) {
        HealthComponent health = entity.getComponent(HealthComponent.class);
        if (health.currentHealth == health.maxHealth) {
            continue;
        }
        BlockComponent blockComponent = entity.getComponent(BlockComponent.class);
        groupedEntitiesByEffect.put(getEffectsNumber(health), blockComponent.getPosition());
    }
    for (EntityRef entity : entityManager.getEntitiesWith(BlockRegionComponent.class, HealthComponent.class)) {
        HealthComponent health = entity.getComponent(HealthComponent.class);
        if (health.currentHealth == health.maxHealth) {
            continue;
        }
        BlockRegionComponent blockRegion = entity.getComponent(BlockRegionComponent.class);
        for (Vector3i blockPos : blockRegion.region) {
            groupedEntitiesByEffect.put(getEffectsNumber(health), blockPos);
        }
    }
    // we know that the texture will be the same for each block effect,  just differnt UV coordinates.  Bind the texture already
    blockSelectionRenderer.beginRenderOverlay();
    for (Integer effectsNumber : groupedEntitiesByEffect.keySet()) {
        Optional<TextureRegionAsset> texture = Assets.getTextureRegion("core:blockdamageeffects#" + effectsNumber);
        if (texture.isPresent()) {
            blockSelectionRenderer.setEffectsTexture(texture.get());
            for (Vector3i position : groupedEntitiesByEffect.get(effectsNumber)) {
                blockSelectionRenderer.renderMark(position);
            }
        }
    }
    blockSelectionRenderer.endRenderOverlay();
}
Also used : BlockComponent(org.terasology.world.block.BlockComponent) BlockRegionComponent(org.terasology.world.block.regions.BlockRegionComponent) Vector3i(org.terasology.math.geom.Vector3i) BlockSelectionRenderer(org.terasology.rendering.world.selection.BlockSelectionRenderer) Texture(org.terasology.rendering.assets.texture.Texture) EntityRef(org.terasology.entitySystem.entity.EntityRef) TextureRegionAsset(org.terasology.rendering.assets.texture.TextureRegionAsset)

Aggregations

EntityRef (org.terasology.entitySystem.entity.EntityRef)1 Vector3i (org.terasology.math.geom.Vector3i)1 Texture (org.terasology.rendering.assets.texture.Texture)1 TextureRegionAsset (org.terasology.rendering.assets.texture.TextureRegionAsset)1 BlockSelectionRenderer (org.terasology.rendering.world.selection.BlockSelectionRenderer)1 BlockComponent (org.terasology.world.block.BlockComponent)1 BlockRegionComponent (org.terasology.world.block.regions.BlockRegionComponent)1