SubKorea > CYBER JAVA GAME > º®µ¹±ú±â2 (Javanoid v1.45) > ¼Ò½º #8

// ## JAVANOID OBJECT CLASS #############################################
// It's the basic object of javanoid. Though it doesn't move by itself,
// it can be moved to absolute coordinates (ex: the paddle, which moves
// to the position of the mouse pointer). A javanoid object lives (i.e.
// is on the screen) & dies when it touches the bottom  

abstract class jnobject extends Object {

 // Width & height of the object
 protected int width, height;

 // Initial & current position of the object
 protected int X0, Y0;
 protected int X, Y;

 // X & Y limits for the canvas
 protected int minX, maxX;
 protected int minY, maxY;

 // Tells if the object is still alive
 protected boolean alive;

 // The buffer where the object is drawn
 jnbuffer buffer;
 
 // The bitmap image of the object
 Image image;

 // Constructor
 public jnobject(jnbuffer c, Image i, int w, int h, int x, int y) {

  buffer = c;

  image = i; width = w; height = h;

  X0 = x; Y0 = y;
  X  = x; Y  = y;
 
  minX = 0; maxX = buffer.getWidth() - width;
  minY = 0; maxY = buffer.getHeight() - height;
 
  alive = true;
  draw();
 
  }              // À§·Î

 // Object position settings
 public void setXY(int x, int y) {
  setX(x); setY(y);
  }

 public void setX(int x) {
  if (x > maxX) X = maxX;
  else if (x < minX) X = minX;
  else X = x;
 
  }               // À§·Î

 public void setY(int y) {
  if (y > maxY) Y = maxY;
  else if (y < minY) Y = minY;
  else Y = y;
  }

 public void reset() { X = X0; Y = Y0; }               // À§·Î

 // Object drawing & erasing
 public void draw() { buffer.drawImage(image, X, Y); }

 // Objet life (what he does until he's dead)
 public void live() { if (alive) draw(); }

 // °´Ã¼ÀÇ ¼Ò¸ê
 public void die() { alive = false; }               // À§·Î

 // Collision detection
 public boolean isHit(int x, int y) {
  return (alive) && (x > X) && (x < X + width) && (y > Y) && (y < Y + height);
  }
  
 // Object graphic changes
 public void change(Image i, int w, int h) {
  image = i; width = w; height = h;
  minX = 0; maxX = buffer.getWidth() - width;
  minY = 0; maxY = buffer.getHeight() - height;
 
  }               // À§·Î
  
 // Object requests
 public boolean isAlive() { return alive; }

 public int getWidth() { return width; }                // À§·Î

 public int getHeight() { return height; }

 public int getX() { return X; }

 public int getY() { return Y; }

 }               // À§·Î
 
  
// ## JANAVOID IMAGE SERVER CLASS ####################
// ·Îµù½Ã°£À» ÁÙÀ̱â À§ÇØ ÇϳªÀÇ À̹ÌÁöÆÄÀÏÀ» ¿Ã¸®°í
// ¸ðµç °´Ã¼ÀÇ À̹ÌÁö - ¼­·Î °øÅëÀ¸·Î »ç¿ëµÊ

final class jnimageserver {

 // À̹ÌÁö ÆÄÀϸí
 final static String imageFile = "javanoid.gif";

 block12°³, ball2°³, paddle4°³, bullet1°³, pill6°³// °´Ã¼´ç À̹ÌÁöÀÇ ¼ö
 final static int nbBallImg   = 2;
 final static int nbPaddleImg = 4;
 final static int nbBlockImg  = 12;
 final static int nbPillImg   = 6;
 final static int nbBulletImg = 1;
 
 // À̹ÌÁöÀÇ ÁÂÇ¥¿Í Å©±â
 final static int ballCoords[][] = {
      {0, 88, 12, 12}, {12, 88, 12, 12} };

 final static int blockCoords[][] = {
  {0, 0, 32, 20}, {0, 20, 32, 20}, {0, 40, 32, 20}, {0, 60, 32, 20},
  {32, 0, 32, 20}, {32, 20, 32, 20}, {32, 40, 32, 20}, {32, 60, 32, 20},
  {64, 0, 32, 20}, {64, 20, 32, 20}, {64, 40, 32, 20}, {64, 60, 32, 20} };

 final static int paddleCoords[][] = {
  {24, 88, 40, 12}, {64, 88, 32, 12}, {0, 108, 48, 12}, {48, 108, 40, 12} };

 final static int pillCoords[][] = {
  {0, 122, 16, 8}, {16, 122, 16, 8}, {32, 122, 16, 8}, {48, 122, 16, 8},
  {64, 122, 16, 8}, {80, 122, 16, 8} };

