package net.greencouchgames.ld27;

import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.util.color.Color;

public class e_Projectile {
	public Sprite spr;
	public TextureRegion sprite;
	public float xs;
	public float ys;
	public int rottype = 0;
	public boolean grabbed = false;
	public boolean thrown = false;
	public int thrownbyplayer = -1;
	public Color trail;
	public float trailsize = 1.5f;

	public boolean tankshell = false;
	public boolean forcedestroy = false;
	public boolean throwlayer = false;
	
	public e_Projectile(float x, float y, boolean isTankShell, float dir) {
		int sprnum = 0;
		if (isTankShell) {
			xs = (float)Math.sin(dir)*600;
			ys = (float)Math.cos(dir)*600;
			sprnum = 3;
			thrown = tankshell = true;
		} else {
			double a = Math.random()*Math.PI*2;
			xs = (float)Math.sin(a)*45;
			ys = (float)Math.cos(a)*45;
			sprnum = (int)Math.floor(Math.random()*3);
		}
		x-=16;
		y-=16;
		switch (sprnum) {
			case 0:	spr = new Sprite(x, y, Main.spr_baked1, Main.root.getVertexBufferObjectManager());
					sprite = Main.spr_baked1;
					rottype = 1;
					trail = new Color(0.78f,0.44f,0.22f);
					((s_Game) Main.root.scene).layer_proj.attachChild(spr);
					break;
			case 1:	spr = new Sprite(x, y, Main.spr_baked2, Main.root.getVertexBufferObjectManager());
					sprite = Main.spr_baked2;
					rottype = 2;
					trail = new Color(1,0.8f,0.66f);
					trailsize = 1;
					((s_Game) Main.root.scene).layer_proj.attachChild(spr);
					break;
			case 2:	spr = new Sprite(x, y, Main.spr_baked3, Main.root.getVertexBufferObjectManager());
					sprite = Main.spr_baked3;
					rottype = 2;
					trail = new Color(0.82f,0.55f,0.37f);
					trailsize = 1;
					((s_Game) Main.root.scene).layer_proj.attachChild(spr);
					break;
			case 3:	spr = new Sprite(x, y, Main.spr_tankshell, Main.root.getVertexBufferObjectManager());
					sprite = Main.spr_tankshell;
					rottype = 2;
					trail = new Color(0.6f,0.6f,0.6f);
					trailsize = 0.5f;
					((s_Game) Main.root.scene).layer_prot.attachChild(spr);
					throwlayer = true;
					break;
		}
		spr.setRotationCenter(16,16);
		if (rottype==1) spr.setRotation((float)(Math.PI*2*Math.random()));
		else if (rottype==2) {
			float a = ((float)Math.atan2(-ys, xs) * ((float)-180/(float)Math.PI));
			spr.setRotation(a);
		}
	}
	
