void _generateGrid() // and here is where the dragon lies { _lineRayEnds = new List <Vector3> (); _lineRayStarts = new List <Vector3> (); Vector2 size = new Vector2(Mathf.Abs(startCoordinate.x - endCoordinate.x), Mathf.Abs(startCoordinate.y - endCoordinate.y)); Debug.Log("Size: " + size); gridNodes = new qpNode[(int)Mathf.Ceil(size.x / spread) + 1 + NodeConnectionDepth, (int)Mathf.Ceil(size.y / spread) + 1 + NodeConnectionDepth]; Debug.Log("gridNodes size: " + gridNodes.GetLength(0) + ":" + gridNodes.GetLength(1)); Debug.Log("For loop bound: " + (size.x / spread) * (size.y / spread)); for (int i = 0; i < (size.x / spread) * (size.y / spread); i++) { int row = (int)Mathf.Floor((i * spread) / size.x); float x = ((i * spread) - (row * size.x)) + startCoordinate.x; float y = (row * spread) + startCoordinate.y; Vector3 rayCastPositionStart = Vector3.zero; Vector3 rayDirection = Vector3.zero; if (UpDirection == Axis.Z) { rayCastPositionStart = new Vector3(x, y, UpRaycastStart); rayDirection = new Vector3(0, 0, -1f); // axis direction! } else if (UpDirection == Axis.Y) { rayCastPositionStart = new Vector3(x, UpRaycastStart, y); rayDirection = new Vector3(0, -1f); } qpGridNode gridNode = null; RaycastHit[] hits; bool placeNode = true; Vector3 point = Vector3.zero; hits = Physics.RaycastAll(rayCastPositionStart, rayDirection, 100.0F); int o = 0; int countIgnoreHits = 0; if (hits.Length == 0) { placeNode = false; } while (o < hits.Length) { RaycastHit hit = hits [o]; if (DisallowedTags.Contains(hit.collider.gameObject.tag)) { placeNode = false; } else if (IgnoreTags.Contains(hit.collider.gameObject.tag)) { countIgnoreHits++; } else if (point == Vector3.zero) { point = hit.point; } else // судя по всему, тут мы ищем верхний объект, в который попали { if (UpDirection == Axis.Y) { if (hit.point.y > point.y) { point = hit.point; } } else if (UpDirection == Axis.Z) { if (hit.point.z > point.z) { point = hit.point; } } } o++; } if (o == countIgnoreHits) { placeNode = false; } if (placeNode) { //Collision detected on floor // ...on walkable surface?.. gridNode = new qpGridNode(point); allNodes.Add(gridNode); if (UpDirection == Axis.Z) // кажется, чисто косметическая хрень { rayCastPositionStart.z = point.z + 1; } else if (UpDirection == Axis.Y) { rayCastPositionStart.y = point.y + 1; } if (point != Vector3.zero) { _lineRayEnds.Add(point); _lineRayStarts.Add(rayCastPositionStart); } } int yindex = (int)Mathf.Floor(i / (size.x / spread)); // чувак ловко итерирует двумерный массив плоским образом о_О int xindex = (int)(i - (Mathf.Ceil(yindex * (size.x / spread)))); gridNodes [xindex, yindex] = gridNode; //Set connection for gridNode if (gridNode != null) { Dictionary <qpNode, bool> isDiagonalsByCandidateNodes = new Dictionary <qpNode, bool> (); bool isDiagonal = false; for (int c = 1; c < (NodeConnectionDepth + 1); c++) { if ((xindex - c) > 0) { //left if (gridNodes [xindex - c, yindex] != null) { isDiagonalsByCandidateNodes.Add(gridNodes [xindex - c, yindex], false); } if ((yindex - c) > 0) { //top Left for (int nc = c; nc > 0; nc--) // dude, wtf { isDiagonal = true; if (c == 0 || nc == 0) { isDiagonal = false; } if (gridNodes [xindex - c, yindex - nc] != null) { isDiagonalsByCandidateNodes.Add(gridNodes [xindex - c, yindex - nc], isDiagonal); } } } } if ((yindex - c) > 0) { //top if (gridNodes [xindex, yindex - c] != null) { isDiagonalsByCandidateNodes.Add(gridNodes [xindex, yindex - c], false); } for (int nc = c - 1; nc > 0; nc--) { isDiagonal = true; if ((xindex - nc) < 0 || (yindex - c) < 0) { continue; } if (nc == 0 || c == 0) { isDiagonal = false; } if (gridNodes [xindex - nc, yindex - c] != null) { isDiagonalsByCandidateNodes.Add(gridNodes [xindex - nc, yindex - c], isDiagonal); } } //top right if (gridNodes [xindex + c, yindex - c] != null) { isDiagonalsByCandidateNodes.Add(gridNodes [xindex + c, yindex - c], true); } for (int nc = c - 1; nc > 0; nc--) { isDiagonal = true; if ((xindex + nc) == xindex || (yindex - c) == yindex) { isDiagonal = false; } if (gridNodes [xindex + nc, yindex - c] != null) { isDiagonalsByCandidateNodes.Add(gridNodes [xindex + nc, yindex - c], isDiagonal); } } //below top right // wat for (int nc = c - 1; nc > 0; nc--) { isDiagonal = true; if ((xindex + c) == xindex || (yindex - nc) == yindex) { isDiagonal = false; } if (gridNodes [xindex + c, yindex - nc] != null) { isDiagonalsByCandidateNodes.Add(gridNodes [xindex + c, yindex - nc], isDiagonal); } } } foreach (var candidate in isDiagonalsByCandidateNodes) { if (RaycastBetweenNodes) { if (gridNode.CanConnectTo(candidate.Key) && Vector3.Distance(gridNode.Coordinate, candidate.Key.Coordinate) < MaxNodeDistance) { gridNode.SetMutualConnection(candidate.Key, candidate.Value); } } else if (!RaycastBetweenNodes) { if (Vector3.Distance(gridNode.Coordinate, candidate.Key.Coordinate) < MaxNodeDistance) { gridNode.SetMutualConnection(candidate.Key, isDiagonal); // looks like it shouldn't be just "isDiagonal" } } } } } } qpManager.Instance.RegisterNodes(allNodes); }
private void _generateGrid() { _lineRayEnds = new List <Vector3>(); _lineRayStarts = new List <Vector3>(); Vector2 size = new Vector2(Mathf.Abs(startCoordinate.x - endCoordinate.x), Mathf.Abs(startCoordinate.y - endCoordinate.y)); gridNodes = new qpNode[(int)(size.x / spread) + 2, (int)(size.y / spread) + 2]; for (int i = 0; i < (size.x / spread) * (size.y / spread); i++) { int row = (int)Mathf.Floor((i * spread) / size.x); float x = ((i * spread) - (row * size.x)) + startCoordinate.x + 0.5f; float y = (row * spread) + startCoordinate.y + 0.5f; Vector3 rayCastPositionEnd = Vector3.zero; Vector3 rayCastPositionStart = Vector3.zero; Vector3 rayDirection = Vector3.zero; if (UpDirection == Axis.Z) { rayCastPositionStart = new Vector3(x, y, UpRaycastStart); rayCastPositionEnd = new Vector3(x, y, UpRayCastEnd); rayDirection = new Vector3(0, 0, -1f); } else if (UpDirection == Axis.Y) { rayCastPositionStart = new Vector3(x, UpRaycastStart, y); rayCastPositionEnd = new Vector3(x, UpRayCastEnd, y); rayDirection = new Vector3(0, -1f); } qpGridNode gridNode = null; RaycastHit[] hits; bool placeNode = true; Vector3 point = Vector3.zero; hits = Physics.RaycastAll(rayCastPositionStart, rayDirection, 100.0F); int o = 0; int countIgnoreHits = 0; if (hits.Length == 0) { placeNode = false; } while (o < hits.Length) { RaycastHit hit = hits[o]; //Debug.Log(hit.transform.tag); if (DisallowedTags.Contains(hit.transform.tag)) { //Debug.Log("Disallowed!"); placeNode = false; } else if (IgnoreTags.Contains(hit.collider.gameObject.tag)) { countIgnoreHits++; } else { point = hit.point; } o++; } if (o == countIgnoreHits) { placeNode = false; } if (placeNode) { //Collision detected on floor Vector3 vec = point; //vec.z += 0.25f; gridNode = new qpGridNode(vec); allNodes.Add(gridNode); if (UpDirection == Axis.Z) { rayCastPositionStart.z = point.z + 1; } else if (UpDirection == Axis.Y) { rayCastPositionStart.y = point.y + 1; } if (point != Vector3.zero) { _lineRayEnds.Add(point); _lineRayStarts.Add(rayCastPositionStart); } } int yindex = (int)Mathf.Floor(i / (size.x / spread)); int xindex = (int)(i - (Mathf.Ceil(yindex * (size.x / spread)))); //Debug.Log("i:" + i + " spread:" + spread + " dif.x:" + size.x + " dif.y:" + size.y + " row:" + row + " x:" + x + " y:" + y+" xindex:"+xindex+" yindex:"+yindex); gridNodes[xindex, yindex] = gridNode; //Set connection for gridnode if (gridNode != null) { if ((xindex - 1) > 0) { if (gridNodes[xindex - 1, yindex] != null) { gridNode.SetMutualConnection(gridNodes[xindex - 1, yindex]); } if ((yindex - 1) > 0) { if (gridNodes[xindex - 1, yindex - 1] != null) { gridNode.SetMutualConnection(gridNodes[xindex - 1, yindex - 1], true); } } } if ((yindex - 1) > 0) { if (gridNodes[xindex, yindex - 1] != null) { gridNode.SetMutualConnection(gridNodes[xindex, yindex - 1]); } if (gridNodes[xindex + 1, yindex - 1] != null) { gridNode.SetMutualConnection(gridNodes[xindex + 1, yindex - 1], true); } } } } qpManager.Instance.RegisterNodes(allNodes); }
public void RegisterGridpoint(qpGridNode point) { gridpoints.Add(point); }