package net.greencouchgames.ld27;

import javax.microedition.khronos.opengles.GL10;

import org.andengine.entity.sprite.Sprite;
import org.andengine.util.color.Color;

public class e_Explosion {
	Sprite light1;
	Sprite light2;
	Sprite light3;
	Sprite smoke1;		float s1x;	float s1y;
	Sprite smoke2;		float s2x;	float s2y;
	Sprite smoke3;		float s3x;	float s3y;
	Sprite circle1;		float c1x;	float c1y;
	Sprite circle2;		float c2x;	float c2y;
	Sprite circle3;		float c3x;	float c3y;
	Sprite circle4;		float c4x;	float c4y;
	Sprite circle5;		float c5x;	float c5y;
	Sprite circle6;		float c6x;	float c6y;
	Sprite circle7;		float c7x;	float c7y;
	Sprite circle8;		float c8x;	float c8y;
	Sprite circle9;		float c9x;	float c9y;
	Sprite circle10;	float c10x;	float c10y;
	Sprite circle11;	float c11x;	float c11y;
	Sprite circle12;	float c12x;	float c12y;
	Sprite circle13;	float c13x;	float c13y;
	Sprite circle14;	float c14x;	float c14y;
	Sprite circle15;	float c15x;	float c15y;
	Sprite circle16;	float c16x;	float c16y;
	Sprite circle17;	float c17x;	float c17y;
	Sprite circle18;	float c18x;	float c18y;
	Sprite circle19;	float c19x;	float c19y;
	Sprite circle20;	float c20x;	float c20y;
	
	float timer = 0;
	int lightsoff = 0;
	float x;
	float y;
	
