public void LoadPuzzle() { //set player settings' current puzzle index to the sibling index of the level icon that was clicked in the scene GameObject selectedLevelButton = EventSystem.current.currentSelectedGameObject; playerSettings.selectedPuzzle = selectedLevelButton.transform.GetSiblingIndex(); //parse level data; set target puzzle data to parsed data targetPuzzleData = selectedLevelButton.GetComponent <LevelSelectButton>().data.ConvertToPicross(); //set current puzzle data to puzzles at indexes based on player settings currentPuzzleData = puzzles[playerSettings.selectedDiffculty][playerSettings.selectedPuzzle]; //if selected puzzle does not have save data, update current puzzle data to new Picross object if (currentPuzzleData.cellData.Cells == null) { currentPuzzleData = new Picross(targetPuzzleData.name, new CellType[targetPuzzleData.RowCount, targetPuzzleData.ColCount]); } //if selected puzzle is already complete, open confirm select menu if (currentPuzzleData.completionStatus == CompletionStatus.Complete) { confirmSelectMenu.Open(); confirmSelectMenu.confirmButton.onClick.AddListener(() => ResetLevel()); Disable(); } //otherwise update current puzzle completion status to Incomplete, and go straight to Game scene else { currentPuzzleData.completionStatus = CompletionStatus.Incomplete; LoadScene(2); } }
public void TestMethod1() { Picross p = new Picross(10, 10); var result = p.Test(false); Assert.IsTrue(result); }
public void UpdateDisplay(Picross puzzle) { UpdateCompletedImage(puzzle); for (int i = 0; i < puzzleIcons.Length; i++) { puzzleIcons[i].enabled = i == (int)puzzle.completionStatus; } if (puzzle.completionStatus == CompletionStatus.Complete) { puzzleName.text = puzzle.name; } }
public static byte[] LoadCompletedImage(Picross puzzle) { string imageFilePath = Path.Combine(saveDataDirectoryPath, $"{puzzle.RowCount}x{puzzle.ColCount}", $"{puzzle.name}.json"); try { BinaryFormatter bf = new BinaryFormatter(); using (var file = File.OpenRead(imageFilePath)) { return(bf.Deserialize(file) as byte[]); } } catch (System.Exception e) when(e is FileNotFoundException || e is DirectoryNotFoundException) { return(null); } }
//convert puzzle data into pixel colours, encode as png, save as byte array public static void SaveAsCompletedImage(this Picross puzzle) { //set image dimensions int imageWidth = puzzle.ColCount; int imageHeight = puzzle.RowCount; //set directory and file path based on puzzle properties string imageDirectoryPath = Path.Combine(saveDataDirectoryPath, $"{imageWidth}x{imageHeight}"); string imageFilePath = Path.Combine(imageDirectoryPath, $"{puzzle.name}.json"); //continue only if file doesn't exist yet if (File.Exists(imageFilePath)) { return; } Directory.CreateDirectory(imageDirectoryPath); Texture2D tex2D = new Texture2D(imageWidth, imageHeight); Color[] pixelColours = new Color[imageWidth * imageHeight]; //set pixel colours for (int row = 0; row < imageHeight; row++) { for (int col = 0; col < imageWidth; col++) { pixelColours[row * imageWidth + col] = puzzle.cellData.Cells[row, imageWidth - 1 - col] == CellType.Filled ? Color.black : Color.white; } } tex2D.SetPixels(pixelColours); tex2D.Apply(); byte[] bytes = tex2D.EncodeToPNG(); BinaryFormatter bf = new BinaryFormatter(); using (var file = File.Open(imageFilePath, FileMode.OpenOrCreate, FileAccess.Write)) { bf.Serialize(file, bytes); } }
void UpdateCompletedImage(Picross puzzle) { byte[] texData = LoadCompletedImage(puzzle); if (texData == null) { return; } Texture2D tex2D = new Texture2D(puzzle.ColCount, puzzle.RowCount) { filterMode = FilterMode.Point }; tex2D.LoadImage(texData); Sprite spr = Sprite.Create( tex2D, new Rect(0, 0, tex2D.width, tex2D.height), Vector2.zero / 2f, Mathf.Min(tex2D.width, tex2D.height) ); puzzleIcons[(int)CompletionStatus.Complete].sprite = spr; }