package com.hexabeast.lasermonster;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Vector2;

public class Laser {
	float x;
	float y;
	float dx;
	float dy;
	
	float current = 10;
	float max = 0;
	float timebegin = 0;
	float time = 0;
	
	boolean attacked = false;
	
	boolean isDead = false;
	
	Vector2 vecPos = new Vector2(0,0);
	
	public Laser(float x, float y, float dx, float dy, float timebegin)
	{
		this.x = x;
		this.y = y;
		this.dx = dx;
		this.dy = dy;
		
		vecPos.x = (dx-x);
		vecPos.y = (dy-y);
		
		max = vecPos.len();
		this.timebegin = timebegin;
	}
	
	public void draw()
	{
		time+=Main.delta;
		if(time>timebegin)
		{
			current += Main.delta*4000;
			
			if(current-400>max)
			{
				isDead = true;
			}
			else
			{

				if(current>max && !attacked)
				{
					for(int i = GameScreen.mobs.size()-1; i>=0; i--)
					{
						if(!GameScreen.mobs.get(i).isHalfDead  && !attacked && GameScreen.mobs.get(i).box.contains(x+(vecPos.x), y+(vecPos.y)))
						{
							attacked = true;
							GameScreen.mobs.get(i).isHalfDead = true;
							GameScreen.killNumber++;
							Main.hitSound.play(0.1f);
						}
					}
				}
				
				float factorB = Math.min(Math.max((current-400)/max,0), 1);
				float factorE = Math.min(Math.max(current/max,0), 1);
				
				drawLine(Main.texLaser, x+(vecPos.x)*factorB, y+(vecPos.y)*factorB, x+(vecPos.x)*factorE, y+(vecPos.y)*factorE);
			}
		}
		
	}
	
	public static void drawLine(Texture tex,float x, float y, float x2, float y2)
	{
		Vector2 vec = new Vector2(x2-x,y2-y);
		
		GameScreen.batch.draw(tex, x-tex.getWidth()/2, y, tex.getWidth()/2, 0, tex.getWidth(), vec.len(), 1, 1, vec.angle()-90, 0, 0, tex.getWidth(),  tex.getHeight(), false, false);
		
		 /*Sprite sprite = new Sprite(tex, 0, 0, tex.getWidth(), tex.getHeight());
		 sprite.setColor(GameScreen.batch.getColor());
	     sprite.setOrigin(0, tex.getWidth()/2);
	     sprite.setPosition(x, y);
	     sprite.setRotation( vec.angle()-90);
	     sprite.draw(GameScreen.batch);*/
	}
}
