EatFood.java
package prey;
 
public class EatFood extends Behavior {    
    /* The hunger is decreased when eating, and this is done on the getMotivation, seeing that it is called periodically.
     * If the hunger was decremented every time the getMotivation was called, it would go from 100 to zero in about one second.
     * To prevent this the eatingCounter increments every time getMotivation is called, and once it reaches maxEatingCount 
     * hunger is decremented.
     */
    private int eatingCounter = 0;
    private int maxEatingCount = 20;
 
    public EatFood(String name, int priority) {
        super(name, priority);
    }
 
    @Override
    public int getMotivation() {
        if (isRunning()) {
            // While the robot is standing on top of food and this behavior is the leading behavior, the robot eats and hunger is decreased. 
            eatingCounter++;
            if (eatingCounter == maxEatingCount) {
                InternalState.decrementHunger();
                eatingCounter = 0;
            }
        }
 
        int colorID = Sensors.getColourSensorValue();
        if (colorID == Sensors.FOODCOLOR) {
            // If the robot is standing on top of food, the motivation depends upon the hunger, ranging from 0 to 90. 
            return (int) (InternalState.getHunger()*0.9);
        } else {
            // If the robot can not see food, there is no point in eating. 
            return 0;
        }
    }
 
    @Override
    public void action() throws InterruptBehaviorException {
        // The eating behavior is standing still an turning the head from left to right. 
        Mover.stop();
 
        HeadTurner.turnFastLeft();
//        delay(500);
        SoundPlayer.playEatFoodSound(this, 500);
 
        HeadTurner.turnFastRight();
//        delay(500);
        SoundPlayer.playEatFoodSound(this, 500);
 
    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License