You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
3.9 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. public class MineSweeperEditor : EditorWindow {
  6. public int MineCount = 10;
  7. Board board;
  8. // Add menu named "My Window" to the Window menu
  9. [MenuItem("Addon/Mine Sweeper")]
  10. static void Init()
  11. {
  12. // Get existing open window or if none, make a new one:
  13. MineSweeperEditor window = (MineSweeperEditor)EditorWindow.GetWindow(typeof(MineSweeperEditor));
  14. window.Show();
  15. }
  16. private void Awake()
  17. {
  18. board = new Board(new Vector2Int(10, 10));
  19. }
  20. void OnGUI()
  21. {
  22. HeaderGUI();
  23. board.OnGUI();
  24. board.OnClick();
  25. }
  26. private void Update()
  27. {
  28. Repaint();
  29. }
  30. private void HeaderGUI()
  31. {
  32. GUIStyle style = GUI.skin.label;
  33. style.normal.textColor = Color.red;
  34. style.normal.background = MineSweeperConstants.CELL_CLICKED;
  35. GUILayout.BeginHorizontal();
  36. GUILayout.Label("000", style);
  37. GUILayout.FlexibleSpace();
  38. GUILayout.EndHorizontal();
  39. }
  40. public static Color getColor(int count){
  41. switch (count) {
  42. case 1:
  43. return Color.blue;
  44. case 2:
  45. return Color.green;
  46. case 3:
  47. return Color.red;
  48. case 4:
  49. return new Color(0, 0, 0.54f);
  50. case 5:
  51. return new Color(0.64f, 0.16f, 0.16f);
  52. case 6:
  53. return Color.cyan;
  54. case 7:
  55. return Color.grey;
  56. case 8:
  57. return Color.black;
  58. default:
  59. return Color.black;
  60. }
  61. }
  62. }
  63. public class Cell {
  64. public bool isOpen = false;
  65. public bool containsMine = false;
  66. public int adjacentMines = 0;
  67. public Board board;
  68. private Rect rect;
  69. Vector2Int coords;
  70. public bool isMarked = false;
  71. public Cell(Board board, Vector2Int coords)
  72. {
  73. this.board = board;
  74. this.coords = coords;
  75. rect = new Rect(coords.x * MineSweeperConstants.CELL_SIZE + 1, coords.y * MineSweeperConstants.CELL_SIZE + 1, MineSweeperConstants.CELL_SIZE, MineSweeperConstants.CELL_SIZE);
  76. }
  77. public void DrawCell()
  78. {
  79. GUIStyle style = new GUIStyle();
  80. style.alignment = TextAnchor.MiddleCenter;
  81. if (!isOpen) {
  82. if (isMarked)
  83. GUI.Label(rect, "!",MineSweeperConstants.STYLE_CELL_BUTTON);
  84. else
  85. {
  86. GUI.Label(rect, "", MineSweeperConstants.STYLE_CELL_BUTTON);
  87. }
  88. } else {
  89. GUI.DrawTexture(rect, MineSweeperConstants.CELL_CLICKED);
  90. if (containsMine)
  91. GUI.Label(rect, "X",style);
  92. else if (adjacentMines > 0) {
  93. style.normal.textColor = MineSweeperEditor.getColor(adjacentMines);
  94. GUI.Label(rect,adjacentMines.ToString(),style);
  95. //GUI.contentColor = Color.black;
  96. }
  97. }
  98. }
  99. public void OnClick(int button, Vector2 mousePos)
  100. {
  101. if (rect.Contains(mousePos)) {
  102. if (button == 0)
  103. DoLeftClick();
  104. else if (button == 1)
  105. DoRightClick();
  106. //EditorUtility.SetDirty(editor);
  107. }
  108. Debug.Log("MousePos: " + mousePos);
  109. }
  110. private void DoLeftClick()
  111. {
  112. if (isOpen)
  113. return;
  114. isOpen = true;
  115. Cell[] adjacentCells = board.AdjacentCells(coords);
  116. foreach (Cell cell in adjacentCells) {
  117. if (cell.containsMine)
  118. adjacentMines++;
  119. }
  120. if (adjacentMines == 0 && !containsMine)
  121. foreach (Cell cell in adjacentCells)
  122. if (!cell.isOpen)
  123. cell.DoLeftClick();
  124. }
  125. private void DoRightClick()
  126. {
  127. isMarked = !isMarked;
  128. }
  129. }