import objectdraw.*;

import java.awt.*;

/*
 * An active object with no visible representation that
 * will just create new Leaf objects at fixed intervals.
 */
public class Tree extends ActiveObject { 
  // # of leaves to generate
  private static final int NUM_LEAVES = 150;

  // pause time between leaf creation
  private static final int DELAY_TIME = 900;

  private DrawingCanvas canvas; // the canvas

  private Image fstLeafPic; // pictures of leaves
  private Image sndLeafPic; // pictures of leaves

  // Dimensions of screen
  private int screenHeight;

  // used to pick between the two images
  private RandomIntGenerator leafGen; 

  // used to generate random speeds and positions for leaves
  private RandomIntGenerator leafLocGen; 
  private RandomDoubleGenerator leafSpeedGen; 

  // Remember parameters and create random number generator
  // in order to create leaves in execute method
  public Tree(Image leafPic1, Image leafPic2, int aScreenWidth,
              int aScreenHeight, DrawingCanvas aCanvas) {
    // save the parameters for the "execute" method
    canvas = aCanvas;
    fstLeafPic = leafPic1;
    sndLeafPic = leafPic2;
    screenHeight = aScreenHeight;

    leafGen = new RandomIntGenerator(1, 2);
    leafLocGen = new RandomIntGenerator(0, aScreenWidth);
    leafSpeedGen = new RandomDoubleGenerator(2, 4);

    this.start();
  }

  // Successively create leaves which will fall to ground
  public void run() {
    Image nextLeaf; // Image to be used in next leaf

    int leafCount = 0; // # of leaves generated so far

    while (leafCount < NUM_LEAVES) {

      // pick x location and speed for a new leaf
      double leafX = leafLocGen.nextValue();
      double leafSpeed = leafSpeedGen.nextValue();

      // pick a random leaf picture and then create a leaf
      if (leafGen.nextValue() == 1) {
        nextLeaf = fstLeafPic;
      } else {
        nextLeaf = sndLeafPic;
      }

      new FallingLeaf(canvas, nextLeaf, leafX, leafSpeed, screenHeight);

      pause(DELAY_TIME);

      leafCount = leafCount + 1;
    }
  }
}
