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

  1. /* Missile.java - Missile. */
  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.awt.Graphics;
  25. import java.awt.Color;
  26.  
  27. class Missile extends GameObject
  28. {
  29.     double m, b, x, y;
  30.     int speed;
  31.     int startx;
  32.     int starty;
  33.     int endx;
  34.     int endy;
  35.     Color color;
  36.     boolean split = false;
  37.     boolean splitme = false;
  38.     
  39.     Missile (int startx, int starty, int endx, int endy, int speed, Color color)
  40.     {
  41.     this.startx = startx;
  42.     this.starty = starty;
  43.     this.endx = endx;
  44.     this.endy = endy;
  45.     this.speed = speed;
  46.     this.color = color;
  47.  
  48.     x = startx;
  49.     y = starty;
  50.     m = (double) (endy - starty) / (endx - startx);
  51.     b = starty - (m * startx);
  52.  
  53.     if (Math.random () > 0.90)
  54.     {
  55.         split = true;
  56.     }
  57.     }
  58.  
  59.     Missile (int startx, int starty, int endx, int endy, int speed)
  60.     {
  61.     this (startx, starty, endx, endy, speed, Color.red);
  62.     }
  63.  
  64.     void erase (Graphics g)
  65.     {
  66.     g.setColor (skyColor);
  67.     g.drawLine (startx, starty, (int)x, (int)y);
  68.     }
  69.  
  70.     void paint (Graphics g)
  71.     {
  72.     erase (g);
  73.     
  74.     if (!alive)
  75.     {
  76.         return;
  77.     }
  78.     else if (explode)
  79.     {
  80.         alive = false;
  81.         explode = false;
  82.         return;
  83.     }
  84.  
  85.     y += speed;
  86.     x = (y - b) / m;
  87.     if (y > endy)
  88.     {
  89.         alive = false;
  90.     }
  91.     else if (split && y > (endy-starty)/3 && Math.random () > 0.50)
  92.     {
  93.         alive = false;
  94.         splitme = true;
  95.     }
  96.  
  97.     if (alive)
  98.     {
  99.         if (split && y > (endy-starty)/3)
  100.         {
  101.         g.setColor (Color.magenta);
  102.         }
  103.         else
  104.         {
  105.         g.setColor (color);
  106.         }
  107.         g.drawLine (startx, starty, (int)x, (int)y);
  108.     }
  109.     }
  110.  
  111.     boolean collision (int x2, int y2, int range)
  112.     {
  113.     if (!alive || explode)
  114.     {
  115.         return false;
  116.     }
  117.     
  118.     int distance = (int) Math.sqrt (((x2 - x) * (x2 - x))
  119.                     + ((y2 - y) * (y2 - y)));
  120.     return distance <= range;
  121.     }
  122.  
  123.     boolean collision (int x2, int y2, int w, int h)
  124.     {
  125.     if (!alive || explode)
  126.     {
  127.         return false;
  128.     }
  129.     
  130.     return x >= x2 && x <= (x2 + w) && y >= y2 && y <= (y2 + h);
  131.     }
  132. }
  133.