package main.entities;

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

public class Snake extends LivingEntity {

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

	private float damage = 0.1f;
	private boolean attackOnce;

	public Snake() {
		loader.generateFigures(32, 32, new int[] { 10, 10, 10, 10, 13, 8 });
		posX = 9f;
		posY = 9f;
		accelleration = 0.006f;
		spriteOffsetY = -0.6f;
		width = 0.65f;
		height = 0.8f;
		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(60);
	}

	@Override
	protected void update() {
		Player player = Main.instance.getWorld().getPlayer();
		if (!actionFigur.aktuellesVerhalten().equals(getDamageAnimationName())) {
			if (lineOfSightClear(player)) {
				if (dist(player) < 1f) {
					zustandSetzen("attack");
					if (actionFigur.aktuelleFigur().aktuellesBild() == 6) {
						if (!attackOnce) {
							attackOnce = true;
							player.takeDamage(damage, this);
						}
					} else {
						attackOnce = false;
					}
				} 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";
	}
}
