|
@ -9,6 +9,7 @@ public static class MineSweeperConstants |
|
|
private static Texture2D Cell_Button; |
|
|
private static Texture2D Cell_Button; |
|
|
private static Texture2D Cell_Clicked; |
|
|
private static Texture2D Cell_Clicked; |
|
|
private static GUIStyle Button_Style; |
|
|
private static GUIStyle Button_Style; |
|
|
|
|
|
private static GUIStyle Clicked_Style; |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
/// Height and Width of a Cell in Pixels
|
|
|
/// Height and Width of a Cell in Pixels
|
|
@ -67,6 +68,24 @@ public static class MineSweeperConstants |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Style to use with a Cell_Button
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static GUIStyle STYLE_CELL_CLICKED |
|
|
|
|
|
{ |
|
|
|
|
|
get |
|
|
|
|
|
{ |
|
|
|
|
|
if (Clicked_Style == null) |
|
|
|
|
|
{ |
|
|
|
|
|
Debug.Log("Creating Cell Style Clicked"); |
|
|
|
|
|
Clicked_Style = new GUIStyle(); |
|
|
|
|
|
Clicked_Style.alignment = TextAnchor.MiddleCenter; |
|
|
|
|
|
Clicked_Style.normal.background = CELL_CLICKED; |
|
|
|
|
|
} |
|
|
|
|
|
return Clicked_Style; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private static Texture2D CreateButtonTexture(Color highLight, Color mainColor, Color lowLight) |
|
|
private static Texture2D CreateButtonTexture(Color highLight, Color mainColor, Color lowLight) |
|
|
{ |
|
|
{ |
|
|
Texture2D retval = new Texture2D(CELL_SIZE, CELL_SIZE); |
|
|
Texture2D retval = new Texture2D(CELL_SIZE, CELL_SIZE); |
|
@ -167,6 +186,93 @@ public class Board |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public class Cell |
|
|
|
|
|
{ |
|
|
|
|
|
public enum Mark { None,Flag,Question} |
|
|
|
|
|
|
|
|
|
|
|
public bool isClicked = 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 (!isClicked) |
|
|
|
|
|
{ |
|
|
|
|
|
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 (isClicked) |
|
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
isClicked = 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.isClicked) |
|
|
|
|
|
cell.DoLeftClick(); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void DoRightClick() |
|
|
|
|
|
{ |
|
|
|
|
|
isMarked = !isMarked; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public struct Vector2Int |
|
|
public struct Vector2Int |
|
|
{ |
|
|
{ |
|
|
public int x; |
|
|
public int x; |
|
|