/**
 *  The BouncingTurtle class has a turtle move and bounce off the sides 
 *  of the Display.
 *
 */
import java.awt.*;
import com.otherwise.jurtle.*;

public class BouncingTurtle extends Turtle
{
    
    static int UPDATE_INTERVAL_INCR = 2; // Controls how fast the turtle accelerates as it bounces.
    static int TOTAL_MOVES = 400000;     // Total number of turtle moves before quiting.

    int updateInterval = 10;             // Number of moves between screen updates.
    int updateCount = 0;                 // Number of moves since last update.

    /**
     * Entry point to the Turtle's code.  
     */
    public void runTurtle()
    {
        setHeading( 35 );
        setAutoUpdate( false );

        int numMoves = 0;
        while ( numMoves < TOTAL_MOVES )
        {
            move();
            numMoves = numMoves + 1;
        }

        hideTurtle();
    }


    private void move()
    {
        // Move the turtle
        forward( 1 );

        // Get current location.  If we have hit a wall then turn and increase
        // the speed
        Point pt = getPosition();
        Dimension size = getDisplaySize();

        if ( pt.x < 0 || pt.x > size.width )
        {
            setHeading( 360 - getHeading() );
            updateInterval = updateInterval + UPDATE_INTERVAL_INCR;
        }
        else if ( pt.y < 0 || pt.y > size.height )
        {
            setHeading( 180 - getHeading() );
            updateInterval = updateInterval + UPDATE_INTERVAL_INCR;
        }

        // Increment updateCount.  If the count is greater than speed then
        // update the display.
        updateCount = updateCount + 1;
        if ( updateCount >= updateInterval )
        {
            updateDisplay();
            updateCount = 0;
        }
    }


}

