home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / enxle1f6 / src / games / battle / client / clientapplet / clientlooktraditional.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  6.7 KB  |  206 lines

  1. /*
  2.  * @(#)ClientLookTraditional.java
  3.  */
  4.  
  5. package games.Battle.client.ClientApplet;
  6.  
  7. import java.awt.Image;
  8. import java.awt.Graphics;
  9. import java.awt.Color;
  10. import java.awt.Component;
  11.  
  12. import games.Battle.shared.sys.Symbols;
  13. import games.Battle.shared.sys.Rules;
  14.  
  15. /**
  16.  * ClientLookTraditional implements a look mimicing the traditional
  17.  * xbattle look. Hills and valleys are represented by varying contour
  18.  * shades.
  19.  *
  20.  * ClientLookTraditional is the first client we implemented to 
  21.  * begin testing and implenting the game.
  22.  *
  23.  * @author Alex Nicolaou
  24.  * @author Jay Steele
  25.  */
  26. public class ClientLookTraditional extends ClientLook {
  27.  
  28.     /**
  29.      * The cached width and height.
  30.      */
  31.     int width, height;
  32.  
  33.     /**
  34.      * Construct a ClientLookTraditional instance with the
  35.      * given ClientBoardComponent.
  36.      * @param c the component to draw in
  37.      */
  38.     public ClientLookTraditional(ClientBoardComponent c) {
  39.         super(c);
  40.     }
  41.  
  42.     /**
  43.      * Update terrain does nothing for this look. All drawing
  44.      * is done in update().
  45.      */
  46.     public void updateTerrain(ClientBoard b) {
  47.     }
  48.  
  49.     /**
  50.      * Update the display for the client board for every turn. For this
  51.      * look, this method does all the drawing for the client, including
  52.      * the terrain. It only draws visible squares.
  53.      */
  54.     public void update(ClientBoard b) {
  55.  
  56.         // Clear the board black (the invisible color)
  57.         offGC.setColor(Color.black);
  58.         offGC.fillRect(0, 0, Symbols.BOARD_W, Symbols.BOARD_H);
  59.  
  60.         float w = (float)Symbols.BOARD_W / (float)Rules.cols;
  61.         float h = (float)Symbols.BOARD_H / (float)Rules.rows;
  62.         width = (int)w;
  63.         height = (int)h;
  64.  
  65.         // Iterate over all the cells and update visible ones
  66.         for (int r=0; r<Rules.rows; r++) {
  67.             for (int c=0; c<Rules.cols; c++) {
  68.                 int xorg = (int)(w * (float)c);
  69.                 int yorg = (int)(h * (float)r);
  70.                 ClientCell cell = (ClientCell)b.getCell(r, c);
  71.                 int occ = cell.getOccupancy();
  72.                 if (occ != Symbols.INVISIBLE
  73.                     && occ != Symbols.UNMODIFIED)
  74.                 {
  75.                     paintTerrain(xorg, yorg, cell.getTerrain());
  76.                     int city = cell.getCity();
  77.                     if (occ != Symbols.UNOCCUPIED) {
  78.                         paintTroops(xorg, yorg, occ, cell.getTroops());
  79.                         paintPipes(xorg, yorg, cell.getPipeMask());
  80.                     }
  81.                     if (city > 0) {
  82.                         paintCity(xorg, yorg, city);
  83.                     }
  84.                 }
  85.             }
  86.         }
  87.     }
  88.  
  89.     static private final float terrainHue = (float)(25.0 / 255.0);
  90.     static private final float terrainSat = (float)(185.0 / 255.0);
  91.     static private final Color[] terrainColor = {
  92.         new Color(0, 0, 170),
  93.         Color.getHSBColor(terrainHue, terrainSat, (float)0.50),
  94.         Color.getHSBColor(terrainHue, terrainSat, (float)0.46),
  95.         Color.getHSBColor(terrainHue, terrainSat, (float)0.42),
  96.         Color.getHSBColor(terrainHue, terrainSat, (float)0.38),
  97.         Color.getHSBColor(terrainHue, terrainSat, (float)0.34),
  98.         Color.getHSBColor(terrainHue, terrainSat, (float)0.30),
  99.         Color.getHSBColor(terrainHue, terrainSat, (float)0.26)
  100.     };
  101.  
  102.     /**
  103.      * Paint the terrain of the given cell.
  104.      * @param xorg the x origin of the cell to draw
  105.      * @param yorg the y origin of the cell to draw
  106.      * @param terrain the level of the terrain to draw
  107.      */
  108.     void paintTerrain(int xorg, int yorg, int terrain) {
  109.         offGC.setColor(terrainColor[terrain]);
  110.         offGC.fillRect(xorg+1, yorg+1, width-1, height-1);
  111.         offGC.setColor(Color.black);
  112.         offGC.drawRect(xorg, yorg, width, height);
  113.     }
  114.  
  115.     static private final Color playerColor[] = {
  116.         new Color(200, 0, 0),
  117.         new Color(0, 200, 0),
  118.         new Color(0, 0, 100),
  119.         new Color(0, 200, 200),
  120.         new Color(200, 0, 200),
  121.         new Color(200, 200, 0),
  122.     };
  123.  
  124.     /**
  125.      * Paint the given number of troops for the player in the supplied
  126.      * location.
  127.      * @param xorg the x location to draw the troops
  128.      * @param yorg the y location to draw the troops
  129.      * @param player the player occupancy to draw
  130.      * @param troops the number of troops (a percentage from 0-31) to draw.
  131.      */
  132.     void paintTroops(int xorg, int yorg, int player, int troops) {
  133.         float percent = (float)(troops+1) / (float)32.0;
  134.         float w = (float)width * percent;
  135.         float h = (float)height * percent;
  136.         offGC.setColor(playerColor[player]);
  137.         float x = (float)(width) / (float)2.0 - w/(float)2.0 + 1;
  138.         float y = (float)(height) / (float)2.0 - h/(float)2.0 + 1;
  139.         offGC.fillRect((int)x+xorg, (int)y+yorg, (int)w, (int)h);
  140.     }
  141.  
  142.     /**
  143.      * Draw the graphic for a city on the game board.
  144.      * @param xorg the x origin of the cell to draw the city in
  145.      * @param yorg the y origin of the cell to draw the city in
  146.      * @param city the size of the city
  147.      */
  148.     void paintCity(int xorg, int yorg, int city) {
  149.         float angle = (float)360 * (float)city/(float)Symbols.MAX_CLIENT_TROOPS;
  150.         offGC.setColor(Color.black);
  151.         offGC.drawArc(xorg+2, yorg+2, width-4, height-4, 0, (int)angle);
  152.         if (city == Symbols.MAX_CLIENT_TROOPS) {
  153.             offGC.drawArc(xorg+3, yorg+3, width-6, height-6, 0, (int)angle);
  154.             offGC.drawArc(xorg+4, yorg+4, width-8, height-8, 0, (int)angle);
  155.         }
  156.     }
  157.  
  158.     /**
  159.      * Paint pipes on a particular cell.
  160.      * @param xorg the x cell origin to paint the pipe
  161.      * @param yorg the y cell origin to paint the pipe
  162.      * @param pipes the flags indicating which pipes are on and off
  163.      * @param player the player (to draw in the correct color)
  164.      */
  165.     void paintPipes(int xorg, int yorg, int pipes) {
  166.         int wh = (int)((float)width / (float)2.0);
  167.         int hh = (int)((float)height / (float)2.0);
  168.         int wa = (int)((float)width / (float)3.0);
  169.         int ha = (int)((float)height / (float)3.0);
  170.         int wb = (int)((float)width * (float)2.0 / (float)3.0);
  171.         int hb = (int)((float)height * (float)2.0 / (float)3.0);
  172.  
  173.         if ((pipes & Symbols.PIPE_MASK[Symbols.NORTH]) != 0) {
  174.             offGC.setColor(Color.black);
  175.             offGC.drawLine(xorg+wh-1, yorg+1, xorg+wh-1, yorg+ha);
  176.             offGC.drawLine(xorg+wh+1, yorg+1, xorg+wh+1, yorg+ha);
  177.             offGC.setColor(Color.white);
  178.             offGC.drawLine(xorg+wh, yorg+1, xorg+wh, yorg+ha);
  179.         }
  180.  
  181.         if ((pipes & Symbols.PIPE_MASK[Symbols.SOUTH]) != 0) {
  182.             offGC.setColor(Color.black);
  183.             offGC.drawLine(xorg+wh-1, yorg+hb, xorg+wh-1, yorg+height-1);
  184.             offGC.drawLine(xorg+wh+1, yorg+hb, xorg+wh+1, yorg+height-1);
  185.             offGC.setColor(Color.white);
  186.             offGC.drawLine(xorg+wh, yorg+hb, xorg+wh, yorg+height-1);
  187.         }
  188.  
  189.         if ((pipes & Symbols.PIPE_MASK[Symbols.EAST]) != 0) {
  190.             offGC.setColor(Color.black);
  191.             offGC.drawLine(xorg+wb, yorg+hh-1, xorg+width, yorg+hh-1);
  192.             offGC.drawLine(xorg+wb, yorg+hh+1, xorg+width, yorg+hh+1);
  193.             offGC.setColor(Color.white);
  194.             offGC.drawLine(xorg+wb, yorg+hh, xorg+width, yorg+hh);
  195.         }
  196.  
  197.         if ((pipes & Symbols.PIPE_MASK[Symbols.WEST]) != 0) {
  198.             offGC.setColor(Color.black);
  199.             offGC.drawLine(xorg+1, yorg+hh-1, xorg+wa, yorg+hh-1);
  200.             offGC.drawLine(xorg+1, yorg+hh+1, xorg+wa, yorg+hh+1);
  201.             offGC.setColor(Color.white);
  202.             offGC.drawLine(xorg+1, yorg+hh, xorg+wa, yorg+hh);
  203.         }
  204.     }
  205. }
  206.