package stray.util;

import stray.Main;
import stray.world.World;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.Pool.Poolable;

public class Particle implements Poolable {

	float x = 0;
	float y = 0;
	float velox = 0;
	float veloy = 0;
	public float lifetime = -1;
	float prelife = 0;
	boolean destroyOnBlock = false;

	float tintr = 1;
	float tintg = 1;
	float tintb = 1;
	float tinta = 1;

	String texture = "poof";

	public Particle() {

	}

	public Particle setVelocity(float x, float y) {
		velox = x;
		veloy = y;
		return this;
	}

	public Particle setPosition(float x, float y) {
		this.x = x;
		this.y = y;
		return this;
	}

	public Particle setTexture(String t) {
		texture = t;
		return this;
	}

	public Particle setLifetime(float sec) {
		lifetime = sec;
		return this;
	}

	public Particle setPrelife(float sec) {
		prelife = sec;
		return this;
	}

	public Particle setTint(Color c) {
		tintr = c.r;
		tintg = c.g;
		tintb = c.b;
		tinta = c.a;
		return this;
	}

	public Particle setTint(float r, float g, float b, float a) {
		tintr = r;
		tintg = g;
		tintb = b;
		tinta = a;
		return this;
	}
	
	public Particle setAlpha(float a){
		tinta = a;
		return this;
	}
	
	public Particle setDestroyOnBlock(boolean b){
		destroyOnBlock = b;
		return this;
	}

	@Override
	public void reset() {
		x = 0;
		y = 0;
		velox = 0;
		veloy = 0;
		lifetime = -1;
		prelife = 0;
		texture = "poof";
		destroyOnBlock = false;
		setTint(Color.WHITE);
	}

	public void render(World world, Main main) {
		if (prelife <= 0) {
			update(Gdx.graphics.getDeltaTime());

			if (texture != null) {
				if (texture.startsWith("_")) {
					main.font.setColor(tintr, tintg, tintb,
							(lifetime <= 0.1f ? (Math.min(lifetime * 10f, tinta)) : tinta));
					main.drawCentered(texture.substring(1), x * World.tilesizex
							- world.camera.camerax,
							Main.convertY(y * World.tilesizey - world.camera.cameray));
					main.font.setColor(Color.WHITE);
				} else if (texture.startsWith("real-")){
					Texture t = main.manager.get(texture.substring(5), Texture.class);
					main.batch.setColor(tintr, tintg, tintb,
							(lifetime <= 0.1f ? (Math.min(lifetime * 10f, tinta)) : tinta));
					main.batch.draw(
							t,
							x * World.tilesizex - (t.getWidth() / 2) - world.camera.camerax,
							Main.convertY(y * World.tilesizey + (t.getHeight() / 2)
									- world.camera.cameray));
					main.batch.setColor(Color.WHITE);
				}else{
					Texture t = main.manager.get(AssetMap.get(texture), Texture.class);
					main.batch.setColor(tintr, tintg, tintb,
							(lifetime <= 0.1f ? (Math.min(lifetime * 10f, tinta)) : tinta));
					main.batch.draw(
							t,
							x * World.tilesizex - (t.getWidth() / 2) - world.camera.camerax,
							Main.convertY(y * World.tilesizey + (t.getHeight() / 2)
									- world.camera.cameray));
					main.batch.setColor(Color.WHITE);
				}
			}

		}

		if (prelife > 0) {
			prelife -= Gdx.graphics.getDeltaTime();
		} else if (prelife <= 0) {
			lifetime -= Gdx.graphics.getDeltaTime();
		}
		
		if(destroyOnBlock){
			if(MathHelper.intersects((int) x, (int) y, 1, 1, x, y, 8 * World.tilepartx, 8 * World.tileparty)){
				lifetime = -1;
				prelife = -1;
			}
		}

	}

	private void update(float delta) {
		x += velox * delta;
		y += veloy * delta;
	}

}

@@ -1195,33 +1195,32 @@
 return this;%0A%09%7D%0A
-%09
 %0A%09public Particl
@@ -1238,16 +1238,17 @@
 float a)
+ 
 %7B%0A%09%09tint
@@ -1272,17 +1272,16 @@
 his;%0A%09%7D%0A
-%09
 %0A%09public
@@ -1318,16 +1318,17 @@
 olean b)
+ 
 %7B%0A%09%09dest
@@ -2077,16 +2077,17 @@
 real-%22))
+ 
 %7B%0A%09%09%09%09%09T
@@ -2516,20 +2516,22 @@
 );%0A%09%09%09%09%7D
+ 
 else
+ 
 %7B%0A%09%09%09%09%09T
@@ -3118,15 +3118,14 @@
 %09%09%7D%0A
-%09%09
 %0A%09%09if
+ 
 (des
@@ -3140,15 +3140,88 @@
 ock)
+ 
 %7B%0A%09%09%09if
+ (world.getBlock((int) x, (int) y).isSolid(world, (int) x, (int) y)) if 
 (Mat
@@ -3227,16 +3227,22 @@
 thHelper
+%0A%09%09%09%09%09
 .interse
@@ -3295,17 +3295,24 @@
 lepartx,
- 
+%0A%09%09%09%09%09%09%09
 8 * Worl
@@ -3324,16 +3324,17 @@
 eparty))
+ 
 %7B%0A%09%09%09%09li

Fix faulty destroyOnBlock