package visual.dynamic.described;

import java.awt.*;
import java.awt.image.RasterFormatException;
import java.awt.geom.*;
import java.util.Random;

import visual.statik.TransformableContent;


/**
 * A Fish that "swims" around and avoids its "protagonists"
 * (in a simple way)
 *
 * Note: This is a simple rule-based Sprite. 
 * Hence, it has rules in handleTick().
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 *
 */
public class Fish extends RuleBasedSprite
{
    protected double      maxX, maxY, speed, x, y;
    protected Random      rng;
    

    

    /**
     * Explicit Value Constructor
     *
     * @param content   The static visual content
     * @param width     The width of the Stage
     * @param height    The height of the Stage
     */
    public Fish(TransformableContent content,
                double width, double height)
    {
       super(content);
       maxX = width;
       maxY = height;       

       rng = new Random();
       
       x     = rng.nextDouble()*maxX;
       y     = rng.nextInt()*maxY;
       speed = 3.0;
    }

    


    /**
     * Handle a tick event (generated by the Stage)
     *
     * @param time  The current time (which is not used)
     */
    public void handleTick(int time)
    {
       Sprite     shark;

       shark = null;       
       if (protagonists.size() > 0) shark = protagonists.get(0);

       if ((shark != null) && (intersects(shark)))
       {
          speed = 20;
       }

       updateLocation(-50., 3.);
    }



    /**
     * Update the location
     *
     * @param initialLocation  The left-most location
     * @param initialSpeed     The speed to start at
     */
    protected void updateLocation(double initialLocation,
                                  double initialSpeed)
    {
       x += speed;
       
       if (x > (int)maxX)
       {
          x     = initialLocation;
          y     = rng.nextDouble()*maxX;
          speed = initialSpeed;          
       }

       // Set the location
       setLocation(x, y);
    }
    
}
