Browse Source

Added colors and clicking on nothing opens everything

feature/Minesweeper
Joshua Reason 6 years ago
parent
commit
1742e5b032
4 changed files with 289 additions and 1 deletions
  1. +8
    -0
      Assets/Editor Add On/MineSweeper.meta
  2. +269
    -0
      Assets/Editor Add On/MineSweeper/MineSweeperEditor.cs
  3. +11
    -0
      Assets/Editor Add On/MineSweeper/MineSweeperEditor.cs.meta
  4. +1
    -1
      ProjectSettings/ProjectVersion.txt

+ 8
- 0
Assets/Editor Add On/MineSweeper.meta View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: d4bd0c05ca2429b43bfc7f02fe4a9101
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

+ 269
- 0
Assets/Editor Add On/MineSweeper/MineSweeperEditor.cs View File

@ -0,0 +1,269 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
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];
// Add menu named "My Window" to the Window menu
[MenuItem("Addon/Mine Sweeper")]
static void Init()
{
// Get existing open window or if none, make a new one:
MineSweeperEditor window = (MineSweeperEditor)EditorWindow.GetWindow(typeof(MineSweeperEditor));
window.Show();
}
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));
}
void OnGUI()
{
HeaderGUI();
BoardGUI();
DoClick();
}
private void Update()
{
Repaint();
}
private void HeaderGUI()
{
GUIStyle style = GUI.skin.label;
style.normal.textColor = Color.red;
style.normal.background = EmptyImage;
GUILayout.BeginHorizontal();
GUILayout.Label("000", style);
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
}
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<Cell> retVal = new List<Cell>();
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){
switch (count) {
case 1:
return Color.blue;
case 2:
return Color.green;
case 3:
return Color.red;
case 4:
return new Color(0, 0, 0.54f);
case 5:
return new Color(0.64f, 0.16f, 0.16f);
case 6:
return Color.cyan;
case 7:
return Color.grey;
case 8:
return Color.black;
default:
return Color.black;
}
}
}
public class Cell {
public bool isOpen = false;
public bool containsMine = false;
public int adjacentMines = 0;
public MineSweeperEditor editor;
private Rect rect;
int x;
int y;
public bool isMarked = false;
public Cell(MineSweeperEditor editor, int x, int y)
{
this.editor = editor;
this.x = x;
this.y = y;
}
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);
} else {
GUI.DrawTexture(rect, editor.EmptyImage);
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;
}
}
}
public void OnClick(int button, Vector2 mousePos)
{
if (rect.Contains(mousePos)) {
if (button == 0)
DoLeftClick();
else if (button == 1)
DoRightClick();
EditorUtility.SetDirty(editor);
}
Debug.Log("MousePos: " + mousePos);
}
private void DoLeftClick()
{
isOpen = true;
Cell[] adjacentCells = editor.AdjacentCells(x, y);
foreach (Cell cell in adjacentCells) {
if (cell.containsMine)
adjacentMines++;
}
if (adjacentMines == 0 && !containsMine)
foreach (Cell cell in adjacentCells)
if (!cell.isOpen)
cell.DoLeftClick();
}
private void DoRightClick()
{
isMarked = !isMarked;
}
}

+ 11
- 0
Assets/Editor Add On/MineSweeper/MineSweeperEditor.cs.meta View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b2427cb8299c3ea4c9acc48e65e85e4d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

+ 1
- 1
ProjectSettings/ProjectVersion.txt View File

@ -1 +1 @@
m_EditorVersion: 2018.1.0f2
m_EditorVersion: 2018.1.3f1

Loading…
Cancel
Save