GridHelper bug解决

上级 2057a6be
...@@ -3,6 +3,9 @@ using System.Collections.Generic; ...@@ -3,6 +3,9 @@ using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEditor; using UnityEditor;
using UnityEngine.UI; using UnityEngine.UI;
using System;
using Object = UnityEngine.Object;
public class MyEditor : MonoBehaviour public class MyEditor : MonoBehaviour
{ {
[MenuItem("Tools/SetNameBySprite")] [MenuItem("Tools/SetNameBySprite")]
...@@ -20,4 +23,34 @@ public class MyEditor : MonoBehaviour ...@@ -20,4 +23,34 @@ public class MyEditor : MonoBehaviour
} }
} }
} }
[MenuItem("Tools/MyWindow")]
static void OpenToolWindow()
{
EditorWindow.GetWindow<MyWondow>();
}
}
class MyWondow:EditorWindow
{
string findTypeName="";
private void OnGUI()
{
findTypeName=EditorGUILayout.TextField("组件类型:",findTypeName);
List<Object> list = new List<Object>();
if (GUILayout.Button("根据组件查找子物体"))
{
Debug.Log(Selection.activeGameObject);
MonoBehaviour[] bhvs= (Selection.activeObject as GameObject).GetComponentsInChildren<MonoBehaviour>();
for (int i = 0; i < bhvs.Length; i++)
{
string type=(bhvs[i].GetType().Name);
if (type==findTypeName)
{
list.Add(bhvs[i].gameObject);
}
}
Selection.objects = list.ToArray();
}
}
} }
...@@ -14,14 +14,14 @@ MonoBehaviour: ...@@ -14,14 +14,14 @@ MonoBehaviour:
m_EditorClassIdentifier: m_EditorClassIdentifier:
levels: levels:
- grid: {fileID: 5905246775763589814, guid: e8d3b3d184ba6a549b53e4070e002383, type: 3} - grid: {fileID: 5905246775763589814, guid: e8d3b3d184ba6a549b53e4070e002383, type: 3}
bombCount: 5 bombCount: 9
r: 9 r: 9
c: 9 c: 9
- grid: {fileID: 6272468980495940245, guid: c5139b7a1d02bd64692db09bfead94c7, type: 3} - grid: {fileID: 6272468980495940245, guid: c5139b7a1d02bd64692db09bfead94c7, type: 3}
bombCount: 0 bombCount: 18
r: 16 r: 16
c: 16 c: 16
- grid: {fileID: 0} - grid: {fileID: 6272468980495940245, guid: f5db5bb63fa770849b8d27d2a7e6dbbe, type: 3}
bombCount: 0 bombCount: 30
r: 0 r: 24
c: 0 c: 16
此差异已折叠。
此差异已折叠。
fileFormatVersion: 2
guid: f5db5bb63fa770849b8d27d2a7e6dbbe
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
...@@ -24,8 +24,8 @@ public class GridHelper ...@@ -24,8 +24,8 @@ public class GridHelper
/// <param name="index">一维索引</param> /// <param name="index">一维索引</param>
public Rc GetRc(int index) public Rc GetRc(int index)
{ {
int r= index / rowLength;//一维索引/行长度得到行 int r= index / columeLength;//一维索引/列长度得到行
int c = index % rowLength; int c = index % columeLength;
return new Rc(r,c); return new Rc(r,c);
} }
...@@ -39,7 +39,7 @@ public class GridHelper ...@@ -39,7 +39,7 @@ public class GridHelper
//二位索引转一维索引 //二位索引转一维索引
public int GetIndex(int r,int c) //2,1 -> 9 public int GetIndex(int r,int c) //2,1 -> 9
{ {
return r * rowLength +c; return r * columeLength +c;
} }
......
...@@ -4,7 +4,8 @@ using System.Collections.Generic; ...@@ -4,7 +4,8 @@ using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
using DG.Tweening; using DG.Tweening;
using System;
using Random = UnityEngine.Random;
public class Panel_Play : MonoBehaviour public class Panel_Play : MonoBehaviour
{ {
Transform grid;//格子,根据用户选择的等级动态加载 Transform grid;//格子,根据用户选择的等级动态加载
...@@ -22,20 +23,20 @@ public class Panel_Play : MonoBehaviour ...@@ -22,20 +23,20 @@ public class Panel_Play : MonoBehaviour
//计时器 1秒刷新一次 //计时器 1秒刷新一次
IEnumerator Timer() IEnumerator Timer()
{ {
startTime = Time.time; startTime = Time.time;
while (true) while (true)
{ {
yield return new WaitForSeconds(1); yield return new WaitForSeconds(1);
float time = (Time.time - startTime); float time = (Time.time - startTime);
int minute= (int)time / 60; int minute = (int)time / 60;
int sec = (int)time % 60; int sec = (int)time % 60;
text_Time.text = string.Format("{0:00}:{1:00}",minute,sec); text_Time.text = string.Format("{0:00}:{1:00}", minute, sec);
} }
} }
private int CurrentTool private int CurrentTool
{ {
...@@ -53,8 +54,10 @@ public class Panel_Play : MonoBehaviour ...@@ -53,8 +54,10 @@ public class Panel_Play : MonoBehaviour
} }
//设置 剩余可标记数量 //设置 剩余可标记数量
public int FlagCount { get => flagCount; public int FlagCount
set {
get => flagCount;
set
{ {
flagCount = value; flagCount = value;
text_bomgFlagCount.text = flagCount.ToString(); text_bomgFlagCount.text = flagCount.ToString();
...@@ -68,7 +71,7 @@ public class Panel_Play : MonoBehaviour ...@@ -68,7 +71,7 @@ public class Panel_Play : MonoBehaviour
if (Random.Range(0, 3) != 0) if (Random.Range(0, 3) != 0)
{ {
item.GetComponent<Image>().DOFade(0, Random.Range(0f, 1.5f)).From().SetEase(Ease.Linear); item.GetComponent<Image>().DOFade(0, Random.Range(0f, 1.5f)).From().SetEase(Ease.Linear);
} }
} }
} }
...@@ -85,24 +88,24 @@ public class Panel_Play : MonoBehaviour ...@@ -85,24 +88,24 @@ public class Panel_Play : MonoBehaviour
cells = new Cell[gameLevel.cellCount]; cells = new Cell[gameLevel.cellCount];
for (int i = 0; i < grid.childCount; i++) for (int i = 0; i < grid.childCount; i++)
{ {
cells[i]= grid.GetChild(i).GetComponent<Cell>(); cells[i] = grid.GetChild(i).GetComponent<Cell>();
} }
//创建格子助手 //创建格子助手
gHelper = new GridHelper(gameLevel.r,gameLevel.c); gHelper = new GridHelper(gameLevel.r, gameLevel.c);
//TODo随机定义雷格子 从所有格子中选出 ?个用来放雷 //TODo随机定义雷格子 从所有格子中选出 ?个用来放雷
List<Cell> cellsList = new List<Cell>(); List<Cell> cellsList = new List<Cell>();
cellsList.AddRange(cells); cellsList.AddRange(cells);
for (int i = 0; i < gameLevel.bombCount; i++) for (int i = 0; i < gameLevel.bombCount; i++)
{ {
int index = Random.Range(0, cellsList.Count); int index = UnityEngine.Random.Range(0, cellsList.Count);
cellsList[index].isBomb=true; cellsList[index].isBomb = true;
cellsList.RemoveAt(index); cellsList.RemoveAt(index);
} }
//给格子添加点击事件 //给格子添加点击事件
foreach (Cell cell in cells) foreach (Cell cell in cells)
{ {
cell.onClick.AddListener(()=> cell.onClick.AddListener(() =>
{ {
//标记开始开始计时 //标记开始开始计时
if (!started) if (!started)
...@@ -111,27 +114,63 @@ public class Panel_Play : MonoBehaviour ...@@ -111,27 +114,63 @@ public class Panel_Play : MonoBehaviour
started = true; started = true;
} }
if (CurrentTool==-1) if (CurrentTool == -1)
{ {
//开格子 //开格子
OpenCell(cell);
OpenCell(cell); if (isGameEnd())//如果游戏结束
{
OnGameEnd();
}
} }
else if(CurrentTool==0) else if (CurrentTool == 0)
{ {
Flag(cell); Flag(cell);
} }
else if (CurrentTool==1) else if (CurrentTool == 1)
{ {
//未知 //未知
if (cell.State != CellState.opened) if (cell.State != CellState.opened)
cell.State = cell.State == CellState.unknow? CellState.normal:CellState.unknow; cell.State = cell.State == CellState.unknow ? CellState.normal : CellState.unknow;
} }
}); });
} }
PlayCellAnimation(); PlayCellAnimation();
} }
private void OnGameEnd()
{
}
private bool isGameEnd()
{
//所有的雷都被标记 //或者所有不是雷的都被打开
bool allFlaged = true;
bool allOpend = true;
for (int i = 0; i < cells.Length; i++)
{
if (cells[i].isBomb)//是雷
{
if (cells[i].State != CellState.flag)
{
allFlaged = false;
}
}
else//不是雷
{
if (cells[i].State != CellState.opened)
{
allOpend = false;
}
}
}
return allFlaged || allOpend;
}
//标记 //标记
private void Flag(Cell cell) private void Flag(Cell cell)
{ {
...@@ -152,11 +191,11 @@ public class Panel_Play : MonoBehaviour ...@@ -152,11 +191,11 @@ public class Panel_Play : MonoBehaviour
private void OpenCell(Cell cell) private void OpenCell(Cell cell)
{ {
if (cell.State != CellState.normal) return; if (cell.State != CellState.normal) return;
if (cell.isBomb) if (cell.isBomb)
{ {
//游戏结束 //TOTO游戏结束
//显示所的雷 //显示所的雷
for (int i = 0; i < cells.Length; i++) for (int i = 0; i < cells.Length; i++)
{ {
...@@ -169,12 +208,12 @@ public class Panel_Play : MonoBehaviour ...@@ -169,12 +208,12 @@ public class Panel_Play : MonoBehaviour
} }
else else
{ {
int bombNumber=0; int bombNumber = 0;
//显示周围有几个雷 //显示周围有几个雷
for (int i = 0; i < Rc.AllDirections.Length; i++) for (int i = 0; i < Rc.AllDirections.Length; i++)
{ {
int dirIndex; int dirIndex;
bool exist= gHelper.DirIndex(cell.transform.GetSiblingIndex(),Rc.AllDirections[i],out dirIndex); bool exist = gHelper.DirIndex(cell.transform.GetSiblingIndex(), Rc.AllDirections[i], out dirIndex);
if (exist) if (exist)
{ {
if (cells[dirIndex].isBomb) if (cells[dirIndex].isBomb)
...@@ -185,14 +224,14 @@ public class Panel_Play : MonoBehaviour ...@@ -185,14 +224,14 @@ public class Panel_Play : MonoBehaviour
} }
//文字显示 //文字显示
string text = bombNumber.ToString(); string text = bombNumber.ToString();
cell.text.text = text == "0" ? "" : text ; cell.text.text = text == "0" ? "" : text;
cell.text.color = Color.black; cell.text.color = Color.black;
//装为 已打开 //装为 已打开
cell.State = CellState.opened; cell.State = CellState.opened;
//换个白色背景图 //换个白色背景图
cell.GetComponent<Image>().sprite = cellWhite; cell.GetComponent<Image>().sprite = cellWhite;
if (bombNumber==0) if (bombNumber == 0)
{ {
//打开周围的所有格子 //打开周围的所有格子
for (int i = 0; i < Rc.AllDirections.Length; i++) for (int i = 0; i < Rc.AllDirections.Length; i++)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册