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.

160 lines
3.8 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 0:
  43. return new Color(0.75f, 0.75f, 0.75f);
  44. case 1:
  45. return Color.blue;
  46. case 2:
  47. return Color.green;
  48. case 3:
  49. return Color.red;
  50. case 4:
  51. return new Color(0, 0, 0.54f);
  52. case 5:
  53. return new Color(0.64f, 0.16f, 0.16f);
  54. case 6:
  55. return Color.cyan;
  56. case 7:
  57. return Color.grey;
  58. case 8:
  59. return Color.black;
  60. default:
  61. return Color.black;
  62. }
  63. }
  64. }
  65. public class Cell {
  66. public bool isOpen = false;
  67. public bool containsMine = false;
  68. public int adjacentMines = 0;
  69. public Board board;
  70. private Rect rect;
  71. Vector2Int coords;
  72. public bool isMarked = false;
  73. public Cell(Board board, Vector2Int coords)
  74. {
  75. this.board = board;
  76. this.coords = coords;
  77. rect = new Rect(coords.x * MineSweeperConstants.CELL_SIZE + 1, coords.y * MineSweeperConstants.CELL_SIZE + 1, MineSweeperConstants.CELL_SIZE, MineSweeperConstants.CELL_SIZE);
  78. }
  79. public void DrawCell()
  80. {
  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. if (containsMine)
  90. GUI.Label(rect, "X",MineSweeperConstants.STYLE_CELL_CLICKED);
  91. else {
  92. MineSweeperConstants.STYLE_CELL_CLICKED.normal.textColor = MineSweeperEditor.getColor(adjacentMines);
  93. GUI.Label(rect,adjacentMines.ToString(), MineSweeperConstants.STYLE_CELL_CLICKED);
  94. MineSweeperConstants.STYLE_CELL_CLICKED.normal.textColor = Color.black;
  95. }
  96. }
  97. }
  98. public void OnClick(int button, Vector2 mousePos)
  99. {
  100. if (rect.Contains(mousePos)) {
  101. if (button == 0)
  102. DoLeftClick();
  103. else if (button == 1)
  104. DoRightClick();
  105. }
  106. }
  107. private void DoLeftClick()
  108. {
  109. if (isOpen)
  110. return;
  111. isOpen = true;
  112. Cell[] adjacentCells = board.AdjacentCells(coords);
  113. foreach (Cell cell in adjacentCells) {
  114. if (cell.containsMine)
  115. adjacentMines++;
  116. }
  117. if (adjacentMines == 0 && !containsMine)
  118. foreach (Cell cell in adjacentCells)
  119. if (!cell.isOpen)
  120. cell.DoLeftClick();
  121. }
  122. private void DoRightClick()
  123. {
  124. isMarked = !isMarked;
  125. }
  126. }