home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 October / PCO1097.ISO / FilesBBS / FREI / MCII.EXE / GameObjectMover.java < prev    next >
Encoding:
Java Source  |  1996-10-03  |  4.2 KB  |  191 lines

  1. /* GameObjectMover.java - Animate GameObjects. */
  2.  
  3. /* 
  4.  * Copyright (C) 1996 Mark Boyns <boyns@sdsu.edu>
  5.  *
  6.  * Missile Commando II
  7.  * <URL:http://www.sdsu.edu/~boyns/java/mcii/>
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License, or
  12.  * (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23.  
  24. import java.util.Enumeration;
  25. import java.util.Vector;
  26.  
  27. class GameObjectMover extends Thread
  28. {
  29.     int rate;
  30.     MissileCommando parent;
  31.     
  32.     GameObjectMover (int rate, MissileCommando parent)
  33.     {
  34.     this.rate = rate;
  35.     this.parent = parent;
  36.     }
  37.  
  38.     public
  39.     void run ()
  40.     {
  41.     for (;;)
  42.     {
  43.         parent.repaint ();
  44.         try
  45.         {
  46.         Thread.sleep (rate);
  47.         }
  48.         catch (Exception e)
  49.         {
  50.         break;
  51.         }
  52.         checkCollisions ();
  53.         removeZombies ();
  54.     }
  55.     }
  56.  
  57.     void checkCollisions ()
  58.     {
  59.     Enumeration shots = parent.shots.elements ();
  60.     Enumeration bases = parent.bases.elements ();
  61.     Shot shot;
  62.     Base base;
  63.  
  64.     for (;;)
  65.     {
  66.         shot = shots.hasMoreElements ()
  67.         ? (Shot) shots.nextElement () : null;
  68.         base = bases.hasMoreElements ()
  69.         ? (Base) bases.nextElement () : null;
  70.         if (shot == null && base == null)
  71.         {
  72.         break;
  73.         }
  74.  
  75.         /* missiles */
  76.         Enumeration missiles = parent.missiles.elements ();
  77.         while (missiles.hasMoreElements ())
  78.         {
  79.         Missile missile = (Missile) missiles.nextElement ();
  80.         if (shot != null
  81.             && missile.collision (shot.x,
  82.                       shot.y,
  83.                       shot.currentSize ()))
  84.         {
  85.             parent.destroyMissile (missile);
  86.         }
  87.         if (base != null
  88.             && base.alive ()
  89.             && missile.collision (base.x, base.y, base.w, base.h))
  90.         {
  91.             parent.destroyBase (base, missile);
  92.         }
  93.         }
  94.         
  95.         /* mrvs */
  96.         Enumeration mrvs = parent.mrvs.elements ();
  97.         while (mrvs.hasMoreElements ())
  98.         {
  99.         MRV mrv = (MRV) mrvs.nextElement ();
  100.         if (shot != null
  101.             && mrv.collision (shot.x,
  102.                       shot.y,
  103.                       shot.currentSize ()))
  104.         {
  105.             parent.destroyMRV (mrv);
  106.         }
  107.         if (base != null
  108.             && base.alive ()
  109.             && mrv.collision (base.x, base.y, base.w, base.h))
  110.         {
  111.             parent.destroyBase (base, mrv);
  112.         }
  113.         }
  114.  
  115.         /* bombs */
  116.         Enumeration bombs = parent.bombs.elements ();
  117.         while (bombs.hasMoreElements ())
  118.         {
  119.         Bomb bomb = (Bomb) bombs.nextElement ();
  120.         if (shot != null
  121.             && bomb.collision (shot.x,
  122.                       shot.y,
  123.                       shot.currentSize ()))
  124.         {
  125.             parent.destroyBomb (bomb);
  126.         }
  127.         else if (base != null
  128.              && base.alive ()
  129.              && bomb.collision (base.x, base.y, base.w, base.h))
  130.         {
  131.             parent.destroyBomb (bomb);
  132.         }
  133.         else if (bomb.y > parent.worldHeight - parent.baseHeight)
  134.         {
  135.             parent.destroyBomb (bomb);
  136.         }
  137.         }
  138.  
  139.         /* bombDebris */
  140.         Enumeration bombDebris = parent.bombDebris.elements ();
  141.         while (bombDebris.hasMoreElements ())
  142.         {
  143.         BombDebris debris = (BombDebris) bombDebris.nextElement ();
  144.         if (base != null
  145.             && base.alive ()
  146.             && debris.collision (base.x + base.w/2,
  147.                      base.y + base.h/2, base.w))
  148.         {
  149.             parent.destroyBase (base, debris);
  150.         }
  151.         }
  152.     }
  153.     }
  154.  
  155.     void removeZombies (Vector v)
  156.     {
  157.     Enumeration e;
  158.  
  159.     e = v.elements ();
  160.     while (e.hasMoreElements ())
  161.     {
  162.         GameObject o = (GameObject) e.nextElement ();
  163.         if (! o.alive ())
  164.         {
  165.         if (o instanceof Missile)
  166.         {
  167.             Missile m = (Missile) o;
  168.             if (m.splitme)
  169.             {
  170.             for (int i = 0; i < 3; i++)
  171.             {
  172.                 parent.createMissile ((int)m.x, (int)m.y, (int)m.speed);
  173.             }
  174.             }
  175.         }
  176.         v.removeElement (o);
  177.         }
  178.     }
  179.     }
  180.     
  181.     void removeZombies ()
  182.     {
  183.     removeZombies (parent.missiles);
  184.     removeZombies (parent.mrvs);
  185.     removeZombies (parent.bombs);
  186.     removeZombies (parent.shots);
  187.     removeZombies (parent.explosions);
  188.     removeZombies (parent.bombDebris);
  189.     }
  190. }
  191.