package net.greencouchgames.ld27;

import org.andengine.entity.sprite.Sprite;

public class e_Enemy {
	public Sprite spr;
	public Sprite spr2;
	public Sprite spr3;
	
	public int dead = 0;
	public boolean deadvis = true;
	public boolean moved = false;
	public float timer = 10;
	public int count = 9;
	public int next = 8;
	
	public int speed = 120;
	
	public int d = 1;
	public boolean forcedestroy = false;
	
	public e_Enemy(float x, float y, boolean isTank, int dir) {
		x-=32;
		y-=32;
		if (isTank) {
			if (dir==1) d=-1;
			x-=32;
			y-=32;
			spr = new Sprite(x, y, Main.spr_tank, Main.root.getVertexBufferObjectManager());
			spr.setRotationCenter(64,64);
			((s_Game) Main.root.scene).layer_tank.attachChild(spr);
			spr2 = new Sprite(x+47, y+32, Main.spr_tankcannon, Main.root.getVertexBufferObjectManager());
			spr2.setRotationCenter(16.490f,32);
			spr2.setScaleCenter(16.490f,32);
			((s_Game) Main.root.scene).layer_tank.attachChild(spr2);
			spr3 = new Sprite(x+32, y-96, Main.spr_count10, Main.root.getVertexBufferObjectManager());
			spr3.setScaleCenter(32,128);
			spr3.setScale(0);
			((s_Game) Main.root.scene).layer_hud.attachChild(spr3);
		} else {
			spr = new Sprite(x, y, Main.spr_enemy, Main.root.getVertexBufferObjectManager());
			spr.setRotationCenter(32,32);
			((s_Game) Main.root.scene).layer_play.attachChild(spr);
		}
	}
	
