public Boolean addBlockPieces(TrayPiece trayPiece, Vector2 topLeftRel) { Boolean[,] layout = trayPiece.layoutInstance; for (int i = 0; i < layout.GetLength(0); i++) { for (int j = 0; j < layout.GetLength(1); j++) { Vector2 relativeLocation = new Vector2(topLeftRel.X + i, topLeftRel.Y + j); if (relativeLocation.X >= boardSize.X || relativeLocation.Y >= boardSize.Y || relativeLocation.X < 0 || relativeLocation.Y < 0) { return(false); } if (layout[i, j]) { if (board[(int)(relativeLocation.X), (int)(relativeLocation.Y)] != null) { return(false); } } } } foreach (BoardPiece piece in trayPiece.pieceInstance) { if (piece == null) { continue; } board[(int)(piece.position.X + topLeftRel.X), (int)(piece.position.Y + topLeftRel.Y)] = piece; } return(true); }
private void NewTrayPieces() //TODO center tray pieces. make tray prettier in general... but fully functioning. { BlockCollection.Blocks[] enums = (BlockCollection.Blocks[])Enum.GetValues(typeof(BlockCollection.Blocks)); Vector2 topVector = topLeftTrayPosition + new Vector2(50, 20); for (int i = 0; i < 3; i++) { TrayPiece trayPiece = BlockCollection.collectionDictionary[enums[Game1.random.Next(0, enums.Length)]].getTrayPiece(); trayPiece.trayPosition = trayPiece.position = topVector + new Vector2(0, 120) * i; trayPieces.Add(trayPiece); } trayCount = 3; }
public void MoveTrayPiece(MouseState mouseState) //this contains all board operational logic. { if (Game1.gameover) { return; } //if (mouseState.LeftButton != ButtonState.Pressed) return; Vector2 mousePosition = mouseState.Position.ToVector2(); if (trayPieceMoving != null) { TrayPiece piece = trayPieceMoving; if (mouseState.LeftButton == ButtonState.Pressed) //moving piece { piece.position = mousePosition + piece.offsetMouse; } else //piece dropped { piece.isMoving = false; Vector2 relativePosition = getApproximateRelativePosition(piece.position, doRound: true); //Console.WriteLine(relativePosition); if (addBlockPieces(piece, relativePosition)) { trayPieces.Remove(piece); //remove tray piece. if (--trayCount <= 0) { NewTrayPieces(); } DoGameUpdate(); //logic should be performed here to make sure the player can place the pieces. //this is a relatively large time complex check, but is also sparsely run. TrayPiece smallestPiece = trayPieces[0]; int smallestPieceSize = smallestPiece.layoutInstance.GetLength(0) * smallestPiece.layoutInstance.GetLength(1); for (int i = 1; i < trayPieces.Count; i++) //start at index 1 because 0 is done above. { TrayPiece tp = trayPieces[i]; int len = tp.layoutInstance.GetLength(0) * tp.layoutInstance.GetLength(1); if (len < smallestPieceSize) { smallestPiece = tp; smallestPieceSize = len; } } bool[,] searchedBoard = new bool[board.GetLength(0), board.GetLength(1)]; //default populated false bool matchFound = false; Vector2[] vectorsToMatch = makeLayoutVectors(smallestPiece.layoutInstance);//relative to top left for (int i = 0; i < board.GetLength(0) - smallestPiece.layoutInstance.GetLength(0); i++) { for (int j = 0; j < board.GetLength(1) - smallestPiece.layoutInstance.GetLength(1); j++) { //if (searchedBoard[i, j]) continue; //if(board[i,j] == null) { if (matchLayout(vectorsToMatch, new Vector2(i, j), searchedBoard)) { matchFound = true; break; } } searchedBoard[i, j] = true; } } if (!matchFound) //game over! { Console.WriteLine("game over!"); Game1.setGameOver(); } } else { piece.position = piece.trayPosition; } trayPieceMoving = null; } } else if (mouseState.LeftButton == ButtonState.Pressed) { foreach (TrayPiece piece in trayPieces) { if (piece == null) { continue; } foreach (BoardPiece boardBlock in piece.pieceInstance) { if (boardBlock == null) { continue; } Point leftCorner = (piece.position + boardBlock.position * 32).ToPoint(); if (new Rectangle(leftCorner, new Point(32, 32)).Contains(mousePosition)) { piece.offsetMouse = (piece.position - mousePosition); piece.isMoving = true; trayPieceMoving = piece; break; } } } } }