//交换两甜品(添加不符合条件回退功能) private void ExchangeSweets(GameSweet sweet1, GameSweet sweet2) { if (sweet1.CanMove() && sweet2.CanMove()) { ////如果横向和纵向交换后都没有可消除的糖果,交换位置重新归位 if (MatchSweet(sweet1, sweet2.X, sweet2.Y) != null || MatchSweet(sweet2, sweet1.X, sweet1.Y) != null) { int TempX = sweet1.X; int TempY = sweet1.Y; sweet1.MoveComponent.Move(sweet2.X, sweet2.Y, fileTime); sweet2.MoveComponent.Move(TempX, TempY, fileTime); sweets[sweet1.X, sweet1.Y] = sweet2; sweets[sweet2.X, sweet2.Y] = sweet1; } else { //两个糖果先进行交换 int TempX = sweet1.X; int TempY = sweet1.Y; sweet1.MoveComponent.Move(sweet2.X, sweet2.Y, fileTime); sweet2.MoveComponent.Move(TempX, TempY, fileTime); StartCoroutine(ClearChangge(sweet2, sweet1)); } } }
//交换甜品的方法 private void ExchangeSweets(GameSweet sweet1, GameSweet sweet2) { if (sweet1.CanMove() && sweet2.CanMove()) { sweets[sweet1.X, sweet1.Y] = sweet2; sweets[sweet2.X, sweet2.Y] = sweet1; //有返回的删除列表 if (MatchSweets(sweet1, sweet2.X, sweet2.Y) != null || MatchSweets(sweet2, sweet1.X, sweet1.Y) != null) { //先交换两个元素 int tempX = sweet1.X; int tempY = sweet1.Y; sweet1.MoveComponent.Move(sweet2.X, sweet2.Y, time); sweet2.MoveComponent.Move(tempX, tempY, time); //清除 ClearAllMatchSweet(); //填充 StartCoroutine(AllFill()); } else { //不做处理 sweets[sweet1.X, sweet1.Y] = sweet1; sweets[sweet2.X, sweet2.Y] = sweet2; } } }
/// <summary> /// 交换两个甜品的方法 /// </summary> public void ExchangeSweets(GameSweet sweet1, GameSweet sweet2) { if (sweet1.CanMove() && sweet2.CanMove()) { sweets[sweet1.X, sweet1.Y] = sweet2; sweets[sweet2.X, sweet2.Y] = sweet1; if (MatchSweets(sweet1, sweet2.X, sweet2.Y) != null || MatchSweets(sweet2, sweet1.X, sweet1.Y) != null) { int tempX = sweet1.X; int tempy = sweet1.Y; sweet1.MovedComponent.Move(sweet2.X, sweet2.Y, fillTime); sweet2.MovedComponent.Move(tempX, tempy, fillTime); ClearAllMatchedSweet(); StartCoroutine(AllFill()); pressedSweet = null; enteredSweet = null; } else { sweets[sweet1.X, sweet1.Y] = sweet1; sweets[sweet2.X, sweet2.Y] = sweet2; } } }
//鼠标操作交换位置时 public void ExChangeSweets(GameSweet sweetOnclick1, GameSweet sweetOnclick2) { if (sweetOnclick1.Equals(sweetOnclick2)) { return; } if (sweetOnclick1.CanMove() && sweetOnclick2.CanMove()) { sweetsarray[sweetOnclick1.Y, sweetOnclick1.X] = sweetOnclick2; sweetsarray[sweetOnclick2.Y, sweetOnclick2.X] = sweetOnclick1;//记录位子更新 if (MatchSweets(sweetOnclick1, sweetOnclick2.X, sweetOnclick2.Y) != null || MatchSweets(sweetOnclick2, sweetOnclick1.X, sweetOnclick1.Y) != null || sweetOnclick1.Type == SweetsType.RAINBOWCANDY || sweetOnclick2.Type == SweetsType.RAINBOWCANDY)//可以交换时 { sweetsarray[sweetOnclick1.Y, sweetOnclick1.X] = sweetOnclick2; sweetsarray[sweetOnclick2.Y, sweetOnclick2.X] = sweetOnclick1;//记录位子更新 int tempx = sweetOnclick1.X , tempy = sweetOnclick1.Y; sweetOnclick1.MoveComponent.Move(sweetOnclick2.X, sweetOnclick2.Y, fillTime); sweetOnclick2.MoveComponent.Move(tempx, tempy, fillTime); if (sweetOnclick1.Type == SweetsType.RAINBOWCANDY && sweetOnclick1.CanClear() && sweetOnclick2.CanClear()) { ClearColorSweet clearcolor = sweetOnclick1.GetComponent <ClearColorSweet>(); if (clearcolor != null) { clearcolor.ClearColor = sweetOnclick2.ColorComponent.Color; } ClearSweet(sweetOnclick1.X, sweetOnclick1.Y); } if (sweetOnclick2.Type == SweetsType.RAINBOWCANDY && sweetOnclick1.CanClear() && sweetOnclick2.CanClear()) { ClearColorSweet clearcolor = sweetOnclick2.GetComponent <ClearColorSweet>(); if (clearcolor != null) { clearcolor.ClearColor = sweetOnclick1.ColorComponent.Color; } ClearSweet(sweetOnclick2.X, sweetOnclick2.Y); } pressedSweet = null; enteredSweet = null; ClearAllMatchedSweet(); StartCoroutine(AllFill()); } else { sweetsarray[sweetOnclick1.Y, sweetOnclick1.X] = sweetOnclick1; sweetsarray[sweetOnclick2.Y, sweetOnclick2.X] = sweetOnclick2;//记录位子更新 } } }
//交换两个甜品的方法 private void ExchangeSweets(GameSweet sweet1, GameSweet sweet2) { if (sweet1.CanMove() && sweet2.CanMove()) { sweets[sweet1.X, sweet1.Y] = sweet2; sweets[sweet2.X, sweet2.Y] = sweet1; if (MatchSweets(sweet1, sweet2.X, sweet2.Y) != null || MatchSweets(sweet2, sweet1.X, sweet1.Y) != null || sweet1.Type == SweetsType.RAINBOWCANDY || sweet2.Type == SweetsType.RAINBOWCANDY) { int tempX = sweet1.X; int tempY = sweet1.Y; sweet1.MovedComponent.Move(sweet2.X, sweet2.Y, fillTime); sweet2.MovedComponent.Move(tempX, tempY, fillTime); if (sweet1.Type == SweetsType.RAINBOWCANDY && sweet1.CanClear() && sweet2.CanClear()) { ClearColorSweet clearColor = sweet1.GetComponent <ClearColorSweet>(); if (clearColor != null) { clearColor.ClearColor = sweet2.ColoredComponent.Color; } ClearSweet(sweet1.X, sweet1.Y); } if (sweet2.Type == SweetsType.RAINBOWCANDY && sweet2.CanClear() && sweet1.CanClear()) { ClearColorSweet clearColor = sweet2.GetComponent <ClearColorSweet>(); if (clearColor != null) { clearColor.ClearColor = sweet1.ColoredComponent.Color; } ClearSweet(sweet2.X, sweet2.Y); } ClearAllMatchedSweet(); StartCoroutine(AllFill()); pressedSweet = null; enteredSweet = null; } else { sweets[sweet1.X, sweet1.Y] = sweet1; sweets[sweet2.X, sweet2.Y] = sweet2; } } }
/// <summary> /// 交换物品位置 /// </summary> /// <param name="sweet1"></param> /// <param name="sweet2"></param> private void ExChangeSweets(GameSweet sweet1, GameSweet sweet2) { if (sweet1.CanMove() && sweet2.CanMove()) //如果2个物品都能移动 { sweets[sweet1.X, sweet1.Y] = sweet2; sweets[sweet2.X, sweet2.Y] = sweet1; if (MatchSweets(sweet1, sweet2.X, sweet2.Y) != null || MatchSweets(sweet2, sweet1.X, sweet1.Y) != null || sweet1.Type == SweetsType.RAINBOWCANDY || sweet2.Type == SweetsType.RAINBOWCANDY) //如果完成匹配 { int tempX = sweet1.X; int tempY = sweet1.Y; sweet1.MovedComponet.Move(sweet2.X, sweet2.Y, fillTime); sweet2.MovedComponet.Move(tempX, tempY, fillTime); if (sweet1.Type == SweetsType.RAINBOWCANDY && sweet1.CanClear() && sweet2.CanClear()) //如果物品1是 特殊物品:消除颜色 { ClearColorSweet clearColor = sweet1.GetComponent <ClearColorSweet>(); if (clearColor != null) //容错 { clearColor.ClearColor = sweet2.ColorComponet.Color; } ClearSweet(sweet1.X, sweet1.Y); } if (sweet2.Type == SweetsType.RAINBOWCANDY && sweet2.CanClear() && sweet2.CanClear()) //如果物品2是 特殊物品:消除颜色 { ClearColorSweet clearColor = sweet2.GetComponent <ClearColorSweet>(); if (clearColor != null) { clearColor.ClearColor = sweet1.ColorComponet.Color; } ClearSweet(sweet2.X, sweet2.Y); } ClearAllMatchedSweet(); //交换位置后,清除物品,并生成空格 StartCoroutine(AllFill()); //交换位置后填充 pressedSweet = null; enterSweet = null; } else { sweets[sweet1.X, sweet1.Y] = sweet1; sweets[sweet2.X, sweet1.Y] = sweet2; } } }
//交换两个甜品的方法 private void ExChangeSweets(GameSweet sweet1, GameSweet sweet2) { if (sweet1.CanMove() && sweet2.CanMove()) { sweets[sweet1.X, sweet1.Y] = sweet2; sweets[sweet2.X, sweet2.Y] = sweet1; int tempX = sweet1.X; int tempY = sweet1.Y; sweet1.MovedComponent.Move(sweet2.X, sweet2.Y, fillTime); sweet2.MovedComponent.Move(tempX, tempY, fillTime); } }
/// <summary> /// 将两个甜品位置交换 /// </summary> /// <param name="sweet1"></param> /// <param name="sweet2"></param> private void ExchangeSweets(GameSweet sweet1, GameSweet sweet2) { if (sweet1.CanMove() && sweet2.CanMove()) { sweets[sweet1.X, sweet1.Y] = sweet2; sweets[sweet2.X, sweet2.Y] = sweet1; int tempX = sweet1.X; int tempY = sweet1.Y; if (MatchSweets(sweet1, sweet2.X, sweet2.Y) != null || MatchSweets(sweet2, sweet1.X, sweet1.Y) != null || sweet1.Type == SweetType.RAINBOWCANDY || sweet2.Type == SweetType.RAINBOWCANDY) { //如果匹配到了最少三个相同的甜品则交换位置,且不能返回之前的位置 sweet1.MoveSweetComponent.Move(sweet2.X, sweet2.Y, fillTime);//由于两个携程会影响甜品的位置,因此之前要先记录之前的位置 sweet2.MoveSweetComponent.Move(tempX, tempY, fillTime); //如果两个甜品中有最少一个彩虹糖也可进入该语句 if (sweet1.Type == SweetType.RAINBOWCANDY && sweet1.CanClear() && sweet2.CanClear()) { ClearSameSweet clearColor = sweet1.GetComponent <ClearSameSweet>(); if (clearColor != null) { clearColor.Color = sweet2.ColorSweetComponent.Color; } ClearSweet(sweet1.X, sweet1.Y); } if (sweet2.Type == SweetType.RAINBOWCANDY && sweet2.CanClear() && sweet1.CanClear()) { ClearSameSweet clearColor = sweet2.GetComponent <ClearSameSweet>(); if (clearColor != null) { clearColor.Color = sweet1.ColorSweetComponent.Color; } ClearSweet(sweet2.X, sweet2.Y); } ClearAllMatchSweet(); StartCoroutine(AllFill()); } else { //如果没匹配到三个相同的三个元素,则先交换后返回 sweets[sweet1.X, sweet1.Y] = sweet1; sweets[sweet2.X, sweet2.Y] = sweet2; sweet1.MoveSweetComponent.Move(sweet2.X, sweet2.Y, fillTime);//由于两个携程会影响甜品的位置,因此之前要先记录之前的位置 sweet2.MoveSweetComponent.Move(tempX, tempY, fillTime); StartCoroutine(Wait(0.5f, sweet1, sweet2)); } } }
/// <summary> /// 两个甜品位置交换 /// </summary> public void ExChangeSweet(GameSweet sweet1, GameSweet sweet2) { if (sweet1.CanMove() && sweet2.CanMove()) { sweetArr[sweet1.X, sweet1.Y] = sweet2; sweetArr[sweet2.X, sweet2.Y] = sweet1; if (MatchSweets(sweet1, sweet2.X, sweet2.Y) != null || MatchSweets(sweet2, sweet1.X, sweet1.Y) != null || sweet1.Type == SweetsType.RainbowCandy || sweet2.Type == SweetsType.RainbowCandy) { //记录x、y的值 防止sweet1已移动完成后位置跟sweet2重叠 var x = sweet1.X; var y = sweet1.Y; sweet1.moveSweet.Move(sweet2.X, sweet2.Y, fillTime); sweet2.moveSweet.Move(x, y, fillTime); //清除指定颜色所有甜品 if (sweet1.Type == SweetsType.RainbowCandy && sweet1.CanClear() && sweet2.CanClear()) { ClearColorSweet clearColor = sweet1.GetComponent <ClearColorSweet>(); if (clearColor != null) { clearColor.ClearColor = sweet2.colorSweet.colorType; } ClearSweet(sweet1.X, sweet1.Y); } if (sweet2.Type == SweetsType.RainbowCandy && sweet1.CanClear() && sweet2.CanClear()) { ClearColorSweet clearColor = sweet2.GetComponent <ClearColorSweet>(); if (clearColor != null) { clearColor.ClearColor = sweet1.colorSweet.colorType; } ClearSweet(sweet2.X, sweet2.Y); } ClearAllMatchedSweet(); StartCoroutine(AllFill()); clickSweet = null; enterSweet = null; } else { sweetArr[sweet1.X, sweet1.Y] = sweet1; sweetArr[sweet2.X, sweet2.Y] = sweet2; } } }
//交换两个甜品 private void ExchangeSweets(GameSweet sweet1, GameSweet sweet2) { if (sweet1.CanMove() && sweet2.CanMove()) { sweets[sweet1.X, sweet1.Y] = sweet2; sweets[sweet2.X, sweet2.Y] = sweet1; if (MatchSweet(sweet1, sweet2.X, sweet2.Y) != null || MatchSweet(sweet2, sweet1.X, sweet1.Y) != null) { //先做缓存一下,要不然下面会把1的位置覆盖掉。 int tempX = sweet1.X; int tempY = sweet1.Y; sweet1.MovedComponent.Move(sweet2.X, sweet2.Y, fillTime); sweet2.MovedComponent.Move(tempX, tempY, fillTime); } else { sweets[sweet1.X, sweet1.Y] = sweet1; sweets[sweet2.X, sweet2.Y] = sweet2; } } }
/// <summary> /// 分步填充 /// </summary> public bool Fill() { bool filledNotFinished = false; //判断本次的填充是否完成; for (int y = yRow - 2; y >= 0; y--) { for (int x = 0; x < xColum; x++) { GameSweet sweet = sweets[x, y]; //得到当前的元素的位置 if (sweet.CanMove()) //如果无法移动,则无法向下填充 { GameSweet sweetBelow = sweets[x, y + 1]; if (sweetBelow.Type == SweetType.EMPTY) //垂直填充 { Destroy(sweetBelow.gameObject); sweet.MovedComponent.Move(x, y + 1, fillTime); sweets[x, y + 1] = sweet; CreateNewSweet(x, y, SweetType.EMPTY); filledNotFinished = true; } else //斜向填充 { for (int down = -1; down < 1; down++) { if (down != 0) { int downX = down + x; //该处判断列的位置 0 < downX < xColum // [0,0] [1,0] [2,0] ...[x,0] // [1,0] [1,1] [1,2] ...[1,yRow] // ... ... //[xColum,0] [xColum] [xColum]...[xColum] if (downX > 0 && downX < xColum) { GameSweet downSweet = sweets[downX, y + 1]; if (downSweet.Type == SweetType.EMPTY) { bool canFill = true; //判断数值方向上是否符合填充的要求 for (int aboveY = y; aboveY >= 0; aboveY--) { GameSweet sweetAbove = sweets[downX, aboveY]; if (sweetAbove.CanMove()) { break; } else if (!sweetAbove.CanMove() && sweetAbove.Type != SweetType.EMPTY) { canFill = false; break; } } if (!canFill) { Destroy(downSweet.gameObject); sweet.MovedComponent.Move(downX, y + 1, fillTime); sweets[downX, y + 1] = sweet; CreateNewSweet(x, y, SweetType.EMPTY); filledNotFinished = true; break; } } } } } } } } //最上排 特殊情况 for (int x = 0; x < xColum; x++) { GameSweet sweet = sweets[x, 0]; if (sweet.Type == SweetType.EMPTY) { GameObject newSweet = Instantiate(sweetPrefabDic[SweetType.NORMAL], CorrectPos(x, -1), Quaternion.identity); newSweet.transform.parent = transform; sweets[x, 0] = newSweet.GetComponent <GameSweet>(); sweets[x, 0].Init(x, -1, this, SweetType.NORMAL); sweets[x, 0].MovedComponent.Move(x, 0, fillTime); sweets[x, 0].ColoredComponent.SetColor((ColorSweet.ColorType)Random.Range(0, sweets[x, 0].ColoredComponent.NumColors)); filledNotFinished = true; } } } return(filledNotFinished); }
//清除完成匹配的元素 public bool ClearAllMatchSweet() { bool needReFill = false; for (int y = 0; y < yRow; y++) { for (int x = 0; x < xColumn; x++) { if (sweets[x, y].CanClear()) { // 返回的需要删除的元素列表 List <GameSweet> matchList = MatchSweets(sweets[x, y], x, y); if (matchList != null) { // Debug.Log("消除列表长度="+matchList.Count); SweetType specialSweetType = SweetType.COUNT; //在随机一个消除位置生成一个行消除或者列消除 GameSweet birthSpecialSweet = matchList[Random.Range(0, matchList.Count)]; int specialX = birthSpecialSweet.X; int specialY = birthSpecialSweet.Y; if (matchList.Count == 4) { // Debug.Log("开始产生特殊物品!!!!!!!!!!!!!!!"); //随机一个行消除或者列消除 specialSweetType = (SweetType)Random.Range((int)SweetType.ROW_CLEAR, (int)SweetType.RAINBOWCANDY); } for (int i = 0; i < matchList.Count; i++) { //if (matchList[i] == null) // continue; if (ClearSweet(matchList[i].X, matchList[i].Y)) { needReFill = true; } } if (specialSweetType != SweetType.COUNT) { //Debug.Log("开始产生特殊物品"); Destroy(sweets[specialX, specialY]); GameSweet newSweet = CreatNewSweet(specialX, specialY, specialSweetType); if (specialSweetType == SweetType.ROW_CLEAR || specialSweetType == SweetType.COLUMN_CLEAR && newSweet.CanMove() && matchList[0].CanColor()) { //newSweet.ColorComponent.SetColor(matchList[0].ColorComponent.Color); } else { } } } } } } return(needReFill); }
//分步填充 private bool Fill() { bool filledNotFinished = false;//这个部分布尔值是用来判断本次填充是否完成。 for (int y = yRow - 2; y >= 0; y--) { for (int x = 0; x < xColumn; x++) { GameSweet sweet = sweets[x, y]; if (sweet.CanMove())//如果无法移动,则无法往下填充 { GameSweet sweetBelow = sweets[x, y + 1]; //垂直填充 if (sweetBelow.Type == SweetsType.EMPTY) { sweet.MovedComponent.Move(x, y + 1, fillTime); sweets[x, y + 1] = sweet; Destroy(sweetBelow.gameObject); //填充上面哪行 CreateNewSweet(x, y, SweetsType.EMPTY); filledNotFinished = true; } else { //斜方向填充 //如果不可以移动,就查看坐下和右下是否可以移动 for (int i = -1; i <= 1; i++) { int downX = x + i; if (i != 0) { if (downX >= 0 && downX < xColumn) { GameSweet downSweet = sweets[downX, y + 1]; if (downSweet.Type == SweetsType.EMPTY) { bool canFill = true; for (int aboveY = y; aboveY >= 0; aboveY--) { GameSweet sweetAbove = sweets[downX, aboveY]; if (sweetAbove.CanMove()) { break; } else if (!sweetAbove.CanMove() && sweetAbove.Type != SweetsType.EMPTY) { canFill = false; break; } } if (!canFill) { Destroy(downSweet.gameObject); sweet.MovedComponent.Move(downX, y + 1, fillTime); sweets[downX, y + 1] = sweet; CreateNewSweet(x, y, SweetsType.EMPTY); filledNotFinished = true; break; } } } } } } } if (y == 0 && sweet.Type == SweetsType.EMPTY) { GameObject newSweetObject = Instantiate(sweetPrefabDict[SweetsType.NORMAL], CorrectPosition(x, y - 1), Quaternion.identity); newSweetObject.transform.SetParent(transform); GameSweet sweetComponent = newSweetObject.GetComponent <GameSweet>(); sweetComponent.Init(x, y - 1, this, SweetsType.NORMAL); sweetComponent.MovedComponent.Move(x, y, fillTime); sweetComponent.ColorComponent.SetColor((ColorSweet.ColorType)Random.Range(0, sweetComponent.ColorComponent.numColors)); sweets[x, y] = sweetComponent; //销毁空物体 Destroy(sweet.gameObject); filledNotFinished = true; } } } return(filledNotFinished); }
//分步填充的方法 public bool Fill() { bool filledNotFinished = false;////判断本次填充是否完成 //最后一行不遍历,因为它不需要往下填充 for (int y = yRow - 2; y >= 0; y--) { for (int x = 0; x < xColumn; x++) { //取到当前元素 GameSweet sweet = sweets[x, y]; //如果无法移动则无法向下填充 if (sweet.CanMove()) { //获取到当前元素下方的元素 GameSweet sweetBelow = sweets[x, y + 1]; //如果下方元素为空,上方元素向下填充 if (sweetBelow.Type == SweetType.EMPTY) { //垂直填充 sweet.MoveComponent.Move(x, y + 1, time); //将其保存在数组当中 sweets[x, y + 1] = sweet; //将当前元素的位置滞空 CreatNewSweet(x, y, SweetType.EMPTY); filledNotFinished = true; } else { //斜向填充 for (int down = -1; down <= 1; down++) { if (down != 0) { int downX = x + down; if (downX > 0 && downX < xColumn) { GameSweet downSweet = sweets[downX, y + 1]; if (downSweet.Type == SweetType.EMPTY) { bool canFill = true; for (int aboveY = y; aboveY >= 0; aboveY--) { GameSweet sweetAbove = sweets[downX, aboveY]; if (sweetAbove.CanMove()) { break; } else if (!sweetAbove.CanMove() && sweetAbove.Type != SweetType.EMPTY) { canFill = false; break; } } if (!canFill) { Destroy(downSweet.gameObject); sweet.MoveComponent.Move(downX, y + 1, time); sweets[downX, y + 1] = sweet; CreatNewSweet(x, y, SweetType.EMPTY); filledNotFinished = true; break; } } } } } } } } } //最上层的特殊情况 for (int x = 0; x < xColumn; x++) { //获取到最上层元素对象 GameSweet sweet = sweets[x, 0]; //如果当前的元素为空 if (sweet.Type == SweetType.EMPTY) { GameObject newSweet = Instantiate(sweetPrefabDict[SweetType.NORMAL], CorrectPosition(x, -1), Quaternion.identity); newSweet.transform.SetParent(transform); sweets[x, 0] = newSweet.GetComponent <GameSweet>(); sweets[x, 0].Init(x, -1, this, SweetType.NORMAL); sweets[x, 0].MoveComponent.Move(x, 0, time); sweets[x, 0].ColorComponent.SetColor((ColorSweet.ColorType)Random.Range(0, sweets[x, 0].ColorComponent.GetNumColor)); filledNotFinished = true; } } return(filledNotFinished); }
/// <summary> /// 填充,循环一轮 /// </summary> public bool Fill() { bool filledNotFinished = false;//判断本次是否填充完成 for (int y = yRow - 2; y >= 0; y--) { for (int x = 0; x < xColumn; x++) { GameSweet sweet = sweets[x, y]; //得到当前元素位置的甜品对象 if (sweet.CanMove()) //如果可以移动 { GameSweet sweetBelow = sweets[x, y + 1]; //获取下一行位置 if (sweetBelow.Type == SweetType.EMPTY) //如果为空,则往下填充 { //Destroy(sweetBelow.gameObject);可保护可不保护 sweet.MovedComponent.Move(x, y + 1, fillTime); sweets[x, y + 1] = sweet; //下一行等于当前行的东西 CreateNewSweet(x, y, SweetType.EMPTY); //移动之后把自身置空 filledNotFinished = true; //自身格子判断填充未完成 } else//否则向左下角,右下角移动 { for (int down = -1; down <= 1; down++) //-1左边 0当前 1右边 { if (down != 0) { int downX = x + down; if (downX >= 0 && downX < xColumn)//判断是否在最左跟最右列之间,是才移动 { GameSweet downSweet = sweets[downX, y + 1]; if (downSweet.Type == SweetType.EMPTY) { bool canfill = true; //用来判断垂直填充是否可以满足填充要求 for (int aboveY = y; aboveY >= 0; aboveY--) //当前y行 , 从下往上遍历 { GameSweet sweetAbove = sweets[downX, aboveY]; if (sweetAbove.CanMove()) { break; } else if (!sweetAbove.CanMove() && sweetAbove.Type != SweetType.EMPTY) { canfill = false; break; } } if (!canfill)//如果垂直填充不满足,则斜向填充 { //Destroy(downSweet.gameObject);可保护可不保护 sweet.MovedComponent.Move(downX, y + 1, fillTime); sweets[downX, y + 1] = sweet; //填充 CreateNewSweet(x, y, SweetType.EMPTY); //将原来格子置空 filledNotFinished = true; break; } } } } } } } } } //最上排的特殊情况 for (int x = 0; x < xColumn; x++) { GameSweet sweet = sweets[x, 0]; //获取最上行的甜品对象 if (sweet.Type == SweetType.EMPTY) //最上行是否为空 { GameObject newSweet = Instantiate(sweetPrefabDict[SweetType.NORMAL], CorrectPosition(x, -1), Quaternion.identity); //实例化-1行(生成) newSweet.transform.parent = transform; sweets[x, 0] = newSweet.GetComponent <GameSweet>(); //当前行拿到组件 sweets[x, 0].Init(x, -1, this, SweetType.NORMAL); //初始化 sweets[x, 0].MovedComponent.Move(x, 0, fillTime); //移动到第一行,位置脚本 sweets[x, 0].ColoredComponent.SetColor((ColorType)Random.Range(0, sweets[x, 0].ColoredComponent.NumColors)); //随机设置什么颜色类型的甜品,由于参数是ColorSweet脚本组件的枚举类型 filledNotFinished = true; } } return(filledNotFinished); }
/// <summary> /// 分步填充 /// </summary> public bool Fill() { //判断本次填充是否完成 bool filledNotFinished = false; for (int y = yRow - 2; y >= 0; y--) { for (int x = 0; x < xCol; x++) { GameSweet sweet = sweetArr[x, y]; //判断是否可以移动,如果不能移动则无法向下填充 if (sweet.CanMove() == true) { //获取当前甜品下面的甜品 GameSweet sweetBelow = sweetArr[x, y + 1]; //垂直填充 if (sweetBelow.Type == SweetsType.Empty) { Destroy(sweetBelow.gameObject); //移动当前甜品 sweet.moveSweet.Move(x, y + 1, fillTime); sweetArr[x, y + 1] = sweet; //在当前位置创建甜品 CreateNewSweet(x, y, SweetsType.Empty); filledNotFinished = true; } //斜向填充 else { //-1 向左 1向右 0垂直 for (int down = -1; down <= 1; down++) { if (down != 0) { //新的x坐标 int downX = x + down; if (downX >= 0 && downX < xCol) { //获取游戏对象 GameSweet downSweet = sweetArr[downX, y + 1]; if (downSweet.Type == SweetsType.Empty) { //是否可以移动标识 bool canFill = true; for (int aboveY = y; aboveY >= 0; aboveY--) { //获取游戏对象 GameSweet aboveSweet = sweetArr[downX, aboveY]; if (aboveSweet.CanMove() == true) { break; } else if (aboveSweet.CanMove() == false && aboveSweet.Type != SweetsType.Empty) { canFill = false; break; } } if (!canFill) { Destroy(downSweet.gameObject); sweet.moveSweet.Move(downX, y + 1, fillTime); sweetArr[downX, y + 1] = sweet; CreateNewSweet(x, y, SweetsType.Empty); filledNotFinished = true; break; } } } } } } } } } //最上排的特殊情况处理 for (int x = 0; x < xCol; x++) { GameSweet sweet = sweetArr[x, 0]; if (sweet.Type == SweetsType.Empty) { GameObject newSweet = Instantiate(prefabDic[SweetsType.Normal], CorrectPos(x, -1), Quaternion.identity); newSweet.transform.parent = transform; //获取基础脚本并初始化 sweetArr[x, 0] = newSweet.GetComponent <GameSweet>(); sweetArr[x, 0].Init(x, -1, this, SweetsType.Normal); if (sweetArr[x, 0].CanMove()) { sweetArr[x, 0].moveSweet.Move(x, 0, fillTime); } if (sweetArr[x, 0].CanColor()) { sweetArr[x, 0].colorSweet.SetSprite((ColorType)Random.Range(0, sweetArr[x, 0].colorSweet.spriteDic.Count)); } filledNotFinished = true; } } return(filledNotFinished); }
public bool Fill() { bool filledNotFinished = false; for (int y = yRow - 2; y >= 0; y--) { for (int loopX = 0; loopX < xColumn; loopX++) { int x = loopX; if (inverse) { x = xColumn - 1 - loopX; } GameSweet sweet = sweets[x, y]; if (sweet.CanMove()) { GameSweet sweetBelow = sweets[x, y + 1]; if (sweetBelow.Type == SweetsType.EMPTY) { Destroy(sweetBelow.gameObject); sweet.MovedComponent.Move(x, y + 1, fillTime); sweets[x, y + 1] = sweet; CreateNewSweet(x, y, SweetsType.EMPTY); filledNotFinished = true; } else { for (int down = -1; down <= 1; down++) { if (down != 0) { int downX = x + down; if (inverse) { downX = x - down; } if (downX >= 0 && downX < xColumn) { GameSweet downSweet = sweets[downX, y + 1]; if (downSweet.Type == SweetsType.EMPTY) { bool canfill = true; for (int aboveY = y; aboveY >= 0; aboveY--) { GameSweet sweetAbove = sweets[downX, aboveY]; if (sweetAbove.CanMove()) { break; } else if (!sweetAbove.CanMove() && sweetAbove.Type != SweetsType.EMPTY) { canfill = false; break; } } if (!canfill) { Destroy(downSweet.gameObject); sweet.MovedComponent.Move(downX, y + 1, fillTime); sweets[downX, y + 1] = sweet; CreateNewSweet(x, y, SweetsType.EMPTY); filledNotFinished = true; break; } } } } } } } } } for (int x = 0; x < xColumn; x++) { GameSweet sweet = sweets[x, 0]; if (sweet.Type == SweetsType.EMPTY) { GameObject newSweet = Instantiate(sweetPrefabDict[SweetsType.NORMAL], CorrectPositon(x, -1), Quaternion.identity); newSweet.transform.parent = transform; sweets[x, 0] = newSweet.GetComponent <GameSweet>(); sweets[x, 0].Init(x, -1, this, SweetsType.NORMAL); sweets[x, 0].MovedComponent.Move(x, 0, fillTime); sweets[x, 0].ColoredComponent.SetColor((ColorSweet.ColorType)Random.Range(0, sweets[x, 0].ColoredComponent.NumColors)); filledNotFinished = true; } } return(filledNotFinished); }
/// <summary> /// 分步填充 /// </summary> public bool Fill() { bool filledNotFinished = false; //判断本次否填,是否完成 for (int y = yHang - 2; y >= 0; y--) //从下往上 { for (int x = 0; x < xLie; x++) //从左到右 { GameSweet sweet = sweets[x, y]; //得到当前元素位置的物品对象 if (sweet.CanMove()) //如果能移动就填充 { GameSweet sweetBelow = sweets[x, y + 1]; //下边元素位置 if (sweetBelow.Type == SweetsType.EMPTY) //如果下方是空格子,就垂直向下填充 { Destroy(sweetBelow.gameObject); sweet.MovedComponet.Move(x, y + 1, fillTime); //上边的元素,往下移动 sweets[x, y + 1] = sweet; //二维数组,对应位置更新。 CreateNewSweet(x, y, SweetsType.EMPTY); filledNotFinished = true; } else //斜着填充 { for (int down = -1; down < 1; down++) { if (down != 0) //不是正下方 { int downX = x + down; if (downX >= 0 && downX < xLie) //规定范围,排除边缘情况 { GameSweet downSweet = sweets[downX, y + 1]; //左下方甜品 if (downSweet.Type == SweetsType.EMPTY) //左下方为空 { bool canfill = true; //用来判断是否可以垂直填充 for (int upY = y; upY >= 0; upY--) { GameSweet upSweet = sweets[downX, upY]; //正上方元素 if (upSweet.CanMove()) { break; //能移动直接跳出 } else if (!upSweet.CanMove() && upSweet.Type != SweetsType.EMPTY) { canfill = false; break; } } if (!canfill) //不能垂直填充 { Destroy(downSweet.gameObject); //删除下边游戏的物体 sweet.MovedComponet.Move(downX, y + 1, fillTime); sweets[downX, y + 1] = sweet; CreateNewSweet(x, y, SweetsType.EMPTY); filledNotFinished = true; break; } } } } } } } } } //最上排的特殊情况 for (int x = 0; x < xLie; x++) { GameSweet sweet = sweets[x, 0]; if (sweet.Type == SweetsType.EMPTY) { GameObject newSweet = (GameObject)Instantiate(sweetPrefabDict[SweetsType.NORMAL], CorrectPosition(x, -1), Quaternion.identity); newSweet.transform.parent = transform; sweets[x, 0] = newSweet.GetComponent <GameSweet>(); sweets[x, 0].Init(x, -1, this, SweetsType.NORMAL); sweets[x, 0].MovedComponet.Move(x, 0, fillTime); sweets[x, 0].ColorComponet.SetColor((ColorSweet.ColorType)Random.Range(0, sweets[x, 0].ColorComponet.NumColors)); filledNotFinished = true; } } return(filledNotFinished); }
public bool Fill() { bool filledNotFinshed = false; for (int i = 0; i < xColumns; i++) //最顶层单独填充因此排除,填充方向为Y。 { for (int j = yRows - 2; j >= 0; j--) { GameSweet sweet = sweetsarray[j, i]; if (sweet.CanMove()) { // print(j + "," + i + 1); GameSweet sweetfill = sweetsarray[j + 1, i]; //由于排列时 从顶到底 j+1为下一层 而不是上一层 if (sweetfill.Type == SweetsType.EMPTY) //垂直填充,下方为空块 { Destroy(sweetfill.gameObject); sweet.MoveComponent.Move(i, j + 1, fillTime); sweetsarray[j + 1, i] = sweet; CreateSweet(i, j, SweetsType.EMPTY); filledNotFinshed = true; // print("基本填充" + i + "," + j); } else //下方有障碍时 填充方法 为斜下 左或右 { for (int down = -1; down <= 1; down++)//优先斜左下方滑动 { if (down != 0) { int downx = i + down; if (downx >= 0 && downx < xColumns) { GameSweet downSweet = sweetsarray[j + 1, downx]; if (downSweet.Type == SweetsType.EMPTY) { bool canfill = true; for (int moveY = j; moveY >= 0; moveY--) { GameSweet sweetmoveY = sweetsarray[moveY, downx]; if (sweetmoveY.CanMove()) { break; } else if (!sweetmoveY.CanMove() && sweetmoveY.Type != SweetsType.EMPTY)//斜向填充条件 { canfill = false; break; } } if (!canfill) { Destroy(downSweet.gameObject); sweet.MoveComponent.Move(downx, j + 1, fillTime); sweetsarray[j + 1, downx] = sweet; CreateSweet(i, j, SweetsType.EMPTY); filledNotFinshed = true; } } } } } } } } } for (int i = 0; i < xColumns; i++)//最顶层 { GameSweet sweet = sweetsarray[0, i]; if (sweet.Type == SweetsType.EMPTY)//生成sweet { GameSweet go = Instantiate(sweetPrefabDict[SweetsType.NORMAL], CorrectPosition(i, -1), Quaternion.identity); go.transform.SetParent(transform); sweetsarray[0, i] = go.GetComponent <GameSweet>(); sweetsarray[0, i].Init(i, -1, Instance, SweetsType.NORMAL); sweetsarray[0, i].MoveComponent.Move(i, 0, fillTime); sweetsarray[0, i].ColorComponent.SetColor((ColorSweet.ColorEnum)(Random.Range(0, sweetsarray[0, i].ColorComponent.NumColors))); filledNotFinshed = true; // print("顶层填充" + i + "," + yRows + ",," + xColumns); } } return(filledNotFinshed); }
/// <summary> /// 清除匹配的方法 /// </summary> #region //匹配方法 public List <GameSweet> MatchSweets(GameSweet sweet, int newX, int newY) { if (sweet.CanMove()) { ColorSweet.ColorType color = sweet.ColoredComponent.Color; List <GameSweet> matchRowSweets = new List <GameSweet>(); List <GameSweet> matchLineSweets = new List <GameSweet>(); List <GameSweet> finishedMatchingSweets = new List <GameSweet>(); //行匹配 //当x=0代表向左,1代表向右遍历sweet所在的行的物体 matchRowSweets.Add(sweet); for (int i = 0; i <= 1; i++) { for (int xDistance = 1; xDistance < xColum; xDistance++) { int x; if (i == 0) { x = newX - xDistance; } else { x = newX + xDistance; } if (x < 0 || x >= xColum) { break; } if (sweets[x, newY].CanColor() && sweets[x, newY].ColoredComponent.Color == color) { matchRowSweets.Add(sweets[x, newY]); } else { break; } } } if (matchRowSweets.Count >= 3) { for (int i = 0; i < matchRowSweets.Count; i++) { finishedMatchingSweets.Add(matchRowSweets[i]); } } //L T型匹配 //检查一下当前行遍历列表中的元素数量是否大于3 if (matchRowSweets.Count >= 3) { for (int i = 0; i < matchRowSweets.Count; i++) { //行匹配列表中满足匹配条件的每个元素上下依次进行列遍历 // 0代表上方 1代表下方 for (int j = 0; j <= 1; j++) { for (int yDistance = 1; yDistance < yRow; yDistance++) { int y; if (j == 0) { y = newY - yDistance; } else { y = newY + yDistance; } if (y < 0 || y >= yRow) { break; } if (sweets[matchRowSweets[i].X, y].CanColor() && sweets[matchRowSweets[i].X, y].ColoredComponent.Color == color) { matchLineSweets.Add(sweets[matchRowSweets[i].X, y]); } else { break; } } } if (matchLineSweets.Count < 2) { matchLineSweets.Clear(); } else { for (int j = 0; j < matchLineSweets.Count; j++) { finishedMatchingSweets.Add(matchLineSweets[j]); } break; } } } if (finishedMatchingSweets.Count >= 3) { return(finishedMatchingSweets); } matchRowSweets.Clear(); matchLineSweets.Clear(); matchLineSweets.Add(sweet); //列匹配 //i=0代表往左,i=1代表往右 for (int i = 0; i <= 1; i++) { for (int yDistance = 1; yDistance < yRow; yDistance++) { int y; if (i == 0) { y = newY - yDistance; } else { y = newY + yDistance; } if (y < 0 || y >= yRow) { break; } if (sweets[newX, y].CanColor() && sweets[newX, y].ColoredComponent.Color == color) { matchLineSweets.Add(sweets[newX, y]); } else { break; } } } if (matchLineSweets.Count >= 3) { for (int i = 0; i < matchLineSweets.Count; i++) { finishedMatchingSweets.Add(matchLineSweets[i]); } } //L T型匹配 //检查一下当前行遍历列表中的元素数量是否大于3 if (matchLineSweets.Count >= 3) { for (int i = 0; i < matchLineSweets.Count; i++) { //行匹配列表中满足匹配条件的每个元素上下依次进行列遍历 // 0代表上方 1代表下方 for (int j = 0; j <= 1; j++) { for (int xDistance = 1; xDistance < xColum; xDistance++) { int x; if (j == 0) { x = newY - xDistance; } else { x = newY + xDistance; } if (x < 0 || x >= xColum) { break; } if (sweets[x, matchLineSweets[i].Y].CanColor() && sweets[x, matchLineSweets[i].Y].ColoredComponent.Color == color) { matchRowSweets.Add(sweets[x, matchLineSweets[i].Y]); } else { break; } } } if (matchRowSweets.Count < 2) { matchRowSweets.Clear(); } else { for (int j = 0; j < matchRowSweets.Count; j++) { finishedMatchingSweets.Add(matchRowSweets[j]); } break; } } } if (finishedMatchingSweets.Count >= 3) { return(finishedMatchingSweets); } } Debug.Log("没有可以匹配的糖果"); return(null); }
public bool Fill() { bool filledNotFinished = false; for (int y = yRow - 2; y >= 0; y--) { for (int x = 0; x < xColumn; x++) { GameSweet sweet = sweets[x, y]; //得到当前原始的位置 if (sweet.CanMove()) //如果可以移动 { GameSweet sweetBelow = sweets[x, y + 1]; if (sweetBelow.Type == SweetsType.EMPTY) { sweet.MoveComponent.Move(x, y + 1, fileTime); sweets[x, y + 1] = sweet; CreateNewSweet(x, y, SweetsType.EMPTY); filledNotFinished = true; } else { //-1是左边,1是右边 for (int down = -1; down <= 1; down++) { if (down != 0) { int downX = x + down; //如果不是最左边或者最后边 if (downX >= 0 && downX < xColumn) { GameSweet downSweet = sweets[downX, y + 1]; //如果为空 if (downSweet.Type == SweetsType.EMPTY) { bool canfill = true; //用来判断垂直填充是否可以满足填充需求 for (int aboveY = y; aboveY >= 0; aboveY--) { GameSweet sweetAbove = sweets[downX, aboveY]; if (sweetAbove.CanMove()) { break; } else if (!sweetAbove.CanMove() && sweetAbove.Type != SweetsType.EMPTY) { canfill = false; break; } } if (!canfill) { Destroy(downSweet.gameObject); sweet.MoveComponent.Move(downX, y + 1, fileTime); sweets[downX, y + 1] = sweet; CreateNewSweet(x, y, SweetsType.EMPTY); filledNotFinished = true; break; } } } } } } } } } //最上排特殊情况,生成一行糖块 for (int x = 0; x < xColumn; x++) { GameSweet sweet = sweets[x, 0]; //获取当前糖块 if (sweet.Type == SweetsType.EMPTY) //如果当前位置糖块为空 { //创建一块糖块,设置糖块的位置为-1行 GameObject newSweet = Instantiate(sweetPrefabDict[SweetsType.NORMAL], CorrectPositon(x, -1), Quaternion.identity); //设置糖块的父物体 newSweet.transform.parent = sweetsTransform; sweets[x, 0] = newSweet.GetComponent <GameSweet>(); sweets[x, 0].Init(x, -1, this, SweetsType.NORMAL); sweets[x, 0].MoveComponent.Move(x, 0, fileTime); sweets[x, 0].ColoredComponent .SetColor((ColorSweet.ColorType)Random.Range(0, sweets[x, 0].ColoredComponent.NumColors)); filledNotFinished = true; } } return(filledNotFinished); }
/// <summary> /// 分步填充 /// </summary> public bool Fill() { bool filledNotFinish = false;//是否填充完成 for (int y = yRow - 2; y >= 0; y--) { for (int x = 0; x < xColumn; x++) { GameSweet sweet = sweets[x, y]; if (sweet.CanMove())//如果可以移动,则可以向下继续填充 { GameSweet sweetBelow = sweets[x, y + 1]; if (sweetBelow.Type == SweetType.EMPTY)//如果当前甜品下方为空甜品,则优先垂直填充 { Destroy(sweetBelow.gameObject); sweet.MoveSweetComponent.Move(x, y + 1, fillTime); sweets[x, y + 1] = sweet; CreatNewSweet(x, y, SweetType.EMPTY); filledNotFinish = true; } else//斜向填充 { for (int down = -1; down <= 1; down++)//-1代表左下方,1代表右下方 { if (down != 0) { int downX = x + down; if (downX >= 0 && downX < xColumn) //排除边界 { GameSweet bevelSweet = sweets[downX, y + 1]; //斜方向的甜品 if (bevelSweet.Type == SweetType.EMPTY) //如果该甜品斜下方向为空甜品 { bool canBevelFill = true; //用来表示是否可以斜向填充 for (int aboveY = y; aboveY >= 0; aboveY--) { GameSweet bevelAboveSweet = sweets[downX, aboveY];//斜下方甜品的正上方的甜品 if (bevelAboveSweet.Type != SweetType.EMPTY && !bevelAboveSweet.CanMove()) { //如果不能移动且不为空,则可以进行斜向填充 canBevelFill = false; break; } else if (bevelAboveSweet.CanMove()) { //不做处理 break; } } if (canBevelFill == false) { Destroy(bevelSweet.gameObject); sweets[x, y].MoveSweetComponent.Move(downX, y + 1, fillTime); sweets[downX, y + 1] = sweet; CreatNewSweet(x, y, SweetType.EMPTY); filledNotFinish = true; break; } } } } } } } } } //最上面一排特殊情况 for (int x = 0; x < xColumn; x++) { GameSweet sweet = sweets[x, 0]; if (sweet.Type == SweetType.EMPTY)//如果该甜品为空甜品,则进行实例化生成新的甜品 { GameObject newSweet = Instantiate(sweetDict[SweetType.NORMAL], CorrectPosition(x, -1), Quaternion.identity); newSweet.transform.SetParent(spawner); sweets[x, 0] = newSweet.GetComponent <GameSweet>(); sweets[x, 0].Init(x, -1, this, SweetType.NORMAL); sweets[x, 0].MoveSweetComponent.Move(x, 0, fillTime); sweets[x, 0].ColorSweetComponent.SetColor((ColorSweet.ColorType)Random.Range(0, sweets[x, 0].ColorSweetComponent.NumColor)); Destroy(sweet.gameObject); filledNotFinish = true; } } return(filledNotFinish); }
private bool StepFill() { bool fillNotFinished = false; for (int y = yRow - 2; y >= 0; y--) { for (int x = 0; x < xColumn; x++) { GameSweet sweet = sweets[x, y]; if (sweet.CanMove()) { GameSweet belowSweet = sweets[x, y + 1]; if (belowSweet.Type == SweetsType.EMPTY) { Destroy(belowSweet.gameObject); sweet.MovedCommpont.Move(x, y + 1, fillTime); sweets[x, y + 1] = sweet; CreateNewSweet(x, y, SweetsType.EMPTY); fillNotFinished = true; } else { for (int down = -1; down <= 1; down++) { if (down != 0) { int downX = x + down; if (downX >= 0 && downX < xColumn) { GameSweet downSweet = sweets[downX, y + 1]; if (downSweet.Type == SweetsType.EMPTY) { bool canfill = true;//用来判断垂直填充是否可以满足填充要求 for (int aboveY = y; aboveY >= 0; aboveY--) { GameSweet sweetAbove = sweets[downX, aboveY]; if (sweetAbove.CanMove()) { break; } else if (!sweetAbove.CanMove() && sweetAbove.Type != SweetsType.EMPTY) { canfill = false; break; } } if (!canfill) { Destroy(downSweet.gameObject); sweet.MovedCommpont.Move(downX, y + 1, fillTime); sweets[downX, y + 1] = sweet; CreateNewSweet(x, y, SweetsType.EMPTY); fillNotFinished = true; break; } } } } } } } } } for (int x = 0; x < xColumn; x++) { GameSweet sweet = sweets[x, 0]; if (sweet.Type == SweetsType.EMPTY) { GameObject obj = Instantiate(sweetPrefabDict[SweetsType.NORMAL], CorrectPosition(x, -1), Quaternion.identity); obj.transform.SetParent(transform); Destroy(sweets[x, 0].gameObject); sweets[x, 0] = obj.GetComponent <GameSweet>(); sweets[x, 0].Init(x, -1, this, SweetsType.NORMAL); sweets[x, 0].MovedCommpont.Move(x, 0, fillTime); sweets[x, 0].ColorCommpont.SetColor((ColorSweet.ColorType)Random.Range(0, sweets[x, 0].ColorCommpont.NumColors)); fillNotFinished = true; } } return(fillNotFinished); }
/// <summary> /// 判断是否能进行消除甜品 /// </summary> /// <param name="sweet"></param> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public List <GameSweet> MatchSweets(GameSweet sweet, int newX, int newY) { if (sweet.CanMove() && sweet.CanColor())//如果当前甜品是能正常更换精灵的甜品(即不是空甜品或者不是障碍物等) { ColorSweet.ColorType color = sweet.ColorSweetComponent.Color; List <GameSweet> matchRowSweetsList = new List <GameSweet>(); //用来做横向匹配的链表 List <GameSweet> matchColumnSweetList = new List <GameSweet>(); // 用来做纵向匹配的链表 List <GameSweet> finishedMatchList = new List <GameSweet>(); //匹配结束后的链表,同时返回该链表 //行匹配 matchRowSweetsList.Add(sweet); for (int i = 0; i <= 1; i++)//0代表往左匹配,1代表往右匹配 { for (int xDistance = 1; xDistance < xColumn; xDistance++) { int x;//和当前甜品要进行匹配的甜品的x坐标 if (i == 0) { x = newX - xDistance; } else { x = newX + xDistance; } if (x < 0 || x >= xColumn)//如果匹配完成则直接退出 { break; } if (sweets[x, newY].CanColor() && color == sweets[x, newY].ColorSweetComponent.Color)//如果类型相同,则可以添加进链表 { matchRowSweetsList.Add(sweets[x, newY]); } else { break; } } } if (matchRowSweetsList.Count >= 3) { for (int m = 0; m < matchRowSweetsList.Count; m++) { finishedMatchList.Add(matchRowSweetsList[m]); } } if (matchRowSweetsList.Count >= 3) { for (int i = 0; i < matchRowSweetsList.Count; i++) { // finishedMatchList.Add(matchRowSweetsList[i]); //行匹配完成后,再进行L,T型匹配,即对每一个二元素的列在进行匹配 for (int j = 0; j <= 1; j++)//j=0代表向上匹配,j=1代表向下匹配 { for (int yDistance = 1; yDistance < yRow; yDistance++) { int y; if (j == 0) { y = newY - yDistance; } else { y = newY + yDistance; } if (y < 0 || y >= yRow) { break; } if (sweets[matchRowSweetsList[i].X, y].CanColor() && sweets[matchRowSweetsList[i].X, y].ColorSweetComponent.Color == color) { matchColumnSweetList.Add(sweets[matchRowSweetsList[i].X, y]); } else { break; } } } if (matchColumnSweetList.Count >= 2)//如果又在列方向找到两个或以上甜品,则添加到完成列表中 { for (int k = 0; k < matchColumnSweetList.Count; k++) { finishedMatchList.Add(matchColumnSweetList[k]); } break; } else//否则进行清空 { matchColumnSweetList.Clear(); } } } if (finishedMatchList.Count >= 3) { return(finishedMatchList); } matchRowSweetsList.Clear(); matchColumnSweetList.Clear(); // finishedMatchList.Clear(); //清空后再进行列匹配 //否则进行列匹配 matchColumnSweetList.Add(sweet); for (int i = 0; i <= 1; i++)//0代表往上匹配,1代表往下匹配 { for (int YDistance = 1; YDistance < yRow; YDistance++) { int y;//和当前甜品要进行匹配的甜品的x坐标 if (i == 0) { y = newY - YDistance; } else { y = newY + YDistance; } if (y < 0 || y >= yRow)//如果匹配完成则直接退出 { break; } if (sweets[newX, y].CanColor() && color == sweets[newX, y].ColorSweetComponent.Color)//如果类型相同,则可以添加进链表 { matchColumnSweetList.Add(sweets[newX, y]); } else { break; } } } if (matchColumnSweetList.Count >= 3) { for (int m = 0; m < matchColumnSweetList.Count; m++) { finishedMatchList.Add(matchColumnSweetList[m]); } } if (matchColumnSweetList.Count >= 3)//列匹配完成后再进行L,T,型匹配,即再对每一个已经确定的甜品进行行遍历 { for (int i = 0; i < matchColumnSweetList.Count; i++) { //finishedMatchList.Add(matchColumnSweetList[i]); for (int j = 0; j <= 1; j++)//j=0代表向左匹配,j=1代表向右匹配 { for (int xDistance = 1; xDistance < xColumn; xDistance++) { int x; if (j == 0) { x = newX - xDistance; } else { x = newX + xDistance; } if (x < 0 || x >= yRow) { break; } if (sweets[x, matchColumnSweetList[i].Y].CanColor() && sweets[x, matchColumnSweetList[i].Y].ColorSweetComponent.Color == color) { matchRowSweetsList.Add(sweets[x, matchColumnSweetList[i].Y]); } else { break; } } } if (matchRowSweetsList.Count >= 2)//如果又在行方向找到两个或以上甜品,则添加到完成列表中 { for (int k = 0; k < matchRowSweetsList.Count; k++) { finishedMatchList.Add(matchRowSweetsList[k]); } break; } else//否则进行清空 { matchRowSweetsList.Clear(); } } } if (finishedMatchList.Count >= 3) { return(finishedMatchList); } } return(null); }
/// <summary> /// 分部填充 /// </summary> /// <returns></returns> public bool Fill() { bool filledNotFinished = false;//判断本次填充是否完成 for (int y = yRow - 2; y >= 0; y--) { for (int x = 0; x < xColumn; x++) { GameSweet sweet = sweets[x, y]; //得到当前元素位置的甜品对象 if (sweet.CanMove()) //如果无法移动,则无法往下填充 { GameSweet sweetBelow = sweets[x, y + 1]; if (sweetBelow.Type == SweetsType.EMPTY) //垂直填充 { Destroy(sweetBelow.gameObject); sweet.MovedComponent.Move(x, y + 1, fillTime); sweets[x, y + 1] = sweet; CreatNewSweet(x, y, SweetsType.EMPTY); filledNotFinished = true; } else //斜向填充 { for (int down = -1; down <= 1; down++) { if (down != 0) //排除0正下 { int downX = x + down; //当前x坐标,-1 左下,0正下,1右下 if (downX >= 0 && downX < xColumn) //排除边界情况 { GameSweet downSweet = sweets[downX, y + 1]; //先从左下方查看 if (downSweet.Type == SweetsType.EMPTY) { bool canfill = true; //判断垂直填充是否能满足填充需求 for (int aboveY = y; aboveY >= 0; aboveY--) { GameSweet sweetAbove = sweets[downX, aboveY]; if (sweetAbove.CanMove())//上方元素可以移动,用上方元素进行垂直填充即可 { break; } else if (!sweetAbove.CanMove() && sweetAbove.Type != SweetsType.EMPTY)//上方元素不能移动,并且上方元素不是空,无法垂直填充,使用斜向填充 { canfill = false; } } if (!canfill) { Destroy(downSweet.gameObject); sweet.MovedComponent.Move(downX, y + 1, fillTime); sweets[downX, y + 1] = sweet; CreatNewSweet(x, y, SweetsType.EMPTY); filledNotFinished = true; break; } } } } } } } } } //最上排的特殊情况 for (int x = 0; x < xColumn; x++) { GameSweet sweet = sweets[x, 0]; if (sweet.Type == SweetsType.EMPTY) { GameObject newSweet = Instantiate(sweetPrefabDict[SweetsType.NORMAL], CorrectPosition(x, 0), Quaternion.identity); newSweet.transform.SetParent(transform); sweets[x, 0] = newSweet.GetComponent <GameSweet>(); sweets[x, 0].Init(x, -1, this, SweetsType.NORMAL); sweets[x, 0].MovedComponent.Move(x, 0, fillTime); sweets[x, 0].ColoredComponent.SetColor((ColorSweet.ColorType)Random.Range(0, sweets[x, 0].ColoredComponent.NumColors)); filledNotFinished = true; } } return(filledNotFinished); }
/// <summary> /// 分步填充 /// </summary> public bool Fill() { bool filledNotFinished = false;//判断本次填充是否完成 for (int y = yRow - 2; y >= 0; y--) { for (int x = 0; x < xColumn; x++) { GameSweet sweet = sweets[x, y]; //得到当前元素位置tianpin if (sweet.CanMove()) //如果无法移动,则无法往下填充 { GameSweet sweetBelow = sweets[x, y + 1]; if (sweetBelow.Type == SweetType.EMPTY) { Destroy(sweetBelow.gameObject); sweet.MovedComponent.Move(x, y + 1, fillTime); sweets[x, y + 1] = sweet; CreateNewSweet(x, y, SweetType.EMPTY); filledNotFinished = true; } else //斜向填充 { for (int down = -1; down <= 1; down++) { if (down != 0) { int downX = x + down; if (downX >= 0 && downX < xColumn) { GameSweet downSweet = sweets[downX, y + 1]; if (downSweet.Type == SweetType.EMPTY) { bool canfill = true; for (int aboveY = y; aboveY >= 0; aboveY--) { GameSweet sweetAbove = sweets[downX, aboveY]; if (sweetAbove.CanMove()) { break; } else if (!sweetAbove.CanMove() && sweetAbove.Type != SweetType.EMPTY) { canfill = false; break; } } if (!canfill) { Destroy(downSweet.gameObject); sweet.MovedComponent.Move(downX, y + 1, fillTime); sweets[downX, y + 1] = sweet; CreateNewSweet(x, y, SweetType.EMPTY); filledNotFinished = true; break; } } } } } } } } } //最上排特殊情况 for (int x = 0; x < xColumn; x++) { GameSweet sweet = sweets[x, 0]; if (sweet.Type == SweetType.EMPTY) { GameObject newSweet = Instantiate(sweetPrefabDic[SweetType.NORMAL], CorrectPosition(x, -1), Quaternion.identity) as GameObject; newSweet.transform.parent = transform; sweets[x, 0] = newSweet.GetComponent <GameSweet>(); sweets[x, 0].Init(x, -1, this, SweetType.NORMAL); sweets[x, 0].MovedComponent.Move(x, 0, fillTime); sweets[x, 0].ColoredComponent.SetColor((ColorSweet.ColorType)(Random.Range(0, sweets[x, 0].ColoredComponent.NumColors))); filledNotFinished = true; } } return(filledNotFinished); }