home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 October / PCO1097.ISO / FilesBBS / FREI / MCII.EXE / MissileCommando.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-03  |  12.4 KB  |  635 lines

  1. import java.applet.Applet;
  2. import java.applet.AudioClip;
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.Component;
  6. import java.awt.Container;
  7. import java.awt.Event;
  8. import java.awt.Font;
  9. import java.awt.FontMetrics;
  10. import java.awt.Graphics;
  11. import java.awt.Image;
  12. import java.awt.MediaTracker;
  13. import java.util.Enumeration;
  14. import java.util.Vector;
  15.  
  16. public class MissileCommando extends Applet implements Runnable {
  17.    final String version = "MCII 1.1";
  18.    final int worldWidth = 500;
  19.    final int worldHeight = 300;
  20.    final int scoreHeight = 25;
  21.    final int controlsHeight = 50;
  22.    final int screenWidth = 500;
  23.    final int screenHeight = 375;
  24.    final int baseWidth = 64;
  25.    final int baseHeight = 32;
  26.    final int baseGap = 30;
  27.    final int maxBases = 5;
  28.    final int missilePoints = 75;
  29.    final int mrvPoints = 150;
  30.    final int bombPoints;
  31.    final int extraShotPoints = 10;
  32.    final int extraBasePoints = 50;
  33.    final int rebuildBasePoints = 10000;
  34.    AudioClip missileSound;
  35.    AudioClip missileExplosionSound;
  36.    AudioClip mrvExplosionSound;
  37.    AudioClip baseExplosionSound;
  38.    AudioClip bombSound;
  39.    boolean playing = false;
  40.    boolean readyToPlay = false;
  41.    boolean clearScreen = false;
  42.    boolean paused = false;
  43.    boolean soundEnabled = true;
  44.    Thread thread;
  45.    GameObjectMover mover;
  46.    Controls controls;
  47.    Vector bases;
  48.    Vector missiles;
  49.    Vector shots;
  50.    Vector mrvs;
  51.    Vector explosions;
  52.    Vector bombs;
  53.    Vector bombDebris;
  54.    int score;
  55.    int extraBaseScore;
  56.    int level;
  57.    int shotCount;
  58.    int missileCount;
  59.    int missileSpeed;
  60.    int missileDelay;
  61.    int mrvSpeed;
  62.    int bombSpeed;
  63.    Color skyColor = new Color(148, 198, 231);
  64.    Graphics world;
  65.    MediaTracker tracker;
  66.    Image baseImage;
  67.    Image mrvImage;
  68.    Image bombImage;
  69.    Font font;
  70.    FontMetrics fontMetrics;
  71.    String screenMessage;
  72.  
  73.    public void init() {
  74.       this.font = new Font("TimesRoman", 1, 20);
  75.       this.fontMetrics = ((Component)this).getFontMetrics(this.font);
  76.       ((Component)this).setFont(this.font);
  77.       this.tracker = new MediaTracker(this);
  78.       this.baseImage = ((Applet)this).getImage(((Applet)this).getDocumentBase(), "images/base.gif");
  79.       this.tracker.addImage(this.baseImage, 1);
  80.       this.mrvImage = ((Applet)this).getImage(((Applet)this).getDocumentBase(), "images/mrv.gif");
  81.       this.tracker.addImage(this.mrvImage, 1);
  82.       this.bombImage = ((Applet)this).getImage(((Applet)this).getDocumentBase(), "images/bomb.gif");
  83.       this.tracker.addImage(this.bombImage, 1);
  84.       this.missileSound = ((Applet)this).getAudioClip(((Applet)this).getDocumentBase(), "sounds/Rocket.au");
  85.       this.missileExplosionSound = ((Applet)this).getAudioClip(((Applet)this).getDocumentBase(), "sounds/Oomph.au");
  86.       this.mrvExplosionSound = ((Applet)this).getAudioClip(((Applet)this).getDocumentBase(), "sounds/Oomph.au");
  87.       this.baseExplosionSound = ((Applet)this).getAudioClip(((Applet)this).getDocumentBase(), "sounds/Explosion-2.au");
  88.       this.bombSound = ((Applet)this).getAudioClip(((Applet)this).getDocumentBase(), "sounds/ship_alarm.au");
  89.       ((Component)this).setBackground(Color.white);
  90.       ((Container)this).setLayout(new BorderLayout());
  91.       this.controls = new Controls(this);
  92.       ((Container)this).add("South", this.controls);
  93.       ((Applet)this).resize(500, 375);
  94.       this.thread = new Thread(this);
  95.       this.thread.start();
  96.    }
  97.  
  98.    public void start() {
  99.       if (this.thread == null) {
  100.          this.thread = new Thread(this);
  101.          this.thread.start();
  102.       }
  103.  
  104.    }
  105.  
  106.    public void stop() {
  107.       if (this.thread != null && this.thread.isAlive()) {
  108.          this.thread.stop();
  109.          this.thread = null;
  110.       }
  111.  
  112.       if (this.mover != null && this.mover.isAlive()) {
  113.          this.mover.stop();
  114.          this.mover = null;
  115.       }
  116.  
  117.    }
  118.  
  119.    public boolean mouseDown(Event var1, int var2, int var3) {
  120.       if (this.playing) {
  121.          this.createShot(var2, var3 - 25);
  122.       }
  123.  
  124.       return true;
  125.    }
  126.  
  127.    void startGame() {
  128.       this.readyToPlay = true;
  129.    }
  130.  
  131.    void quitGame() {
  132.       this.playing = false;
  133.    }
  134.  
  135.    void resetScore() {
  136.       this.clearScreen = true;
  137.       this.score = 0;
  138.       this.extraBaseScore = 0;
  139.       this.level = 0;
  140.       this.message((String)null);
  141.    }
  142.  
  143.    void resetGame() {
  144.       this.clearScreen = true;
  145.       this.missiles = new Vector(100);
  146.       this.shots = new Vector(100);
  147.       this.mrvs = new Vector(100);
  148.       this.explosions = new Vector(100);
  149.       this.bombs = new Vector(100);
  150.       this.bombDebris = new Vector(100);
  151.       this.createBases();
  152.       this.message((String)null);
  153.    }
  154.  
  155.    void suspendGame() {
  156.       if (this.thread != null && this.playing) {
  157.          this.thread.suspend();
  158.          if (this.mover != null && this.mover.isAlive()) {
  159.             this.mover.suspend();
  160.          }
  161.  
  162.          this.paused = true;
  163.          this.clearScreen = true;
  164.          ((Component)this).repaint();
  165.       }
  166.  
  167.    }
  168.  
  169.    void resumeGame() {
  170.       if (this.thread != null && this.playing) {
  171.          this.thread.resume();
  172.          if (this.mover != null && this.mover.isAlive()) {
  173.             this.mover.resume();
  174.          }
  175.  
  176.          this.paused = false;
  177.          this.clearScreen = true;
  178.       }
  179.  
  180.    }
  181.  
  182.    void enableSound() {
  183.       this.soundEnabled = true;
  184.    }
  185.  
  186.    void disableSound() {
  187.       this.soundEnabled = false;
  188.    }
  189.  
  190.    public void run() {
  191.       ((Applet)this).showStatus("Loading images...");
  192.       this.tracker.checkAll(true);
  193.  
  194.       try {
  195.          this.tracker.waitForAll();
  196.       } catch (Exception var9) {
  197.          return;
  198.       }
  199.  
  200.       ((Applet)this).showStatus("");
  201.       this.resetGame();
  202.       this.resetScore();
  203.  
  204.       while(true) {
  205.          if (!this.playing && this.readyToPlay) {
  206.             this.resetGame();
  207.             this.resetScore();
  208.             this.playing = true;
  209.             this.readyToPlay = false;
  210.          }
  211.  
  212.          if (this.playing) {
  213.             ++this.level;
  214.          } else if (this.level == 0) {
  215.             this.level = 1;
  216.          }
  217.  
  218.          if (this.playing) {
  219.             this.missileDelay = 2000 - (this.level - 1) * 200;
  220.             if (this.missileDelay < 500) {
  221.                this.missileDelay = 500;
  222.             }
  223.          } else {
  224.             this.missileDelay = 500;
  225.          }
  226.  
  227.          this.missileSpeed = 5;
  228.          this.mrvSpeed = 6;
  229.          this.bombSpeed = 4;
  230.          this.missileCount = 5 + (this.level - 1) * 5;
  231.          this.shotCount = this.missileCount * 2;
  232.          this.message("Level " + this.level);
  233.  
  234.          try {
  235.             Thread.sleep(3000L);
  236.          } catch (Exception var8) {
  237.          }
  238.  
  239.          this.message((String)null);
  240.          this.mover = new GameObjectMover(100, this);
  241.          this.mover.start();
  242.  
  243.          while(this.missileCount > 0) {
  244.             int var1 = this.missileSpeed;
  245.             if (this.level > 1 && Math.random() > (double)0.75F) {
  246.                var1 += (int)(Math.random() * (double)this.missileSpeed);
  247.             }
  248.  
  249.             this.createMissile(var1);
  250.             --this.missileCount;
  251.             if (this.mrvs.size() < this.level) {
  252.                double var2 = Math.random();
  253.                if (var2 > 0.7) {
  254.                   this.createMRV(this.mrvSpeed);
  255.                }
  256.             }
  257.  
  258.             if (this.level > 1 && this.bombs.size() < 2) {
  259.                double var12 = Math.random();
  260.                if (var12 > 0.85) {
  261.                   this.createBomb(this.bombSpeed);
  262.                }
  263.             }
  264.  
  265.             if (!this.playing) {
  266.                this.starWars();
  267.             }
  268.  
  269.             if (!this.playing && this.readyToPlay) {
  270.                break;
  271.             }
  272.  
  273.             try {
  274.                Thread.sleep((long)this.missileDelay);
  275.             } catch (Exception var7) {
  276.             }
  277.          }
  278.  
  279.          if (!this.playing && this.readyToPlay) {
  280.             this.mover.stop();
  281.          } else {
  282.             while((this.missiles.size() > 0 || this.mrvs.size() > 0 || this.explosions.size() > 0 || this.bombs.size() > 0 || this.bombDebris.size() > 0 || this.shots.size() > 0) && (this.playing || !this.readyToPlay)) {
  283.                if (!this.playing && (this.missiles.size() > 0 || this.mrvs.size() > 0 || this.bombs.size() > 0)) {
  284.                   this.starWars();
  285.                }
  286.  
  287.                try {
  288.                   Thread.sleep(500L);
  289.                } catch (Exception var6) {
  290.                }
  291.             }
  292.  
  293.             this.mover.stop();
  294.             int var10 = this.shotCount * 10;
  295.             var10 += this.countAliveBases() * 50;
  296.             this.incrementScore(var10);
  297.             if (this.playing) {
  298.                this.message("Bonus: " + var10);
  299.  
  300.                try {
  301.                   Thread.sleep(2000L);
  302.                } catch (Exception var5) {
  303.                }
  304.  
  305.                this.message((String)null);
  306.             }
  307.  
  308.             this.rebuildBases();
  309.             if (this.countAliveBases() == 0) {
  310.                this.playing = false;
  311.                this.mover = new GameObjectMover(400, this);
  312.                this.mover.start();
  313.                this.destroyWorld();
  314.  
  315.                while(this.explosions.size() > 0) {
  316.                   try {
  317.                      Thread.sleep(500L);
  318.                   } catch (Exception var4) {
  319.                   }
  320.                }
  321.  
  322.                this.mover.stop();
  323.                this.resetGame();
  324.             }
  325.          }
  326.       }
  327.    }
  328.  
  329.    void starWarsFireAt(Vector var1) {
  330.       Enumeration var4 = var1.elements();
  331.  
  332.       while(var4.hasMoreElements()) {
  333.          GameObject var5 = (GameObject)var4.nextElement();
  334.          int var2;
  335.          int var3;
  336.          if (var5 instanceof Missile) {
  337.             Missile var6 = (Missile)var5;
  338.             var2 = (int)var6.x;
  339.             var3 = (int)var6.y;
  340.          } else if (var5 instanceof MRV) {
  341.             MRV var7 = (MRV)var5;
  342.             var2 = var7.x;
  343.             var3 = var7.y;
  344.          } else if (var5 instanceof Bomb) {
  345.             Bomb var8 = (Bomb)var5;
  346.             var2 = var8.x;
  347.             var3 = var8.y;
  348.          } else {
  349.             var2 = 250;
  350.             var3 = 150;
  351.          }
  352.  
  353.          if (var3 > 60) {
  354.             byte var9;
  355.             if (Math.random() > (double)0.5F) {
  356.                var9 = 1;
  357.             } else {
  358.                var9 = -1;
  359.             }
  360.  
  361.             this.shots.addElement(new Shot(var2 + var9 * (int)(Math.random() * (double)40.0F), var3 + var9 * (int)(Math.random() * (double)40.0F), 60));
  362.          }
  363.       }
  364.  
  365.    }
  366.  
  367.    void starWars() {
  368.       this.starWarsFireAt(this.missiles);
  369.       this.starWarsFireAt(this.mrvs);
  370.       this.starWarsFireAt(this.bombs);
  371.    }
  372.  
  373.    void createShot(int var1, int var2) {
  374.       if (this.shotCount > 0) {
  375.          this.shots.addElement(new Shot(var1, var2, 60));
  376.          --this.shotCount;
  377.       }
  378.  
  379.    }
  380.  
  381.    void createMissile(int var1, int var2, int var3) {
  382.       int var4 = 30 + (int)(Math.random() * (double)470.0F);
  383.       short var5 = 299;
  384.       if (this.soundEnabled && this.missileSound != null && this.playing) {
  385.          this.missileSound.play();
  386.       }
  387.  
  388.       this.missiles.addElement(new Missile(var1, var2, var4, var5, var3));
  389.    }
  390.  
  391.    void createMissile(int var1) {
  392.       int var2 = (int)(Math.random() * (double)500.0F);
  393.       byte var3 = 1;
  394.       this.createMissile(var2, var3, var1);
  395.    }
  396.  
  397.    void destroyMissile(Missile var1) {
  398.       if (this.soundEnabled && this.missileExplosionSound != null && this.playing) {
  399.          this.missileExplosionSound.play();
  400.       }
  401.  
  402.       ((GameObject)var1).explode();
  403.       this.createExplosion((int)var1.x, (int)var1.y, 20);
  404.       this.incrementScore(75);
  405.    }
  406.  
  407.    void createMRV(int var1) {
  408.       int var2 = (int)(Math.random() * (double)500.0F);
  409.       byte var3 = 1;
  410.       short var4 = 299;
  411.       this.mrvs.addElement(new MRV(var2, var3, var4, var1, this.mrvImage, this));
  412.    }
  413.  
  414.    void destroyMRV(MRV var1) {
  415.       if (this.soundEnabled && this.mrvExplosionSound != null && this.playing) {
  416.          this.mrvExplosionSound.play();
  417.       }
  418.  
  419.       ((GameObject)var1).explode();
  420.       this.createExplosion(var1.x, var1.y, 10);
  421.       this.incrementScore(150);
  422.    }
  423.  
  424.    void createBomb(int var1) {
  425.       if (this.soundEnabled && this.bombSound != null && this.playing) {
  426.          this.bombSound.play();
  427.       }
  428.  
  429.       int var2 = (int)(Math.random() * (double)500.0F);
  430.       byte var3 = 1;
  431.       short var4 = 299;
  432.       this.bombs.addElement(new Bomb(var2, var3, var2, var4, var1, this.bombImage, this));
  433.    }
  434.  
  435.    void destroyBomb(Bomb var1) {
  436.       ((GameObject)var1).explode();
  437.       this.createBombDebris(var1.x, var1.y);
  438.    }
  439.  
  440.    void createBombDebris(int var1, int var2) {
  441.       this.bombDebris.addElement(new BombDebris(var1, var2, 200));
  442.    }
  443.  
  444.    void createBases() {
  445.       this.bases = new Vector(5);
  446.       int var1 = 0;
  447.  
  448.       for(int var2 = 30; var1 < 5; ++var1) {
  449.          this.bases.addElement(new Base(var2, 268, 64, 32, this.baseImage, this));
  450.          var2 += 94;
  451.       }
  452.  
  453.    }
  454.  
  455.    int countAliveBases() {
  456.       int var1 = 0;
  457.  
  458.       for(int var2 = 0; var2 < 5; ++var2) {
  459.          Base var3 = (Base)this.bases.elementAt(var2);
  460.          if (((GameObject)var3).alive()) {
  461.             ++var1;
  462.          }
  463.       }
  464.  
  465.       return var1;
  466.    }
  467.  
  468.    void destroyBase(Base var1, Missile var2) {
  469.       if (this.soundEnabled && this.baseExplosionSound != null && this.playing) {
  470.          this.baseExplosionSound.play();
  471.       }
  472.  
  473.       ((GameObject)var2).explode();
  474.       ((GameObject)var1).explode();
  475.       this.createExplosion((int)var2.x, (int)var2.y, 100);
  476.    }
  477.  
  478.    void destroyBase(Base var1, MRV var2) {
  479.       if (this.soundEnabled && this.baseExplosionSound != null && this.playing) {
  480.          this.baseExplosionSound.play();
  481.       }
  482.  
  483.       ((GameObject)var2).explode();
  484.       ((GameObject)var1).explode();
  485.       this.createExplosion(var2.x, var2.y, 100);
  486.    }
  487.  
  488.    void destroyBase(Base var1, BombDebris var2) {
  489.       if (this.soundEnabled && this.baseExplosionSound != null && this.playing) {
  490.          this.baseExplosionSound.play();
  491.       }
  492.  
  493.       ((GameObject)var1).explode();
  494.       this.createExplosion(var1.x + var1.w / 2, var1.y + var1.h / 2, 100);
  495.    }
  496.  
  497.    void rebuildBases() {
  498.       int[] var1 = new int[5];
  499.  
  500.       for(int var2 = 0; var2 < var1.length; ++var2) {
  501.          var1[var2] = -1;
  502.       }
  503.  
  504.       int var3;
  505.       for(int var4 = 0; var4 < 5; var1[var3] = var4++) {
  506.          var3 = (int)(Math.random() * (double)5.0F);
  507.  
  508.          while(var1[var3] != -1) {
  509.             ++var3;
  510.             if (var3 == 5) {
  511.                var3 = 0;
  512.             }
  513.          }
  514.       }
  515.  
  516.       for(int var5 = 0; var5 < 5; ++var5) {
  517.          Base var6 = (Base)this.bases.elementAt(var1[var5]);
  518.          if (!((GameObject)var6).alive() && this.extraBaseScore >= 10000) {
  519.             this.extraBaseScore -= 10000;
  520.             var6.rebuild();
  521.          }
  522.       }
  523.  
  524.    }
  525.  
  526.    void createExplosion(int var1, int var2, int var3) {
  527.       this.explosions.addElement(new Explosion(var1, var2, var3));
  528.    }
  529.  
  530.    void incrementScore(int var1) {
  531.       if (this.playing) {
  532.          this.score += var1;
  533.          this.extraBaseScore += var1;
  534.       }
  535.  
  536.    }
  537.  
  538.    void destroyWorld() {
  539.       for(int var1 = 0; var1 < 10; ++var1) {
  540.          int var2 = (int)(Math.random() * (double)500.0F);
  541.          int var3 = (int)(Math.random() * (double)300.0F);
  542.          this.createExplosion(var2, var3, 100);
  543.       }
  544.  
  545.    }
  546.  
  547.    public void update(Graphics var1) {
  548.       this.paint(var1);
  549.    }
  550.  
  551.    void paintGameObjects(Vector var1, Graphics var2) {
  552.       if (var1 != null) {
  553.          Enumeration var3 = var1.elements();
  554.  
  555.          while(var3.hasMoreElements()) {
  556.             GameObject var4 = (GameObject)var3.nextElement();
  557.             var4.paint(var2);
  558.          }
  559.  
  560.       }
  561.    }
  562.  
  563.    void message(String var1) {
  564.       if (var1 != null) {
  565.          this.screenMessage = var1;
  566.       } else {
  567.          this.screenMessage = null;
  568.          this.clearScreen = true;
  569.       }
  570.  
  571.       ((Component)this).repaint();
  572.    }
  573.  
  574.    void paintMessage(String var1, Graphics var2) {
  575.       this.fontMetrics.getHeight();
  576.       int var3 = this.fontMetrics.stringWidth(var1);
  577.       var2.setColor(Color.black);
  578.       var2.drawString(var1, 250 - var3 / 2, 150);
  579.    }
  580.  
  581.    void paintStatus(Graphics var1) {
  582.       StringBuffer var2 = new StringBuffer();
  583.       var2.append("Score: ");
  584.       var2.append(this.score);
  585.       var2.append(" Level: ");
  586.       var2.append(this.level);
  587.       var2.append(" Shots: ");
  588.       var2.append(this.shotCount);
  589.       int var3 = this.fontMetrics.getHeight();
  590.       int var4 = this.fontMetrics.stringWidth(var2.toString());
  591.       int var5 = 250 - var4 / 2;
  592.       var1.setColor(Color.white);
  593.       var1.fillRect(0, 0, 500, 25);
  594.       var1.setColor(Color.black);
  595.       var1.drawString(var2.toString(), var5, var3);
  596.       var4 = this.fontMetrics.stringWidth("MCII 1.1");
  597.       var1.drawString("MCII 1.1", 500 - var4, var3);
  598.    }
  599.  
  600.    public void paint(Graphics var1) {
  601.       if (this.world == null) {
  602.          this.world = var1.create(0, 25, 500, 300);
  603.       }
  604.  
  605.       if (this.clearScreen) {
  606.          var1.setColor(Color.white);
  607.          var1.fillRect(0, 0, 500, 300);
  608.          this.world.setColor(this.skyColor);
  609.          this.world.fillRect(0, 0, 500, 300);
  610.          this.clearScreen = false;
  611.       }
  612.  
  613.       if (this.paused) {
  614.          this.paintMessage("PAUSED", this.world);
  615.       } else {
  616.          this.paintStatus(var1);
  617.          this.paintGameObjects(this.mrvs, this.world);
  618.          this.paintGameObjects(this.missiles, this.world);
  619.          this.paintGameObjects(this.bombs, this.world);
  620.          this.paintGameObjects(this.bases, this.world);
  621.          this.paintGameObjects(this.shots, this.world);
  622.          this.paintGameObjects(this.explosions, this.world);
  623.          this.paintGameObjects(this.bombDebris, this.world);
  624.          if (!this.playing) {
  625.             this.paintMessage("GAME OVER", this.world);
  626.          } else {
  627.             if (this.screenMessage != null) {
  628.                this.paintMessage(this.screenMessage, this.world);
  629.             }
  630.  
  631.          }
  632.       }
  633.    }
  634. }
  635.