//新的交换规则 private void ExchangeModel_New(ModelBase m1, ModelBase m2) { if (m1.CanMove() && m2.CanMove()) { models[m1.X, m1.Y] = m2; models[m2.X, m2.Y] = m1; if (step == 0) { int tempX = m1.X; int tempY = m1.Y; if (MatchModels(m2, m1.X, m1.Y) != null || MatchModels(m1, m2.X, m2.Y) != null) { m1.ModelMoveComponent.Move(m2.X, m2.Y, fillTime);//交换 m2.ModelMoveComponent.Move(tempX, tempY, fillTime); step = 0; ClearAllMatchModels(); //清除所有匹配的model StartCoroutine(FillAll(fillTime)); //将消除后的空位进行填充 curStep.text = 2 + ""; } else { m1.ModelMoveComponent.Move(m2.X, m2.Y, fillTime);//交换 m2.ModelMoveComponent.Move(tempX, tempY, fillTime); step++; curStep.text = "1"; } } else { int tempX = m1.X; int tempY = m1.Y; if (MatchModels(m2, m1.X, m1.Y) != null || MatchModels(m1, m2.X, m2.Y) != null) { m1.ModelMoveComponent.Move(m2.X, m2.Y, fillTime); //交换 m2.ModelMoveComponent.Move(tempX, tempY, fillTime); ClearAllMatchModels(); //清除所有匹配的model StartCoroutine(FillAll(fillTime)); //将消除后的空位进行填充 step = 0; curStep.text = "2"; } else { //还原基础脚本 models[m1.X, m1.Y] = m1; models[m2.X, m2.Y] = m2; models[m1.X, m1.Y].ModelMoveComponent.Undo(m1, m2, fillTime);//交换位置再还原 step = 1; curStep.text = "1"; } } } m1.ModelColorComponent.UnSelect(m1.ModelColorComponent.highlight); m2.ModelColorComponent.UnSelect(m2.ModelColorComponent.highlight); lastSelectModel = selectModel; selectModel = null; }
//交换model private void ExchangeModel(ModelBase m1, ModelBase m2) { //Debug.Log(selectModel.ModelColorComponent.Color+" "+targetModel.ModelColorComponent.Color); if (m1.CanMove() && m2.CanMove()) { models[m1.X, m1.Y] = m2; models[m2.X, m2.Y] = m1; if (step == 0) { if (MatchModels(m2, m1.X, m1.Y) != null || MatchModels(m1, m2.X, m2.Y) != null || m1.Type == ModelType.RainBow || m2.Type == ModelType.RainBow) { int tempX = m1.X; int tempY = m1.Y; m1.ModelMoveComponent.Move(m2.X, m2.Y, fillTime);//交换 m2.ModelMoveComponent.Move(tempX, tempY, fillTime); if (m1.Type == ModelType.RainBow && m1.CanClear() && m2.CanClear()) { ModelColorClearByType mcct = m1.transform.GetComponent <ModelColorClearByType>(); //Debug.Log(mcct == null); if (mcct != null) { mcct.Color = m2.ModelColorComponent.Color; ClearByType(mcct.Color); } m1.ModelClearComponent.Clear(); models[m1.X, m1.Y] = CreatNewModel(m1.X, m1.Y, ModelType.Empty); ClearModel(m1.X, m1.Y); //StartCoroutine(FillAll(fillTime));//将消除后的空位进行填充 } if (m2.Type == ModelType.RainBow && m2.CanClear() && m1.CanClear()) { ModelColorClearByType mcct = m2.transform.GetComponent <ModelColorClearByType>(); //Debug.Log(mcct == null); if (mcct != null) { mcct.Color = m1.ModelColorComponent.Color; ClearByType(mcct.Color); } m2.ModelClearComponent.Clear(); models[m2.X, m2.Y] = CreatNewModel(m2.X, m2.Y, ModelType.Empty); ClearModel(m2.X, m2.Y); //StartCoroutine(FillAll(time));//将消除后的空位进行填充 } ClearAllMatchModels(); //清除所有匹配的model StartCoroutine(FillAll(fillTime)); //将消除后的空位进行填充 selectModel = null; targetModel = null; step = 0; curStep.text = "0"; } else { int tempX = m1.X; int tempY = m1.Y; m1.ModelMoveComponent.Move(m2.X, m2.Y, fillTime);//交换 m2.ModelMoveComponent.Move(tempX, tempY, fillTime); step++; targetModel = null; curStep.text = "1"; } } else { if (MatchModels(m2, m1.X, m1.Y) != null || MatchModels(m1, m2.X, m2.Y) != null || m1.Type == ModelType.RainBow || m2.Type == ModelType.RainBow) { int tempX = m1.X; int tempY = m1.Y; m1.ModelMoveComponent.Move(m2.X, m2.Y, fillTime);//交换 m2.ModelMoveComponent.Move(tempX, tempY, fillTime); if (m1.Type == ModelType.RainBow && m1.CanClear() && m2.CanClear()) { ModelColorClearByType mcct = m1.transform.GetComponent <ModelColorClearByType>(); //Debug.Log(mcct == null); if (mcct != null) { mcct.Color = m2.ModelColorComponent.Color; ClearByType(mcct.Color); } m1.ModelClearComponent.Clear(); models[m1.X, m1.Y] = CreatNewModel(m1.X, m1.Y, ModelType.Empty); ClearModel(m1.X, m1.Y); //StartCoroutine(FillAll(fillTime));//将消除后的空位进行填充 } if (m2.Type == ModelType.RainBow && m2.CanClear() && m1.CanClear()) { ModelColorClearByType mcct = m2.transform.GetComponent <ModelColorClearByType>(); //Debug.Log(mcct == null); if (mcct != null) { mcct.Color = m1.ModelColorComponent.Color; ClearByType(mcct.Color); } m2.ModelClearComponent.Clear(); models[m2.X, m2.Y] = CreatNewModel(m2.X, m2.Y, ModelType.Empty); ClearModel(m2.X, m2.Y); //StartCoroutine(FillAll(time));//将消除后的空位进行填充 } ClearAllMatchModels(); //清除所有匹配的model StartCoroutine(FillAll(fillTime)); //将消除后的空位进行填充 selectModel = null; targetModel = null; step = 0; curStep.text = "0"; } else { //还原基础脚本 models[m1.X, m1.Y] = m1; models[m2.X, m2.Y] = m2; models[m1.X, m1.Y].ModelMoveComponent.Undo(m1, m2, fillTime);//交换位置再还原 step = 1; curStep.text = "1"; } } } //StartCoroutine(FillAll(fillTime));//将消除后的空位进行填充 }
//分布填充 public bool Fill(float t) { isSkill = true; bool notFinished = false;//本次填充是否完成 for (int y = yRow - 2; y >= 0; y--) { for (int x = 0; x < xCol; x++) { ModelBase model = models[x, y];//当前元素的基础组件 //向下填充空缺 if (model.CanMove()) { ModelBase modelBelow = models[x, y + 1]; //正下方model组件 if (modelBelow.Type == ModelType.Empty) //垂直填充 { if (modelBelow.gameObject != null) { Destroy(modelBelow.gameObject); model.ModelMoveComponent.Move(x, y + 1, t); //向下移动 models[x, y + 1] = model; //正下方的组件指向当前组件 CreatNewModel(x, y, ModelType.Empty); //当前元素置空 notFinished = true; } else { CreatNewModel(x, y + 1, ModelType.Empty);//生成一个空物体 } } //斜向填充,用于解决存在障碍物的情况 // else { // for (int down = -8; down <= 8; down++) { // if (down != 0) { // int downX = x + down; // if (downX >= 0 && downX < xCol) {//排除最右侧 // ModelBase downModel = models[Mathf.Clamp(downX, 0, 7), y + 1]; // if (downModel.Type == ModelType.Empty) { // bool canFill = true;//是否满足垂直填充 // for (int aboveY = y; aboveY >= 0; aboveY--) { // ModelBase modelAbove = models[downX, aboveY]; // if (modelAbove.CanMove()) { // break; // } // else if (!modelAbove.CanMove() && modelAbove.Type != ModelType.Empty) { // canFill = false; // break; // } // } // //斜向填充 // if (!canFill) { // if (downModel.gameObject != null) { // Destroy(downModel.gameObject); // model.ModelMoveComponent.Move(downX, y + 1, t); // models[downX, y + 1] = model; // CreatNewModel(x, y, ModelType.Empty); // notFinished = true; // break; // } // } // } // } // } // } // } } } } //最底下的一层 for (int x = 0; x < xCol; x++) { ModelBase model = models[x, 0];//当前元素的基础组件 if (model.Type == ModelType.Empty) { //在y坐标为-1的位置生成 GameObject newModel = Instantiate(modelPrefabDict[ModelType.Normal], CalGridPos(x, -1), Quaternion.identity); newModel.transform.parent = this.transform; //设置父物体 models[x, 0] = newModel.GetComponent <ModelBase>(); //更新基础组件位置 models[x, 0].Init(x, -1, this, ModelType.Normal); //初始化 if (models[x, 0].CanMove()) { models[x, 0].ModelMoveComponent.Move(x, 0, fillTime);//向下移动 } //随机一个颜色 models[x, 0].ModelColorComponent.SetColor((ModelColor.ColorType)UnityEngine.Random.Range(0, models[x, 0].ModelColorComponent.Nums)); notFinished = true; } } canClick = true; return(notFinished); }