package main.entities;

import ea.Vektor;
import main.Main;
import main.SheetLoader;
import main.entities.player.Player;

public class Spider extends LivingEntity {

	private static SheetLoader loader = new SheetLoader("/res/images/spider_sprite_sheet.png");

	public Spider() {
		loader.generateFigures(32, 32, new int[] { 5, 3, 6, 9, 9, 8 });

		width = 0.7f;
		height = 0.8f;
		spriteOffsetY = -0.14f;
		spriteScale = 0.8f;
		posX = 4f;
		posY = 4f;
		mirrored = true;

		actionFigur.neuerZustand(loader.getFigur(0), "idle");
		actionFigur.neuerZustand(loader.getFigur(1), "lost_sight");
		actionFigur.neuerZustand(loader.getFigur(2), "walk");
		actionFigur.neuerZustand(loader.getFigur(3), "attack");
		actionFigur.neuerZustand(loader.getFigur(4), getDeathAnimationName());
		actionFigur.neuerZustand(loader.getFigur(5), getDamageAnimationName());

		loader.getFigur(3).animationsGeschwindigkeitSetzen(80);
		loader.getFigur(5).animationsGeschwindigkeitSetzen(40);
	}

	protected void update() {
		Player player = Main.instance.getWorld().getPlayer();
		if (!actionFigur.aktuellesVerhalten().equals(getDamageAnimationName())) {
			if (lineOfSightClear(player)) {
				if (dist(player) < 1f) {
					zustandSetzen("attack");
				} else {
					Vektor toPlayer = vectorToEntity(player).normiert();
					velX += toPlayer.x * accelleration;
					velY += toPlayer.y * accelleration;
					zustandSetzen("walk");
				}
			} else {
				if (!actionFigur.aktuellesVerhalten().equals("idle")) {
					zustandSetzen("lost_sight");
					if (actionFigur.aktuelleFigur().aktuellesBild() == actionFigur.aktuelleFigur().animation().length - 1) {
						zustandSetzen("idle");
					}
				}
			}
		} else {
			if (actionFigur.aktuelleFigur().aktuellesBild() == actionFigur.aktuelleFigur().animation().length - 1) {
				zustandSetzen("walk");
			}
		}
		this.checkTileCollisions(Main.instance.getWorld().getCurrentMap());
		super.update();
	}

	@Override
	public String getDeathAnimationName() {
		return "die";
	}

	@Override
	public String getDamageAnimationName() {
		return "damage";
	}

}
