package edu.kit.mindstormer.program.implementation;

import lejos.hardware.Button;
import lejos.hardware.Key;
import edu.kit.mindstormer.movement.Movement;
import edu.kit.mindstormer.movement.State;
import edu.kit.mindstormer.program.AbstractProgram;
import edu.kit.mindstormer.sensor.Sensor;

public class RollerBox extends AbstractProgram {

	private final int speed = 34;
	private final int rotationSpeed = speed / 2;

	public void run() {
		// increase distance from wall
		setBack(10);

		// rotate in order to drive through roller box backwards
		turnAround();

		// drive into roller box
		setBack(20);

		// drive through roller box
		driveThroughBox();

		// drive out of roller box
		driveOut();

		// reposition
		turnAround();

		Button.ESCAPE.simulateEvent(Key.KEY_RELEASED);
	}

	private void driveOut() {
		Movement.moveDistance(-25, speed);
		while (!State.stopped(true, true)) {
		}
	}

	private void driveThroughBox() {
		Movement.move(-speed);
		while (!State.stopped(true, true))
			if (Sensor.sampleDistance() > 20)
				break;
		Movement.stop();
	}

	private void turnAround() {
		Movement.rotate(180, rotationSpeed);
		while (!State.stopped(true, true)) {
		}
	}

	private void setBack(int distance) {
		Movement.moveDistance(-distance, speed);
		while (!State.stopped(true, true)) {
		}
	}
}

@@ -368,10 +368,10 @@
 d = 
-34
+50
 ;%0A%09p
@@ -407,17 +407,10 @@
 d = 
-speed / 2
+30
 ;%0A%0A%09
@@ -832,17 +832,17 @@
 tance(-2
-5
+0
 , speed)
@@ -1022,16 +1022,39 @@
 () %3E 20)
+ %7B%0A%09%09%09%09Movement.stop();
 %0A%09%09%09%09bre
@@ -1057,16 +1057,21 @@
 %09break;%0A
+%09%09%09%7D%0A
 %09%09Moveme
@@ -1069,29 +1069,47 @@
 %0A%09%09Movement.
-stop(
+moveDistance(-10, speed
 );%0A%09%7D%0A%0A%09priv

Fix roller box program