import greenfoot.*;
import java.lang.Math.*;

/**
 * Gun: shoots missiles. Handles timeouts. Can be put into a spaceshit.
 * 
 * @author Jannis Andrija Schnitzer
 * @version 2011-01-16
 */
public class Gun  
{
    protected int timer;
    protected int recovery;
    protected Vector speed;
    // initial distance of launched bullets from spaceship center
    protected int dx;
    protected int dy;

    /**
     * Constructor for objects of class Gun
     * 
     * @param abs_speed: absolute value of the speed vector
     * @param angle: angle of speed vector to y axis, in radians
     */
    public Gun(double abs_speed, double angle)
    {
        timer = 0;
        double x_speed, y_speed;
        x_speed = abs_speed * Math.sin(angle);
        y_speed = abs_speed * Math.cos(angle);
        speed = new Vector((int)x_speed, (int)y_speed);
        dx = 0;
        dy = 0;
        recovery = 10;
    }
    public Gun()
    {
        this(1.0, 0.0);
    }

    /**
     * Send a tick to the gun every time act() is called in the containing
     * spaceship. This is used to handle the internal firing timer.
     */
    public void tick()
    {
        timer -= 1;
    }
    
    /**
     * Emit a new bullet or null if not ready yet.
     * 
     * @return     the bullet or null
     */
    public Missile missile()
    {
        if (timer <= 0) {
            // return bullet
            Missile m = new Missile();
            m.setSpeed(speed);
            timer = recovery;
            return m;
        }
        else
        {
            return null;
        }
    }
    
    public int get_dx()
    {
        return dx;
    }
    public int get_dy()
    {
        return dy;
    }
    public void set_dx(int new_dx)
    {
        dx = new_dx;
    }
    public void set_dy(int new_dy)
    {
        dy = new_dy;
    }
}
