package net.greencouchgames.ld27;

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

import org.andengine.util.color.Color;

public class u_Trail {
	public static ArrayList<e_Trail> t = new ArrayList<e_Trail>();
	
	public static void doTrail(float x1,float y1,float x2,float y2,Color c,float tm,int n,float s) {
		for (int i=0;i<n;i++) {
			float p = (float)i/(float)n;
			float x = x1+((x2-x1)*p);
			float y = y1+((y2-y1)*p);
			t.add(new e_Trail(x,y,tm,c,p,s));
		}
	}
	public static void doRoundTrail(float x1,float y1,float x2,float y2,float r,float a1,float a2,Color c,float tm,int n,float s) {
		if (Math.abs(a1-a2)>Math.PI) {
			if (a1<a2) a1+=Math.PI*2;
			else a2+=Math.PI*2;
		}
		for (int i=0;i<n;i++) {
			float p = (float)i/(float)n;
			float a = a2+(a1-a2)*p;
			float x = x2+(x1-x2)*p;
			float y = y2+(y1-y2)*p;
			t.add(new e_Trail(x-(float)Math.sin(a)*r,y-(float)Math.cos(a)*r,tm,c,p,s));
		}
	}
	
	public static void updateTrail(float d) {
		if (t.size()<=0) return;
		for (Iterator<e_Trail> tri = t.iterator(); tri.hasNext(); ) {
			e_Trail tr = tri.next();
			tr.timer -= d;
			if (tr.timer<0) {
				tr.spr.detachSelf();
				tr.spr.dispose();
				tri.remove();
				continue;
			}
			float s = tr.scale*(tr.timer/tr.maxtimer)-tr.soff;
			if (s<0) {
				tr.spr.detachSelf();
				tr.spr.dispose();
				tri.remove();
				continue;
			}
			tr.spr.setScale(s);
		}
	}
	
	public static void removeTrail() {
		for (e_Trail tr : t) {
			tr.spr.detachSelf();
			tr.spr.dispose();
		}
		t.clear();
	}
}
