private IEnumerator InitRangeNodes() { yield return(null); // wait for grid initialization rangeNodes = new List <Node>(); List <Node> helperList = new List <Node>(); List <Node> containerList = new List <Node>(); rangeNodes.Add(currentNode); helperList.Add(currentNode); for (int i = 0; i < itemData.RangeToInteract; i++) { foreach (var nodeMain in helperList) { foreach (var nodeNeighbour in grid.GetNeighbours(nodeMain)) { if (!rangeNodes.Contains(nodeNeighbour)) { containerList.Add(nodeNeighbour); } } } helperList.Clear(); helperList = new List <Node>(containerList); rangeNodes.AddRange(containerList); containerList.Clear(); } }
// https://github.com/davecusatis/A-Star-Sharp/blob/master/Astar.cs - FindPath method public List <Node> FindPath(Node start, Node target) { this.pathDebug = null; Stack <Node> path = new Stack <Node>(); List <Node> openList = new List <Node>(); List <Node> closedList = new List <Node>(); List <Node> adjacencies; Node current = start; // add start node to Open List openList.Add(start); while (openList.Count != 0 && !closedList.Exists(x => x.transform.position == target.transform.position)) { current = openList[0]; openList.Remove(current); closedList.Add(current); adjacencies = graph.GetNeighbours(current); foreach (Node n in adjacencies) { if (!closedList.Contains(n) && !n.isOccupied) { if (!openList.Contains(n)) { n.Parent = current; float distanceToTarget = Mathf.Abs(n.transform.position.x - target.transform.position.x) + Mathf.Abs(n.transform.position.z - target.transform.position.z); openList.Add(n); openList = openList.OrderBy(node => distanceToTarget).ToList <Node>(); } } } } // construct path, if end was not closed return null if (!closedList.Exists(x => x.transform.position == target.transform.position)) { return(null); } // if all good, return path Node temp = closedList[closedList.IndexOf(current)]; if (temp == null) { return(null); } do { path.Push(temp); temp = temp.Parent; } while (temp != start && temp != null); this.pathDebug = path.ToList(); return(path.ToList()); }
private void InitGhosts() { Debug.Log(currentNode.transform.eulerAngles.y); List <Node> nodeNeighbours = grid.GetNeighbours(currentNode.GetComponent <Node>()); List <bool> isNodeExists = Enumerable.Repeat(false, 6).ToList(); // Depending on clockwise direction find sides with hex float originPositionX = currentNode.transform.position.x; float originPositionZ = currentNode.transform.position.z; if ((int)Mathf.Abs(currentNode.transform.eulerAngles.y) % 60 != 0) { float tmpVal = originPositionX; originPositionX = originPositionZ; originPositionZ = tmpVal; } foreach (Node neighbourNode in nodeNeighbours) { float targetPositionX = neighbourNode.transform.position.x; float targetPositionZ = neighbourNode.transform.position.z; if ((int)Mathf.Abs(neighbourNode.transform.eulerAngles.y) % 60 != 0) { float tmpVal = targetPositionX; targetPositionX = targetPositionZ; targetPositionZ = tmpVal; } // TODO rework to model width istead of strict values (because sometimes it doesn't see exist nodes) if (targetPositionX - originPositionX == 0.0f && targetPositionZ - originPositionZ == -1.0f) { isNodeExists[0] = true; } else if (targetPositionX - originPositionX <= -0.8f && targetPositionZ - originPositionZ == -0.5f) { isNodeExists[1] = true; } else if (targetPositionX - originPositionX <= -0.8f && targetPositionZ - originPositionZ == 0.5f) { isNodeExists[2] = true; } else if (targetPositionX - originPositionX == 0.0f && targetPositionZ - originPositionZ == 1.0f) { isNodeExists[3] = true; } else if (targetPositionX - originPositionX >= 0.8f && targetPositionZ - originPositionZ == 0.5f) { isNodeExists[4] = true; } else if (targetPositionX - originPositionX >= 0.8f && targetPositionZ - originPositionZ == -0.5f) { isNodeExists[5] = true; } } // Highlight (green hex ghost) sides without hex for (int i = 0; i < isNodeExists.Count; i++) { if (!isNodeExists[i]) { Transform parent = currentNode.transform.parent; Vector3 position = currentNode.transform.position; float offsetZ = 0f; float offsetX = 0f; switch (i) { case (0): offsetZ -= 1.0f; break; case (1): offsetX -= 0.866f; offsetZ -= 0.5f; break; case (2): offsetX -= 0.866f; offsetZ += 0.5f; break; case (3): offsetZ += 1.0f; break; case (4): offsetX += 0.866f; offsetZ += 0.5f; break; case (5): offsetX += 0.866f; offsetZ -= 0.5f; break; } if ((int)Mathf.Abs(currentNode.transform.eulerAngles.y) % 60 != 0) { float tmpVal = offsetX; offsetX = offsetZ; offsetZ = tmpVal; } position.x += offsetX; position.z += offsetZ; GameObject newNode = Instantiate(hexGhostPrefab, position, currentNode.transform.rotation); newNode.GetComponent <NodeGhost>().ParentForNode = parent; hexGhosts.Add(newNode); } } }