Mover.java
package prey;
 
import java.util.Random;
 
import lejos.nxt.MotorPort;
 
/**
 * Basecode is copied from Ole Caprani's Car.java
 * 
 */
public class Mover {
    private final static int forward  = 1;
    private final static int backward = 2;
    private final static int stop     = 3;
 
    private static MotorPort leftMotor = MotorPort.C;
    private static MotorPort rightMotor= MotorPort.B;
    private static Random rand = new Random();
    private static boolean movingBackward = false;
 
    private static boolean interrupt = false;
 
    private Mover() {} 
 
    public static void stop() throws InterruptBehaviorException {
        if (interrupt)  { 
            interrupt = false;
            throw new InterruptBehaviorException();
        } 
        movingBackward = false;
 
        leftMotor.controlMotor(0,stop);
        rightMotor.controlMotor(0,stop);
    }
 
    public static void forward(int leftPower, int rightPower) throws InterruptBehaviorException {
        int diff = leftPower - rightPower;
        if (Math.abs(diff) <= 5) {
            HeadTurner.centerHead();
        } else if (diff < 0) {
            HeadTurner.turnLeft();
        } else {
            HeadTurner.turnRight();
        }
 
        if (interrupt)  { 
            interrupt = false;
            throw new InterruptBehaviorException();
        }
        movingBackward = false;
        leftMotor.controlMotor(leftPower,forward);
        rightMotor.controlMotor(rightPower,forward);
    }
 
    public static void backward(int leftPower, int rightPower) throws InterruptBehaviorException {
        if (interrupt)  { 
            interrupt = false;
            throw new InterruptBehaviorException();
        }
        movingBackward = true;
 
        leftMotor.controlMotor(leftPower,backward);
        rightMotor.controlMotor(rightPower,backward);
    }
 
    public static void interruptBehavior()  {
        interrupt = true;
    }
 
    public static void interrupted() throws InterruptBehaviorException {
        if (interrupt) {
            throw new InterruptBehaviorException();
        }
    }
 
    public static void turnInRandomdirection(int power, boolean backwards) throws InterruptBehaviorException {
        if (interrupt)  { 
            interrupt = false;
            throw new InterruptBehaviorException();
        }
 
        int mode = (backwards ? backward : forward);
        if (mode == backward) {
            movingBackward = true;
        } else {
            movingBackward = false;
        }
 
        if (rand.nextBoolean()) {
            leftMotor.controlMotor(power, mode);
            rightMotor.controlMotor(0, stop);
            HeadTurner.turnRight();
        } else {
            rightMotor.controlMotor(power, mode);
            leftMotor.controlMotor(0, stop);
            HeadTurner.turnLeft();
        }
    }
 
    public static boolean isMovingBackward() {
        return movingBackward;
    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License