private void SetPiece(EditorLevelPiece piece) { List <GridVO> grids = EditorVO.Instance.piecesGrids; int minx = piece.grids[0].x; int maxy = piece.grids[0].y; for (int i = 1; i < piece.grids.Count; i++) { if (piece.grids[i].x < minx) { minx = piece.grids[i].x; } if (piece.grids[i].y > maxy) { maxy = piece.grids[i].y; } } int offx = minx % 2 == 0 ? -minx : -minx + 1; int offy = -maxy; for (int y = 0; y > -10; y--) { for (int x = 0; x < 22; x++, x++) { bool find = true; for (int i = 0; i < piece.grids.Count; i++) { GridVO grid = EditorVO.Instance.GetGridPiece((int)piece.grids[i].x + offx + x, (int)piece.grids[i].y + offy + y); if (grid == null || grid.color.value != 0) { find = false; break; } else { //获取周围的格子 List <Point2D> nextCoords = HaxgonCoord <Point2D> .GetCoordsNextTo(Point2D.Create((int)piece.grids[i].x + offx + x, (int)piece.grids[i].y + offy + y)); for (int n = 0; n < nextCoords.Count; n++) { GridVO nextGrid = EditorVO.Instance.GetGridPiece((int)nextCoords[n].x, (int)nextCoords[n].y); if (nextGrid != null && nextGrid.color.value != 0 && nextGrid.color.value != grid.color.value) { find = false; break; } } } } if (find) { for (int i = 0; i < piece.grids.Count; i++) { EditorVO.Instance.GetGridPiece((int)piece.grids[i].x + offx + x, (int)piece.grids[i].y + offy + y).color.value = piece.grids[i].color; } return; } } } }
private Point2D TrySetPiece(int x, int y, Piece piece, HaxgonCoord <Coord> sys) { bool find = true; for (int i = 0; i < piece.coords.length; i++) { Point2D p = HaxgonCoord <Coord> .CoordToPosition(Point2D.Create(piece.coords[i].x, piece.coords[i].y), 0.2f); p.x += piece.offx * 0.5f; p.y += piece.offy * 0.5f; Point2D p2 = HaxgonCoord <Coord> .CoordToPosition(Point2D.Create(x, y), 0.2f); p2.x += p.x; p2.y += p.y; p = HaxgonCoord <Coord> .PositionToCoord(p2, 0.2f); Coord grid = sys.GetCoord(p); if (grid == null || grid.type != 0) { find = false; break; } else { //获取周围的格子 List <Point2D> nextCoords = HaxgonCoord <Coord> .GetCoordsNextTo(p); for (int n = 0; n < nextCoords.Count; n++) { Coord nextGrid = sys.GetCoord(Point2D.Create(nextCoords[n].x, nextCoords[n].y)); if (nextGrid != null && nextGrid.piece != grid.piece) { find = false; break; } } } } if (find) { for (int i = 0; i < piece.coords.length; i++) { Point2D p = HaxgonCoord <Coord> .CoordToPosition(Point2D.Create(piece.coords[i].x, piece.coords[i].y), 0.2f); p.x += piece.offx * 0.5f; p.y += piece.offy * 0.5f; Point2D p2 = HaxgonCoord <Coord> .CoordToPosition(Point2D.Create(x, y), 0.2f); p2.x += p.x; p2.y += p.y; p = HaxgonCoord <Coord> .PositionToCoord(p2, 0.2f); sys.SetCoord(p, piece.coords[i]); } return(Point2D.Create(x, y)); } return(null); }
private GridVO GetGridNextTo(int x, int y, int color, List <GridVO> grids) { List <Point2D> nextCoords = HaxgonCoord <Point2D> .GetCoordsNextTo(Point2D.Create(x, y)); for (int i = 0; i < grids.Count; i++) { for (int n = 0; n < nextCoords.Count; n++) { if (grids[i].x.value == nextCoords[n].x && grids[i].y.value == nextCoords[n].y && grids[i].color.value == color) { return(grids[i]); } } } return(null); }
private Point2D AutoSetPiece(Piece piece, HaxgonCoord <Coord> sys) { List <Point2D> list = new List <Point2D>(); float minX = 1000; float maxX = -1000; float minY = 1000; float maxY = -1000; for (int py = 0; py > this.miny; py--) { for (int x = 0; x < this.maxx; x++) { int y = py - 3 + movesy[x]; list.Add(Point2D.Create(x, y)); Point2D position = HaxgonCoord <Coord> .CoordToPosition(list[list.Count - 1], 0.2f); if (position.x < minX) { minX = position.x; } if (position.x > maxX) { maxX = position.x; } if (position.y < minY) { minY = position.y; } if (position.y > maxY) { maxY = position.y; } } } Point2D center = HaxgonCoord <Coord> .PositionToCoord(Point2D.Create((minX + maxX) * 0.5f, (minY + maxY) * 0.5f), 0.2f); //从可选格子的正中心开始,以六边形的方式向外扩展找可以放的地方 Dictionary <string, bool> findMap = new Dictionary <string, bool>(); Dictionary <string, bool> findMap2 = new Dictionary <string, bool>(); findMap2.Add(center.x + "," + center.y, true); List <Point2D> currentList = new List <Point2D>(); currentList.Add(center); while (list.Count > 0) { for (int i = 0; i < currentList.Count; i++) { //在 list 中查找该点 bool findInList = false; for (int l = 0; l < list.Count; l++) { if (list[l].x == currentList[i].x && list[l].y == currentList[i].y) { list.RemoveAt(l); findInList = true; break; } } if (findInList) { //如果找到则查看该点是否可以放 Point2D result = TrySetPiece((int)currentList[i].x, (int)currentList[i].y, piece, sys); if (result != null) { return(result); } } } List <Point2D> newList = new List <Point2D>(); //展开所有的点 for (int i = 0; i < currentList.Count; i++) { if (findMap.ContainsKey(currentList[i].x + "," + currentList[i].y)) { continue; } //展开 List <Point2D> nextCoords = HaxgonCoord <Coord> .GetCoordsNextTo(currentList[i]); for (int n = 0; n < nextCoords.Count; n++) { if (findMap2.ContainsKey(nextCoords[n].x + "," + nextCoords[n].y)) { continue; } findMap2.Add(nextCoords[n].x + "," + nextCoords[n].y, true); newList.Add(nextCoords[n]); } } currentList = newList; } return(null); }