package com.hexabeast.lasermonster;

import java.util.ArrayList;

import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;

public class Mob 
{

	public float x;
	public float y;
	
	public float w = 130;
	public float h = 158;
	
	public boolean attacking = false;
	
	public int isTurned = 1;
	
	public boolean isHalfDead = false;
	public float deadSize = 1;
	
	public boolean isDead = false;
	
	int currentAnim = 1;
	float currentAnimTime = 0;
	float animRate = 0.07f;
	
	float currentFireTime = 0;
	float fireRate = 0.03f;
	
	public Vector2 direction;
	
	public Rectangle box;
	
	public float maxDist = 400;
	
	public ArrayList<Flame> fires = new ArrayList<Flame>();
	
	
	public Mob(int x, int y)
	{
		this.x = x;
		this.y = y;
		if(x>900)isTurned = -1;
		else isTurned = 1;
		
		box = new Rectangle(x+10,y+10,w-50, h-30);
		
		direction = new Vector2(Main.width/2-x-40, Main.height/2-y);
		direction.clamp(150, 150);
		direction.rotate((float) (15*Math.random()-7.5));
		
		maxDist+=(Math.random()*140)-70;
	}
	
	public void draw()
	{
		
		if(!isHalfDead)
		{
			currentAnimTime += Main.delta;
			if(currentAnimTime>animRate)
			{
				currentAnimTime=0;
				currentAnim++;
				if(currentAnim>6)
				{
					currentAnim=1;
					//Main.stepSound.play(0.3f);
				}
				
				
			}
			
			Vector2 distance = new Vector2(Main.width/2-x-40, Main.height/2-y);
			if(distance.len()>maxDist)
			{
				x+=direction.x*Main.delta;
				y+=direction.y*Main.delta;
			}
			else
			{
				attacking = true;
			}
			
			if(attacking)
			{
				currentAnim = 0;
				
				currentFireTime += Main.delta;
				if(currentFireTime>fireRate && !GameScreen.end)
				{
					currentFireTime=0;
					if(isTurned == 1)
					fires.add(new Flame(x+120,y+75));
					else fires.add(new Flame(x+w-120,y+75));
					
					GameScreen.health -= 15*Main.delta;
				}
			}
					
			float offset = 0;
			if(isTurned<0)offset = w;
			GameScreen.batch.draw(Main.texEnnemies[currentAnim], x+offset, y, w*isTurned, h);
			
			
		}
		else
		{
			deadSize-=10*Main.delta;
			
			if(deadSize<=0)
			{
				deadSize = 0;
				if(fires.isEmpty())isDead = true;
			}
			else
			{
				float offset = 0;
				if(isTurned<0)offset = w;
				GameScreen.batch.draw(Main.texEnnemies[currentAnim], x+offset, y+(h-h*deadSize)/2, w*isTurned, h*deadSize);
			}
			
		}
		
		for(int i = fires.size()-1; i>=0; i--)
		{
			fires.get(i).draw();
			if(fires.get(i).isDead)fires.remove(i);
		}
		
		
		
		
		
		box.setPosition(x+20,y+20);
	}
}