	public e_Explosion(float xp, float yp) {
		x=xp;
		y=yp;
		
		circle1 = new Sprite(x-8, y-8, Main.spr_circle, Main.root.getVertexBufferObjectManager());
		circle(circle1);
		circle2 = new Sprite(x-8, y-8, Main.spr_circle, Main.root.getVertexBufferObjectManager());
		circle(circle2);
		circle3 = new Sprite(x-8, y-8, Main.spr_circle, Main.root.getVertexBufferObjectManager());
		circle(circle3);
		circle4 = new Sprite(x-8, y-8, Main.spr_circle, Main.root.getVertexBufferObjectManager());
		circle(circle4);
		circle5 = new Sprite(x-8, y-8, Main.spr_circle, Main.root.getVertexBufferObjectManager());
		circle(circle5);
		circle6 = new Sprite(x-8, y-8, Main.spr_circle, Main.root.getVertexBufferObjectManager());
		circle(circle6);
		circle7 = new Sprite(x-8, y-8, Main.spr_circle, Main.root.getVertexBufferObjectManager());
		circle(circle7);
		circle8 = new Sprite(x-8, y-8, Main.spr_circle, Main.root.getVertexBufferObjectManager());
		circle(circle8);
		circle9 = new Sprite(x-8, y-8, Main.spr_circle, Main.root.getVertexBufferObjectManager());
		circle(circle9);
		circle10 = new Sprite(x-8, y-8, Main.spr_circle, Main.root.getVertexBufferObjectManager());
		circle(circle10);
		circle11 = new Sprite(x-8, y-8, Main.spr_circle, Main.root.getVertexBufferObjectManager());
		circle(circle11);
		circle12 = new Sprite(x-8, y-8, Main.spr_circle, Main.root.getVertexBufferObjectManager());
		circle(circle12);
		circle13 = new Sprite(x-8, y-8, Main.spr_circle, Main.root.getVertexBufferObjectManager());
		circle(circle13);
		circle14 = new Sprite(x-8, y-8, Main.spr_circle, Main.root.getVertexBufferObjectManager());
		circle(circle14);
		circle15 = new Sprite(x-8, y-8, Main.spr_circle, Main.root.getVertexBufferObjectManager());
		circle(circle15);
		circle16 = new Sprite(x-8, y-8, Main.spr_circle, Main.root.getVertexBufferObjectManager());
		circle(circle16);
		circle17 = new Sprite(x-8, y-8, Main.spr_circle, Main.root.getVertexBufferObjectManager());
		circle(circle17);
		circle18 = new Sprite(x-8, y-8, Main.spr_circle, Main.root.getVertexBufferObjectManager());
		circle(circle18);
		circle19 = new Sprite(x-8, y-8, Main.spr_circle, Main.root.getVertexBufferObjectManager());
		circle(circle19);
		circle20 = new Sprite(x-8, y-8, Main.spr_circle, Main.root.getVertexBufferObjectManager());
		circle(circle20);

		smoke1 = new Sprite(x-128, y-128, Main.spr_ex2, Main.root.getVertexBufferObjectManager());
		smoke1.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		((s_Game) Main.root.scene).layer_hud.attachChild(smoke1);
		smoke2 = new Sprite(x-128, y-128, Main.spr_ex2, Main.root.getVertexBufferObjectManager());
		smoke2.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		((s_Game) Main.root.scene).layer_hud.attachChild(smoke2);
		smoke3 = new Sprite(x-128, y-128, Main.spr_ex2, Main.root.getVertexBufferObjectManager());
		smoke3.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		((s_Game) Main.root.scene).layer_hud.attachChild(smoke3);
		
		light1 = new Sprite(x-145, y-145, Main.spr_ex1, Main.root.getVertexBufferObjectManager());
		((s_Game) Main.root.scene).layer_hud.attachChild(light1);
		light2 = new Sprite(x-145, y-145, Main.spr_ex1, Main.root.getVertexBufferObjectManager());
		((s_Game) Main.root.scene).layer_hud.attachChild(light2);
		light3 = new Sprite(x-145, y-145, Main.spr_ex1, Main.root.getVertexBufferObjectManager());
		((s_Game) Main.root.scene).layer_hud.attachChild(light3);
		
		double a = Math.random()*Math.PI*2;
		double d = Math.random()*128;
		s1x = (float) (Math.sin(a)*d);	s1y = (float) (Math.cos(a)*d);
		a = Math.random()*Math.PI*2;	d = Math.random()*128;
		s2x = (float) (Math.sin(a)*d);	s2y = (float) (Math.cos(a)*d);
		a = Math.random()*Math.PI*2;	d = Math.random()*128;
		s3x = (float) (Math.sin(a)*d);	s3y = (float) (Math.cos(a)*d);
		a = Math.random()*Math.PI*2;	d = Math.random()*512;
		c1x = (float) (Math.sin(a)*d);	c1y = (float) (Math.cos(a)*d);
		a = Math.random()*Math.PI*2;	d = Math.random()*512;
		c2x = (float) (Math.sin(a)*d);	c2y = (float) (Math.cos(a)*d);
		a = Math.random()*Math.PI*2;	d = Math.random()*512;
		c3x = (float) (Math.sin(a)*d);	c3y = (float) (Math.cos(a)*d);
		a = Math.random()*Math.PI*2;	d = Math.random()*512;
		c4x = (float) (Math.sin(a)*d);	c4y = (float) (Math.cos(a)*d);
		a = Math.random()*Math.PI*2;	d = Math.random()*512;
		c5x = (float) (Math.sin(a)*d);	c5y = (float) (Math.cos(a)*d);
		a = Math.random()*Math.PI*2;	d = Math.random()*512;
		c6x = (float) (Math.sin(a)*d);	c6y = (float) (Math.cos(a)*d);
		a = Math.random()*Math.PI*2;	d = Math.random()*512;
		c7x = (float) (Math.sin(a)*d);	c7y = (float) (Math.cos(a)*d);
		a = Math.random()*Math.PI*2;	d = Math.random()*512;
		c8x = (float) (Math.sin(a)*d);	c8y = (float) (Math.cos(a)*d);
		a = Math.random()*Math.PI*2;	d = Math.random()*512;
		c9x = (float) (Math.sin(a)*d);	c9y = (float) (Math.cos(a)*d);
		a = Math.random()*Math.PI*2;	d = Math.random()*512;
		c10x = (float) (Math.sin(a)*d);	c10y = (float) (Math.cos(a)*d);
		a = Math.random()*Math.PI*2;	d = Math.random()*512;
		c11x = (float) (Math.sin(a)*d);	c11y = (float) (Math.cos(a)*d);
		a = Math.random()*Math.PI*2;	d = Math.random()*512;
		c12x = (float) (Math.sin(a)*d);	c12y = (float) (Math.cos(a)*d);
		a = Math.random()*Math.PI*2;	d = Math.random()*512;
		c13x = (float) (Math.sin(a)*d);	c13y = (float) (Math.cos(a)*d);
		a = Math.random()*Math.PI*2;	d = Math.random()*512;
		c14x = (float) (Math.sin(a)*d);	c14y = (float) (Math.cos(a)*d);
		a = Math.random()*Math.PI*2;	d = Math.random()*512;
		c15x = (float) (Math.sin(a)*d);	c15y = (float) (Math.cos(a)*d);
		a = Math.random()*Math.PI*2;	d = Math.random()*512;
		c16x = (float) (Math.sin(a)*d);	c16y = (float) (Math.cos(a)*d);
		a = Math.random()*Math.PI*2;	d = Math.random()*512;
		c17x = (float) (Math.sin(a)*d);	c17y = (float) (Math.cos(a)*d);
		a = Math.random()*Math.PI*2;	d = Math.random()*512;
		c18x = (float) (Math.sin(a)*d);	c18y = (float) (Math.cos(a)*d);
		a = Math.random()*Math.PI*2;	d = Math.random()*512;
		c19x = (float) (Math.sin(a)*d);	c19y = (float) (Math.cos(a)*d);
		a = Math.random()*Math.PI*2;	d = Math.random()*512;
		c20x = (float) (Math.sin(a)*d);	c20y = (float) (Math.cos(a)*d);
	}
	
