public void AvoidanceRoutine() { print ("Doing Avoidance Routine."); float distanceTraveledDuringAvoidance = 0.0f; //Pretty much, this means we need to save our state, switch to a new set of behaviors, and then return to this one afterwards. SimpleCarProperties temp = new SimpleCarProperties (this); SimpleCarPreservation.Insert (iterationCount, temp); iterationCount = iterationCount + 1; timeSinceChange = 0; distanceTraveled = 0; avoidanceDistanceTraveled = 0; previousDistanceTraveled = temp.distanceTraveled; mainState = State.Avoidance; }
void handleCollision() { //Back up slightly. (Make sure to deduct from distance traveled (beforeAvoidance) & store how much we backed up.) //AvoidObstacle by turning to the right, going (a minimum distance) turn left, and go forward (store this distance traveled duringAvoidance) //If we go forward farther than we backed up (maybe with some fudge amount) - beep //We're at the side of the obstacle. Go forward (a minimum distance), turn left, and go forward (distanceTraveledDuringAvoidance) while tracking distance (subtracting). //If we hit a wall now, do the whole Avoidance thing kinda recursively. //ELSE If we made another collision before (distance traveled duringAvoidance) hit zero... //Do another level of the avoidance thing. if (mainState == State.Base) { //Normally, copypasta is bad, but I'm gonna do it right now because reasons. //Pretty much, this means we need to save our state, switch to a new set of behaviors, and then return to this one afterwards. print ("Going from Base to Avoidance."); SimpleCarProperties temp = new SimpleCarProperties (this); //CAREFUL, replaced distanceTraveled with the minimum for a quick recovery. temp.distanceTraveled = AMINIMUMDISTANCE * 2; SimpleCarPreservation.Insert (iterationCount, temp); iterationCount = iterationCount + 1; timeSinceChange = 0; distanceTraveled = 0; TurnRight (); mainState = State.Avoidance; } else if (mainState == State.Avoidance) { print ("Hit a wall during initial dodge."); //DO SOMETHING!!? //This is the part where we start a new phase of dodging. TurnRight (); AvoidanceRoutine (); } else if (mainState == State.Side) { print ("Wall is still here! From Side redoing Avoidance."); TurnRight (); mainState = State.Avoidance; } else if (mainState == State.Return) { //print("Hit wall during return trip! Avoiding..."); print ("Hit wall during return trip! From Return redoing Side."); TurnRight (); sideDistanceTraveled = 0; mainState = State.Side; //AvoidanceRoutine(); } else if (mainState == State.BackUp) { print ("Car collided when backing up?"); mainState = State.BAD; } else { print ("Fix it later"); } /* else if (distanceTraveled < AMINIMUMDISTANCE) { AvoidanceRoutine(); } else if (distanceTraveled < previousDistanceTraveled) { } */ }