|
@ -5,13 +5,9 @@ using UnityEditor; |
|
|
|
|
|
|
|
|
public class MineSweeperEditor : EditorWindow { |
|
|
public class MineSweeperEditor : EditorWindow { |
|
|
|
|
|
|
|
|
public const int CELL_SIZE = 20; |
|
|
|
|
|
public Texture2D ButtonImage; |
|
|
|
|
|
public Texture2D EmptyImage; |
|
|
|
|
|
|
|
|
|
|
|
public int MineCount = 10; |
|
|
public int MineCount = 10; |
|
|
|
|
|
|
|
|
private Cell[,] Board = new Cell[10, 10]; |
|
|
|
|
|
|
|
|
Board board; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -26,37 +22,14 @@ public class MineSweeperEditor : EditorWindow { |
|
|
|
|
|
|
|
|
private void Awake() |
|
|
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)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
board = new Board(new Vector2Int(10, 10)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void OnGUI() |
|
|
void OnGUI() |
|
|
{ |
|
|
{ |
|
|
HeaderGUI(); |
|
|
HeaderGUI(); |
|
|
BoardGUI(); |
|
|
|
|
|
DoClick(); |
|
|
|
|
|
|
|
|
board.OnGUI(); |
|
|
|
|
|
board.OnClick(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void Update() |
|
|
private void Update() |
|
@ -69,7 +42,7 @@ public class MineSweeperEditor : EditorWindow { |
|
|
GUIStyle style = GUI.skin.label; |
|
|
GUIStyle style = GUI.skin.label; |
|
|
|
|
|
|
|
|
style.normal.textColor = Color.red; |
|
|
style.normal.textColor = Color.red; |
|
|
style.normal.background = EmptyImage; |
|
|
|
|
|
|
|
|
style.normal.background = MineSweeperConstants.CELL_CLICKED; |
|
|
|
|
|
|
|
|
GUILayout.BeginHorizontal(); |
|
|
GUILayout.BeginHorizontal(); |
|
|
GUILayout.Label("000", style); |
|
|
GUILayout.Label("000", style); |
|
@ -79,78 +52,6 @@ public class MineSweeperEditor : EditorWindow { |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
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<Cell> retVal = new List<Cell>(); |
|
|
|
|
|
|
|
|
|
|
|
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){ |
|
|
public static Color getColor(int count){ |
|
|
|
|
|
|
|
@ -184,41 +85,36 @@ public class Cell { |
|
|
public bool containsMine = false; |
|
|
public bool containsMine = false; |
|
|
public int adjacentMines = 0; |
|
|
public int adjacentMines = 0; |
|
|
|
|
|
|
|
|
public MineSweeperEditor editor; |
|
|
|
|
|
|
|
|
public Board board; |
|
|
private Rect rect; |
|
|
private Rect rect; |
|
|
|
|
|
|
|
|
int x; |
|
|
|
|
|
int y; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Vector2Int coords; |
|
|
public bool isMarked = false; |
|
|
public bool isMarked = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Cell(MineSweeperEditor editor, int x, int y) |
|
|
|
|
|
|
|
|
public Cell(Board board, Vector2Int coords) |
|
|
{ |
|
|
{ |
|
|
this.editor = editor; |
|
|
|
|
|
this.x = x; |
|
|
|
|
|
this.y = y; |
|
|
|
|
|
|
|
|
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() |
|
|
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(); |
|
|
GUIStyle style = new GUIStyle(); |
|
|
style.alignment = TextAnchor.MiddleCenter; |
|
|
style.alignment = TextAnchor.MiddleCenter; |
|
|
|
|
|
|
|
|
if (!isOpen) { |
|
|
if (!isOpen) { |
|
|
GUI.DrawTexture(rect, editor.ButtonImage); |
|
|
|
|
|
if (isMarked) |
|
|
if (isMarked) |
|
|
GUI.Label(rect, "!",style); |
|
|
|
|
|
|
|
|
GUI.Label(rect, "!",MineSweeperConstants.STYLE_CELL_BUTTON); |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
GUI.Label(rect, "", MineSweeperConstants.STYLE_CELL_BUTTON); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} else { |
|
|
} else { |
|
|
GUI.DrawTexture(rect, editor.EmptyImage); |
|
|
|
|
|
|
|
|
GUI.DrawTexture(rect, MineSweeperConstants.CELL_CLICKED); |
|
|
if (containsMine) |
|
|
if (containsMine) |
|
|
GUI.Label(rect, "X",style); |
|
|
GUI.Label(rect, "X",style); |
|
|
else if (adjacentMines > 0) { |
|
|
else if (adjacentMines > 0) { |
|
@ -238,7 +134,7 @@ public class Cell { |
|
|
else if (button == 1) |
|
|
else if (button == 1) |
|
|
DoRightClick(); |
|
|
DoRightClick(); |
|
|
|
|
|
|
|
|
EditorUtility.SetDirty(editor); |
|
|
|
|
|
|
|
|
//EditorUtility.SetDirty(editor);
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
Debug.Log("MousePos: " + mousePos); |
|
|
Debug.Log("MousePos: " + mousePos); |
|
@ -246,9 +142,12 @@ public class Cell { |
|
|
|
|
|
|
|
|
private void DoLeftClick() |
|
|
private void DoLeftClick() |
|
|
{ |
|
|
{ |
|
|
|
|
|
if (isOpen) |
|
|
|
|
|
return; |
|
|
|
|
|
|
|
|
isOpen = true; |
|
|
isOpen = true; |
|
|
|
|
|
|
|
|
Cell[] adjacentCells = editor.AdjacentCells(x, y); |
|
|
|
|
|
|
|
|
Cell[] adjacentCells = board.AdjacentCells(coords); |
|
|
|
|
|
|
|
|
foreach (Cell cell in adjacentCells) { |
|
|
foreach (Cell cell in adjacentCells) { |
|
|
if (cell.containsMine) |
|
|
if (cell.containsMine) |
|
|