// 检测可合并的物体 public void CheckLinkSurround(Tile tile) { // 重置状态、清空合并列表、清空得分列表 ResetCombineList(); _combineUnitList.Clear(); ScoreManager.Instance.ClearScoreList(); // 重置CurrentUnit的NextLevel和Special CurrentUnit.NextLevel = CurrentUnit.Level; CurrentUnit.Special = 1; // 合并过程 GF.MyPrint("=========== CheckLinkSurround Began: " + tile.name); LinkSurround(tile); GF.MyPrint("=========== CheckLinkSurround Ended: " + tile.name); // 连接完成,检查是否会合并 if (_combineUnitList.Count >= 2) { foreach (var unit in _combineUnitList) { unit.ReadyToCombine(tile.transform.position); } } }
// 单次合并(递归) private void DoLink(Tile tile) { GF.MyPrint("----------- DoLink Began"); int start_x = tile.X - 1; int end_x = tile.X + 1; start_x = start_x < 0 ? tile.X : start_x; end_x = end_x > _row - 1 ? tile.X : end_x; for (int x = start_x; x <= end_x; x++) { int offset_y = 1 - Mathf.Abs(x - tile.X); int start_y = tile.Y - offset_y; int end_y = tile.Y + offset_y; start_y = start_y < 0 ? tile.Y : start_y; end_y = end_y > _col - 1 ? tile.Y : end_y; for (int y = start_y; y <= end_y; y++) { if (x == tile.X && y == tile.Y) { continue; // 不检测自身 } Tile surroundTile = GetTile(x, y); if (surroundTile.IsEmpty()) { GF.MyPrint(surroundTile.name + " is Empty"); continue; // 如果是空的,跳过 } Unit unit = surroundTile.Unit; if (CheckIsInCombineList(unit) || unit == CurrentUnit) { GF.MyPrint(unit.name + " is already added"); continue; // 已加入列表,跳过 } if (unit.Level != CurrentUnit.NextLevel) { GF.MyPrint(string.Format("Level not match: {0}/Current : {1}/{2}", unit.name, unit.Level, CurrentUnit.NextLevel)); continue; // 等级不一样,跳过 } if (unit.Type != CurrentUnit.Type) { GF.MyPrint(string.Format("Type not match: {0}/Current : {1}/{2}", unit.name, unit.Type, CurrentUnit.Type)); continue; // 类型不一样,跳过 } MoveUnit moveUnit = unit.GetComponent <MoveUnit>(); if (moveUnit != null && moveUnit.IsAlive) { GF.MyPrint(string.Format("{0} is still alive", moveUnit.name)); continue; // 移动物体还活着 } GF.MyPrint(unit.name + " will be added"); _combineUnitList.Add(unit); _singleCombineUnitList.Add(unit); // 递归检测下一个邻位 DoLink(surroundTile); } } GF.MyPrint("----------- DoLink Ended"); }
// 链接可以合并的物体(递归) private void LinkSurround(Tile tile) { GF.MyPrint("----------- LinkSurround Began: " + tile.name); // 清空单次合并列表 _singleCombineUnitList.Clear(); // 开始单次合并 DoLink(tile); GF.MyPrint("Single Combine contains: " + _singleCombineUnitList.Count); /** 计算分数,分数规则为: * /* 1. 未合体:得到物体本身的分数 * /* 2. 合体:得到合体过程中每一级物体的分数(但不包含初始的物体)*/ // 单次合并如果成功后,继续查找下一等级的物体 if (_singleCombineUnitList.Count >= 2) { CurrentUnit.NextLevel++; // 等级提升 CurrentUnit.Special = (_singleCombineUnitList.Count > 2 ? 2 : 1); // 是否超过3个合体,变为Special物体 // 将分数加入分数列表准备做分步运算 ScoreManager.Instance.AddToScoreList(CurrentUnit.Score); // 递归检测下一等级的Unit是否会合体 LinkSurround(CurrentUnit.Tile); } else { GF.MyPrint("Remove unit which there is only one...\t" + _singleCombineUnitList.Count); _combineUnitList = _combineUnitList.Except(_singleCombineUnitList).ToList(); // 没有升过级,得分为本身的分数 if (CurrentUnit.NextLevel == CurrentUnit.Level) { ScoreManager.Instance.AddToScoreList(CurrentUnit.Score); } } GF.MyPrint("----------- LinkSurround Ended"); }
// 游戏结束 private void GameOver() { GF.MyPrint("Game Over!"); }
// 移除地块 public void EraseUnit(Tile tile) { // 如果当前物体是消除操作,消除选中地块上的物体 GF.MyPrint("=========== EraseUnit Began: " + tile.name); }