	public boolean update(float t) {
		if (spr2!=null) {
			float a = (float)Math.atan2(spr.getY()+64-(((s_Game) Main.root.scene).player.y+32), -(spr.getX()+64-(((s_Game) Main.root.scene).player.x+32)));
			spr2.setRotation(a*((float)-180/(float)Math.PI));
			
			timer-=t;
			spr3.setScale(Math.abs((timer%1)-1));
			if (timer<0) {
				u_Enemy.stop=true;
				return true;
			} else {
				if (timer<count) {
					spr3.detachSelf();
					spr3.dispose();
					if (count==9) spr3 = new Sprite(0, 0, Main.spr_count9, Main.root.getVertexBufferObjectManager());
					else if (count==8) spr3 = new Sprite(0, 0, Main.spr_count8, Main.root.getVertexBufferObjectManager());
					else if (count==7) spr3 = new Sprite(0, 0, Main.spr_count7, Main.root.getVertexBufferObjectManager());
					else if (count==6) spr3 = new Sprite(0, 0, Main.spr_count6, Main.root.getVertexBufferObjectManager());
					else if (count==5) spr3 = new Sprite(0, 0, Main.spr_count5, Main.root.getVertexBufferObjectManager());
					else if (count==4) spr3 = new Sprite(0, 0, Main.spr_count4, Main.root.getVertexBufferObjectManager());
					else if (count==3) spr3 = new Sprite(0, 0, Main.spr_count3, Main.root.getVertexBufferObjectManager());
					else if (count==2) spr3 = new Sprite(0, 0, Main.spr_count2, Main.root.getVertexBufferObjectManager());
					else spr3 = new Sprite(0, 0, Main.spr_count1, Main.root.getVertexBufferObjectManager());
					spr3.setScaleCenter(32,128);
					spr3.setScale(0);
					((s_Game) Main.root.scene).layer_hud.attachChild(spr3);
					count--;
				}
				if (timer<next) {
					next-=2;
					Main.tankshoot.play();
					float fa = spr2.getRotation() / ((float)-180/(float)Math.PI) +(float)Math.PI/(float)2;
					float fx = spr2.getRotationCenterX()+spr2.getX()+spr2.getRotationCenterX()+(float)Math.sin(fa)*48;
					float fy = spr2.getRotationCenterY()+spr2.getY()-spr2.getRotationCenterY()+(float)Math.cos(fa)*48+32;
					u_Projectiles.addTankShell(fx,fy,fa);
				}
			}
			
			float nx = spr.getX()+60*t*d;
			spr.setX(nx);
			spr2.setPosition(nx+47,spr.getY()+32);
			spr3.setPosition(nx+32,spr.getY()-96);
			moved = true;
			
			e_Projectile p = u_Projectiles.testRange(spr.getX()+48, spr.getY()+48, 64, true, -1, true);
			if (p!=null) {
				p.forcedestroy=true;
				Main.tankdie.play();
				Main.tankmusic.pause();
				Main.tankmusic.seekTo(0);
				e_Enemy e = u_Enemy.testRange(nx, spr.getY(), 256, this, false, true);
				while (e!=null) {
					float tempx = e.spr.getX();
					float tempy = e.spr.getY();
					float tempr = e.spr.getRotation();
					e.spr.detachSelf();
					e.spr.dispose();
					e.spr = new Sprite(tempx, tempy, Main.spr_enemydown, Main.root.getVertexBufferObjectManager());
					e.spr.setRotationCenter(32,32);
					e.spr.setRotation(tempr);
					((s_Game) Main.root.scene).layer_part.attachChild(e.spr);
					e.dead=30;
					e = u_Enemy.testRange(nx, spr.getY(), 256, this, false, true);
				}
				e_Player pl = ((s_Game) Main.root.scene).player; 
				if (Math.sqrt(Math.pow((spr.getX()+64)-(pl.x+32), 2)+Math.pow((spr.getY()+64)-(pl.y+32), 2))<256) {
					pl.dodgetimer=29;
					pl.dodgecool=29;
					pl.dmgtimer=29;
					pl.dodgestartx=pl.x;
					pl.dodgestarty=pl.y;
					float apl = (float)Math.atan2(pl.y+32-(spr.getY()+64), pl.x+32-(spr.getX()+64))+(float)Math.PI/2;
					pl.dodgeendx=pl.x+(float)Math.sin(apl)*100;
					pl.dodgeendy=pl.y-(float)Math.cos(apl)*100;
					pl.dodgeangle=((apl+(float)Math.PI/3*2) * ((float)-180/(float)Math.PI));
				}
				u_Explosion.boom(spr.getX()+64, spr.getY()+64);
				return true;
			} else {
				p = u_Projectiles.testRange(spr.getX()+48, spr.getY()+48, 64, true, -1, false);
				if (p!=null) {
					p.thrown = false;
					p.xs *= -0.5f;
					p.ys *= -0.5f;
				}
			}
		} else {
			if (dead<=0) {
				float a = (float)Math.atan2(spr.getY()-((s_Game) Main.root.scene).player.y, -(spr.getX()-((s_Game) Main.root.scene).player.x));
				float nx = spr.getX()-(float)Math.sin(a-(float)Math.PI/2)*speed*t;
				float ny = spr.getY()-(float)Math.cos(a-(float)Math.PI/2)*speed*t;
				e_Enemy e = u_Enemy.testRange(nx, ny, 64, this, true, false);
				int enemies = 0;
				while (e!=null && enemies<4) {
					float a2 = (float)Math.atan2(spr.getY()-e.spr.getY(), -(spr.getX()-e.spr.getX()));
					nx += (float)Math.sin(a2-(float)Math.PI/2);
					ny += (float)Math.cos(a2-(float)Math.PI/2);
					e = u_Enemy.testRange(nx, ny, 64, this, true, false);
					enemies++;
				}
				spr.setPosition(nx,ny);
				spr.setRotation(a*((float)-180/(float)Math.PI));
				moved = true;
				
				e_Projectile p = u_Projectiles.testRange(spr.getX()+16, spr.getY()+16, 32, true, -2, false);
				if (p!=null || forcedestroy) {
					Main.enemydie.play();
					float tempx = spr.getX();
					float tempy = spr.getY();
					float tempr = spr.getRotation();
					spr.detachSelf();
					spr.dispose();
					spr = new Sprite(tempx, tempy, Main.spr_enemydown, Main.root.getVertexBufferObjectManager());
					spr.setRotationCenter(32,32);
					spr.setRotation(tempr);
					((s_Game) Main.root.scene).layer_part.attachChild(spr);
					dead=30;
				}
			} else {
				spr.setVisible(deadvis);
				deadvis = !deadvis;
				dead--;
				if (dead<=0) return true;
			}
		}
		return false;
	}
}
