package me.hammerle.supersnuvi.tiles;

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

public class WaterTile extends Tile
{
    private int counter;
    private int frame;
    private final Image[] images;
    
    public WaterTile(boolean full) 
    {
        counter = 0;
        frame = 0;
        images = new Image[8];
        String path;
        if(full)
        {
            path = "water/water_frame";
        }
        else
        {
            path = "water/water_top_frame";
        }
        for(int i = 0; i < 8; i++)
        {
            images[i] = Utils.getTileImage(path + (i + 1));
        }
        super.setCollisionBox(CollisionBox.createScaledBox(0.1, 0.1, 0.9, 0.9));
        super.setMovementBox(CollisionBox.NULL_BOX);
    }

    @Override
    public void onEntityCollide(Entity ent, int x, int y, Face face) 
    {
        super.onEntityCollide(ent, x, y, face);
        
        ent.setInWater(true);
        
        double motionY = ent.getMotionY();
        if(motionY < 0)
        {
            motionY *= 0.92d;
            if(Math.abs(motionY) < 0.1)
            {
                motionY = 0;
            }
            ent.setMotionY(motionY);
        }
    }

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

    @Override
    public void tick() 
    {
        counter++;
        if(counter > 12)
        {
            counter = 0;
            frame++;
            if(frame >= 8)
            {
                frame = 0;
            }
        }
    }
}
