%include Robot.pde
%include Hit.pde
%include World.pde
Robot robot;
World world;
int mx, my;
void mousePressed() {
mx = mouseX;
my = mouseY;
}
void mouseDragged() {
robot.forward((mouseX - mx)/100.0);
robot.turn(-(mouseY - my)/1000.0);
}
void mouseReleased() {
//robot.stop();
robot.state = "stop";
}
class MyRobot extends Robot {
MyRobot(float x, float y, float r) {
super(x, y, r);
this.state = "start";
}
void brain() {
PImage pic = this.takePicture();
// Show pic on canvas:
image(pic, 500 - 256, 250);
fill(0);
text("getIR(0): " + nf(this.getIR(0), 1, 4), 0, 265);
text("getIR(1): " + nf(this.getIR(1), 1, 4), 0, 280);
text("stalled: " + this.stalled, 0, 295);
text("state: " + this.state, 0, 310);
text("time: " + this.time, 0, 325);
float speed = 3.0;
// Start of good stuff:
if (this.state == "start") {
this.forward(3);
this.state = "s1";
} else if (this.state == "s1") {
if (this.stalled) {
this.state = "s2";
} else {
this.forward(3);
}
} else if (this.state == "s2") {
this.backward(3);
this.state = "s3";
} else if (this.state == "s3") {
if (this.stalled) {
this.forward(3);
this.state = "s4";
} else {
this.backward(3);
}
} else if (this.state == "s4") {
if (this.getIR(0) < 1) {
this.state = "s5";
} else {
this.forward(3);
}
} else if (this.state == "s5") {
this.stop();
}
}
}
void setup() {
// Set up canvas 500, 250 for World, 128 for camera:
size(500, 250 + 128);
// Set rate so it doesn't overwhelm your browser:
frameRate(10);
// Create a simulated world:
world = new World(500, 250);
world.addWall(100, 0, 110, 110);
world.addBox(200, 95, 210, 170, color(255, 0, 255));
world.addBox(250, 95, 260, 130, color(255, 255, 0));
world.addBox(300, 190, 310, 240, color(255, 128, 0));
// Create robot, and add to world:
robot = new MyRobot(400, 100, 0);
world.addRobot(robot);
}
void draw() {
// Clear:
background(255);
// Run simulation for one step:
world.update();
}