package me.hammerle.supersnuvi.entity.components.ai;

import me.hammerle.supersnuvi.entity.Entity;
import me.hammerle.supersnuvi.javafx.KeyHandler;
import me.hammerle.supersnuvi.util.SoundUtils;

public class HumanController extends EntityController
{
    public HumanController(Entity ent) 
    {
        super(ent);
    }
    
    @Override
    public boolean isRunning() 
    {
        return KeyHandler.RUN.isDown();
    }

    @Override
    public void tick() 
    {
        if(KeyHandler.JUMP.isDown() && ent.getMovement().jump())
        {
            SoundUtils.playSound(SoundUtils.Sound.JUMP);
        }
        
        double speed = 3.0;
        if(isRunning())
        {
            if(ent.getEnergy().getEnergyPercent() > 0.0)
            {
                speed = 4.5;
            }
            if(ent.isMoving())
            {
                ent.getEnergy().addEnergy(-0.3);
            }
        }
        
        if(KeyHandler.LEFT.isDown())
        {
            ent.setMotionX(-speed * ent.getMovementPenalty().getFactor());
        }
        else if(KeyHandler.RIGHT.isDown())
        {
            ent.setMotionX(speed * ent.getMovementPenalty().getFactor());
        }
        
        if(ent.isOnGround() && ent.getPreviousMotionX() != 0)
        {
            if(ent.getMovementPenalty().isInWater())
            {
                SoundUtils.playSound(SoundUtils.Sound.WALK_WATER);
            }
            else
            {
                SoundUtils.playSound(SoundUtils.Sound.WALK);
            }           
        }
    }
}
