package net.mostlyoriginal.game.system.drag;

import com.artemis.Aspect;
import com.artemis.Entity;
import com.artemis.EntitySystem;
import com.artemis.World;
import com.artemis.annotations.Wire;
import com.artemis.utils.IntBag;
import net.mostlyoriginal.api.system.physics.CollisionSystem;
import net.mostlyoriginal.game.component.Draggable;

/**
 * @author Daan van Yperen
 */
@Wire
public class GridOverlapHelperSystem extends EntitySystem {

	CollisionSystem collisionSystem;
	private Entity flyweightEntity;

	public GridOverlapHelperSystem() {
		super(Aspect.all(Draggable.class));
	}

	@Override
	protected void setWorld(World world) {
		super.setWorld(world);
		flyweightEntity = createFlyweightEntity();
	}

	/**
	 * Does passed entity overlap any grid entity? (except self)
	 */
	public boolean overlaps(Entity e) {
		IntBag actives = subscription.getEntities();
		int[] array = actives.getData();
		for (int i = 0, s = actives.size(); s > i; i++) {
			flyweightEntity.id = array[i];
			if (flyweightEntity.getId() != e.getId() && collisionSystem.overlaps(flyweightEntity, e)) {
				return true;
			}
		}
		return false;
	}


	@Override
	protected void processSystem() {
	}
}

@@ -280,24 +280,75 @@
 sionSystem;%0A
+import net.mostlyoriginal.game.component.Conveyer;%0A
 import net.m
@@ -613,11 +613,11 @@
 ect.
-all
+one
 (Dra
@@ -628,16 +628,32 @@
 le.class
+, Conveyer.class
 ));%0A%09%7D%0A%0A

Fix draggability on top of other conveyers.