public IEnumerator DisplayWalkPath() { //change layer an update graphs so the current space does not count this.gameObject.layer = LayerMask.NameToLayer("SelectedUnit"); AstarPath.active.UpdateGraphs(new GraphUpdateObject(this.GetComponent <BoxCollider2D>().bounds)); // find path ConstantPath constPath = ConstantPath.Construct(this.transform.position, (this.Movement.GetDistance() + 1) * 1000, null); this.WalkSeeker.StartPath(constPath); yield return(StartCoroutine(constPath.WaitForPath())); // display tiles List <Vector3> walkTilePositions = new List <Vector3>(); List <GraphNode> nodes = constPath.allNodes; for (int i = nodes.Count - 1; i >= 0; i--) { GraphNode node = nodes[i]; Vector3 position = (Vector3)node.position; position.x = Mathf.Round(position.x); position.y = Mathf.Round(position.y); position.z = 0; this.walkTiles.Add(position, Instantiate(this.walkTile, position, Quaternion.identity, this.transform)); } StartCoroutine(this.DisplayAttackPath()); }
public IEnumerator DisplayAttackPath() { foreach (Vector3 pos in this.walkTiles.Keys.ToArray()) { // find path ConstantPath constPath = ConstantPath.Construct(pos, (this.Attack.GetRange() + 1) * 1000, null); this.AttackSeeker.StartPath(constPath); yield return(StartCoroutine(constPath.WaitForPath())); // display tiles List <GraphNode> nodes = constPath.allNodes; for (int i = nodes.Count - 1; i >= 0; i--) { GraphNode node = nodes[i]; Vector3 position = (Vector3)node.position; position.x = Mathf.Round(position.x); position.y = Mathf.Round(position.y); position.z = 0; if (!this.walkTiles.ContainsKey(position) && !this.attackTiles.ContainsKey(position)) { this.attackTiles.Add(position, Instantiate(this.attackTile, position, Quaternion.identity, this.transform)); } } } }
/// <summary> /// This method generates all the nodes available for the pathfinding (no visual overlays). /// </summary> private void GeneratePossibleMoves() { GraphNodes.Clear(); ConstantPath path = ConstantPath.Construct(selected.transform.position, selected.unitData.stats.Ap * 750); path.traversalProvider = selected.traversalProvider; AstarPath.StartPath(path); path.BlockUntilCalculated(); foreach (GraphNode node in path.allNodes) { if (node != path.startNode) { GameObject go = Instantiate(nodePrefab, (Vector3)node.position, Quaternion.identity); possibleMoves.Add(go); go.GetComponent <AStarNode>().node = node; node.position.z = 0; GraphNodes.Add((Vector3)node.position); } } }
void GeneratePossibleMoves(TurnBasedAI unit) { var path = ConstantPath.Construct(unit.transform.position, unit.movementPoints * 1000 + 1); path.traversalProvider = unit.traversalProvider; // Schedule the path for calculation AstarPath.StartPath(path); // Force the path request to complete immediately // This assumes the graph is small enough that // this will not cause any lag path.BlockUntilCalculated(); foreach (var node in path.allNodes) { if (node != path.startNode) { // Create a new node prefab to indicate a node that can be reached // NOTE: If you are going to use this in a real game, you might want to // use an object pool to avoid instantiating new GameObjects all the time var go = GameObject.Instantiate(nodePrefab, (Vector3)node.position, Quaternion.identity) as GameObject; possibleMoves.Add(go); go.GetComponent <Astar3DButton>().node = node; } } }
public IEnumerator CalculateConstantPath() { ConstantPath constPath = ConstantPath.Construct(end.position, searchLength, OnPathComplete); AstarPath.StartPath(constPath); lastPath = constPath; yield return(constPath.WaitForPath()); }
public IEnumerator CalculateConstantPath() { ConstantPath constPath = ConstantPath.Construct(this.end.position, this.searchLength, new OnPathDelegate(this.OnPathComplete)); AstarPath.StartPath(constPath, false); this.lastPath = constPath; yield return constPath.WaitForPath(); yield break; }
/** Starts a path specified by PathTypesDemo.activeDemo */ public void DemoPath() { Path p = null; if (activeDemo == 0) { p = ABPath.Construct(start.position, end.position, OnPathComplete); } else if (activeDemo == 1) { MultiTargetPath mp = MultiTargetPath.Construct(multipoints.ToArray(), end.position, null, OnPathComplete); p = mp; } else if (activeDemo == 2) { RandomPath rp = RandomPath.Construct(start.position, searchLength, OnPathComplete); rp.spread = spread; rp.aimStrength = aimStrength; rp.aim = end.position; rp.replaceChance = replaceChance; p = rp; } else if (activeDemo == 3) { FleePath fp = FleePath.Construct(start.position, end.position, searchLength, OnPathComplete); fp.aimStrength = aimStrength; fp.replaceChance = replaceChance; fp.spread = spread; p = fp; } else if (activeDemo == 4) { ConstantPath constPath = ConstantPath.Construct(end.position, searchLength, OnPathComplete); p = constPath; } else if (activeDemo == 5) { FloodPath fp = FloodPath.Construct(end.position, null); lastFlood = fp; p = fp; } else if (activeDemo == 6 && lastFlood != null) { FloodPathTracer fp = FloodPathTracer.Construct(end.position, lastFlood, OnPathComplete); p = fp; } if (p != null) { AstarPath.StartPath(p); lastPath = p; } }
public IEnumerator Constant() { ConstantPath constPath = ConstantPath.Construct(CoverTarget.transform.position, 5 * 3000, OnConstantPathComplete); AstarPath.StartPath(constPath); yield return(constPath.WaitForPath()); Debug.Log(constPath.pathID + " " + constPath.allNodes.Count); }
public IEnumerator Constant() { mAstarPath.astarData.gridGraph.GetNearest(transform.position).node.Walkable = true; ConstantPath constPath = ConstantPath.Construct((transform.position - new Vector3(0, 1, 0)), (MoveSpeed / 3) * 3000, OnConstantPathComplete); AstarPath.StartPath(constPath); yield return(constPath.WaitForPath()); Debug.Log(constPath.pathID + " " + constPath.allNodes.Count); }
public IEnumerator Constant() { ConstantPath constPath = ConstantPath.Construct(end.position, searchLength, OnPathComplete); AstarPath.StartPath(constPath); lastPath = constPath; yield return(constPath.WaitForPath()); Debug.Log(constPath.pathID + " " + constPath.allNodes.Count); }
public static Vector3 RandomPointConstantPath(Vector3 origin, int searchLength) { ConstantPath path = ConstantPath.Construct(origin, searchLength); AstarPath.StartPath(path); path.BlockUntilCalculated(); var randomPoint = PathUtilities.GetPointsOnNodes(path.allNodes, 1)[0]; return(randomPoint); }
//计算常量路径 public ConstantPath StartConstantPath(Vector3 start, int maxGScore, ITraversalProvider traversalProvider, OnPathDelegate callback) { var path = ConstantPath.Construct(start, maxGScore + 1, callback); path.traversalProvider = traversalProvider; AstarPath.StartPath(path); if (callback != null) { } else { path.BlockUntilCalculated(); } return(path); }
// Token: 0x06002A30 RID: 10800 RVA: 0x001C7418 File Offset: 0x001C5618 private void GeneratePossibleMoves(TurnBasedAI unit) { ConstantPath constantPath = ConstantPath.Construct(unit.transform.position, unit.movementPoints * 1000 + 1, null); constantPath.traversalProvider = unit.traversalProvider; AstarPath.StartPath(constantPath, false); constantPath.BlockUntilCalculated(); foreach (GraphNode graphNode in constantPath.allNodes) { if (graphNode != constantPath.startNode) { GameObject gameObject = UnityEngine.Object.Instantiate <GameObject>(this.nodePrefab, (Vector3)graphNode.position, Quaternion.identity); this.possibleMoves.Add(gameObject); gameObject.GetComponent <Astar3DButton>().node = graphNode; } } }
public void GeneratePossibleMoves(Unit.Unit unit) { ConstantPath path = null; GameObject nodePrefab = null; if (UnitStateManager.currentState == State.SelectAttackTarget) { path = ConstantPath.Construct(unit.transform.position, unit.AttackRangePoints * 1000 + 1); nodePrefab = nodePrefabs[1]; } else if (UnitStateManager.currentState == State.SelectMoveTarget) { path = ConstantPath.Construct(unit.transform.position, unit.MovementPoints * 1000 + 1); nodePrefab = nodePrefabs[0]; path.traversalProvider = unit.TraversalProvider; } // Schedule the path for calculation AstarPath.StartPath(path); // Force the path request to complete immediately // This assumes the graph is small enough that // this will not cause any lag if (path == null) { return; } path.BlockUntilCalculated(); foreach (var node in path.allNodes) { if (node != path.startNode) { // Create a new node prefab to indicate a node that can be reached // NOTE: If you are going to use this in a real game, you might want to // use an object pool to avoid instantiating new GameObjects all the time var go = Instantiate(nodePrefab, (Vector3)node.position, Quaternion.identity); possibleMoves.Add(go); go.GetComponent <MoveReservable>().Node = node; } } OnPathHasBeenGenerated?.Invoke(true); }
//计算常量路径 public ConstantPath StartConstantPath(Vector3 start, int movePoint, OnPathDelegate callback, ITraversalProvider traversalProvider = null) { var path = ConstantPath.Construct(start, Mathf.Clamp(movePoint, 1, 1000) * 1000, callback); path.traversalProvider = CommonTraversalProvider; if (traversalProvider != null) { path.traversalProvider = traversalProvider; } AstarPath.StartPath(path); if (callback != null) { } else { path.BlockUntilCalculated(); } return(path); }
// Token: 0x060029EE RID: 10734 RVA: 0x001C234B File Offset: 0x001C054B public IEnumerator DemoConstantPath() { ConstantPath constPath = ConstantPath.Construct(this.end.position, this.searchLength, null); AstarPath.StartPath(constPath, false); this.lastPath = constPath; yield return(base.StartCoroutine(constPath.WaitForPath())); this.ClearPrevious(); List <GraphNode> allNodes = constPath.allNodes; Mesh mesh = new Mesh(); List <Vector3> list = new List <Vector3>(); bool flag = false; for (int i = allNodes.Count - 1; i >= 0; i--) { Vector3 a = (Vector3)allNodes[i].position + this.pathOffset; if (list.Count == 65000 && !flag) { Debug.LogError("Too many nodes, rendering a mesh would throw 65K vertex error. Using Debug.DrawRay instead for the rest of the nodes"); flag = true; } if (flag) { Debug.DrawRay(a, Vector3.up, Color.blue); } else { GridGraph gridGraph = AstarData.GetGraph(allNodes[i]) as GridGraph; float d = 1f; if (gridGraph != null) { d = gridGraph.nodeSize; } list.Add(a + new Vector3(-0.5f, 0f, -0.5f) * d); list.Add(a + new Vector3(0.5f, 0f, -0.5f) * d); list.Add(a + new Vector3(-0.5f, 0f, 0.5f) * d); list.Add(a + new Vector3(0.5f, 0f, 0.5f) * d); } } Vector3[] array = list.ToArray(); int[] array2 = new int[3 * array.Length / 2]; int j = 0; int num = 0; while (j < array.Length) { array2[num] = j; array2[num + 1] = j + 1; array2[num + 2] = j + 2; array2[num + 3] = j + 1; array2[num + 4] = j + 3; array2[num + 5] = j + 2; num += 6; j += 4; } Vector2[] array3 = new Vector2[array.Length]; for (int k = 0; k < array3.Length; k += 4) { array3[k] = new Vector2(0f, 0f); array3[k + 1] = new Vector2(1f, 0f); array3[k + 2] = new Vector2(0f, 1f); array3[k + 3] = new Vector2(1f, 1f); } mesh.vertices = array; mesh.triangles = array2; mesh.uv = array3; mesh.RecalculateNormals(); GameObject gameObject = new GameObject("Mesh", new Type[] { typeof(MeshRenderer), typeof(MeshFilter) }); gameObject.GetComponent <MeshFilter>().mesh = mesh; gameObject.GetComponent <MeshRenderer>().material = this.squareMat; this.lastRender.Add(gameObject); yield break; }
public IEnumerator DemoConstantPath() { ConstantPath constPath = ConstantPath.Construct(end.position, searchLength, null); AstarPath.StartPath(constPath); lastPath = constPath; // Wait for the path to be calculated yield return(StartCoroutine(constPath.WaitForPath())); ClearPrevious(); // The following code will build a mesh with a square for each node visited List <GraphNode> nodes = constPath.allNodes; Mesh mesh = new Mesh(); List <Vector3> verts = new List <Vector3>(); bool drawRaysInstead = false; // This will loop through the nodes from furthest away to nearest, not really necessary... but why not :D for (int i = nodes.Count - 1; i >= 0; i--) { Vector3 pos = (Vector3)nodes[i].position + pathOffset; if (verts.Count == 65000 && !drawRaysInstead) { Debug.LogError("Too many nodes, rendering a mesh would throw 65K vertex error. Using Debug.DrawRay instead for the rest of the nodes"); drawRaysInstead = true; } if (drawRaysInstead) { Debug.DrawRay(pos, Vector3.up, Color.blue); continue; } // Add vertices in a square GridGraph gg = AstarData.GetGraph(nodes[i]) as GridGraph; float scale = 1F; if (gg != null) { scale = gg.nodeSize; } verts.Add(pos + new Vector3(-0.5F, 0, -0.5F) * scale); verts.Add(pos + new Vector3(0.5F, 0, -0.5F) * scale); verts.Add(pos + new Vector3(-0.5F, 0, 0.5F) * scale); verts.Add(pos + new Vector3(0.5F, 0, 0.5F) * scale); } // Build triangles for the squares Vector3[] vs = verts.ToArray(); int[] tris = new int[(3 * vs.Length) / 2]; for (int i = 0, j = 0; i < vs.Length; j += 6, i += 4) { tris[j + 0] = i; tris[j + 1] = i + 1; tris[j + 2] = i + 2; tris[j + 3] = i + 1; tris[j + 4] = i + 3; tris[j + 5] = i + 2; } Vector2[] uv = new Vector2[vs.Length]; // Set up some basic UV for (int i = 0; i < uv.Length; i += 4) { uv[i] = new Vector2(0, 0); uv[i + 1] = new Vector2(1, 0); uv[i + 2] = new Vector2(0, 1); uv[i + 3] = new Vector2(1, 1); } mesh.vertices = vs; mesh.triangles = tris; mesh.uv = uv; mesh.RecalculateNormals(); GameObject go = new GameObject("Mesh", typeof(MeshRenderer), typeof(MeshFilter)); MeshFilter fi = go.GetComponent <MeshFilter>(); fi.mesh = mesh; MeshRenderer re = go.GetComponent <MeshRenderer>(); re.material = squareMat; lastRender.Add(go); }