package com.deftwun.zombiecopter.systems;

import com.badlogic.ashley.core.Engine;
import com.badlogic.ashley.core.EntitySystem;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.deftwun.zombiecopter.App;
import com.deftwun.zombiecopter.SpawnZone;

public class SpawnSystem extends EntitySystem {

	private Rectangle camRect,
					  boundsRect = new Rectangle(App.engine.entityBounds),
					  tmpRect = new Rectangle();
	
	private Vector2 cameraCenter = new Vector2(),
					spawnPoint = new Vector2();

	private Array<SpawnZone> points = new Array<SpawnZone>();
	private Array<SpawnZone> deadPoints = new Array<SpawnZone>();
	private final float units = App.engine.PIXELS_PER_METER;
	private final int maxEntities = 25;
	
	public void add(SpawnZone point){
		points.add(point);
	}

	public SpawnSystem(){
		this.setProcessing(true);
	}
	
	@Override
	public void removedFromEngine(Engine engine) {
		points.clear();
		deadPoints.clear();
	}

	@Override
	public void update(float deltaTime) {
		if (App.engine.getEntityCount() > maxEntities) return;
		
		
		camRect = App.engine.systems.camera.getCameraRect(1/units);
		boundsRect.setCenter(camRect.getCenter(cameraCenter));
		
		for (SpawnZone z : points){
			z.time += deltaTime;
			
			//Spawn new entity
			if (z.time > z.delay && z.count < z.maximum){
				boolean withinBounds = Intersector.intersectRectangles(z.rectangle,boundsRect,tmpRect);
				boolean outsideCamera = !camRect.overlaps(z.rectangle);
				z.time = 0;
				
				if (withinBounds && outsideCamera){
					z.count++;
					spawnPoint.x = MathUtils.random(tmpRect.x,tmpRect.x+tmpRect.width);
					spawnPoint.y = MathUtils.random(tmpRect.y,tmpRect.y+tmpRect.height);
					App.engine.factory.build(z.type,spawnPoint);
				}
			}
			
			//Retire spawn point if it has reached max spawn count
			if (z.maximum > 0 && z.count > z.maximum){
				deadPoints.add(z);
			}
		}
		
		//Remove the retired spawn points
		for (SpawnZone p : deadPoints){
			points.removeValue(p, true);
		}
		deadPoints.clear();
	}

	public Array<SpawnZone> getZones() {
		return points;
	}
	
}

@@ -53,76 +53,20 @@
 com.
-badlogic.ashley.core.Engine;%0D%0Aimport com.badlogic.ashley.core.Entity
+artemis.Base
 Syst
@@ -389,22 +389,20 @@
 extends 
-Entity
+Base
 System %7B
@@ -910,223 +910,46 @@
 %0A%0D%0A%09
-public SpawnSystem()%7B%0D%0A%09%09this.setProcessing(true);%0D%0A%09%7D%0D%0A%09%0D%0A%09@Override%0D%0A%09public void removedFromEngine(Engine engine) %7B%0D%0A%09%09points.clear();%0D%0A%09%09deadPoints.clear();%0D%0A%09%7D%0D%0A%0D%0A%09@Override%0D%0A%09public void update(float deltaTime
+@Override%0D%0A%09public void processSystem(
 ) %7B%0D
@@ -1188,17 +1188,19 @@
  += 
+world.
 delta
-Time
 ;%0D%0A%09

Migrate SpawnSystem.java