// @topic T01050 FLTK Graphics
// @brief Class Turtle ( version 1.0 )

#ifndef _TURTLES_H_INCLUDED_
#define _TURTLES_H_INCLUDED_

using namespace Graph_lib;

class Turtle {
    static const int TURTLE_BODY_SIZE = 20; // pixels
    static const int TURTLE_HEAD_SIZE = TURTLE_BODY_SIZE / 2; // pixels
    static const int TURTLE_ANIMATION_PIXELS = 1;

    // the turtle appears in a form of two circles,
    // the body and the head:
    Circle m_cir_body;
    Circle m_cir_head;

    // Animation support -- if distance is zero, there is no movement.
    // Otherwise, distance in pixels to move in response to a single "go" call:
    int m_distance;

public:
    // Constructor
    Turtle( Simple_window& wnd, Point origin )
        :
    m_cir_body( origin, TURTLE_BODY_SIZE ),

    // by default, construct "easterly" head
    m_cir_head(
        Point ( origin.x + TURTLE_HEAD_SIZE, origin.y ),
        TURTLE_HEAD_SIZE
        ),
    m_distance( 0 )
    {
        wnd.attach( m_cir_body );
        m_cir_head.set_fill_color( Color::dark_green );
        wnd.attach( m_cir_head );
    }

    // start moving in the direction the turtle is facing:
    void go( int distance )
    {
        m_distance = distance;
        go();
    }

    // overloaded go starts the animation
    void go()
    {
        Fl::add_timeout( ANIMATION_TIMEOUT, callback_animate, this );
    }

    // core animation function -- invoked by the timer callback
    void animate()
    {
        if ( m_distance == 0 ) {
            // no distance left, so stop
            return;
        }
        m_cir_body.move( TURTLE_ANIMATION_PIXELS, 0 );
        m_cir_head.move( TURTLE_ANIMATION_PIXELS, 0 );
        Fl::redraw();
        --m_distance;
        Fl::repeat_timeout( ANIMATION_TIMEOUT, callback_animate, this );
    }

    // timer call back entry point:
    static void callback_animate( void* pthing )
    {
        // Cast void pointer to the pointer to turtle object:
        Turtle* self = static_cast< Turtle* >( pthing );
        // pass this timer event on to the animation function:
        self->animate();
    }

};//class Turtle

#endif //_TURTLES_H_INCLUDED_