private int Heuristic(Chimera.ChimeraNode pos0, Chimera.ChimeraNode pos1) { int d1 = pos1.x - pos0.x; int d2 = pos1.y - pos0.y; d1 = d1 < 0 ? -d1 : d1; d2 = d2 < 0 ? -d2 : d2; return(d1 + d2); }
public Chimera.ChimeraNode GetNode(int chX, int chY) { Chimera.ChimeraNode cNode = null; try { cNode = gridNodes[chX][chY]; } catch { Debug.LogError("GetNode => in => (" + chX + ", " + chY + ")"); } return(cNode); }
public Vector3 ChimeraToGlobal(Vector3 chPoint) { Vector3 point = Vector3.zero.error(); try { Chimera.ChimeraNode node = gridNodes[(int)chPoint.x][(int)chPoint.y]; point = new Vector3(node.x, node.y); } catch { Debug.LogError("ChimeraToGlobal => in => (" + chPoint.x + ", " + chPoint.y + ")"); } return(point); }
public Vector3 GlobalToChimera(Vector3 point) { Vector3 chPoint = new Vector3().error(); int g = Mathf.CeilToInt((point.x / 2 + point.y / 1) / (hCell)); int f = Mathf.CeilToInt(-(point.x / 2 - point.y / 1) / (hCell)); int cg = topLeftNode.a - g; int cf = topLeftNode.b - f; try { Chimera.ChimeraNode node = gridNodes[cg][cf]; chPoint = new Vector3(cg, cf); ChimeraWorld.camera.GetComponent <GridRendering>().DrawNode(node); } catch { Debug.LogError("GlobalToChimera => in => (" + point.x + ", " + point.y + ") out => (" + cg + ", " + cf + ")"); } return(chPoint); }
private List <Chimera.ChimeraNode> GetNeighbors(Chimera.ChimeraNode currentNode) { _neighbors.Clear(); int x = currentNode.x; int y = currentNode.y; _widthMax = (int)wGrid - wCell / 2; _heightMax = (int)hGrid - hCell / 2; _widthMin = wCell; _heightMin = (int)hCell / 2; ChimeraNode top = null, bottom = null, left = null, right = null; if (y > _heightMin) { top = GetNode(currentNode.a, currentNode.b - 1); _neighbors.Add(top); } if (y < _heightMax) { bottom = GetNode(currentNode.a, currentNode.b + 1); _neighbors.Add(bottom); } if (x > _widthMin) { left = GetNode(currentNode.a - 1, currentNode.b); _neighbors.Add(left); // try // { // if(!left.isWall) // { // if (y > _heightMin && !top.isWall) // { // _neighbors.Add(GetNode(currentNode.a - 1, currentNode.b - 1)); // } // if (y < _heightMax && !bottom.isWall) // { // _neighbors.Add(GetNode(currentNode.a - 1, currentNode.b + 1)); // } // } // } // catch // { // if (_neighbors[_neighbors.Count - 1] == null) // { // _neighbors.Remove(_neighbors[_neighbors.Count - 1]); // } // } } if (x < _widthMax) { right = GetNode(currentNode.a + 1, currentNode.b); _neighbors.Add(right); // try // { // if (!right.isWall) // { // if (y > _heightMin && !top.isWall) // { // _neighbors.Add (GetNode(currentNode.a + 1, currentNode.b - 1)); // } // if(y < _heightMax && !bottom.isWall) // { // _neighbors.Add(GetNode(currentNode.a + 1, currentNode.b + 1)); // } // } // } // catch // { // if (_neighbors[_neighbors.Count - 1] == null) // { // _neighbors.Remove(_neighbors[_neighbors.Count - 1]); // } // } } return(_neighbors); }
public List <Chimera.ChimeraNode> SearchWay(Chimera.ChimeraNode start, Chimera.ChimeraNode end) { openList.Clear(); closeList.Clear(); if (start.isWall || end.isWall) { return(null); } start.parent = null; start.g = 0; openList.Add(start); while (openList.Count > 0) { Chimera.ChimeraNode currentNode = openList[0]; for (int i = 0; i < openList.Count; i++) { if (openList[i].f < currentNode.f) { currentNode = openList[i]; } } if (currentNode.a == end.a && currentNode.b == end.b) { List <ChimeraNode> result = new List <Chimera.ChimeraNode>(); while (currentNode != null) { result.Add(currentNode); currentNode = currentNode.parent; } result.Reverse(); return(result); } openList.Remove(currentNode); closeList.Add(currentNode); List <Chimera.ChimeraNode> neighbors = GetNeighbors(currentNode); for (int j = 0; j < neighbors.Count; j++) { Chimera.ChimeraNode neighbor = null; try { neighbor = neighbors[j]; } catch { } if (neighbor == null || closeList.IndexOf(neighbor) != -1 || neighbor.isWall) { continue; } int curG = currentNode.g + 1; bool isNew = openList.IndexOf(neighbor) == -1; if (isNew) { neighbor.h = Heuristic(neighbor, end); openList.Add(neighbor); } if (isNew || curG < neighbor.g) { neighbor.parent = currentNode; neighbor.g = curG; neighbor.f = neighbor.g + neighbor.h; } } } return(null); }