package com.deftwun.zombiecopter.systems;

import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.core.Family;
import com.badlogic.ashley.systems.IteratingSystem;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Logger;
import com.deftwun.zombiecopter.ComponentMappers;
import com.deftwun.zombiecopter.App;
import com.deftwun.zombiecopter.components.ChildComponent;
import com.deftwun.zombiecopter.components.ControllerComponent;
import com.deftwun.zombiecopter.components.GunComponent;
import com.deftwun.zombiecopter.components.MeleeComponent;
import com.deftwun.zombiecopter.components.PhysicsComponent;
import com.deftwun.zombiecopter.components.TimeToLiveComponent;

public class WeaponSystem extends IteratingSystem {
	
	private Logger logger = new Logger("WeaponSystem",Logger.INFO);	
	private Vector2 pos = new Vector2(), 
					vel = new Vector2();
	
	@SuppressWarnings("unchecked")
	public WeaponSystem(){
		super(Family.one(GunComponent.class,MeleeComponent.class).get());	
		logger.debug("initializing");
	}
	
	@Override
	protected void processEntity(Entity entity, float deltaTime) {
		
		ComponentMappers mappers = App.engine.mappers;
		PhysicsComponent physics = mappers.physics.get(entity);
		GunComponent gun = mappers.gun.get(entity);
		MeleeComponent melee = mappers.melee.get(entity);
		ControllerComponent controller = mappers.controller.get(entity);
		
		//Shoot Gun
		if (gun != null){
			gun.triggerPulled = controller.attack;
			gun.time += deltaTime;
			if (gun.triggerPulled && gun.time >= gun.cooldown){
				logger.debug(String.format("Entity %s is firing",entity.getId()));
				
				vel.set(controller.lookVector).nor().scl(gun.bulletSpeed).add(physics.getLinearVelocity());
				vel.rotate(MathUtils.random(gun.spreadAngle)-gun.spreadAngle/2);
				pos.set(gun.offset).rotate(controller.lookVector.angle()).add(physics.getPosition());
				
				Entity bulletEntity = App.engine.factory.build(gun.projectileName,pos,vel,controller.lookVector.angle());
				if (bulletEntity != null){
					//Time to live
					TimeToLiveComponent ttl = App.engine.createComponent(TimeToLiveComponent.class);
					ttl.timeLimit = gun.range / gun.bulletSpeed;
					
					//Parent 
					ChildComponent child = App.engine.createComponent(ChildComponent.class);
					child.parentEntity = entity;
				
					bulletEntity.add(ttl);
					bulletEntity.add(child);
					gun.time = 0;
				}
			}
		}
		
		//Melee Attack
		if (melee != null){
			melee.triggerPulled = controller.attack; 
			melee.time += deltaTime;
			String status = String.format("Melee.target = %b\n TriggerPulled = %b\n Cooled = %b",
										   melee.target != null,melee.triggerPulled,melee.time >= melee.coolDown);
			logger.debug(status);
			if (melee.target != null && melee.triggerPulled && melee.time >= melee.coolDown){
				logger.debug("Entity " + entity.getId() + " is meleeing");
				Vector2 targetPosition = mappers.physics.get(melee.target).getPosition();
				App.engine.systems.damage.dealDamage(entity,melee.target,melee.damage,targetPosition.sub(physics.getPosition()).angle());
				melee.time = 0;
			}
		}
	}

}

@@ -53,120 +53,93 @@
 com.
-badlogic.ashley.core.Entity;%0D%0Aimport com.badlogic.ashley.core.Family;%0D%0Aimport com.badlogic.ashley.systems.Iterat
+artemis.Aspect;%0D%0Aimport com.artemis.Entity;%0D%0Aimport com.artemis.systems.EntityProcess
 ingS
@@ -301,367 +301,62 @@
 ter.
-ComponentMappers;%0D%0Aimport com.deftwun.zombiecopter.App;%0D%0Aimport com.deftwun.zombiecopter.components.ChildComponent;%0D%0Aimport com.deftwun.zombiecopter.components.ControllerComponent;%0D%0Aimport com.deftwun.zombiecopter.components.GunComponent;%0D%0Aimport com.deftwun.zombiecopter.components.MeleeComponent;%0D%0Aimport com.deftwun.zombiecopter.components.PhysicsComponent
+App;%0D%0Aimport com.deftwun.zombiecopter.ComponentMappers
 ;%0D%0Ai
@@ -397,35 +397,17 @@
 ponents.
-TimeToLiveComponent
+*
 ;%0D%0A%0D%0Apub
@@ -441,14 +441,21 @@
 nds 
-Iterat
+EntityProcess
 ingS
@@ -673,14 +673,14 @@
 per(
-Family
+Aspect
 .one
@@ -699,16 +699,17 @@
 t.class,
+ 
 MeleeCom
@@ -725,17 +725,10 @@
 ass)
-.get()
 );
-%09
 %0D%0A%09%09
@@ -800,22 +800,16 @@
  process
-Entity
 (Entity 
@@ -818,25 +818,8 @@
 tity
-, float deltaTime
 ) %7B%0D
@@ -1194,33 +1194,35 @@
 gun.time += 
+world.
 delta
-Time
 ;%0D%0A%09%09%09if (gu
@@ -2077,16 +2077,23 @@
 ity.
+edit().
 add(ttl)
 ;%0D%0A%09
@@ -2092,28 +2092,8 @@
 ttl)
-;%0D%0A%09%09%09%09%09bulletEntity
 .add
@@ -2252,17 +2252,20 @@
  += 
+world.
 delta
-Time
+;
 ;%0D%0A%09

Migrate WeaponSystem.java