import java.awt.*;
import java.applet.*;


public class Horde {
   static int MAX_BILLS  = 100;	/*max Bills per level*/
   static int WCELS = 4;	/* # of bill walking animation frames*/
   static int DCELS = 5;	/* # of bill dying animation frames*/
   static int ACELS = 13;	/* # of bill switching OS frames*/
   Monster[] list=new Monster[MAX_BILLS];	/* list of monsters in all states*/
   int width, height;
   int on_screen, off_screen;
   Picture[] lcels, rcels, acels, dcels;
   JBill2 game;
   
   public Horde(JBill2 a) {
      game=a;
      for (int i=0;i<MAX_BILLS;i++) {
	 list[i] = new Monster(game);
      }
   }
   
   void setup() {
      for (int i=0; i < MAX_BILLS; i++)
	list[i].state = list[i].OFF;
      off_screen = on (game.level);
      on_screen = 0;
   }
   
   /*  Launches Bills whenever called  */
   void launch(int max){
      int i, n;
      if ((max==0) || (off_screen==0)) return;
      n=game.RAND(1, game.MIN(max, off_screen));
      for (i=0; n!=0; n--) {
	 for (/*i++*/; i < MAX_BILLS; i++)
	   if (list[i].state == list[i].OFF) break;
	 if (i == MAX_BILLS) return;
	 list[i++].enter();
      }
   }
   
   int on (int lev) {
      return game.MIN(8+3*lev, MAX_BILLS);
   }

   int max_at_once (int lev) {
      return game.MIN(2+lev/4, 12);
   }

   int between (int lev) {
      return game.MAX(14-lev/3, 10);
   }

   void load_pix () {
      int i;
      lcels = new Picture[WCELS];
      rcels = new Picture[WCELS];
      acels = new Picture[ACELS];
      dcels = new Picture[DCELS];
	
      for (i=0; i < WCELS-1; i++) {
	 lcels[i] = new Picture(game);
	 rcels[i] = new Picture(game);
	 lcels[i].load("billL", i);
	 rcels[i].load("billR", i);
      }
      lcels[WCELS-1] = lcels[1]; 
      rcels[WCELS-1] = rcels[1]; 
      
      for (i=0; i < DCELS; i++) {
	 dcels[i] = new Picture(game);
	 dcels[i].load("billD", i);
      }
      width=dcels[0].width;
      height=dcels[0].height;
      
      for (i=0; i < ACELS; i++) {
	 acels[i] = new Picture(game);	 
	 acels[i].load("billA", i);
      }
   }

   void update() {
      int i;
      if ((game.iteration % between(game.level))==0)
	launch(max_at_once(game.level));
      for (i=0; i < MAX_BILLS; i++) 
	list[i].update();
   }
   
   void draw() {
      int i;
      for (i=0; i < MAX_BILLS; i++) 
	list[i].draw();
   }
} 
