void LateUpdate() { if (Global.Instance.wayFinding == true) { foreach (Transform child in parent.transform) { Destroy(child.gameObject); } foreach (AStar.Node n in Global.Instance.grid.path) { // print(n.position.X + ", " + n.position.Y); GameObject newWaypoint = Instantiate(waypoint, new Vector3(n.position.X, 0, n.position.Y), Quaternion.identity); newWaypoint.transform.parent = parent.transform; } ; AStar.Node goalNode = Global.Instance.grid.path.Last(); GameObject newGoal = Instantiate(goal, new Vector3(goalNode.position.X, 0, goalNode.position.Y), Quaternion.identity); newGoal.transform.parent = parent.transform; print("Waypoints Instantiated"); } }
public static Dictionary <RegionWithEquipment, AStar.Node> CreateNodes(Dictionary <Point, GeographicalRegion> geographicalRegions, RegionWithEquipment target) { var nodeId = 1; var equipmentSet = Enum.GetValues(typeof(Equipment)); var nodeMap = new Dictionary <RegionWithEquipment, AStar.Node>(); foreach (var geographicalRegion in geographicalRegions) { foreach (Equipment equipment in equipmentSet) { var regionWithEquipment = new RegionWithEquipment(geographicalRegion.Value, equipment); var node = new AStar.Node(nodeId++, regionWithEquipment.Location.ManhattanDistance(target.Location), regionWithEquipment.ToString()); nodeMap.Add(regionWithEquipment, node); } } foreach (var regionNode in nodeMap) { var regionWithEquipment = regionNode.Key; var node = regionNode.Value; var neighbors = regionWithEquipment.Neighbors(geographicalRegions); node.Neighbors = neighbors .Select(n => new AStar.Neighbor(nodeMap[n], regionWithEquipment.DistanceTo(n))) .ToList(); } return(nodeMap); }
/// <summary> /// ルートを変更して移動する関数 /// </summary> void rootChangeMove() { if (state != State.ROOT_CHANGE) { return; } int x = (int)retCell().x; int y = (int)retCell().y; if (astarstate == AstarState.SEARCH) { while (astar_trycount < 5000) { nodemanager.removeOpenNode(node); nodemanager.openAround(node); node = nodemanager.searchMinScoreNodeFromOpenNodeList(); if (node == null) { break; } if (node.x == goalpos.x && node.y == goalpos.y) { nodemanager.removeOpenNode(node); node.getRootList(rootlist); rootlist.Reverse(); astarstate = AstarState.WORK_START; astar_trycount = 0; break; } astar_trycount++; } } else if (astarstate == AstarState.WORK_START) { if (astarmovecount >= rootlist.Count - 1) { astarmovecount = 0; state = State.ROOT_NORMALMOVE; return; } astarmovecount++; nextmovecell = new Vector2(rootlist[astarmovecount].x, rootlist[astarmovecount].y); directionChange(x, y, (int)nextmovecell.x, (int)nextmovecell.y); astarstate = AstarState.WORK; } else if (astarstate == AstarState.WORK) { float s = up_speed; directionMove(s); move_value += s; if (move_value >= mapchip.chip_size) { adjustPosition(x, y); astarstate = AstarState.WORK_START; move_value = 0; } } }
static void Main() { int[,] gridMatrix = new int[, ] { { 1, 1, 1, 0 }, { 1, 0, 1, 0 }, { 1, 1, 0, 1 }, { 0, 0, 0, 1 }, { 0, 0, 1, 1 }, }; //Starting node at row 0, column 0 AStar.Node start = new AStar.Node(0, 0); //Goal node at row 4, column 2 AStar.Node goal = new AStar.Node(4, 2); List <AStar.Node> path = AStar.FindPath(start, goal, gridMatrix); if (path != null) { Console.WriteLine("This is the path from " + start + " to " + goal); foreach (AStar.Node p in path) { Console.WriteLine(p); } } else { Console.WriteLine("There is no path from " + start + " to " + goal); } Console.Read(); }
void Start() { //change flags for the gameObject they represent toRespaw.ForEach(x => { var __flagOfThisColorInScreen = map.GetTileObjectsByImageName(GetFlagName(x.flag)); while (__flagOfThisColorInScreen.Count > 0) { var __flagObj = __flagOfThisColorInScreen[0]; __flagOfThisColorInScreen.Remove(__flagObj); Vector3 __positionToSpawn = __flagObj.transform.position; Destroy(__flagObj.gameObject); GameObject.Instantiate(x.respaw, __positionToSpawn, x.respaw.transform.rotation); } }); //get pathable nodes List <TileObject> __iaWalkableTiles = map.GetIaWalkableTiles(); foreach (TileObject tile in __iaWalkableTiles) { AStar.Node __node = new AStar.Node(); __node.position = new Vector2(tile.transform.position.x, tile.transform.position.z); __node.nodeTransform = tile.transform; _pathableNodes.Add(__node); } //set up neighbours foreach (var node in _pathableNodes) { foreach (var possibleNeighbour in _pathableNodes) { if (possibleNeighbour == node) { continue; } //check to see if the possible neightbour is indeed a neighbour if ((possibleNeighbour.position.x + 1 == node.position.x || possibleNeighbour.position.x - 1 == node.position.x || possibleNeighbour.position.x == node.position.x) && (possibleNeighbour.position.y + 1 == node.position.y || possibleNeighbour.position.y - 1 == node.position.y || possibleNeighbour.position.y == node.position.y)) { node.neighbours.Add(possibleNeighbour); } } } if (onSpawningFinished != null) { onSpawningFinished(); } }
/// <summary> /// A*準備 /// </summary> void astarSetup() { rootRandomDecide(); astarstate = AstarState.SEARCH; int x = (int)retCell().x; int y = (int)retCell().y; var startpos = new AStar.RootPosition(x, y); Vector2 tempgoalpos; tempgoalpos = getGoalCell(); goalpos = new AStar.RootPosition((int)tempgoalpos.x, (int)tempgoalpos.y); nodemanager = new AStar.NodeManager(goalpos.x, goalpos.y, mapchip); node = nodemanager.openNode(startpos.x, startpos.y, 0, null); nodemanager.addOpenNode(node); rootlist = new List <AStar.RootPosition>(); astar_trycount = 0; astarmovecount = 0; }