package net.greencouchgames.ld27;

import java.util.ArrayList;
import java.util.Iterator;

public class u_Enemy {
	public static ArrayList<e_Enemy> e = new ArrayList<e_Enemy>();
	public static boolean stop = false;
	
	public static e_Enemy addEnemy(float x,float y) {
		e_Enemy er = new e_Enemy(x,y,false,0);
		e.add(er);
		return er;
	}
	public static e_Enemy addTank(float x,float y,int dir) {
		Main.tankmusic.resume();
		e_Enemy er = new e_Enemy(x,y,true,dir);
		e.add(er);
		return er;
	}
	
	public static void updateEnemies(float d) {
		if (e.size()<=0) return;
		for (e_Enemy er : e) er.moved=false;
		for (Iterator<e_Enemy> eri = e.iterator(); eri.hasNext(); ) {
			e_Enemy er = eri.next();
			if (er.update(d)) {
				er.spr.detachSelf();
				er.spr.dispose();
				er.spr=null;
				((s_Game) Main.root.scene).player.godspeedbar += 0.02f;
				((s_Game) Main.root.scene).score += 20*Main.multi;
				if (er.spr2 != null) {
					er.spr2.detachSelf();
					er.spr2.dispose();
					((s_Game) Main.root.scene).player.godspeedbar += 0.18f;
					((s_Game) Main.root.scene).score += 300*Main.multi;
				}
				if (er.spr3 != null) {
					er.spr3.detachSelf();
					er.spr3.dispose();
				}
				eri.remove();
				if (stop) break;
				continue;
			}
		}
		if (stop) ((s_Game) Main.root.scene).gameover();
	}
	
	public static void removeEnemies() {
		for (e_Enemy er : e) {
			er.spr.detachSelf();
			er.spr.dispose();
		}
		e.clear();
	}
	
	public static e_Enemy testRange(float x, float y, float range, e_Enemy except, boolean movedOnly, boolean noTanks) {
		for (e_Enemy er : e) {
			float xd = x-(er.spr.getX()+((er.spr2==null) ? 0 : 32));
			float yd = y-(er.spr.getY()+((er.spr2==null) ? 0 : 32));
			if (Math.sqrt(xd*xd+yd*yd)<range+((er.spr2==null) ? 0 : 32) && (except==null || er!=except) && (!movedOnly || er.moved || er.spr2!=null) && (!noTanks || er.spr2==null) && er.dead<=0) {
				return er;
			}
		}
		return null;
	}
	
	public static e_Enemy closest(float x, float y, e_Enemy except, boolean movedOnly, boolean noTanks, boolean tanksOnly) {
		double bestvalue = -1;
		e_Enemy bestenemy = null;
		for (e_Enemy er : e) {
			float xd = x-(er.spr.getX()+((er.spr2==null) ? 0 : 32));
			float yd = y-(er.spr.getY()+((er.spr2==null) ? 0 : 32));
			if ((except==null || er!=except) && (!movedOnly || er.moved) && (!noTanks || er.spr2==null) && (!tanksOnly || er.spr2!=null) && er.dead<=0) {
				if (bestvalue==-1 || Math.sqrt(xd*xd+yd*yd)<bestvalue) {
					bestvalue = Math.sqrt(xd*xd+yd*yd);
					bestenemy = er;
				}
			}
		}
		return bestenemy;
	}
}
