//清除匹配的model列表 public bool ClearAllMatchModels() { bool needFill = false; for (int y = 0; y < yRow; y++) { for (int x = 0; x < xCol; x++) { if (models[x, y].CanClear()) { List <ModelBase> matchList = MatchModels(models[x, y], x, y); if (matchList != null) { int num = matchList.Count; //根据消除个数处理分数 if (gameBegin) { switch (num) { case 3: scoreStep = 20; score += scoreStep; ScoreAddEffect(0); break; case 4: Excellent(0); scoreStep = 60; score += scoreStep; ScoreAddEffect(1); break; case 5: scoreStep = 80; score += scoreStep; if (isSkill == false) { gameTime += 6f; curLevelTime += 6; Excellent(1); PlayerPrefs.SetInt("TimeAddNums", PlayerPrefs.GetInt("TimeAddNums", 0) + 6); //记录累计加时 } ScoreAddEffect(2); break; case 6: matchSixTimes++; scoreStep = 180; score += scoreStep; if (isSkill == false) { gameTime += 10; curLevelTime += 10; Excellent(2); PlayerPrefs.SetInt("TimeAddNums", PlayerPrefs.GetInt("TimeAddNums", 0) + 10); //记录累计加时 } ScoreAddEffect(3); break; case 7: scoreStep = 310; score += scoreStep; if (isSkill == false) { curLevelTime += 15; gameTime += 15; Excellent(3); PlayerPrefs.SetInt("TimeAddNums", PlayerPrefs.GetInt("TimeAddNums", 0) + 15); //记录累计加时 } PlayerPrefs.SetInt("Diamand", PlayerPrefs.GetInt("Diamand", 0) + award); diamandText.text = award + ""; ScoreAddEffect(4); showTaskFinishedPanel = true; break; } //生成奖励球 ModelType specialModelType = ModelType.Count;//是否产生特殊奖励 ModelBase model = matchList[UnityEngine.Random.Range(0, matchList.Count)]; //ModelBase model = models[UnityEngine.Random.Range(0, xCol), UnityEngine.Random.Range(0, yRow)]; int specialModelX = model.X; int specialModelY = model.Y; if (matchList.Count == 5) { specialModelType = ModelType.CrossClear; } else if (matchList.Count >= 6 && !isSkill) { specialModelType = ModelType.RainBow; Debug.Log("rainbow " + matchList.Count); } if (isSkill) { specialModelType = ModelType.Count; } matchList.Add(models[model.x, model.y]); foreach (var m in matchList) { if (ClearModel(m.X, m.Y)) { needFill = true; } } if (specialModelType != ModelType.Count) { isSkill = true; } if (specialModelType != ModelType.Count && gameBegin) { if (models[specialModelX, specialModelY].type == ModelType.Empty) { Destroy(models[specialModelX, specialModelY]); ModelBase newModel = CreatNewModel(specialModelX, specialModelY, specialModelType); if (specialModelType == ModelType.CrossClear && newModel.CanColor() && matchList[0].CanColor()) { newModel.ModelColorComponent.SetColor(ModelColor.ColorType.Cross); } //Rainbow else if (specialModelType == ModelType.RainBow && newModel.CanColor()) { newModel.ModelColorComponent.SetColor(ModelColor.ColorType.Rainbow); } } } } } } } } return(needFill); }
//匹配model public List <ModelBase> MatchModels(ModelBase model, int newX, int newY) { if (model.CanColor()) { ModelColor.ColorType color = model.ModelColorComponent.Color; List <ModelBase> matchRow = new List <ModelBase>(); //存取行 List <ModelBase> matchCol = new List <ModelBase>(); //存取列 List <ModelBase> match = new List <ModelBase>(); //存取全部可消除的列表 //行匹配 matchRow.Add(model); //i=0代表往左,i=1代表往右 for (int i = 0; i <= 1; i++) { for (int xDistance = 1; xDistance < xCol; xDistance++) { int x; if (i == 0) { x = newX - xDistance; } else { x = newX + xDistance; } if (x < 0 || x >= xCol) { break; } if (models[x, newY].CanColor() && models[x, newY].ModelColorComponent.Color == color) { matchRow.Add(models[x, newY]); } else { break; } } } if (matchRow.Count >= 3) { foreach (var r in matchRow) { match.Add(r); } } //L T型匹配 //检查一下当前行遍历列表中的元素数量是否大于3 if (matchRow.Count >= 3) { for (int i = 0; i < matchRow.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 (models[matchRow[i].X, y].CanColor() && models[matchRow[i].X, y].ModelColorComponent.Color == color) { matchCol.Add(models[matchRow[i].X, y]); } else { break; } } } if (matchCol.Count < 2) { matchCol.Clear(); } else { for (int j = 0; j < matchCol.Count; j++) { match.Add(matchCol[j]); } break; } } } if (match.Count >= 3) { return(match); } matchRow.Clear(); matchCol.Clear(); matchCol.Add(model); //列匹配 //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 (models[newX, y].CanColor() && models[newX, y].ModelColorComponent.Color == color) { matchCol.Add(models[newX, y]); } else { break; } } } if (matchCol.Count >= 3) { for (int i = 0; i < matchCol.Count; i++) { match.Add(matchCol[i]); match.Add(matchCol[i]); } } //L T型匹配 //检查一下当前行遍历列表中的元素数量是否大于3 if (matchCol.Count >= 3) { for (int i = 0; i < matchCol.Count; i++) { //行匹配列表中满足匹配条件的每个元素上下依次进行列遍历 // 0代表上方 1代表下方 for (int j = 0; j <= 1; j++) { for (int xDistance = 1; xDistance < xCol; xDistance++) { int x; if (j == 0) { x = newX - xDistance; } else { x = newX + xDistance; } if (x < 0 || x >= xCol) { break; } if (models[x, matchCol[i].Y].CanColor() && models[x, matchCol[i].Y].ModelColorComponent.Color == color) { matchRow.Add(models[x, matchCol[i].Y]); } else { break; } } } if (matchRow.Count < 2) { matchRow.Clear(); } else { for (int j = 0; j < matchRow.Count; j++) { match.Add(matchRow[j]); } break; } } } if (match.Count >= 3) { return(match); } } return(null); }