scg/ch13/core/NextPieceBox

From FANG

Jump to: navigation, search

001 package scg.ch13.core;
002 
003 import java.awt.Color;
004 
005 import fang2.core.Game;
006 import fang2.sprites.CompositeSprite;
007 import fang2.sprites.RectangleSprite;
008 
009 import scg.ch13.pieces.I_Piece;
010 import scg.ch13.pieces.J_Piece;
011 import scg.ch13.pieces.L_Piece;
012 import scg.ch13.pieces.O_Piece;
013 import scg.ch13.pieces.Piece;
014 import scg.ch13.pieces.S_Piece;
015 import scg.ch13.pieces.T_Piece;
016 import scg.ch13.pieces.Z_Piece;
017 
018 public class NextPieceBox
019   extends CompositeSprite {
020   /** background color swatch */
021   private final RectangleSprite background;
022 
023   /** outline color swatch */
024   private final RectangleSprite edges;
025 
026   /** the current piece inside the box */
027   private Piece piece;
028 
029   /**
030    * Make a new {@link NextPieceBox} with the given scale for the
031    * internal piece squares. Uses the same colors as defaulted in {@link
032    * Board}.
033    *
034    @param  squareEdgeSize  size (in screens) of edge of one square in
035    *                         a piece as displayed
036    */
037   public NextPieceBox(double squareEdgeSize{
038     edges = new RectangleSprite(1.41.4);
039     setEdgeColor(Board.DEFAULT_EDGE_COLOR);
040     addSprite(edges);
041     background = new RectangleSprite(1.01.0);
042     setColor(Board.DEFAULT_BACKGROUND_COLOR);
043     addSprite(background);
044     setScale(squareEdgeSize * 5);
045   }
046 
047   /**
048    * Get the current edge color
049    *
050    @return  the current edge color
051    */
052   public Color getEdgeColor() {
053     return edges.getColor();
054   }
055 
056   /**
057    * Get the current piece
058    *
059    @return  reference to the {@link Piece} currently in the {@link
060    *          NextPieceBox}.
061    */
062   public Piece getPiece() {
063     return piece;
064   }
065 
066   /**
067    * Generate a new piece. The piece is randomly created: it is one of 7
068    * pieces with one of 4 facings and has a random contrasting color.
069    */
070   public void nextPiece() {
071     // remove old piece from display
072     if (piece != null{
073       removeSprite(piece);
074     }
075 
076     // randomize piece, facing, and color
077     int randomPiece = Game.getCurrentGame().randomInt(
078         Piece.PIECE_COUNT);
079     int facing = Game.getCurrentGame().randomInt(4);
080     Color randomColor = getRandomContrastingColor(getColor());
081 
082     // the factory code; call the right constructor
083     if (randomPiece == 0{
084       piece = new I_Piece(facing, randomColor);
085     } else if (randomPiece == 1{
086       piece = new L_Piece(facing, randomColor);
087     } else if (randomPiece == 2{
088       piece = new J_Piece(facing, randomColor);
089     } else if (randomPiece == 3{
090       piece = new Z_Piece(facing, randomColor);
091     } else if (randomPiece == 4{
092       piece = new S_Piece(facing, randomColor);
093     } else if (randomPiece == 5{
094       piece = new O_Piece(facing, randomColor);
095     } else if (randomPiece == 6{
096       piece = new T_Piece(facing, randomColor);
097     }
098     addSprite(piece);
099   }
100 
101   /**
102    Set the color (the background color) of this sprite. Caught here to
103    * set background, too.
104    *
105    @param  color  the color to set
106    */
107   @Override
108   public void setColor(Color color{
109     super.setColor(color);
110     background.setColor(color);
111   }
112 
113   /**
114    Set the color of edge, the edge box around the piece box.
115    *
116    @param  color  the color to set
117    */
118   public void setEdgeColor(Color color{
119     edges.setColor(color);
120   }
121 
122   /**
123    * Calculate average brightness of bgColor. If it is light, generate a
124    * dark color and if it is dark, generate a light color. A random
125    * brightness is generated for each color, R, G, and B. It is in the
126    * low range [0-128) to make dark colors or in the high range
127    [128-256) to make light colors.
128    *
129    @param   bgColor  the color with which a contrast is desired; if it
130    *                   is dark, go light and if it is light, go dark.
131    *
132    @return  a random color contrasting with the background color.
133    */
134   private Color getRandomContrastingColor(Color bgColor{
135     int low = 128;// assume we want a light color
136     int high = 256;
137     int averageBrightness = (bgColor.getRed() + bgColor.getGreen() +
138         bgColor.getBlue()) 3;
139 
140     if (averageBrightness >= 128{// bgColor is light
141       low = 0;// need dark color
142       high = 128;// range
143     }
144 
145     Game curr = Game.getCurrentGame();
146     Color randomColor = Game.getColor(curr.randomInt(low, high),
147         curr.randomInt(low, high), curr.randomInt(low, high));
148     return randomColor;
149   }
150 }
151 
152 //Uploaded on Mon Mar 29 21:40:41 EDT 2010


Download/View scg/ch13/core/NextPieceBox.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter

Games
Games