private void DrawPath(PathFinder.Path path, Vector3 navigator_pos, Color color) { if (path.nodes != null && path.nodes.Count > 1) { GL.PushMatrix(); material.SetPass(0); GL.Begin(1); GL.Color(color); GL.Vertex(navigator_pos); PathFinder.Path.Node node = path.nodes[1]; int cell = node.cell; PathFinder.Path.Node node2 = path.nodes[1]; GL.Vertex(NavTypeHelper.GetNavPos(cell, node2.navType)); for (int i = 1; i < path.nodes.Count - 1; i++) { PathFinder.Path.Node node3 = path.nodes[i]; int cell2 = node3.cell; PathFinder.Path.Node node4 = path.nodes[i]; Vector3 navPos = NavTypeHelper.GetNavPos(cell2, node4.navType); PathFinder.Path.Node node5 = path.nodes[i + 1]; int cell3 = node5.cell; PathFinder.Path.Node node6 = path.nodes[i + 1]; Vector3 navPos2 = NavTypeHelper.GetNavPos(cell3, node6.navType); GL.Vertex(navPos); GL.Vertex(navPos2); } GL.End(); GL.PopMatrix(); } }
private void DebugDrawSelectedNavigator() { if (DebugHandler.DebugPathFinding && !((Object)SelectTool.Instance == (Object)null) && !((Object)SelectTool.Instance.selected == (Object)null)) { Navigator component = SelectTool.Instance.selected.GetComponent <Navigator>(); if (!((Object)component == (Object)null)) { int mouseCell = DebugHandler.GetMouseCell(); if (Grid.IsValidCell(mouseCell)) { PathFinder.PotentialPath potential_path = new PathFinder.PotentialPath(Grid.PosToCell(component), component.CurrentNavType, component.flags); PathFinder.Path path = default(PathFinder.Path); PathFinder.UpdatePath(component.NavGrid, component.GetCurrentAbilities(), potential_path, PathFinderQueries.cellQuery.Reset(mouseCell), ref path); string empty = string.Empty; string text = empty; empty = text + "Source: " + Grid.PosToCell(component) + "\n"; text = empty; empty = text + "Dest: " + mouseCell + "\n"; empty = empty + "Cost: " + path.cost; DrawPath(path, component.GetComponent <KAnimControllerBase>().GetPivotSymbolPosition(), Color.green); DebugText.Instance.Draw(empty, Grid.CellToPosCCC(mouseCell, Grid.SceneLayer.Move), Color.white); } } } }
public static void DebugDrawPath(PathFinder.Path path) { if (path.nodes != null) { for (int i = 0; i < path.nodes.Count - 1; i++) { PathFinder.Path.Node node = path.nodes[i]; int cell = node.cell; PathFinder.Path.Node node2 = path.nodes[i + 1]; DebugDrawPath(cell, node2.cell); } } }
private void OnPostRender() { DrawPath(path, navigatorPos, Color.white); path = default(PathFinder.Path); DebugDrawSelectedNavigator(); if ((Object)navigator != (Object)null) { GL.PushMatrix(); material.SetPass(0); GL.Begin(1); PathFinderQuery query = PathFinderQueries.drawNavGridQuery.Reset(null); navigator.RunQuery(query); GL.End(); GL.PopMatrix(); } }
private new void OnCollisionStay2D(Collision2D collision) { base.OnCollisionStay2D(collision); Entity other = collision.gameObject.GetComponent <Entity>(); if (other && other.gameObject == Target) { ChasePath = new PathFinder.Path(); UpdateMarkers(); } else if (InChase && IgnoreTimer <= 0 && collision.gameObject.tag == "Obstacle" && Physics2D.Raycast(transform.position, Target.transform.position - transform.position, Mathf.Infinity, 512 + 1).collider.gameObject.tag != "Player") { ChasePath = PathFinder.Instance.GetPathTo(gameObject, Target.gameObject); if (ChasePath.Directions == null) { IgnoreTimer = 3; InChase = false; } UpdateMarkers(); } }
private bool ValidatePath(ref PathFinder.Path path) { PathFinderAbilities currentAbilities = GetCurrentAbilities(); return(PathFinder.ValidatePath(NavGrid, currentAbilities, ref path)); }
public void DrawPath(Vector3 navigator_pos, PathFinder.Path path) { navigatorPos = navigator_pos; navigatorPos.y += 0.5f; this.path = path; }
// Update is called once per frame void Update() { int oldX = posX; int oldY = posY; posX = (int)(trans.position.x); posY = (int)(trans.position.y); if(oldX == posX && oldY == posY) { stuckTimer += Time.deltaTime; } if(path != null) { PathFinder.Node currentNode = path.nodes[currentNodeIndex]; bool shouldChangeDir = true; bool isJump = currentNode.isJump; //Next node is the last if(currentNodeIndex + 1 == path.Distance) { if(posX == currentNode.posX && posY == currentNode.posY) { path = null; currentNodeIndex = 0; } } else { PathFinder.Node nextNode = path.nodes[currentNodeIndex + 1]; isJump |= nextNode.isJump; if(posX == currentNode.posX && posY >= currentNode.posY) { shouldChangeDir = false; } //We are at the node position. Find next node if(posX == currentNode.posX && posY == currentNode.posY) { currentNodeIndex++; stuckTimer = 0; } else if(posX == currentNode.posX && posY == nextNode.posY) { currentNodeIndex++; stuckTimer = 0; } } if(shouldChangeDir) { moveDir = currentNode.posX - posX; //Smooth the apprach float dist = Vector3.Distance(trans.position, new Vector3(currentNode.posX + 0.5f, currentNode.posY + 0.5f, 0.0f)); moveDir /= (dist * 1.4f); } move(moveDir, isJump); } if(stuckTimer >= timeStuckBeforeNewPath) { path = null; } if(pathFinder != null) { if(path == null) { Vector3 gotoPos= Vector3.zero; Key key = GameObject.FindObjectOfType<Key>(); if(key != null) { gotoPos = key.transform.position; } else if(holder.hasKey()) { Door door = GameObject.FindObjectOfType<Door>(); gotoPos = door.transform.position; } else { bool foundChest = false; Chest[] chests = GameObject.FindObjectsOfType<Chest>(); //Try to find a random unopened chest with a max tries of 100 for(int c = 0; c < 10; c++) { int i = Random.Range(0, chests.Length); if(!chests[i].HasOpened) { gotoPos = chests[i].transform.position; foundChest = true; continue; } } if(!foundChest) { Vector3 randomPos = Vector3.zero; randomPos.x = Random.Range(0, grid.GridWidth); randomPos.y = Random.Range(0, grid.GridHeight); randomPos.y = pathFinder.FallTillGround((int)randomPos.x, (int)randomPos.y); if(!grid.IsGridSpaceCollidable((int)randomPos.x, (int)randomPos.y)) { randomPos.x += 0.5f; randomPos.y += 0.5f; gotoPos = randomPos; } } } path = pathFinder.FindPath(trans.position, gotoPos); currentNodeIndex = 0; } /* if(Input.GetMouseButton(0)) { if(Camera.current != null) { path = pathFinder.FindPath(trans.position, Camera.current.ScreenToWorldPoint(Input.mousePosition)); currentNodeIndex = 0; } }*/ pathFinder.DebugDrawPath(path); } }
private void KeyPickedUp() { path = null; }
public override void Move() { IgnoreTimer -= Time.fixedDeltaTime; if (Target == null) { Target = Player; } if (IgnoreTimer > 0 || (Target.transform.position - transform.position).sqrMagnitude > MaxChaseRange * MaxChaseRange) { if (InChase) { List <Waypoint> points = new List <Waypoint>(Path.waypoints); points.Sort((a, b) => (int)(((a.transform.position - transform.position).sqrMagnitude - (b.transform.position - transform.position).sqrMagnitude) * 100)); Progress = Path.waypoints.IndexOf(points.First()); InChase = false; } base.Move(); return; } InChase = true; if (ChasePath.Target == null || ChasePath.Directions == null || ChasePath.Directions.Count == 0) { ChasePath = new PathFinder.Path(); Vector3 direction = Target.transform.position - transform.position; FacingLeft = direction.x < 0; direction = direction.normalized * MovementSpeed * Time.fixedDeltaTime; Rigidbody.MovePosition(transform.position + direction); } else { ChasePath.age -= Time.fixedDeltaTime; if (ChasePath.age < 0) { ChasePath = PathFinder.Instance.GetPathTo(gameObject, Target.gameObject); } UpdateMarkers(); if (ChasePath.Directions == null) { return; } Vector3 nextTarget = ChasePath.Directions[0]; Vector3 direction = nextTarget - transform.position; if (direction.sqrMagnitude < 1f) { ChasePath.Directions.Remove(nextTarget); RaycastHit2D hit = Physics2D.Raycast(transform.position, Target.transform.position - transform.position, Mathf.Infinity, 512 + 1); if (hit && hit.collider && hit.collider.tag == "Player") { ChasePath = new PathFinder.Path(); UpdateMarkers(); } } FacingLeft = direction.x < 0; direction = direction.normalized * MovementSpeed * Time.fixedDeltaTime; Rigidbody.MovePosition(transform.position + direction); } }