Search in sources :

Example 1 with EntityRenderable

use of io.xol.chunkstories.api.rendering.entity.EntityRenderable in project chunkstories by Hugobros3.

the class CulledEntitiesRenderer method renderEntities.

public int renderEntities(RenderingInterface renderer) {
    ((WorldImplementation) world).entitiesLock.readLock().lock();
    // Sort them by type
    Map<EntityDefinition, List<EntityRenderable>> renderableEntitiesTypes = new HashMap<EntityDefinition, List<EntityRenderable>>();
    for (Entity entity : world.getAllLoadedEntities()) {
        if (entity instanceof EntityRenderable) {
            EntityRenderable entityRenderable = (EntityRenderable) entity;
            List<EntityRenderable> entitiesOfThisType = renderableEntitiesTypes.get(entityRenderable.getDefinition());
            if (entitiesOfThisType == null) {
                renderableEntitiesTypes.put(entityRenderable.getDefinition(), new ArrayList<EntityRenderable>());
                entitiesOfThisType = renderableEntitiesTypes.get(entityRenderable.getDefinition());
            }
            entitiesOfThisType.add(entityRenderable);
        }
    }
    int entitiesRendered = 0;
    for (Entry<EntityDefinition, List<EntityRenderable>> entry : renderableEntitiesTypes.entrySet()) {
        List<EntityRenderable> entities = entry.getValue();
        // Caches entity renderers until we f12
        if (!entityRenderers.containsKey(entry.getKey()))
            entityRenderers.put(entry.getKey(), entities.get(0).getEntityRenderer());
        EntityRenderer<? extends EntityRenderable> entityRenderer = entityRenderers.get(entry.getKey());
        if (entityRenderer == null)
            continue;
        try {
            int e = entityRenderer.renderEntities(renderer, new EntitiesRendererIterator<>(renderer, entities));
            entitiesRendered += e;
        } catch (Throwable e) {
            System.out.println("Exception rendering entities " + entities.get(0).getClass().getSimpleName() + " using " + entityRenderer.getClass().getSimpleName());
            e.printStackTrace();
        }
    }
    ((WorldImplementation) world).entitiesLock.readLock().unlock();
    return entitiesRendered;
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) EntityRenderable(io.xol.chunkstories.api.rendering.entity.EntityRenderable) HashMap(java.util.HashMap) WorldImplementation(io.xol.chunkstories.world.WorldImplementation) EntityDefinition(io.xol.chunkstories.api.entity.EntityDefinition) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Entity (io.xol.chunkstories.api.entity.Entity)1 EntityDefinition (io.xol.chunkstories.api.entity.EntityDefinition)1 EntityRenderable (io.xol.chunkstories.api.rendering.entity.EntityRenderable)1 WorldImplementation (io.xol.chunkstories.world.WorldImplementation)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1