package holo.bolt.entity;

import holo.bolt.render.RenderEntity;
import holo.bolt.world.World;
import holo.bolt.world.map.tile.Tile;

import org.newdawn.slick.geom.Vector2f;

public abstract class Entity
{
	public float[] pos;
	public Vector2f velocity;
	public Hitbox box;
	public Vector2f facing;
	public float health;
	public World world;
	
	public Entity(float[] position, float[] hitboxSize, World world)
	{
		pos = position;
		velocity = new Vector2f(0.0F, 0.0F);
		facing = new Vector2f(1.0F, 0.0F);
		box = new Hitbox(pos, hitboxSize);
		health = maxHealth();
		this.world = world;
	}
	
	public void onUpdate(int delta)
	{
		this.pos[0] += this.velocity.x * delta;
		this.pos[1] += this.velocity.y * delta;
		
		if (velocity.x < 0.001F && velocity.x > -0.001F)
			velocity.x = 0;
		if (velocity.y < 0.001F && velocity.y > -0.001F)
			velocity.y  = 0;
		Vector2f vec = this.velocity.copy().scale(0.05F);
		velocity.sub(vec);
		this.getRender().updateHit(delta);
	}
	
	public void addVelocity(Vector2f vel)
	{
		velocity.add(vel);
	}
	
	public void moveEntity(Vector2f vec)
	{
		pos[0] += vec.x;
		pos[1] += vec.y;
	}
	
	public void setVelocity (Vector2f vel)
	{
		velocity.set(vel);
	}
	
	public void lookat(float[] coords)
	{
		facing.set(coords);
		facing.normalise();
		this.getRender().setImageRotation(facing.getTheta());
	}
	
	public void lookat(Entity ent)
	{
		float[] coords = new float[] {ent.pos[0] - this.pos[0], ent.pos[1] - this.pos[1]};
		facing.set(coords);
		facing.normalise();
		this.getRender().setImageRotation(facing.getTheta());
	}
	
	public void damage(float damage, Vector2f direction)
	{
		health -= damage;
		this.addVelocity(direction);
		this.getRender().onHit(400);
		if(health <= 0)
			this.setDead();
	}
	
	public void heal(float amount)
	{
		if(health + amount < maxHealth())
			health += amount;
		else
			health = maxHealth();
	}
	
	public void setDead()
	{
		world.removeEntity(this);
	}
	
	public float distanceToEntity(Entity ent)
	{
		float x = pos[0] - ent.pos[0];
		float y = pos[1] - ent.pos[1];
		return (float) StrictMath.sqrt(x * x + y * y);
	}
	
	public abstract String getTextureLoc();
	
	public abstract RenderEntity getRender();
	
	public abstract void onCollideWithEntity(Entity ent);
	
	public abstract float maxHealth();
	
	public abstract void onCollideWithTile(Tile t);
	
	public void attack()
	{
		
	}
}
