home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / enxle1f6 / src / games / battle / shared / comm / gameinit.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  2.5 KB  |  98 lines

  1. /*
  2.  * @(#)GameInit.java
  3.  */
  4.  
  5. package games.Battle.shared.comm;
  6.  
  7. import games.Battle.shared.sys.*;
  8. import java.awt.Color;
  9.  
  10. /**
  11.  * A GameInit packet is the information which is sent from the
  12.  * server to each client at the beginning of a game in order to
  13.  * set up preamble information like the Rules of the game, the
  14.  * colors of each of the players.
  15.  * 
  16.  * @version 1.00 02/02/96
  17.  * @author Jay Steele
  18.  */
  19. public class GameInit extends BattlePacket {
  20.     /**
  21.      * the player number that this game init packet is for.
  22.      */
  23.     int player;
  24.  
  25.     /**
  26.      * the array of colours used for this players in this game.
  27.      */
  28.     Color[] colors;
  29.  
  30.     /** 
  31.      * the default colours used by games.
  32.      */
  33.     public static final Color[] DEFAULT_COLORS = {
  34.         new Color(255, 0, 0),
  35.         new Color(0, 255, 0),
  36.         new Color(0, 0, 255),
  37.         new Color(255, 0, 255),
  38.         new Color(255, 255, 0)
  39.     };
  40.  
  41.     /**
  42.      * The default constructor which fills the GameInit packet with
  43.      * default data. This contructor is used by the client application
  44.      * prior to calling the readFrom(is) method to obtain a game init
  45.      * packet from a data stream.
  46.      */
  47.     public GameInit() {
  48.         this.player = 0;
  49.         this.colors = new Color[Symbols.MAX_PLAYERS];
  50.     }
  51.  
  52.     /**
  53.      * The data constructor for the GameInit which fills it with
  54.      * the given data. This contructor is used by the server application
  55.      * prior to calling the writeTo(os) method to send a game init
  56.      * packet to a data stream.
  57.      * @param player the player identifier
  58.      * @param an array of colors representing the color for each player
  59.      */
  60.     public GameInit(int player, Color[] colors) {
  61.         this.player = player;
  62.         this.colors = colors;
  63.     }
  64.  
  65.     /**
  66.      * Return the player identifier of this game initializer
  67.      */
  68.     public int getPlayer() { return player; }
  69.  
  70.     /**
  71.      * Produce an array of bytes representing a GameInit
  72.      */
  73.     public byte[] toBytes() {
  74.         byte[] buffer = new byte[1 + 3 * (Symbols.MAX_PLAYERS)];
  75.         buffer[0] = (byte)player;
  76.         for (int i=0; i<Symbols.MAX_PLAYERS; i++) {
  77.             buffer[3*i+1] = (byte)colors[i].getRed();
  78.             buffer[3*i+2] = (byte)colors[i].getGreen();
  79.             buffer[3*i+3] = (byte)colors[i].getBlue();
  80.         }
  81.         return buffer;
  82.     }
  83.  
  84.     /**
  85.      * Fill this GameInit packet with the given array of bytes.
  86.      * @param buffer the byte array from which to fill this GameInit
  87.      */
  88.     public void fromBytes(byte[] buffer) {
  89.         player = buffer[0];
  90.         for (int i=0; i<Symbols.MAX_PLAYERS; i++) {
  91.             int r = (int)buffer[3*i+1];
  92.             int g = (int)buffer[3*i+2];
  93.             int b = (int)buffer[3*i+3];
  94.             colors[i] = new Color(r, g, b);
  95.         }
  96.     }
  97. }
  98.