Browse Source

Made the clicked cell also use styles

feature/Minesweeper
DESKTOP-B4ME9GF\Joshua Reason 6 years ago
parent
commit
a447a350a7
2 changed files with 113 additions and 14 deletions
  1. +7
    -14
      Assets/Editor Add On/MineSweeper/MineSweeperEditor.cs
  2. +106
    -0
      Assets/Editor Add On/MineSweeper/MineSweeperUtil.cs

+ 7
- 14
Assets/Editor Add On/MineSweeper/MineSweeperEditor.cs View File

@ -56,6 +56,8 @@ public class MineSweeperEditor : EditorWindow {
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:
@ -102,9 +104,6 @@ public class Cell {
public void DrawCell()
{
GUIStyle style = new GUIStyle();
style.alignment = TextAnchor.MiddleCenter;
if (!isOpen) {
if (isMarked)
GUI.Label(rect, "!",MineSweeperConstants.STYLE_CELL_BUTTON);
@ -114,14 +113,12 @@ public class Cell {
}
} 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;
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;
}
}
}
@ -133,11 +130,7 @@ public class Cell {
DoLeftClick();
else if (button == 1)
DoRightClick();
//EditorUtility.SetDirty(editor);
}
Debug.Log("MousePos: " + mousePos);
}
private void DoLeftClick()

+ 106
- 0
Assets/Editor Add On/MineSweeper/MineSweeperUtil.cs View File

@ -9,6 +9,7 @@ public static class MineSweeperConstants
private static Texture2D Cell_Button;
private static Texture2D Cell_Clicked;
private static GUIStyle Button_Style;
private static GUIStyle Clicked_Style;
/// <summary>
/// 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)
{
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 int x;

Loading…
Cancel
Save