public void RemoveBorderBetweenCells(GraphVertex otherGraphVertex) { QuadCell otherCell = (QuadCell)otherGraphVertex; foreach (QuadCell c in this.connectedCells.Values) { // We look for the adjacent cell to remove the border if (c.Equals(otherCell)) { // We look for the corner points which are common to the two cells List <Vertex> commonVertices = new List <Vertex> (); foreach (Vertex v1 in this.cornerPoints.Values) { foreach (Vertex v2 in otherCell.cornerPoints.Values) { if (v1.Equals(v2)) { commonVertices.Add(v1); } } } // We disconnect all the border points which are common to the 2 cells so that the way is open foreach (Vertex v1 in commonVertices) { foreach (Vertex v2 in commonVertices) { v1.RemoveNeighbour(v2); } } } } }
private void CreateMapGrid() { ClearMapGrid(); //计算行数,列数 int rowCount = height / cellSize; int colCount = width / cellSize; //先绘制行 for (int i = 0; i < rowCount; i++) { for (int j = 0; j < colCount; j++) { QuadCell cell = Instantiate <QuadCell>(cellPrefab); cell.transform.SetParent(transform, false); Vector3 position = new Vector3(); position.x = cellSize / 2 + j * cellSize; position.y = 0; position.z = cellSize / 2 + i * cellSize; cell.transform.localPosition = position; cell.transform.localScale = new Vector3(cellSize, 0.001f, cellSize); QuadCoordinates coordinate = new QuadCoordinates(j, i); cell.coordinate = coordinate; this.cells.Add(cell); //添加标签 // Text label = Instantiate<Text>(cellLabelPrefab); // label.transform.SetParent(gridCanvas.transform); // label.transform.localPosition = new Vector3(position.x,position.z,1); // label.text = coordinate.ToString(); } } }
// Connects 2 cells (connects the corner points each other and the cores) public void ConnectToOtherGraphVertex(GraphVertex other) { QuadCell cell = (QuadCell)other; if (cell.core.Coo.x < this.core.Coo.x - cellSize / 2) { // The cell is on the left, so we connect the appropriate corners ReplaceCorner("up left", cell.cornerPoints["up right"]); ReplaceCorner("down left", cell.cornerPoints ["down right"]); this.connectedCells.Add("left", cell); cell.connectedCells.Add("right", this); } else if (cell.core.Coo.x > this.core.Coo.x + cellSize / 2) { // The cell is on the right ReplaceCorner("up right", cell.cornerPoints["up left"]); ReplaceCorner("down right", cell.cornerPoints ["down left"]); this.connectedCells.Add("right", cell); cell.connectedCells.Add("left", this); } else if (cell.core.Coo.y < this.core.Coo.y - cellSize / 2) { // The cell is down ReplaceCorner("down right", cell.cornerPoints["up right"]); ReplaceCorner("down left", cell.cornerPoints ["up left"]); this.connectedCells.Add("down", cell); cell.connectedCells.Add("up", this); } else if (cell.core.Coo.y > this.core.Coo.y + cellSize / 2) { // The cell is up ReplaceCorner("up left", cell.cornerPoints["down left"]); ReplaceCorner("up right", cell.cornerPoints ["down right"]); this.connectedCells.Add("up", cell); cell.connectedCells.Add("down", this); } // Now we connect the cores this.core.AddNeighbour(cell.core); cell.core.AddNeighbour(this.core); }