package com.inari.firefly.graphics.rendering;

import com.inari.commons.lang.aspect.Aspects;
import com.inari.commons.lang.indexed.IndexedTypeSet;
import com.inari.commons.lang.list.DynArray;
import com.inari.firefly.entity.EntityComponent;
import com.inari.firefly.graphics.ETransform;
import com.inari.firefly.graphics.particle.EParticle;
import com.inari.firefly.graphics.particle.Particle;
import com.inari.firefly.system.RenderEvent;

public final class SpriteParticleRenderer extends Renderer {
    
    public static final RenderingChain.RendererKey CHAIN_KEY = new RenderingChain.RendererKey( "SpriteParticleRenderer", SimpleSpriteRenderer.class );
    public static final Aspects MATCHING_ASPECTS = EntityComponent.ASPECT_GROUP.createAspects( 
        EParticle.TYPE_KEY 
    );

    protected SpriteParticleRenderer( int index ) {
        super( index );
        setName( CHAIN_KEY.name );
    }

    @Override
    public boolean match( Aspects aspects ) {
        return aspects.include( MATCHING_ASPECTS );
    }

    @Override
    public final void render( RenderEvent event ) {
        final DynArray<IndexedTypeSet> particleToRender = getEntites( event.getViewId(), event.getLayerId(), false );
        if ( particleToRender == null ) {
            return;
        }
        
        for ( int i = 0; i < particleToRender.capacity(); i++ ) {
            final IndexedTypeSet components = particleToRender.get( i );
            if ( components == null ) {
                continue;
            }

            final DynArray<Particle> particle = components.<EParticle>get( EParticle.TYPE_KEY ).getParticle();
            if ( components.contains( ETransform.TYPE_KEY.index() ) ) {
                final ETransform transform = components.get( ETransform.TYPE_KEY );
                for ( int ip = 0; ip < particle.capacity(); ip++ ) {
                    final Particle p = particle.get( ip );
                    if ( p == null ) {
                        continue;
                    }
                    
                    transformCollector.set( transform );
                    transformCollector.add( p );
                    graphics.renderSprite( p, transformCollector );
                }
            } else {
                for ( int ip = 0; ip < particle.capacity(); ip++ ) {
                    final Particle p = particle.get( ip );
                    if ( p == null ) {
                        continue;
                    }
                    
                    graphics.renderSprite( p, p );
                }
            }
        }
    }

}

@@ -746,16 +746,45 @@
 pects( %0A
+        ETransform.TYPE_KEY,%0A
         
@@ -1648,84 +1648,8 @@
 ();%0A
-            if ( components.contains( ETransform.TYPE_KEY.index() ) ) %7B%0A    
     
@@ -1720,28 +1720,24 @@
 TYPE_KEY );%0A
-    
             
@@ -1797,36 +1797,32 @@
                 
-    
 final Particle p
@@ -1852,36 +1852,32 @@
                 
-   
- 
 if ( p == null )
@@ -1891,36 +1891,32 @@
                 
-    
 continue;%0A      
@@ -1917,36 +1917,32 @@
                 
-   
- 
 %7D%0A              
@@ -1935,39 +1935,33 @@
                 
-    %0A  
+%0A
                 
@@ -1952,34 +1952,32 @@
                 
-  
 transformCollect
@@ -2005,36 +2005,32 @@
                 
-    
 transformCollect
@@ -2050,36 +2050,32 @@
                 
- 
-   
 graphics.renderS
@@ -2110,360 +2110,8 @@
  );%0A
-                %7D%0A            %7D else %7B%0A                for ( int ip = 0; ip %3C particle.capacity(); ip++ ) %7B%0A                    final Particle p = particle.get( ip );%0A                    if ( p == null ) %7B%0A                        continue;%0A                    %7D%0A                    %0A                    graphics.renderSprite( p, p );%0A                %7D%0A
     

fix particle renderer