package com.pesets.game;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.pesets.resources.ResourceManager;

public class FourWaySprite extends Actor {
	public static int DirectionDown = 0;
	public static int DirectionLeft = 2;
	public static int DirectionRight = 1;
	public static int DirectionUp = 3;
	
	
	private TextureRegion[][] sprites;
	private float frame = 0;
	private int direction = 0;
	
	public FourWaySprite(String name, int width, int height)
	{
		Texture tex = ResourceManager.getAssetManager().get(ResourceManager.textureFolder + name, Texture.class);
		sprites = TextureRegion.split(tex, tex.getWidth() / 4, tex.getHeight() / 4);
		this.width = width;
		this.height = height;
	}
	
	@Override
	public void draw(SpriteBatch batch, float parentAlpha) {
		batch.draw(sprites[direction][(int) frame], x - width / 2, y - 16, width, height);
	}
	
	public void setPos(float x, float y)
	{
		this.x = x;
		this.y = y;
	}

	public void update(float dt)
	{
		frame = (frame + dt * 4) % 4;
	}
	
	@Override
	public Actor hit(float x, float y) {
		return null;
	}

	public int getDirection() {
		return direction;
	}

	public void setDirection(int direction) {
		this.direction = direction;
	}
	
	public void setDirectionToPos(float vx, float vy)
	{
		float dx = vx - x;
		float dy = vy - y;
		if (Math.abs(dx) > Math.abs(dy))
		{
			if (dx < 0)
				setDirection(FourWaySprite.DirectionRight);
			else
				setDirection(FourWaySprite.DirectionLeft);
		}
		else
		{

			if (dy > 0)
				setDirection(FourWaySprite.DirectionUp);
			else
				setDirection(FourWaySprite.DirectionDown);
		}
	}

	public void setDirectionToPos(Vector2 v) {
		setDirectionToPos(v.x, v.y);
		
	}

}
