/*******************************************************************************
 * Copyright (c) 2015, Andreas Hefti, inarisoft@yahoo.de 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/ 
package com.inari.firefly.movement;

import com.inari.commons.lang.aspect.AspectBitSet;
import com.inari.commons.lang.indexed.IndexedTypeAspectBuilder;
import com.inari.commons.lang.indexed.IndexedTypeKey;
import com.inari.commons.lang.indexed.IndexedTypeSet;
import com.inari.firefly.entity.ETransform;
import com.inari.firefly.entity.Entity;
import com.inari.firefly.entity.EntityComponent.EntityComponentTypeKey;
import com.inari.firefly.entity.EntitySystem;
import com.inari.firefly.movement.event.MoveEvent;
import com.inari.firefly.system.FFContext;
import com.inari.firefly.system.FFSystem;
import com.inari.firefly.system.UpdateEvent;
import com.inari.firefly.system.UpdateEventListener;

public final class MovementSystem implements FFSystem, UpdateEventListener {
    
    public static final FFSystemTypeKey<MovementSystem> SYSTEM_KEY = FFSystemTypeKey.create( MovementSystem.class );
    
    private final static AspectBitSet MOVEMENT_ASPECT = IndexedTypeAspectBuilder.build( EntityComponentTypeKey.class, EMovement.class );

    private FFContext context;
    private EntitySystem entitySystem;

    MovementSystem() {
    }
    
    @Override
    public final IndexedTypeKey indexedTypeKey() {
        return SYSTEM_KEY;
    }

    @Override
    public final FFSystemTypeKey<MovementSystem> systemTypeKey() {
        return SYSTEM_KEY;
    }
    
    @Override
    public void init( FFContext context ) {
        this.context = context;
        
        entitySystem = context.getSystem( EntitySystem.SYSTEM_KEY );
        
        context.registerListener( UpdateEvent.class, this );
    }
    
    @Override
    public final void dispose( FFContext context ) {
        context.disposeListener( UpdateEvent.class, this );
    }

    @Override
    public final void update( UpdateEvent event ) {
        MoveEvent moveEvent = new MoveEvent();
        for ( Entity entity : entitySystem.entities( MOVEMENT_ASPECT ) ) {
            IndexedTypeSet components = entitySystem.getComponents( entity.getId() );
            EMovement movement = components.get( EMovement.TYPE_KEY );
            if ( !movement.isMoving() ) {
                continue;
            }
            
            ETransform transform = components.get( ETransform.TYPE_KEY );
            transform.move( movement.getVelocityVector(), true );
            
            moveEvent.add( entity.getId() );
        }
        context.notify( moveEvent );
    }

}

@@ -810,16 +810,59 @@
 ement;%0A%0A
+import com.inari.commons.lang.IntIterator;%0A
 import c
@@ -1124,48 +1124,8 @@
 rm;%0A
-import com.inari.firefly.entity.Entity;%0A
 impo
@@ -2646,29 +2646,30 @@
     
-for ( Entity
+IntIterator
  entit
-y :
+ies =
  ent
@@ -2705,20 +2705,100 @@
 ASPECT )
- ) %7B
+;%0A        while ( entities.hasNext() ) %7B%0A            int entityId = entities.next();
 %0A       
@@ -2860,32 +2860,26 @@
 ents( entity
-.getId()
+Id
  );%0A        
@@ -3226,16 +3226,10 @@
 tity
-.getId()
+Id
  );%0A

remove Entity class entity is just an id