// Tarantula Class
// Tarantula.java

// Imports
import java.awt.*;
import java.applet.Applet;
import java.util.*;

public class Tarantula extends DirectionalSprite {
  // Tarantula action flags (extended sprite action flags, must be > 2)
  public static final int SA_ADDTARANTULA = 3,
                          SA_ADDSPIDERLING = 4,
                          SA_ADDSPIDERCIDE = 5;
  public static Image[][] image;
  protected static Random rand = new Random(
    System.currentTimeMillis());

  public Tarantula(Component comp, Point pos) {
    super(comp, image, 0, 1, 2, pos, new Point(1, 1), 50,
      Sprite.BA_WRAP, 0);
  }

  public static void initResources(Applet app, MediaTracker tracker,
    int id) {
    image = new Image[8][2];
    for (int i = 0; i < 8; i++) {
      for (int j = 0; j < 2; j++) {
        image[i][j] = app.getImage(app.getCodeBase(),
          "Res/Tarant" + i + j + ".gif");
        tracker.addImage(image[i][j], id);
      }
    }
  }

  public BitSet update() {
    // Randomly change direction
    if ((rand.nextInt() % 10) == 0) {
      velocity.x = velocity.y = 1;
      setDirection(direction + rand.nextInt() % 2);
    }

    // Call parent's update()
    BitSet action = super.update();

    // Give birth?
    if (rand.nextInt() % 250 == 0) {
      action.set(Sprite.SA_ADDSPRITE);
      action.set(Tarantula.SA_ADDSPIDERLING);
    }

    // Die?
    if (rand.nextInt() % 250 == 0) {
      action.set(Sprite.SA_KILL);
      action.set(Sprite.SA_ADDSPRITE);
      action.set(Tarantula.SA_ADDSPIDERCIDE);
    }

    return action;
  }

  protected Sprite addSprite(BitSet action) {
    // Add spiderling?
    if (action.get(Tarantula.SA_ADDSPIDERLING))
      return new Spiderling(component, new Point(position.x,
        position.y));

    // Add spidercide?
    else if (action.get(Tarantula.SA_ADDSPIDERCIDE))
      return new Spidercide(component, new Point(position.x,
        position.y));

    return null;
  }
}

