using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; public class MineSweeperEditor : EditorWindow { public int MineCount = 10; Board board; // Add menu named "My Window" to the Window menu [MenuItem("Addon/Mine Sweeper")] static void Init() { // Get existing open window or if none, make a new one: MineSweeperEditor window = (MineSweeperEditor)EditorWindow.GetWindow(typeof(MineSweeperEditor)); window.Show(); } private void Awake() { board = new Board(new Vector2Int(10, 10)); } void OnGUI() { HeaderGUI(); board.OnGUI(); board.OnClick(); } private void Update() { Repaint(); } private void HeaderGUI() { GUIStyle style = GUI.skin.label; style.normal.textColor = Color.red; style.normal.background = MineSweeperConstants.CELL_CLICKED; GUILayout.BeginHorizontal(); GUILayout.Label("000", style); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); } public static Color getColor(int count){ switch (count) { case 0: return new Color(0.75f, 0.75f, 0.75f); case 1: return Color.blue; case 2: return Color.green; case 3: return Color.red; case 4: return new Color(0, 0, 0.54f); case 5: return new Color(0.64f, 0.16f, 0.16f); case 6: return Color.cyan; case 7: return Color.grey; case 8: return Color.black; default: return Color.black; } } } public class Cell { public bool isOpen = false; public bool containsMine = false; public int adjacentMines = 0; public Board board; private Rect rect; Vector2Int coords; public bool isMarked = false; public Cell(Board board, Vector2Int coords) { this.board = board; this.coords = coords; rect = new Rect(coords.x * MineSweeperConstants.CELL_SIZE + 1, coords.y * MineSweeperConstants.CELL_SIZE + 1, MineSweeperConstants.CELL_SIZE, MineSweeperConstants.CELL_SIZE); } public void DrawCell() { if (!isOpen) { if (isMarked) GUI.Label(rect, "!",MineSweeperConstants.STYLE_CELL_BUTTON); else { GUI.Label(rect, "", MineSweeperConstants.STYLE_CELL_BUTTON); } } else { if (containsMine) GUI.Label(rect, "X",MineSweeperConstants.STYLE_CELL_CLICKED); else { MineSweeperConstants.STYLE_CELL_CLICKED.normal.textColor = MineSweeperEditor.getColor(adjacentMines); GUI.Label(rect,adjacentMines.ToString(), MineSweeperConstants.STYLE_CELL_CLICKED); MineSweeperConstants.STYLE_CELL_CLICKED.normal.textColor = Color.black; } } } public void OnClick(int button, Vector2 mousePos) { if (rect.Contains(mousePos)) { if (button == 0) DoLeftClick(); else if (button == 1) DoRightClick(); } } private void DoLeftClick() { if (isOpen) return; isOpen = true; Cell[] adjacentCells = board.AdjacentCells(coords); foreach (Cell cell in adjacentCells) { if (cell.containsMine) adjacentMines++; } if (adjacentMines == 0 && !containsMine) foreach (Cell cell in adjacentCells) if (!cell.isOpen) cell.DoLeftClick(); } private void DoRightClick() { isMarked = !isMarked; } }