package visual.dynamic.described;


import java.util.*;
import javax.microedition.lcdui.*;

import geom.*;
import visual.statik.TransformableContent;


/**
 * A character that falls from the top of the screen to the bottom
 * (as in "The Matrix")
 *
 * Note: This is a simple rule-based Sprite.  It does not use key-frames
 * or tweening.  Hence, it has rules in handleTick().
 *
 * Changes in the J2ME Version:
 *
 *     render() was re-written
 *     getContent() is not needed
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0 (J2ME)
 *
 */
public class FallingCharacter extends Sprite
{
    private char                character;
    private int                 green, speed;
    private Rectangle2D         bounds;
    
    private static Font         font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_LARGE);
    private static Random       rng  = new Random();
    
    
    
    /**
     * DefaultConstructor
     *
     */
    public FallingCharacter()
    {
        super();

        character     = (char)(33 + rng.nextInt(95));
        green         = rng.nextInt(255);

        bounds        = new Rectangle2D();
        bounds.x      = rng.nextInt(180);
        bounds.y      = -1 * rng.nextInt(196/2);
        bounds.width  = font.charWidth(character);
        bounds.height = font.getHeight();

        speed         = rng.nextInt(5) + 1;
    }

    
    
    
    /**
     * Handle a tick event (generated by the Stage)
     *
     * @param time  The current time (in milliseconds)
     */
    public void handleTick(int time)
    {
        bounds.y += speed;
        if (bounds.y > 196+30)
        {
            bounds.y = 0;
            bounds.x = rng.nextInt(180);
            speed = rng.nextInt(5) + 1;
        }
    }
    
    
    
    /**
     * Get the bounding rectangle for this Sprite
     *
     * @return  The bounding rectangle
     */
    public Rectangle2D getBounds2D()
    {
        return bounds;
    }
    
    
    public TransformableContent getContent()
    {
        return null;
    }
    
    /**
     * Render the Sprite in its current state
     *
     * @param g  The rendering engine to use
     */
    public void render(Graphics g)
    {
        // Setup the rendering engine
        g.setFont(font);
        g.setColor(0, green, 0);
        
        // Render the character
        g.drawChar(character, bounds.x, bounds.y, Graphics.TOP|Graphics.LEFT);
    }
    

}
