package net.greencouchgames.ld27;

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

public class u_Projectiles {
	public static ArrayList<e_Projectile> p = new ArrayList<e_Projectile>();
	
	public static e_Projectile addBakedGood(float x,float y) {
		e_Projectile pr = new e_Projectile(x,y,false,0);
		p.add(pr);
		return pr;
	}
	public static e_Projectile addTankShell(float x,float y,float a) {
		e_Projectile pr = new e_Projectile(x,y,true,a);
		p.add(pr);
		return pr;
	}
	
	public static void updateProjectiles(float d) {
		if (p.size()<=0) return;
		for (Iterator<e_Projectile> pri = p.iterator(); pri.hasNext(); ) {
			e_Projectile pr = pri.next();
			if (!pr.grabbed) {
				if (pr.update(d)) {
					pr.spr.detachSelf();
					pr.spr.dispose();
					pri.remove();
					continue;
				}
			}
		}
	}
	
	public static e_Projectile testRange(float x, float y, float range, boolean thrown, int num, boolean shellOnly) {
		for (e_Projectile pr : p) {
			float xd = x-pr.spr.getX();
			float yd = y-pr.spr.getY();
			if (Math.sqrt(xd*xd+yd*yd)<range && ((pr.thrown && !pr.grabbed) || !thrown) && (pr.thrownbyplayer!=num || num==-2) && (!shellOnly || pr.tankshell)) {
				return pr;
			}
		}
		return null;
	}
	
	public static void removeProjectiles() {
		for (e_Projectile pr : p) {
			pr.spr.detachSelf();
			pr.spr.dispose();
		}
		p.clear();
	}
}
