/* GameObjectMover.java - Animate GameObjects. */

/* 
 * 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.util.Enumeration;
import java.util.Vector;

class GameObjectMover extends Thread
{
    int rate;
    MissileCommando parent;
    
    GameObjectMover (int rate, MissileCommando parent)
    {
	this.rate = rate;
	this.parent = parent;
    }

    public
    void run ()
    {
	for (;;)
	{
	    parent.repaint ();
	    try
	    {
		Thread.sleep (rate);
	    }
	    catch (Exception e)
	    {
		break;
	    }
	    checkCollisions ();
	    removeZombies ();
	}
    }

    void checkCollisions ()
    {
	Enumeration shots = parent.shots.elements ();
	Enumeration bases = parent.bases.elements ();
	Shot shot;
	Base base;

	for (;;)
	{
	    shot = shots.hasMoreElements ()
		? (Shot) shots.nextElement () : null;
	    base = bases.hasMoreElements ()
		? (Base) bases.nextElement () : null;
	    if (shot == null && base == null)
	    {
		break;
	    }

	    /* missiles */
	    Enumeration missiles = parent.missiles.elements ();
	    while (missiles.hasMoreElements ())
	    {
		Missile missile = (Missile) missiles.nextElement ();
		if (shot != null
		    && missile.collision (shot.x,
					  shot.y,
					  shot.currentSize ()))
		{
		    parent.destroyMissile (missile);
		}
		if (base != null
		    && base.alive ()
		    && missile.collision (base.x, base.y, base.w, base.h))
		{
		    parent.destroyBase (base, missile);
		}
	    }
	    
	    /* mrvs */
	    Enumeration mrvs = parent.mrvs.elements ();
	    while (mrvs.hasMoreElements ())
	    {
		MRV mrv = (MRV) mrvs.nextElement ();
		if (shot != null
		    && mrv.collision (shot.x,
				      shot.y,
				      shot.currentSize ()))
		{
		    parent.destroyMRV (mrv);
		}
		if (base != null
		    && base.alive ()
		    && mrv.collision (base.x, base.y, base.w, base.h))
		{
		    parent.destroyBase (base, mrv);
		}
	    }

	    /* bombs */
	    Enumeration bombs = parent.bombs.elements ();
	    while (bombs.hasMoreElements ())
	    {
		Bomb bomb = (Bomb) bombs.nextElement ();
		if (shot != null
		    && bomb.collision (shot.x,
				      shot.y,
				      shot.currentSize ()))
		{
		    parent.destroyBomb (bomb);
		}
		else if (base != null
			 && base.alive ()
			 && bomb.collision (base.x, base.y, base.w, base.h))
		{
		    parent.destroyBomb (bomb);
		}
		else if (bomb.y > parent.worldHeight - parent.baseHeight)
		{
		    parent.destroyBomb (bomb);
		}
	    }

	    /* bombDebris */
	    Enumeration bombDebris = parent.bombDebris.elements ();
	    while (bombDebris.hasMoreElements ())
	    {
		BombDebris debris = (BombDebris) bombDebris.nextElement ();
		if (base != null
		    && base.alive ()
		    && debris.collision (base.x + base.w/2,
					 base.y + base.h/2, base.w))
		{
		    parent.destroyBase (base, debris);
		}
	    }
	}
    }

    void removeZombies (Vector v)
    {
	Enumeration e;

	e = v.elements ();
	while (e.hasMoreElements ())
	{
	    GameObject o = (GameObject) e.nextElement ();
	    if (! o.alive ())
	    {
		if (o instanceof Missile)
		{
		    Missile m = (Missile) o;
		    if (m.splitme)
		    {
			for (int i = 0; i < 3; i++)
			{
			    parent.createMissile ((int)m.x, (int)m.y, (int)m.speed);
			}
		    }
		}
		v.removeElement (o);
	    }
	}
    }
    
    void removeZombies ()
    {
	removeZombies (parent.missiles);
	removeZombies (parent.mrvs);
	removeZombies (parent.bombs);
	removeZombies (parent.shots);
	removeZombies (parent.explosions);
	removeZombies (parent.bombDebris);
    }
}
