package visual.dynamic.described;

import java.util.Random;
import javax.microedition.lcdui.*;

import geom.*;
import visual.statik.TransformableContent;


/**
 * A Balloon that drops to the ground
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0 (J2ME)
 */
public class Balloon extends RuleBasedSprite
{
    private double          left, speed, top;
    private int             maxY, maxX, minY;
    

    private static Random   rng = new Random();
    

    

    /**
     * Explicit Value Constructor
     *
     * @param content     The static content for the balloon
     * @param stageWidth  The width of the Stage
     * @param stageHeight The height of the Stage
     */
    public Balloon(TransformableContent content, 
                   double stageWidth, double stageHeight)
    {
       super(content);
       Rectangle2D        bounds;       

       bounds = content.getBounds2D(false);

       maxX   = (int)(stageWidth  - bounds.getWidth());
       maxY   = (int)(stageHeight);
       minY   = (int)(-bounds.getHeight());       

       left   = rng.nextInt(maxX);
       top    = minY;
       speed  = 1 + rng.nextInt(3);
    }


    /**
     * Handle a tick event (generated by the Stage)
     *
     * @param time  The current time (which is not used)
     */
    public void handleTick(int time)
    {
       Sprite   cupola;

       // Check for an intersection
       cupola = null;       
       if (protagonists.size() > 0) cupola = (Sprite)protagonists.elementAt(0);

       if ((cupola != null) && (intersects(cupola)))
       {
          speed = 0;
          setVisible(false);
       }
       
       // Update the location
       top += speed;
       
       if (top > maxY)
       {
          left   = rng.nextInt(maxX);
          top    = minY;
          speed  = rng.nextInt(10);
       }
       
       
       // Set the location
       setLocation(left, top);
    }
}
