/// <summary> /// 组件是否可以合并 (根据id范围快速确定) /// </summary> /// <param name="obj"></param> /// <returns></returns> public bool CanBeMerge(Serializable.MapObject obj) { var strId = obj.prefab; int id = 0; int.TryParse(strId, out id); if (id >= 14000 && id < 15000) { return(true); } else { return(false); } }
private void AddGridObject(Serializable.MapObject obj) { int rowIndex = Mathf.FloorToInt(obj.transform.position.y) + rowOffset; if (rowIndex < 0) { Debug.LogError("Error rowIndex: " + obj.prefab + " pos: " + obj.transform.position); rowIndex = 0; } // int columnIndex = Mathf.FloorToInt(obj.transform.position.z) + columnOfsset; if (columnIndex < 0) { Debug.LogError("Error rowIndex: " + obj.prefab + " pos: " + obj.transform.position); columnIndex = 0; } array[rowIndex, columnIndex] = obj; }
/// <summary> /// 合并 /// </summary> public void MergeGrid() { /////////////////////////////////////////////////////// //先横向合 for (int i = 0; i <= maxRow; i++) { int start = -1; int end = -1; for (int j = 0; j <= maxColumn; j++) { if (array[i, j] != null) { if (start == -1) { start = j; } else { end = j; } } else //横向中断了 { //之前已经有连续的了 if ((end > start) && (start >= 0)) { Serializable.MapObject obj = new Serializable.MapObject(); obj.Clone(array[i, start]); //调整位置要合并组件的中间 obj.transform.position = (array[i, start].transform.position + array[i, end].transform.position) / 2; //调整缩放系数 obj.transform.scale.z = obj.transform.scale.z * (end - start + 1); for (int k = start; k <= end; k++) { //被合了 this.terrainMapObjects.Add(array[i, k]); array[i, k] = null; } this.AddGridObject(obj); } //重置变量 start = -1; end = -1; } } } /////////////////////////////////////////////////////// //后竖向合 for (int j = 0; j <= maxColumn; j++) { int start = -1; int end = -1; for (int i = 0; i <= maxRow; i++) { if (array[i, j] != null) { if (start == -1) { start = i; end = i; } else { end = i; } } else //竖向中断了 { //之前已经有连续的了 if ((end >= start) && (start >= 0)) { //新生成的合并组件 Serializable.MapObject obj = new Serializable.MapObject(); obj.Clone(array[start, j]); //获取横向最小的格子(Object) float minZScale = array[start, j].transform.scale.z; for (int a = start; a <= end; a++) { if (array[a, j].transform.scale.z < minZScale) { minZScale = array[a, j].transform.scale.z; } } //如果两端还有更宽的组件,延伸一格或两格 bool hasMoreStartGrid = this.HasMoreStartGrid(start, j, minZScale); bool hasMoreEndGrid = this.HasMoreEndGrid(end, j, minZScale); if (hasMoreStartGrid && hasMoreEndGrid) { //调整位置要合并组件的中间 obj.transform.position = (array[start, j].transform.position + array[end, j].transform.position) / 2; //多二格 obj.transform.scale.y = obj.transform.scale.y * (end - start + 3); obj.transform.scale.z = minZScale; } else if (hasMoreStartGrid) { //调整位置要合并组件的中间 obj.transform.position = (array[start, j].transform.position + array[end, j].transform.position) / 2; //start更小 if (array[end, j].transform.position.y >= array[start, j].transform.position.y) { obj.transform.position.y -= 0.5f; } else { obj.transform.position.y += 0.5f; } //多一格 obj.transform.scale.y = obj.transform.scale.y * (end - start + 2); obj.transform.scale.z = minZScale; } else if (hasMoreEndGrid) { //调整位置要合并组件的中间 obj.transform.position = (array[start, j].transform.position + array[end, j].transform.position) / 2; //end更大 if (array[end, j].transform.position.y >= array[start, j].transform.position.y) { obj.transform.position.y += 0.5f; } else { obj.transform.position.y -= 0.5f; } //多一格 obj.transform.scale.y = obj.transform.scale.y * (end - start + 2); obj.transform.scale.z = minZScale; } else { //调整位置要合并组件的中间 obj.transform.position = (array[start, j].transform.position + array[end, j].transform.position) / 2; //调整缩放系数 obj.transform.scale.y = obj.transform.scale.y * (end - start + 1); obj.transform.scale.z = minZScale; } //新的不再被合并了,输出 if (end > start || obj.transform.scale.z > 1.0f) { this.collideMapObjects.Add(obj); } //处理要被合并的格子(Object) for (int k = start; k <= end; k++) { //宽度相等的格子(Object) if (this.IsFloatEqual(array[k, j].transform.scale.z, minZScale)) { if (end > start) //2个及以上格子要合并 { //之前没有被合并过的格子(Object) if (this.IsFloatEqual(array[k, j].transform.scale.z, 1.0f)) { this.terrainMapObjects.Add(array[k, j]); } } array[k, j] = null; } else// !=minZScale { //之前被合过的,又大于最小宽度, 并放入collide中 if (array[k, j].transform.scale.z > 1.0f) { this.collideMapObjects.Add(array[k, j]); array[k, j] = null; } } } //合并后的继续放在array里因为后续的合并还需要之前的信息(看看合并能不能扩展一格) this.AddGridObject(obj); } //重置变量 start = -1; end = -1; } } } }