	public void circle(Sprite s) {
		s.setScaleCenter(8,8);
		s.setScale(0.5f+(float)Math.random());
		s.setColor(new Color(1,(float)Math.random(),0));
		s.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		((s_Game) Main.root.scene).layer_hud.attachChild(s);
	}

	public boolean update(float t) {
		timer += t;
		if (timer>1) {
			light1.detachSelf();	light1.dispose();
			light2.detachSelf();	light2.dispose();
			light3.detachSelf();	light3.dispose();
			smoke1.detachSelf();	smoke1.dispose();
			smoke2.detachSelf();	smoke2.dispose();
			smoke3.detachSelf();	smoke3.dispose();
			circle1.detachSelf();	circle1.dispose();
			circle2.detachSelf();	circle2.dispose();
			circle3.detachSelf();	circle3.dispose();
			circle4.detachSelf();	circle4.dispose();
			circle5.detachSelf();	circle5.dispose();
			circle6.detachSelf();	circle6.dispose();
			circle7.detachSelf();	circle7.dispose();
			circle8.detachSelf();	circle8.dispose();
			circle9.detachSelf();	circle9.dispose();
			circle10.detachSelf();	circle10.dispose();
			circle11.detachSelf();	circle11.dispose();
			circle12.detachSelf();	circle12.dispose();
			circle13.detachSelf();	circle13.dispose();
			circle14.detachSelf();	circle14.dispose();
			circle15.detachSelf();	circle15.dispose();
			circle16.detachSelf();	circle16.dispose();
			circle17.detachSelf();	circle17.dispose();
			circle18.detachSelf();	circle18.dispose();
			circle19.detachSelf();	circle19.dispose();
			circle20.detachSelf();	circle20.dispose();
			return true;
		}
		
		if (lightsoff==0) light1.setVisible(false);
		else if (lightsoff==1) light2.setVisible(false);
		else if (lightsoff==2) light3.setVisible(false);
		lightsoff++;
		
		smoke1.setPosition(x+s1x*timer-128,y+s1y*timer-128);
		smoke2.setPosition(x+s2x*timer-128,y+s2y*timer-128);
		smoke3.setPosition(x+s3x*timer-128,y+s3y*timer-128);
		
		circle1.setPosition(x+c1x*timer-8,y+c1y*timer-8);
		circle2.setPosition(x+c2x*timer-8,y+c2y*timer-8);
		circle3.setPosition(x+c3x*timer-8,y+c3y*timer-8);
		circle4.setPosition(x+c4x*timer-8,y+c4y*timer-8);
		circle5.setPosition(x+c5x*timer-8,y+c5y*timer-8);
		circle6.setPosition(x+c6x*timer-8,y+c6y*timer-8);
		circle7.setPosition(x+c7x*timer-8,y+c7y*timer-8);
		circle8.setPosition(x+c8x*timer-8,y+c8y*timer-8);
		circle9.setPosition(x+c9x*timer-8,y+c9y*timer-8);
		circle10.setPosition(x+c10x*timer-8,y+c10y*timer-8);
		circle11.setPosition(x+c11x*timer-8,y+c11y*timer-8);
		circle12.setPosition(x+c12x*timer-8,y+c12y*timer-8);
		circle13.setPosition(x+c13x*timer-8,y+c13y*timer-8);
		circle14.setPosition(x+c14x*timer-8,y+c14y*timer-8);
		circle15.setPosition(x+c15x*timer-8,y+c15y*timer-8);
		circle16.setPosition(x+c16x*timer-8,y+c16y*timer-8);
		circle17.setPosition(x+c17x*timer-8,y+c17y*timer-8);
		circle18.setPosition(x+c18x*timer-8,y+c18y*timer-8);
		circle19.setPosition(x+c19x*timer-8,y+c19y*timer-8);
		circle20.setPosition(x+c20x*timer-8,y+c20y*timer-8);
		
		float a = (timer-1)*-1;
		smoke1.setAlpha(a/(float)2); smoke2.setAlpha(a/(float)2); smoke3.setAlpha(a/(float)2);
		circle1.setAlpha(a); circle2.setAlpha(a); circle3.setAlpha(a);
		circle4.setAlpha(a); circle5.setAlpha(a); circle6.setAlpha(a);
		circle9.setAlpha(a); circle8.setAlpha(a); circle7.setAlpha(a);
		circle10.setAlpha(a); circle11.setAlpha(a); circle12.setAlpha(a); circle13.setAlpha(a);
		circle14.setAlpha(a); circle15.setAlpha(a); circle16.setAlpha(a);
		circle19.setAlpha(a); circle18.setAlpha(a); circle17.setAlpha(a);
		circle20.setAlpha(a);
		
		return false;
	}

}
