From 4871b8f94264bd5847d50aaeeb2c850949f906c0 Mon Sep 17 00:00:00 2001 From: "DESKTOP-B4ME9GF\\Joshua Reason" Date: Wed, 1 Aug 2018 15:17:35 +1000 Subject: [PATCH] Re-working Gui to use contants and no longer use guilayout + created board class --- .../MineSweeper/MineSweeperEditor.cs | 147 ++----------- .../MineSweeper/MineSweeperUtil.cs | 197 ++++++++++++++++++ .../MineSweeper/MineSweeperUtil.cs.meta | 11 + ProjectSettings/ProjectSettings.asset | 2 +- ProjectSettings/ProjectVersion.txt | 2 +- 5 files changed, 233 insertions(+), 126 deletions(-) create mode 100644 Assets/Editor Add On/MineSweeper/MineSweeperUtil.cs create mode 100644 Assets/Editor Add On/MineSweeper/MineSweeperUtil.cs.meta diff --git a/Assets/Editor Add On/MineSweeper/MineSweeperEditor.cs b/Assets/Editor Add On/MineSweeper/MineSweeperEditor.cs index c2e9be0..3f2d6bc 100644 --- a/Assets/Editor Add On/MineSweeper/MineSweeperEditor.cs +++ b/Assets/Editor Add On/MineSweeper/MineSweeperEditor.cs @@ -5,13 +5,9 @@ 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]; + Board board; @@ -26,37 +22,14 @@ public class MineSweeperEditor : EditorWindow { 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() { HeaderGUI(); - BoardGUI(); - DoClick(); + board.OnGUI(); + board.OnClick(); } private void Update() @@ -69,7 +42,7 @@ public class MineSweeperEditor : EditorWindow { GUIStyle style = GUI.skin.label; style.normal.textColor = Color.red; - style.normal.background = EmptyImage; + style.normal.background = MineSweeperConstants.CELL_CLICKED; GUILayout.BeginHorizontal(); 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 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){ @@ -184,41 +85,36 @@ public class Cell { public bool containsMine = false; public int adjacentMines = 0; - public MineSweeperEditor editor; + public Board board; private Rect rect; - int x; - int y; - + Vector2Int coords; 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() { - 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); + GUI.Label(rect, "!",MineSweeperConstants.STYLE_CELL_BUTTON); + else + { + GUI.Label(rect, "", MineSweeperConstants.STYLE_CELL_BUTTON); + } } else { - GUI.DrawTexture(rect, editor.EmptyImage); + GUI.DrawTexture(rect, MineSweeperConstants.CELL_CLICKED); if (containsMine) GUI.Label(rect, "X",style); else if (adjacentMines > 0) { @@ -238,7 +134,7 @@ public class Cell { else if (button == 1) DoRightClick(); - EditorUtility.SetDirty(editor); + //EditorUtility.SetDirty(editor); } Debug.Log("MousePos: " + mousePos); @@ -246,9 +142,12 @@ public class Cell { private void DoLeftClick() { + if (isOpen) + return; + isOpen = true; - Cell[] adjacentCells = editor.AdjacentCells(x, y); + Cell[] adjacentCells = board.AdjacentCells(coords); foreach (Cell cell in adjacentCells) { if (cell.containsMine) diff --git a/Assets/Editor Add On/MineSweeper/MineSweeperUtil.cs b/Assets/Editor Add On/MineSweeper/MineSweeperUtil.cs new file mode 100644 index 0000000..a705955 --- /dev/null +++ b/Assets/Editor Add On/MineSweeper/MineSweeperUtil.cs @@ -0,0 +1,197 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEditor; + +public static class MineSweeperConstants +{ + + private static Texture2D Cell_Button; + private static Texture2D Cell_Clicked; + private static GUIStyle Button_Style; + + /// + /// Height and Width of a Cell in Pixels + /// + public static int CELL_SIZE { get { return 20; } } + + /// + /// Texture of Cell when it hasn't been clicked + /// + public static Texture2D CELL_BUTTON + { + get + { + if (Cell_Button == null) + { + Cell_Button = CreateButtonTexture(Color.white, new Color(0.75f, 0.75f, 0.75f), Color.black); + Debug.Log("Generating CELL_BUTTON"); + } + + return Cell_Button; + } + } + + /// + /// Texture of Cell when it has been clicked + /// + public static Texture2D CELL_CLICKED + { + get + { + if (Cell_Clicked == null) + { + Cell_Clicked = CreateButtonTexture(new Color(0.2f, 0.2f, 0.2f), new Color(0.75f, 0.75f, 0.75f), new Color(0.8f, 0.8f, 0.8f)); + Debug.Log("Generating CELL_CLicked"); + } + + return Cell_Clicked; + } + } + + /// + /// Style to use with a Cell_Button + /// + public static GUIStyle STYLE_CELL_BUTTON + { + get + { + if (Button_Style == null) + { + Debug.Log("Creating Cell Style"); + Button_Style = new GUIStyle(); + Button_Style.alignment = TextAnchor.MiddleCenter; + Button_Style.normal.background = CELL_BUTTON; + } + return Button_Style; + } + } + + private static Texture2D CreateButtonTexture(Color highLight, Color mainColor, Color lowLight) + { + Texture2D retval = new Texture2D(CELL_SIZE, CELL_SIZE); + + for (int i = 0; i < CELL_SIZE; i++) + { + for (int j = 0; j < CELL_SIZE; j++) + { + if (i == 0 || j == CELL_SIZE - 1) + { + retval.SetPixel(i, j, highLight); + } + else if (i == CELL_SIZE - 1 || j == 0) + { + retval.SetPixel(i, j, lowLight); + } + else + retval.SetPixel(i, j, mainColor); + } + } + retval.Apply(); + return retval; + } +} + +public class Board +{ + private Cell[,] Cells; + private int MineCount = 10; + + + public Board(Vector2Int Size) + { + Cells = new Cell[Size.x, Size.y]; + + for (int i = 0; i < Size.x; i++) + for (int j = 0; j < Size.y; j++) + Cells[i, j] = new Cell(this, new Vector2Int(i, j)); + + PlaceMines(new Vector2Int(0, 0)); + } + + public void OnGUI() + { + foreach (Cell cell in Cells) + cell.DrawCell(); + } + + public void OnClick() + { + Event e = Event.current; + if (e.type == EventType.MouseUp) + { + foreach (Cell cell in Cells) + cell.OnClick(e.button, e.mousePosition); + } + } + + public Cell[] AdjacentCells(Vector2Int coords) + { + List retVal = new List(); + + for (int i = -1; i <= 1; i++) + { + + if (coords.x + i < 0 || coords.x + i >= Cells.GetLength(0)) + continue; + + for (int j = -1; j <= 1; j++) + { + if (coords.y + j < 0 || coords.y + j >= Cells.GetLength(0)) + continue; + if (i == 0 && j == 0) + continue; + + retVal.Add(Cells[coords.x + i, coords.y + j]); + } + } + return retVal.ToArray(); + } + + public void PlaceMines(Vector2Int startPos) + { + int placedMines = 0; + while (placedMines < MineCount) + { + int x = Random.Range(0, Cells.GetLength(0) - 1); + int y = Random.Range(0, Cells.GetLength(1) - 1); + + if (!Cells[x, y].containsMine || startPos != new Vector2Int(x,y)) + { + Cells[x, y].containsMine = true; + Debug.Log("Mine at: (" + x + ", " + y + ")"); + placedMines++; + } + } + } + +} + +public struct Vector2Int +{ + public int x; + public int y; + + public Vector2Int(int x, int y) + { + this.x = x; + this.y = y; + } + + public override bool Equals(System.Object obj) + { + return obj is Vector2Int && this == (Vector2Int)obj; + } + public override int GetHashCode() + { + return x.GetHashCode() ^ y.GetHashCode(); + } + public static bool operator ==(Vector2Int x, Vector2Int y) + { + return x.x == y.x && x.y == y.y; + } + public static bool operator !=(Vector2Int x, Vector2Int y) + { + return !(x == y); + } +} diff --git a/Assets/Editor Add On/MineSweeper/MineSweeperUtil.cs.meta b/Assets/Editor Add On/MineSweeper/MineSweeperUtil.cs.meta new file mode 100644 index 0000000..a72aa43 --- /dev/null +++ b/Assets/Editor Add On/MineSweeper/MineSweeperUtil.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3e07fb6972ad4834d89d7b7dd378fbd1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index 2726353..23a3304 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -581,7 +581,7 @@ PlayerSettings: incrementalIl2cppBuild: {} allowUnsafeCode: 0 additionalIl2CppArgs: - scriptingRuntimeVersion: 0 + scriptingRuntimeVersion: 1 apiCompatibilityLevelPerPlatform: {} m_RenderingPath: 1 m_MobileRenderingPath: 1 diff --git a/ProjectSettings/ProjectVersion.txt b/ProjectSettings/ProjectVersion.txt index b171987..22977b3 100644 --- a/ProjectSettings/ProjectVersion.txt +++ b/ProjectSettings/ProjectVersion.txt @@ -1 +1 @@ -m_EditorVersion: 2018.1.3f1 +m_EditorVersion: 2018.1.0f2