	public boolean update(float t) {
		if (forcedestroy) return true;
		if (!thrown) {
			if (throwlayer) {
				float tempx = spr.getX();
				float tempy = spr.getY();
				float tempr = spr.getRotation();
				spr.detachSelf();
				spr.dispose();
				spr = new Sprite(tempx, tempy, sprite, Main.root.getVertexBufferObjectManager());
				spr.setRotationCenter(16,16);
				spr.setRotation(tempr);
				((s_Game) Main.root.scene).layer_proj.attachChild(spr);
				throwlayer = false;
			}
			xs*=0.99;
			ys*=0.99;
		} else {
			if (!throwlayer) {
				float tempx = spr.getX();
				float tempy = spr.getY();
				float tempr = spr.getRotation();
				spr.detachSelf();
				spr.dispose();
				spr = new Sprite(tempx, tempy, sprite, Main.root.getVertexBufferObjectManager());
				spr.setRotationCenter(16,16);
				spr.setRotation(tempr);
				((s_Game) Main.root.scene).layer_prot.attachChild(spr);
				throwlayer = true;
			}
			float s = (float)Math.sqrt(xs*xs+ys*ys);
			if (s<300) thrown = false;
			u_Trail.doTrail(spr.getX()+8,spr.getY()+8,spr.getX()+xs*t+8,spr.getY()+ys*t+8,trail,0.5f,(int)s/150,trailsize);
			if (rottype==1) spr.setRotation(spr.getRotation()+1);
			if (thrownbyplayer==0) {
				e_Enemy e;
				if (!tankshell) e = u_Enemy.closest(spr.getX()+xs*60+8, spr.getY()+ys*60+8, null, false, true, false);
				else {
					e = u_Enemy.closest(spr.getX()+xs*60+8, spr.getY()+ys*60+8, null, false, false, true);
					if (e==null) e = u_Enemy.closest(spr.getX()+xs*60+8, spr.getY()+ys*60+8, null, false, false, false);
				}
				if (e!=null) {
					float a = (float)Math.atan2(spr.getY()+8-(e.spr.getY()+32+((e.spr2==null) ? 0 : 32)), -(spr.getX()+8-(e.spr.getX()+32+((e.spr2==null) ? 0 : 32))));
					float dir = (float)Math.atan2(-ys,xs);
					if(Math.abs(dir-a)>Math.PI*2) {
						if (dir<a) dir+=Math.PI*2;
						else a+=Math.PI*2;
					}
					if(Math.abs(dir-a)<Math.PI/2) {
						float nxs = -(float)Math.sin(a-(float)Math.PI/2)*s;
						float nys = -(float)Math.cos(a-(float)Math.PI/2)*s;
						xs += (nxs-xs)*(0.012*s/300*Main.multi*((tankshell) ? 2 : 1));
						ys += (nys-ys)*(0.012*s/300*Main.multi*((tankshell) ? 2 : 1));
					}
				}
			}
		}
		if (rottype==2) {
			float a = ((float)Math.atan2(-ys, xs) * ((float)-180/(float)Math.PI));
			spr.setRotation(a);
		}
		spr.setPosition(spr.getX()+xs*t,spr.getY()+ys*t);
		if (Main.root.scene instanceof s_Multi) {
			if (spr.getX()<=-20) {
				((s_Multi)Main.root.scene).score2++;
				return true;
			} else if (spr.getX()>=1300) {
				((s_Multi)Main.root.scene).score1++;
				return true;
			} else if (spr.getX()<=48) xs--;
			else if (spr.getX()>=1232) xs++;
			
			if (spr.getY()>=640 && ys>0) {
				ys *= -1;
				spr.setY(640);
			} else if (spr.getY()<=48 && ys<0) {
				ys *= -1;
				spr.setY(48);
			}
			
			if (spr.getX()>48 && spr.getX()<1232) {
				float n = spr.getX();
				float m = spr.getY();
				boolean c = false;
				while ((n-48)+(m-48)<196) {
					n++; m++;
					c = true;
				}
				if (c && (xs<=0 || ys<=0)) {
					float temp = xs;
					xs = -ys;
					ys = -temp;
					c = false;
				}
				while ((n-48)+(-m+672-48)<196) {
					n++; m--;
					c = true;
				}
				if (c && (xs<=0 || ys>=0)) {
					float temp = xs;
					xs = ys;
					ys = temp;
					c = false;
				}
				while ((-n+1232-48)+(m-48)<196) {
					n--; m++;
					c = true;
				}
				if (c && (xs>=0 || ys<=0)) {
					float temp = xs;
					xs = ys;
					ys = temp;
					c = false;
				}
				while ((-n+1232-48)+(-m+672-48)<196) {
					n--; m--;
					c = true;
				}
				if (c && (xs>=0 || ys>=0)) {
					float temp = xs;
					xs = -ys;
					ys = -temp;
					c = false;
				}
				spr.setPosition(n,m);
			}
		} else {
			if (spr.getX()<-20 || spr.getX()>1300 || spr.getY()<-20 || spr.getY()>740) return true;
		}
		return false;
	}
}
