Added a new GamePiece class to the project, which represents one of the components that can be placed on the board. I intend for this class to be subclassed for each of the specific types of components (wires, resistors, etc). The GameLevel class represents the game board and the piece queue. I could have created a separate GameBoard class, but I decided otherwise since it is just a 2D array of GamePiece objects. Each GamePiece has a fill value (how many frames until the flow goes to the adjacent cell(s)), an Elex value, and booleans for keeping track of which sides accept flow. Author and date information have been added as a header comment in each of the code files. It just looks odd not seeing the GPL there.
I had two alternatives. I could have just created a list of pieces that have been placed on the board, having each piece know its row and column location on the board. That is the method used in many 2D games, where each player and enemy knows its own x-y coordinate position. The approach I took is creating the 2D array of pieces, which represent each cell on the board. If a cell does not contain a piece, then null is stored for that array location. I think the 2D array approach will work better when trying to determine the connections between wires. It’s like Easter eggs in an egg carton. There’s really no reason for each egg to know it’s x-y coordinate. The egg carton just needs to know which egg goes into which slot. The location of the egg can be derived by it’s slot in the egg carton, so it’s really redundant for each egg to also keep track of its position. The 2D array approach shouldn’t be any more memory intensive, since storing null in a cell doesn’t require memory to be allocated.
Four new methods have been added to the GameLevel class: confirmButtonPressed, cancelButtonPressed, actionButtonPressed, and optionButtonPressed. These correlate to the A, B, X, and Y buttons on the XBox controller respectively. I wanted to keep everything in the GameLevel class platform independent, and keep all of the platform specific code in the ResistorGame (formerly Game1) class. This will make it easier if I want to add keyboard controls or give the user the ability to customize the controls. Since this is a simple game, the confirm button will be used to drop a piece to the board, and the cancel and action buttons will be used to rotate the piece. Resistors and I wires can only be rotated in two positions. L wires (aka elbow wires) can be rotated in four positions.
One difficulty that I’m having is that every time I want to test on the XBox (after it has been shut off), I have to Reset All Connections in the XNA Game Studio Connect and remove then re-add the XBox device in XNA Game Studio Device Center using the new code generated on the XBox. I’m not sure if there is a way to keep from having to do this every time.
Adding Pieces to the Board
When the confirm button is pressed, I simply add a GamePiece object (just a new one for now) to the GamePiece array in the GameLevel class using the setPiece method, passing the piece, row, and column as parameter. That method simply adds the passed-in piece to the game piece array at the specified row and column. When the draw method is called, it loops through each cell in that array, and if it is not null (i.e. contains a piece), then it calls the draw method on the GamePiece object. Since the GamePiece object will be subclassed, it should correctly draw whatever component it is. For now, it just draws the regular tile using a red shade. One problem that I had to fix was when the user held down the confirm button, then it would continually drop pieces to the board until that button is released. Therefore, I added code to detect when the A button was pressed and released, then only called confirmButtonPressed when the A ButtonState went from Released to Pressed.