private Vector2Int NumberOfCubesPerArea; // Vector that describes the number of cubes per area in the sudoku board /// <summary> /// Method that instantiates the cube game objects for the sudoku board. /// </summary> /// <param name="boardDataAsset">Asset that contains sudoku board data.</param> /// <param name="offsetBetweenAreas">Offset distance between areas in the sudoku board.</param> public void CreateSudokuBoard(SudokuBoardDataAsset boardDataAsset, float offsetBetweenAreas) { NumberOfAreasInBoard = boardDataAsset.NumberOfAreasInBoard; NumberOfCubesPerArea = boardDataAsset.NumberOfCubesPerArea; // Calculate board dimensions + initialise matrix for storing cubes of the sudoku board Vector3 boardDimensions = new Vector3(NumberOfAreasInBoard.x * NumberOfCubesPerArea.x, 0, NumberOfAreasInBoard.y * NumberOfCubesPerArea.y); CubeControllerMatrix = new CubeController[(int)boardDimensions.x, (int)boardDimensions.z]; // Calculate initial position of cube + offset vector for correcting the position of instantiated cube game objects Vector3 cubePosition = Vector3.zero + transform.position; Vector3 offsetCorrection = new Vector3(boardDimensions.x / 2 - (NumberOfAreasInBoard.x - 1) * (NumberOfAreasInBoard.y - 1) * offsetBetweenAreas, 0f, boardDimensions.z / 2 - (NumberOfAreasInBoard.x - 1) * (NumberOfAreasInBoard.y - 1) * offsetBetweenAreas); for (int i = 0; i < CubeControllerMatrix.GetLength(0); i++) { // Add row offset between areas - if needed if ((i != 0) && (i % NumberOfCubesPerArea.x == 0)) { cubePosition += new Vector3(0f, 0f, offsetBetweenAreas); } for (int j = 0; j < CubeControllerMatrix.GetLength(1); j++) { // Add column offset between areas - if needed if ((j != 0) && (j % NumberOfCubesPerArea.y == 0)) { cubePosition += new Vector3(offsetBetweenAreas, 0f, 0f); } // Instantiate + set parenting for the cube game objects GameObject newCube = Instantiate(CubePrefab, cubePosition - offsetCorrection, Quaternion.identity); newCube.transform.SetParent(transform); // Reference, initialise, and update cube with appropriate data CubeControllerMatrix[i, j] = newCube.GetComponent <CubeController>(); CubeControllerMatrix[i, j].Initialise(); CubeControllerMatrix[i, j].SetCubeData(boardDataAsset.GetSudokuCubeData(i, j)); // Add listeners to appropriate events CubeControllerMatrix[i, j].AddOnCubeEventListener(OnCubeUpdateCallback); CubeControllerMatrix[i, j].AddOnAvailableNumbersUpdateEventListener(UpdateAvailableNumberCountMapSelect, true, false); CubeControllerMatrix[i, j].AddOnAvailableNumbersUpdateEventListener(UpdateAvailableNumberCountMapPropagate, false, false); // Adjust column position for next cube cubePosition += new Vector3(1f, 0f, 0f); } // Reset column position + adjust row position for next row of cubes cubePosition = new Vector3(0f, 0f, cubePosition.z + 1f); } }
void OnGUI() { EditorGUILayout.BeginVertical(GUI.skin.box); CurrentSudokuBoardDataAsset = (SudokuBoardDataAsset)EditorGUILayout.ObjectField("Asset", CurrentSudokuBoardDataAsset, typeof(SudokuBoardDataAsset), true); EditorGUILayout.EndVertical(); RefreshDataParametersFromAsset(); EditorGUILayout.Space(); DrawSudokuBoardDataParametersGUI(); EditorGUILayout.Space(); DrawSudokuBoardGUI(NumberOfAreasInBoard.x * NumberOfCubesPerArea.x, NumberOfAreasInBoard.y * NumberOfCubesPerArea.y); DrawSaveChangesGUI(); }