public void AddNeighbor(int index, MapNode node, bool assignPos = false) { if (mNeighbor [index] == null) { mNeighbor [index] = node; if (assignPos) { node.mUnit.transform.Translate(GetNeighborPosition(index)); } int oppsite = GetOppsiteEdge (index); node.AddNeighbor (oppsite, this); //Check connect left node node.CheckConnect (index, node, true); //Check connect right node node.CheckConnect (index, node, false); } }
//registers neigbors with nodes during construction of the map //should be called in the constructor or a loader, where the nodes array is filled private void AssignNeighbors(MapNode node, int x, int z) { if (x > 0 && x < (SizeX - 1) && z > 0 && z < (SizeZ - 1)) { //middle of the map node.AddNeighbor(_nodes[x - 1, z]); node.AddNeighbor(_nodes[x, z - 1]); } else if (x == 0 && z > 0 && z < (SizeZ - 1)) { //left border node.AddNeighbor(_nodes[x, z - 1]); } else if (x == (SizeX - 1) && z > 0 && z < (SizeZ - 1)) { //right border node.AddNeighbor(_nodes[x, z - 1]); node.AddNeighbor(_nodes[x - 1, z]); } else if (z == 0 && x > 0 && x < (SizeX - 1)) { //bottom border node.AddNeighbor(_nodes[x - 1, z]); } else if (z == (SizeZ - 1) && x > 0 && x < (SizeX - 1)) { //top border node.AddNeighbor(_nodes[x, z - 1]); node.AddNeighbor(_nodes[x - 1, z]); } else if (x == 0 && z == 0) { //BL corner } else if (x == (SizeX - 1) && z == (SizeZ - 1)) { //TR corner node.AddNeighbor(_nodes[x - 1, z]); node.AddNeighbor(_nodes[x, z - 1]); } else if (x == (SizeX - 1) && z == 0) { //BR corner node.AddNeighbor(_nodes[x - 1, z]); } else if (x == 0 && z == (SizeZ - 1)) { //TL corner node.AddNeighbor(_nodes[x, z - 1]); } }
/** * For each map node, assign neighbors that are diagonal to it and horiz/vert to it, allowing npc's the ability to move * up, down, or diagonal to reach their goal * */ private void MakeNodeNeighbors() { for (int x_idx = 0; x_idx < sizeX; x_idx++) { for (int y_idx = 0; y_idx < sizeY; y_idx++) { MapNode node = mapNodes[x_idx, y_idx]; if (x_idx != 0) { node.AddNeighbor(mapNodes[x_idx - 1, y_idx]); } if (x_idx != sizeX - 1) { node.AddNeighbor(mapNodes[x_idx + 1, y_idx]); } if (y_idx != 0) { node.AddNeighbor(mapNodes[x_idx, y_idx - 1]); } if (y_idx != sizeY - 1) { node.AddNeighbor(mapNodes[x_idx, y_idx + 1]); } if (y_idx > 1) { node.AddNeighbor(mapNodes[x_idx, y_idx - 2]); } if (y_idx < sizeY - 2) { node.AddNeighbor(mapNodes[x_idx, y_idx + 2]); } //Because every other row is offset, depending on offset neighbor is going //to be reversed if (y_idx % 2 == 0) { if (x_idx != 0 && y_idx != 0) { node.AddNeighbor(mapNodes[x_idx - 1, y_idx - 1]); } if (x_idx != 0 && y_idx != sizeY - 1) { node.AddNeighbor(mapNodes[x_idx - 1, y_idx + 1]); } } else { //y_idx should never be zero but just in case if (x_idx != sizeX - 1 && y_idx != 0) { node.AddNeighbor(mapNodes[x_idx + 1, y_idx - 1]); } if (x_idx != sizeX - 1 && y_idx != sizeY - 1) { node.AddNeighbor(mapNodes[x_idx + 1, y_idx + 1]); } } mapNodes[x_idx, y_idx] = node; } } }
public void Join(Edge edge, MapNode neighbor) { this.AddNeighbor(edge, neighbor); neighbor.AddNeighbor(edge.Opposite(), this); }