BackBumper.java
package prey;
 
import java.util.Random;
 
public class BackBumper extends Behavior {
 
    private Random rand = new Random();
 
    public BackBumper(String name, int priority) {
        // Calls the super constructor. NOTE: selfInterruptable=true. 
        super(name, priority, true);
    }
 
    @Override
    public int getMotivation() {
        // If the touchsensor in the back is pushed avoiding something in the back is most important.
        if (Sensors.getTouchSensorValue()) {
            return 100; 
        } else if (isRunning()) {
            // If the robot is avoiding the motivation is 50. 
            return 50; 
        } else {
            return 0;
        }
    }
 
    @Override
    public void action() throws InterruptBehaviorException {
        // To avoid something behind the robot, the robot drives forward for a bit.
        Mover.forward(75, 75);
        SoundPlayer.playBackBumperSound(this, 500 + rand.nextInt(500));
        actionDone();
    }
 
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License