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 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()
|
|
{
|
|
GUIStyle style = new GUIStyle();
|
|
style.alignment = TextAnchor.MiddleCenter;
|
|
|
|
if (!isOpen) {
|
|
if (isMarked)
|
|
GUI.Label(rect, "!",MineSweeperConstants.STYLE_CELL_BUTTON);
|
|
else
|
|
{
|
|
GUI.Label(rect, "", MineSweeperConstants.STYLE_CELL_BUTTON);
|
|
}
|
|
|
|
} else {
|
|
GUI.DrawTexture(rect, MineSweeperConstants.CELL_CLICKED);
|
|
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()
|
|
{
|
|
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;
|
|
}
|
|
}
|