home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 12,000 to 12,999 / 12000.zip / AOLDLs / Online-Tools / Java-Applets / JAVAAPPS.lzh / JAVAAPPS / BRICK / BRICK.EXE / Ball.java < prev    next >
Encoding:
Java Source  |  1996-04-23  |  7.4 KB  |  328 lines

  1. import java.awt.Graphics;
  2. import java.awt.Color;
  3. import java.awt.Point;
  4. import java.util.Random;
  5. import java.applet.AudioClip;
  6.  
  7. public class Ball extends Thread {
  8.   private BricksGame parent;
  9.   private Graphics g;
  10.   private int x, y, xl, yl;
  11.   private int initial_x, initial_y;
  12.   private int radius, score;
  13.   private int speed, sleep;
  14.   private Color color;
  15.   private int direction;
  16.   private Point tmp;
  17.   private Random rand;
  18.  
  19.   /** 
  20.     0 degrees is right horizontal
  21.   */ 
  22.   final static int DIR_HORIZ = 0;
  23.   final static int DIR_MAX = 180;
  24.   final static int DIR_VERT = DIR_MAX / 2;
  25.  
  26.   public Ball (int x, int y, int radius, int level, int score, Color color) {
  27.     rand = new Random();
  28.  
  29.     this.score = score;
  30.  
  31.     initial_x = x;
  32.     initial_y = y;
  33.  
  34.     this.x = x;
  35.     this.y = y;
  36.     this.radius = radius;
  37.     this.color = color;
  38.     direction = -1 * (DIR_VERT / 2 + (int) ((DIR_VERT * rand.nextFloat()) / 8));
  39.  
  40.     tmp = new Point(0,0);
  41.  
  42.     switch (level) {
  43.       case 0:
  44.         speed = 6;
  45.         sleep = 12;
  46.         break;
  47.       case 1:
  48.         speed = 5;
  49.         sleep = 10;
  50.         break;
  51.       case 2:
  52.         speed = 5;
  53.         sleep = 8;
  54.         break;
  55.       case 3:
  56.         speed = 6;
  57.         sleep = 12;
  58.         break;
  59.       case 4:
  60.         speed = 6;
  61.         sleep = 10;
  62.         break;
  63.       default:
  64.         speed = 1 + level;
  65.         sleep = 8;
  66.         break;
  67.     }
  68.  
  69.     System.out.println("Ball:New Ball! loc: (" + x + "," + y + "), @ (" + speed + ":" + sleep + "," + direction + ").");
  70.   }
  71.  
  72.   public void init (BricksGame parent, Graphics g) {
  73.     this.parent = parent;
  74.     this.g = g;
  75.   }
  76.  
  77.   public int speedx() {
  78.     return (int) (java.lang.Math.cos(direction * java.lang.Math.PI / DIR_MAX) * speed);
  79.   }
  80.  
  81.   public int speedy() {
  82.     return (int) (java.lang.Math.sin(direction * java.lang.Math.PI / DIR_MAX) * speed);
  83.   }
  84.  
  85.   public void run () {
  86.     boolean tmp, impact=false;
  87.     int remain=0;
  88.  
  89.     xl = x; yl = y;
  90.     draw();
  91.     parent.repaint();
  92.  
  93.     try {
  94.       sleep(3600 * 1);
  95.     } catch (InterruptedException e) {
  96.       System.out.println("Ball:: " + e);
  97.     }
  98.  
  99.     this.setPriority(9);
  100.  
  101.     while (true) {
  102.  
  103.       xl = x; yl = y;
  104.       x += speedx(); y += speedy();
  105.  
  106.       impact = parent.paddle.impact(this);
  107.       if (impact) addScore(1);
  108.  
  109.       for (int i=0; i < parent.bricks.length; ++i)
  110.         for (int j=0; j < parent.bricks[0].length; ++j) {
  111.           if (parent.bricks[i][j].active) ++remain;
  112.           if (!impact) {
  113.             impact = parent.bricks[i][j].impact(this);
  114.           }
  115.         }
  116.  
  117.  
  118.       if (!impact) {
  119.         impact = parent.wall.impact(this);
  120.       }
  121.         
  122.       draw();
  123.       if (impact) parent.wall.draw();
  124.  
  125.       parent.repaint();
  126.  
  127.       if (parent.wall.outofBounds(this)) {
  128.         xl = x; yl = y;
  129.         x = initial_x; y=initial_y;
  130.  
  131.         score = 0;
  132.  
  133.         draw();
  134.         parent.repaint();
  135.  
  136.         play(BricksConstants.EFX_LOST_BALL);
  137.  
  138.         try {
  139.           sleep(3600 * 1);
  140.         } catch (InterruptedException e) {
  141.           System.out.println("Ball:: " + e);
  142.         }
  143.  
  144.       }
  145.  
  146.       if (remain == 0) {
  147.  
  148.         parent.nextLevel();
  149.         play(BricksConstants.EFX_NEXT_LEVEL);
  150.  
  151.         System.out.println(this + "Killing myself.");
  152.  
  153.         this.stop();
  154.       }
  155.  
  156.       impact=false;
  157.       remain=0;
  158.       
  159.       if (sleep > 0) {
  160.         try {
  161.           sleep(sleep);
  162.         } catch (InterruptedException e) {
  163.           System.out.println("Ball:: " + e);
  164.         }
  165.       }
  166.  
  167.     }
  168.  
  169.   }
  170.  
  171.   public void impact (int approach, int a, int b, float slick) {
  172. //    System.out.println(toString() + " IMPACT! ");
  173.  
  174.     int dd, da;
  175.  
  176.     if (a > b) System.out.println("Ball: impact vector out of order.");
  177.  
  178.     if (approach == BricksConstants.IMPACT_LEFT || 
  179.     approach == BricksConstants.IMPACT_RIGHT) {
  180.       if (a > y || b < y) System.out.println("Ball: Invalid impact vector");
  181.      
  182.       if (a == y) a -= 1;
  183.       if (b == y) b += 1;
  184.  
  185.       if ((dd = (int) (a + (b - a) * 0.4f)) > y) {
  186.         da = (int) ( (float) (y - dd) / (b - a) * DIR_MAX * slick);
  187.  
  188. /*
  189.         System.out.println(this + "LEFT V; dd=" + dd + "; da=" + da + "; slick=" + slick + 
  190.             "; a,b=" + a + "," + b + "; y=" + y + ".");
  191. */
  192.  
  193.         direction += sign(direction) * (DIR_VERT - java.lang.Math.abs(direction)) * 2 + da;
  194.  
  195.       } else if ((dd = (int) (b - (b - a) * 0.4f)) < y) {
  196.  
  197.         da = (int) ( (float) (y - dd) / (b - a) * DIR_MAX * slick);
  198. /*
  199.         System.out.println(this + "RIGHT V; dd=" + dd + "; slick=" + slick + 
  200.             "; a,b=" + a + "," + b + "; y=" + y + ".");
  201. */
  202.  
  203.         direction += sign(direction) * 
  204.         (DIR_VERT - java.lang.Math.abs(direction)) * 2 + da;
  205.  
  206.       } else {
  207.         direction += sign(direction) * 
  208.         (DIR_VERT - java.lang.Math.abs(direction)) * 2;
  209.       }
  210.  
  211.     } else
  212.  
  213.     if (approach == BricksConstants.IMPACT_TOP ||
  214.     approach == BricksConstants.IMPACT_BOTTOM) {
  215.       if (a > x || b < x) System.out.println("Ball: Invalid impact vector");
  216.      
  217.       if (a == x) a -= 1;
  218.       if (b == x) b += 1;
  219.  
  220.       if ((dd = (int) (a + (b - a) * 0.4f)) > x) {
  221.         da = (int) ( (float) (x - dd) / (b - a) * DIR_MAX * slick);
  222.  
  223. /*
  224.         System.out.println(this + "LEFT H; dd=" + dd + "; da=" + da + "; slick=" + slick + 
  225.             "; a,b=" + a + "," + b + "; x=" + x + ".");
  226. */
  227.  
  228.         direction = -1 * direction + da;
  229.  
  230.       } else if ((dd = (int) (b - (b - a) * 0.4f)) < x) {
  231.  
  232.         da = (int) ((float) (x - dd) / (b - a) * DIR_MAX * slick);
  233.  
  234. /*
  235.         System.out.println(this + "RIGHT H; dd=" + dd + "; da=" + da + "; slick=" + slick + 
  236.             "; a,b=" + a + "," + b + "; x=" + x + ".");
  237. */
  238.  
  239.         direction = -1 * direction + da;
  240.  
  241.       } else
  242.         direction = -1 * direction;
  243.  
  244.     } else 
  245.       return;
  246.  
  247.     if (java.lang.Math.abs(speedy()) < 1) {
  248.       System.out.println(this + "Getting out of horizontal stalness.");
  249.       direction += sign(speedy()) * 
  250.         java.lang.Math.abs((int) (rand.nextGaussian() * DIR_MAX * 0.1d));
  251.     } else {
  252.       direction += (int) (rand.nextGaussian() * DIR_MAX * 0.01d);
  253.     }
  254.       
  255.     direction = direction % DIR_MAX;
  256.   }
  257.  
  258.   public void play (int snd) {
  259.     parent.play(snd);
  260.   }
  261.  
  262.   public int sign (int x) {
  263.     if (x == 0) return 1;
  264.     return (x / java.lang.Math.abs(x));
  265.   }
  266.  
  267.   public void draw () {
  268.     synchronized(g) {
  269.       g.setColor(Color.black);
  270.       g.fillOval(xl, yl, radius, radius);
  271.  
  272.       g.setColor(color);
  273.       g.fillOval(x, y, radius, radius);
  274.     }
  275.   }
  276.  
  277.   public int setScore(int val) {
  278.     score=val;
  279.     return score;
  280.   }
  281.  
  282.   public int getScore() {
  283.     return score;
  284.   }
  285.  
  286.   public int addScore(int val) {
  287.     score += val;
  288.  
  289.     System.out.println(this + " Score = " + score + ".");
  290.  
  291.     return score;
  292.   }
  293.  
  294.   public void setSpeed (int speed) {
  295.     this.speed = speed;
  296.   }
  297.  
  298.   public Point left() {
  299.     tmp.x = x;
  300.     tmp.y = y + radius / 2;
  301.     return tmp;
  302.   }
  303.  
  304.   public Point right() {
  305.     tmp.x = x + radius;
  306.     tmp.y = y + radius / 2;
  307.     return tmp;
  308.   }
  309.  
  310.   public Point top() {
  311.     tmp.x = x + radius / 2;
  312.     tmp.y = y;
  313.     return tmp;
  314.   }
  315.  
  316.   public Point bottom() {
  317.     tmp.x = x + radius / 2;
  318.     tmp.y = y + radius;
  319.     return tmp;
  320.   }
  321.  
  322.   public String toString() {
  323.     return ("Ball:: (" + x + "," + y + "); (" + speed + "," + direction + ").");
  324.   }
  325.  
  326. }
  327.  
  328.