import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Add one gun to a spaceship.
 * 
 * @author Jannis Andrija Schnitzer, Martin Schend
 * @version 2011-01-20
 */
public class MoreGuns  extends Collectible
{
    public void act() 
    {
        super.act();
    }
    
    public boolean hit(ManuallyControlledSpaceship ship)
    {
        setNumberOfGuns(ship, ship.getNumberOfGuns()+1);
        return false;
    }
    
    /**
     * Set the number of guns for a spaceship. If new_n_guns is greater than the current number of guns,
     * copy the ship's first gun and add the copies.
     * 
     * @param ship: guess what
     * @param new_n_guns: number of guns to set
     */
    public void setNumberOfGuns(ManuallyControlledSpaceship ship, int new_n_guns)
    {
        if (new_n_guns > Spaceship.N_GUNS) // too many gunz.
            return;
        
        int n_guns = ship.getNumberOfGuns();
        if (new_n_guns < n_guns)
        {
            for (int i = n_guns-1; i >= new_n_guns; i--)
                ship.removeGun(i);
            n_guns = new_n_guns;
        }
        else if (new_n_guns > n_guns)
        {
            Gun old_gun = ship.guns[0];
            while (n_guns < new_n_guns)
            {
                Gun gun = old_gun.copy_gun();
                gun.setLevel(old_gun.getLevel());
                ship.addGun(n_guns++, gun);
            }
        }
        
        ship.reorderGuns();
        
        for (int i = 0; i < n_guns; i++)
        {
            ship.guns[i].setTimer(ship.guns[i].getRecovery());
        }
    }
}
