private void Set(PathNode node) { openList.Remove(node); closedList.Add(node); PathNode newNode; foreach (Coords node_nb in node.node.neighbours) { Environment.Node neighbour = Environment.instance[node_nb.x, node_nb.y]; if (closedListContains(node_nb) != -1) { continue; } int index = openListContains(node_nb); //add neighbours to open list if (index == -1) { newNode = new PathNode(neighbour, node, targetNode.coords); openList.Add(newNode); if (Equals(neighbour, targetNode)) { foundTarget = true; break; } //check if it is destination } else //repartent if needed { if (openList[index].gValue > node.gValue + node.movementCost(node.node.coords, openList[index].node.coords)) { newNode = new PathNode(openList[index].node, node, targetNode.coords); openList[index] = newNode; } } } }
public PathNode(Environment.Node node, PathNode parent, Coords target) { this.node = node; this.parent = parent; if (parent != null) { this.gValue = movementCost(node.coords, parent.node.coords); } else { gValue = 0; } hValue = Mathf.Abs(target.x - node.coords.x) + Mathf.Abs(target.y - node.coords.y); }
private void show() { foreach (Environment.Node nd in Environment.instance) { GameObject d; try { d = new GameObject("node: " + nd.coords.x + "," + nd.coords.y); } catch (NullReferenceException e) { continue; } d.transform.position = nd.worldLocation; byte x = 0; foreach (Coords nbCoords in nd.neighbours) { Environment.Node nb = Environment.instance[nbCoords.x, nbCoords.y]; var c = new GameObject("nb: " + nb.coords.x + "," + nb.coords.y + " dir: " + nd.direction[x]); c.transform.position = nb.worldLocation; c.transform.SetParent(d.transform); x++; } } }
public Pathfinding(Vector2 from, Vector2 to, MonoBehaviour a) { startNode = Environment.Position2Node(from); targetNode = Environment.Position2Node(to); Start(); }
public PathNode(Environment.Node node, PathNode parent, Coords target) { this.node = node; this.parent = parent; if (parent != null) this.gValue = movementCost(node.coords, parent.node.coords); else gValue = 0; hValue = Mathf.Abs(target.x - node.coords.x)+Mathf.Abs(target.y - node.coords.y); }
private bool Equals(Environment.Node a, Environment.Node b) { return(a.coords.x == b.coords.x && a.coords.y == b.coords.y); }