package me.hammerle.supersnuvi.entity.components;

import me.hammerle.supersnuvi.entity.Entity;

public class MovementPenalty extends Component
{
    public final static MovementPenalty NULL = new MovementPenalty(null);
    
    private double friction;
    private boolean inWater;
    
    public MovementPenalty(Entity ent)
    {
        super(ent);
    }
    
    public final void reset()
    {
        inWater = false;
    }
    
    public final void setInWater(boolean b)
    {
        inWater = b;
    }
    
    public final boolean isInWater()
    {
        return inWater;
    }
    
    public final double getFactor()
    {
        double factor = 1.0;
        if(inWater)
        {
            if(isSlowedByWater())
            {
                factor *= 0.65;
            }
        }
        else if(isSlowedByLand())
        {
            factor *= 0.65;
        }
        return factor;
    }

    public void setFriction(double friction) 
    {
        this.friction = friction;
    }

    public void applyFriction() 
    {
        double motionX = ent.getMotionX();
        if(motionX < 0.0)
        {
            motionX += friction;
            if(motionX > 0.0)
            {
                motionX = 0.0;
            }
            ent.setMotionX(motionX);
        }
        else if(motionX > 0.0)
        {
            motionX -= friction;
            if(motionX < 0.0)
            {
                motionX = 0.0;
            }
            ent.setMotionX(motionX);
        }
    }
    
    //--------------------------------------------------------------------------
    // overwriteable data
    //--------------------------------------------------------------------------
    
    public boolean isSlowedByWater()
    {
        return true;
    }
    
    public boolean isSlowedByLand()
    {
        return true;
    }
}