 final static int bulletCoords[][] = {
  {88, 114, 4, 6} };
 
 // °¢ À̹ÌÁöÀÇ ¹è¿­»ý¼º
 protected Image ballImg[]   = new Image[nbBallImg];
 protected Image paddleImg[] = new Image[nbPaddleImg];
 protected Image blockImg[]  = new Image[nbBlockImg];
 protected Image pillImg[]   = new Image[nbPillImg];
 protected Image bulletImg[] = new Image[nbBulletImg];

 Applet applet;
 MediaTracker tracker;
 String imageRep;
  
 // À̹ÌÁö¼­¹öÀÇ »ý¼ºÀÚ
 public jnimageserver (Applet a) {
    applet = a;
  }                // À§·Î
  
 // À̹ÌÁö¼­¹öÀÇ ÃʱâÈ­
 public void init() {

  tracker = new MediaTracker(applet);
  URL url = applet.getCodeBase();
 
  // ¸ðµç ±×·¡ÇÈÀÌ ÇϳªÀÇ gif ÆÄÀÏ¿¡¼­ ·Îµù
  Image imageTmp = applet.getImage(url, imageFile);
  tracker.addImage(imageTmp, 0);

  // Extraction for images
  extractImages(ballImg, imageTmp, nbBallImg, ballCoords);
  extractImages(paddleImg, imageTmp, nbPaddleImg, paddleCoords);
  extractImages(blockImg, imageTmp, nbBlockImg, blockCoords);
  extractImages(pillImg, imageTmp, nbPillImg, pillCoords);
  extractImages(bulletImg, imageTmp, nbBulletImg, bulletCoords);

  try { tracker.waitForAll(); }
     catch (InterruptedException e) {}
 
  }                // À§·Î
 

 // Extraction of the images
 public void extractImages(Image[] images, Image tmp, int n, int[][] coords) {

  ImageFilter   filter;
  ImageProducer producer;

  for (int i=0;i<n;i++) {

   filter   = new CropImageFilter(coords[i][0], coords[i][1], coords[i][2], coords[i][3]);
 
   producer = new FilteredImageSource(tmp.getSource(), filter);

   images[i] = applet.createImage(producer);
   tracker.addImage(images[i], 0);
   }
  }                // À§·Î

 // ImageServer functions
 public Image paddleImage(int i) { if (i >= nbPaddleImg) i = 0; return paddleImg[i]; }

 public Image ballImage(int i) { if (i >= nbBallImg) i = 0; return ballImg[i]; }

 public Image blockImage(int i) { if (i >= nbBlockImg) i = 0; return blockImg[i]; }                // À§·Î

 public Image pillImage(int i) { if (i >= nbPillImg) i = 0; return pillImg[i]; }

 public Image bulletImage(int i) { if (i >= nbBulletImg) i = 0; return bulletImg[i]; }
 
 }                // À§·Î
 

// ## JAVANOID SOUND SERVER CLASS ###############
// This class loads and plays the sounds of javanoid

final class jnsoundserver {

 // Static strings for sounds filenames
 protected static String soundFiles[] = {"start.au", "wall.au", "shoot.au"};
 protected static int nbSounds = 3;

 protected boolean soundOn = false;
 protected int soundPlay;
 protected String soundRep;

 Applet applet;
 AudioClip sounds[] = new AudioClip[nbSounds];
  
 // Constructor
 public jnsoundserver(Applet a) {
   applet = a;
  }                // À§·Î

 // SoundServer initialization (i.e. sounds loading)
 public void init() {

  URL url = applet.getCodeBase();

  for (int i=0;i<soundFiles.length;i++) {

   sounds[i] = applet.getAudioClip(url, soundFiles[i]);

   try { Thread.sleep(1000); }
   catch (InterruptedException e) { }
   }
  }                 // À§·Î

 // Make SoundServer silent
 public void silent() { for (int i=0;i<soundFiles.length;i++) sounds[i].stop(); }

 // Main soundServer method
 public void iterate() {

  if ((soundOn) && (soundPlay>=0)) {
   for (int i=0;i<nbSounds;i++) { sounds[i].stop(); }
   sounds[soundPlay].play();
   soundPlay = -1;
   }

  }                 // À§·Î
  
 // SoundServer On / Off
 public void on() { soundOn = true; }

 public void off() { if (soundOn) {soundOn = false; silent();} }

 public void toggle() { if (soundOn) off(); else on(); }

 // Asking for a sound
 public void playStart() { soundPlay = 0; }                 // À§·Î

 public void playTouch() { soundPlay = 1; }

 public void playHit() { soundPlay = 2; }

 }                 // À§·Î