/* Missile.java - Missile. */

/* 
 * 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;

class Missile extends GameObject
{
    double m, b, x, y;
    int speed;
    int startx;
    int starty;
    int endx;
    int endy;
    Color color;
    boolean split = false;
    boolean splitme = false;
    
    Missile (int startx, int starty, int endx, int endy, int speed, Color color)
    {
	this.startx = startx;
	this.starty = starty;
	this.endx = endx;
	this.endy = endy;
	this.speed = speed;
	this.color = color;

	x = startx;
	y = starty;
	m = (double) (endy - starty) / (endx - startx);
	b = starty - (m * startx);

	if (Math.random () > 0.90)
	{
	    split = true;
	}
    }

    Missile (int startx, int starty, int endx, int endy, int speed)
    {
	this (startx, starty, endx, endy, speed, Color.red);
    }

    void erase (Graphics g)
    {
	g.setColor (skyColor);
	g.drawLine (startx, starty, (int)x, (int)y);
    }

    void paint (Graphics g)
    {
	erase (g);
	
	if (!alive)
	{
	    return;
	}
	else if (explode)
	{
	    alive = false;
	    explode = false;
	    return;
	}

	y += speed;
	x = (y - b) / m;
	if (y > endy)
	{
	    alive = false;
	}
	else if (split && y > (endy-starty)/3 && Math.random () > 0.50)
	{
	    alive = false;
	    splitme = true;
	}

	if (alive)
	{
	    if (split && y > (endy-starty)/3)
	    {
		g.setColor (Color.magenta);
	    }
	    else
	    {
		g.setColor (color);
	    }
	    g.drawLine (startx, starty, (int)x, (int)y);
	}
    }

    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)));
	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);
    }
}
