IEnumerator MoveToNode(TurnBasedAI unit, GraphNode node) { var path = ABPath.Construct(unit.transform.position, (Vector3)node.position); path.traversalProvider = unit.traversalProvider; // Schedule the path for calculation AstarPath.StartPath(path); // Wait for the path calculation to complete yield return(StartCoroutine(path.WaitForPath())); if (path.error) { // Not obvious what to do here, but show the possible moves again // and let the player choose another target node // Likely a node was blocked between the possible moves being // generated and the player choosing which node to move to Debug.LogError("Path failed:\n" + path.errorLog); state = State.SelectTarget; GeneratePossibleMoves(selected); yield break; } // Set the target node so other scripts know which // node is the end point in the path unit.targetNode = path.path[path.path.Count - 1]; yield return(StartCoroutine(MoveAlongPath(unit, path, movementSpeed))); unit.blocker.BlockAtCurrentPosition(); // Select a new unit to move state = State.SelectUnit; }
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; } } }
// Token: 0x06002A2E RID: 10798 RVA: 0x001C739F File Offset: 0x001C559F private static IEnumerator MoveAlongPath(TurnBasedAI unit, ABPath path, float speed) { if (path.error || path.vectorPath.Count == 0) { throw new ArgumentException("Cannot follow an empty path"); } float distanceAlongSegment = 0f; int num; for (int i = 0; i < path.vectorPath.Count - 1; i = num + 1) { Vector3 p0 = path.vectorPath[Mathf.Max(i - 1, 0)]; Vector3 p = path.vectorPath[i]; Vector3 p2 = path.vectorPath[i + 1]; Vector3 p3 = path.vectorPath[Mathf.Min(i + 2, path.vectorPath.Count - 1)]; float segmentLength = Vector3.Distance(p, p2); while (distanceAlongSegment < segmentLength) { Vector3 position = AstarSplines.CatmullRom(p0, p, p2, p3, distanceAlongSegment / segmentLength); unit.transform.position = position; yield return(null); distanceAlongSegment += Time.deltaTime * speed; } distanceAlongSegment -= segmentLength; p0 = default(Vector3); p = default(Vector3); p2 = default(Vector3); p3 = default(Vector3); num = i; } unit.transform.position = path.vectorPath[path.vectorPath.Count - 1]; yield break; }
/// <summary>Interpolates the unit along the path</summary> static IEnumerator MoveAlongPath(TurnBasedAI unit, ABPath path, float speed) { if (path.error || path.vectorPath.Count == 0) { throw new System.ArgumentException("Cannot follow an empty path"); } // Very simple movement, just interpolate using a catmull rom spline float distanceAlongSegment = 0; for (int i = 0; i < path.vectorPath.Count - 1; i++) { var p0 = path.vectorPath[Mathf.Max(i - 1, 0)]; // Start of current segment var p1 = path.vectorPath[i]; // End of current segment var p2 = path.vectorPath[i + 1]; var p3 = path.vectorPath[Mathf.Min(i + 2, path.vectorPath.Count - 1)]; var segmentLength = Vector3.Distance(p1, p2); while (distanceAlongSegment < segmentLength) { var interpolatedPoint = AstarSplines.CatmullRom(p0, p1, p2, p3, distanceAlongSegment / segmentLength); unit.transform.position = interpolatedPoint; yield return(null); distanceAlongSegment += Time.deltaTime * speed; } distanceAlongSegment -= segmentLength; } unit.transform.position = path.vectorPath[path.vectorPath.Count - 1]; }
// Token: 0x06002A1E RID: 10782 RVA: 0x001C7108 File Offset: 0x001C5308 private void OnTriggerEnter(Collider coll) { TurnBasedAI componentInParent = coll.GetComponentInParent <TurnBasedAI>(); GraphNode node = AstarPath.active.GetNearest(base.transform.position).node; if (componentInParent != null && componentInParent.targetNode == node) { this.button.interactable = true; this.visible = true; this.anim.CrossFade("show", 0.1f); } }
// 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; } } }
// Token: 0x06002A2D RID: 10797 RVA: 0x001C7382 File Offset: 0x001C5582 private IEnumerator MoveToNode(TurnBasedAI unit, GraphNode node) { ABPath path = ABPath.Construct(unit.transform.position, (Vector3)node.position, null); path.traversalProvider = unit.traversalProvider; AstarPath.StartPath(path, false); yield return(base.StartCoroutine(path.WaitForPath())); if (path.error) { Debug.LogError("Path failed:\n" + path.errorLog); this.state = TurnBasedManager.State.SelectTarget; this.GeneratePossibleMoves(this.selected); yield break; } unit.targetNode = path.path[path.path.Count - 1]; yield return(base.StartCoroutine(TurnBasedManager.MoveAlongPath(unit, path, this.movementSpeed))); unit.blocker.BlockAtCurrentPosition(); this.state = TurnBasedManager.State.SelectUnit; yield break; }
// Token: 0x06002A29 RID: 10793 RVA: 0x001C7258 File Offset: 0x001C5458 private void Update() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (this.eventSystem.IsPointerOverGameObject()) { return; } if (this.state == TurnBasedManager.State.SelectTarget) { this.HandleButtonUnderRay(ray); } if ((this.state == TurnBasedManager.State.SelectUnit || this.state == TurnBasedManager.State.SelectTarget) && Input.GetKeyDown(KeyCode.Mouse0)) { TurnBasedAI byRay = this.GetByRay <TurnBasedAI>(ray); if (byRay != null) { this.Select(byRay); this.DestroyPossibleMoves(); this.GeneratePossibleMoves(this.selected); this.state = TurnBasedManager.State.SelectTarget; } } }
void Select(TurnBasedAI unit) { selected = unit; }
// Token: 0x06002A2C RID: 10796 RVA: 0x001C7379 File Offset: 0x001C5579 private void Select(TurnBasedAI unit) { this.selected = unit; }