//************************************************************************************
//
// Project: New class FollowWallBehavior extends AgentBehavior
// Authors: Joshua New and Aaron Garrett
// Source : FollowWallBehavior.java
//
//************************************************************************************
//                                    Description
//************************************************************************************
//
// This class is an implementation of the abstract AgentBehavior class. It defines an 
// agent behavior in which the agent follows a wall. First, the agent travels forward 
// from its initial location and orientation until it reaches a wall. It then turns 
// right and continues forward. If a wall is encountered, the agent turns right. If 
// the end of a wall is encountered (i.e., a jutting corner), the agent turns left.
//
//************************************************************************************
//************************************************************************************
//                                  Public Methods
//************************************************************************************
//
// public void run()
//        This method performs a follow-wall behavior 
//        
//************************************************************************************


// Make the necessary imports from the JAS3D.jar library.
import edu.jsu.ksl.jas3d.AgentBehavior;
import edu.jsu.ksl.jas3d.Agent;


// Extend the AgentBehavior class.
public class FollowWallBehavior extends AgentBehavior
{

    //************************************************************************************
    //                                  Public Methods
    //************************************************************************************

    public void run()
    {
	// Get the current agent so that we may move it and query its sensors.
	Agent agent = this.getAgent();
	
	// While it is possible, we should move forward.
	// Note that the moveForward() method both moves the agent, as
	// well as returns whether or not the move was completed. 
	// Therefore, we should loop until either the behavior is stopped
	// (as denoted in the continueBehavior() method) or until the agent
	// can no longer move forward (as denoted in the moveForward() method).
	while (this.continueBehavior() && agent.moveForward())
	{
	    // Pause the agent's behavior so that we can clearly see the
	    // transitions from move to move. This is not necessary to the
	    // execution of the behavior.
	    this.pause(0.5);
	}
	
	// Turn the agent to the right so that we can begin 
	// following the wall.
	agent.turnRight();

	// Always query the AgentBehavior class to determine if we should
	// continue the behavior or if it has been terminated.
	while(this.continueBehavior())
	{
	    if ((agent.readSensor(1) == 1) && (agent.readSensor(3) == 0))
	    { 
		agent.turnRight();
		agent.moveForward();
	    }
	    else if ((agent.readSensor(6)== 1) && (agent.readSensor(7) == 0)) 
	    { 
		agent.turnLeft();
		agent.moveForward();
	    }
	    else 
	    { 
		agent.moveForward();
	    }

	    // Pause the agent's behavior so that we can clearly see the
	    // transitions from move to move. This is not necessary to the
	    // execution of the behavior.
	    this.pause(0.5);
	}

	// Reset this behavior after it has been completed or terminated.	
	this.reset();
    }

    //************************************************************************************
}
