using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; public class MineSweeperEditor : EditorWindow { public const int CELL_SIZE = 20; public Texture2D ButtonImage; public Texture2D EmptyImage; public int MineCount = 10; private Cell[,] Board = new Cell[10, 10]; // 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 Cell[10, 10]; for (int i = 0; i < Board.GetLength(0); i++) { for (int j = 0; j < Board.GetLength(1); j++) { Board[i, j] = new Cell(this, i, j); } } int placedMines = 0; while (placedMines < MineCount) { int x = Random.Range(0, Board.GetLength(0) - 1); int y = Random.Range(0, Board.GetLength(1) - 1); if (!Board[x, y].containsMine) { Board[x, y].containsMine = true; Debug.Log("Mine at: (" + x + ", " + y + ")"); placedMines++; } } ButtonImage = CreateButtonTexture(CELL_SIZE, Color.white, new Color(0.75f, 0.75f, 0.75f), Color.black); EmptyImage = CreateButtonTexture(CELL_SIZE, new Color(0.2f, 0.2f, 0.2f), new Color(0.75f, 0.75f, 0.75f), new Color(0.8f, 0.8f, 0.8f)); } void OnGUI() { HeaderGUI(); BoardGUI(); DoClick(); } private void Update() { Repaint(); } private void HeaderGUI() { GUIStyle style = GUI.skin.label; style.normal.textColor = Color.red; style.normal.background = EmptyImage; GUILayout.BeginHorizontal(); GUILayout.Label("000", style); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); } private void BoardGUI() { for (int i = 0; i < Board.GetLength(0); i++) { GUILayout.BeginHorizontal(); for (int j = 0; j < Board.GetLength(1); j++) { Board[i, j].DrawCell(); } GUILayout.EndHorizontal(); GUILayout.Space(CELL_SIZE + 1); } } private void DoClick() { Event e = Event.current; if (e.type == EventType.MouseUp) { foreach (Cell cell in Board) { cell.OnClick(e.button, e.mousePosition); } } } public static Texture2D CreateButtonTexture(int size, Color highLight, Color mainColor, Color lowLight) { Texture2D retval = new Texture2D(size, size); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (i == 0 || j == size - 1) { retval.SetPixel(i, j, highLight); } else if (i == size - 1 || j == 0) { retval.SetPixel(i, j, lowLight); } else retval.SetPixel(i, j, mainColor); } } retval.Apply(); return retval; } public Cell[] AdjacentCells(int x, int y) { List retVal = new List(); for (int i = -1; i <= 1; i++) { if (x + i < 0 || x + i >= Board.GetLength(0)) continue; for (int j = -1; j <= 1; j++) { if (y + j < 0 || y + j >= Board.GetLength(0)) continue; if (i == 0 && j == 0) continue; retVal.Add(Board[x + i, y + j]); } } return retVal.ToArray(); } public static Color getColor(int count){ switch (count) { 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 MineSweeperEditor editor; private Rect rect; int x; int y; public bool isMarked = false; public Cell(MineSweeperEditor editor, int x, int y) { this.editor = editor; this.x = x; this.y = y; } public void DrawCell() { GUILayout.Space(MineSweeperEditor.CELL_SIZE + 1); if (Event.current.type == EventType.Repaint) rect = GUILayoutUtility.GetLastRect(); rect.height = MineSweeperEditor.CELL_SIZE; rect.width = MineSweeperEditor.CELL_SIZE; GUIStyle style = new GUIStyle(); style.alignment = TextAnchor.MiddleCenter; if (!isOpen) { GUI.DrawTexture(rect, editor.ButtonImage); if (isMarked) GUI.Label(rect, "!",style); } else { GUI.DrawTexture(rect, editor.EmptyImage); if (containsMine) GUI.Label(rect, "X",style); else if (adjacentMines > 0) { style.normal.textColor = MineSweeperEditor.getColor(adjacentMines); GUI.Label(rect,adjacentMines.ToString(),style); //GUI.contentColor = Color.black; } } } public void OnClick(int button, Vector2 mousePos) { if (rect.Contains(mousePos)) { if (button == 0) DoLeftClick(); else if (button == 1) DoRightClick(); EditorUtility.SetDirty(editor); } Debug.Log("MousePos: " + mousePos); } private void DoLeftClick() { isOpen = true; Cell[] adjacentCells = editor.AdjacentCells(x, y); 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; } }