/* Bomb.java - The Big One */
/* 
 * Copyright (C) 1996 Mark Boyns <boyns@sdsu.edu>
 *
 * Missile Commando II
 * <URL:http://www.sdsu.edu/~boyns/java/mcii/>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;

class Bomb extends GameObject
{
    final int bombWidth = 19;
    final int bombHeight = 26;
    
    int x, y, endx, endy, speed;
    Image bombImage;
    MissileCommando parent;
    
    Bomb (int startx, int starty, int endx, int endy, int speed, Image bombImage, MissileCommando parent)
    {
	this.x = startx;
	this.y = starty;
	this.endx = endx;
	this.endy = endy;
	this.speed = speed;
	this.bombImage = bombImage;
	this.parent = parent;
    }

    void erase (Graphics g)
    {
	g.setColor (skyColor);
	g.fillRect (x - bombWidth/2, y - bombWidth/2, bombWidth, bombHeight);
    }

    void paint (Graphics g)
    {
	erase (g);

	if (!alive)
	{
	    return;
	}
	else if (explode)
	{
	    alive = false;
	    explode = false;
	    return;
	}

	y += speed;

	if (y > endy)
	{
	    alive = false;
	}

	if (alive)
	{
	    g.drawImage (bombImage, x - bombWidth/2, y - bombWidth/2, bombWidth, bombHeight, parent);
	}
    }

    boolean collision (int x2, int y2, int range)
    {
	if (!alive || explode)
	{
	    return false;
	}
	
	int distance = (int) Math.sqrt (((x2 - x) * (x2 - x))
					+ ((y2 - y) * (y2 - y)));
	range += bombWidth/2;
	return distance <= range;
    }

    boolean collision (int x2, int y2, int w, int h)
    {
	if (!alive || explode)
	{
	    return false;
	}
	
	return x >= x2 && x <= (x2 + w) && y >= y2 && y <= (y2 + h);
    }
}
