package me.hammerle.supersnuvi.entity;

import me.hammerle.supersnuvi.gamelogic.Level;
import me.hammerle.supersnuvi.javafx.Image;

public class AnimatedEntity extends Entity
{
    private int counter;
    private int frame;
    
    protected Image face;
    protected Image[] move;
    protected Image[] moveAir;
    private boolean flipped;
    
    public AnimatedEntity(Level level, double x, double y, double width, double height, double health) 
    {
        super(level, x, y, width, height, health);
        flipped = false;
        counter = 0;
        frame = 0;   
    }

    @Override
    public boolean drawImageFlipped() 
    {
        return flipped;
    }

    @Override
    public void tick() 
    {
        super.tick();
        
        counter++;
        if(counter >= 5)
        {
            counter = 0;
            frame++;
        }
        
        if(frame < 0)
        {
            frame = 0;
        }
    }
    
    @Override
    public Image getImage() 
    {
        double motionX = getPreviousMotionX();
        if(isOnGround())
        {
            setImage(face);
            if(motionX != 0)
            {
                if(frame >= move.length)
                {
                    frame = 0;
                }
                flipped = motionX < 0;
                return move[frame];
            }
            else
            {
                frame = 0;
            }
        }
        else
        {
            setImage(moveAir[0]);
            if(motionX != 0)
            {
                if(frame >= moveAir.length)
                {
                    frame = 0;
                }
                flipped = motionX < 0;
                return moveAir[frame];
            }
            else
            {
                frame = 0;
            }
        }
        return super.getImage();
    }
}
