package me.hammerle.supersnuvi.tiles;

import me.hammerle.supersnuvi.javafx.Image;
import me.hammerle.supersnuvi.entity.Entity;
import me.hammerle.supersnuvi.entity.Hero;
import me.hammerle.supersnuvi.util.CollisionBox;
import me.hammerle.supersnuvi.util.Face;
import me.hammerle.supersnuvi.util.Utils;

public class SpikeTile extends Tile
{
    private int counter;
    private int frame;
    private final Image[] images;
    
    public SpikeTile() 
    {
        counter = 0;
        frame = 0;
        images = new Image[8];
        images[0] = Utils.getTileImage("spike_trap/spike_trap_frame5");
        images[1] = images[0];
        images[2] = images[0];
        images[3] = images[0];
        images[4] = Utils.getTileImage("spike_trap/spike_trap_frame1");
        images[5] = Utils.getTileImage("spike_trap/spike_trap_frame2");
        images[6] = Utils.getTileImage("spike_trap/spike_trap_frame3");
        images[7] = Utils.getTileImage("spike_trap/spike_trap_frame4");
        super.setCollisionBox(CollisionBox.createScaledBox(0.1, 0.1, 0.9, 0.9));
    }

    @Override
    public void onEntityCollide(Entity ent, int x, int y, Face face) 
    {
        super.onEntityCollide(ent, x, y, face);
        if(frame >= 4 && ent instanceof Hero)
        {
            ent.addHealth(-0.05);
        }
    }

    @Override
    public Image getImage(int x, int y) 
    {
        return images[frame];
    }

    @Override
    public void tick() 
    {
        counter++;
        if(counter > 20)
        {
            counter = 0;
            frame++;
            if(frame >= images.length)
            {
                frame = 0;
            }
        }
    }
}
