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

public class GunManager {
    
    private GunSprite gun;               // your gun

    private int gun_width;               // width of gun
    private int gun_height;

    private MissileSprite missile;       // missile

    static int width, height;            // applet dimensions

    private int min_x,max_x;             // min and max x coords

    // for gun movement

    private int gun_min_x,gun_max_x; 
    private int mis_min_x,mis_max_x;
    private int gun_y;
    
    static final int MISSILE_WIDTH = 3;
    static final int MISSILE_HEIGHT = 27;
    static final int MISSILE_SPEED = -27; // missile flies upward

    static final Color MISSILE_COLOR= Color.red;
    
    public GunManager(int width,int height,
		      Image gunImage,
		      Intersect target[],
		      Applet a) 
    {
	this.width = width;
	this.height = height;

	gun = new GunSprite(gunImage,a);
	
	gun_width = gunImage.getWidth(a)/2;
	gun_height = gunImage.getHeight(a);

	System.out.println(gun_width);
	
	gun_y = height - gun_height;

	min_x = gun_width;
	max_x = width - gun_width;

	gun_min_x = 0;

	gun_max_x = width - 2*gun_width;

	mis_min_x = min_x-2;
	mis_max_x = max_x-2;

	gun.setPosition(width/2-gun_width,gun_y);

	missile = new MissileSprite(MISSILE_WIDTH, MISSILE_HEIGHT,
				    MISSILE_COLOR, MISSILE_SPEED,
				    height-gun_height,
				    0,target);
    }
    
    public void moveGun(int x) { // move gun to the given x coordinate

	if (x <= min_x) 
	    gun.setPosition(gun_min_x,gun_y);

	else if (x >= max_x) 
	    gun.setPosition(gun_max_x,gun_y);

	else 
	    gun.setPosition(x-gun_width,gun_y);
    }
    
    public void fireMissile(int x) { // fire missile from given x coordinate

	if (!missile.isActive()) {   // if missile sprite isn't active 

	    if (x <= min_x) 
		missile.init(mis_min_x);
	    
	    else if (x >= max_x) 
		missile.init(mis_max_x);
	    
	    else 
		missile.init(x-2);   // initialize missile
	   
	}
    }
    
    // update all the parameters associated with the
    // gun. In this case, only the missile needs to move
    // automatically. Also the gun manager checks if the
    // missile hits anything
    
    public void update() {

	missile.update();

    }
    
    // paint all sprites associated with gun

    public void paint(Graphics g) {

	gun.paint(g);
	missile.paint(g);

    }
    
    // accessor function for gun

    public GunSprite getGun() {

	return gun;

    }
    
    public int getGunY() {

	return gun_y;

    }
    
}